From 7d7f5dde4242dd019d7a9b0fe0bebbc5c72e9d0c Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Tue, 10 Dec 2024 13:20:34 +0100 Subject: [PATCH] Address parsing in NewMetadataHandler() Signed-off-by: Marco Pracucci --- pkg/querier/metadata_handler.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pkg/querier/metadata_handler.go b/pkg/querier/metadata_handler.go index ba6f8178b6f..d67e1d7d7c9 100644 --- a/pkg/querier/metadata_handler.go +++ b/pkg/querier/metadata_handler.go @@ -48,24 +48,28 @@ type metadataErrorResult struct { // Mimir for a given tenant. It is kept and returned as a set. func NewMetadataHandler(m MetadataSupplier) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - limit := -1 + limit := int32(-1) if s := r.FormValue("limit"); s != "" { - var err error - if limit, err = strconv.Atoi(s); err != nil { + if parsed, err := strconv.ParseInt(s, 10, 32); err != nil { w.WriteHeader(http.StatusBadRequest) util.WriteJSONResponse(w, metadataErrorResult{Status: statusError, Error: "limit must be a number"}) return + } else { + limit = int32(parsed) } } - limitPerMetric := -1 + + limitPerMetric := int32(-1) if s := r.FormValue("limit_per_metric"); s != "" { - var err error - if limitPerMetric, err = strconv.Atoi(s); err != nil { + if parsed, err := strconv.ParseInt(s, 10, 32); err != nil { w.WriteHeader(http.StatusBadRequest) util.WriteJSONResponse(w, metadataErrorResult{Status: statusError, Error: "limit_per_metric must be a number"}) return + } else { + limitPerMetric = int32(parsed) } } + metric := r.FormValue("metric") req := &client.MetricsMetadataRequest{ Limit: int32(limit), @@ -87,11 +91,11 @@ func NewMetadataHandler(m MetadataSupplier) http.Handler { // We enforce this both here and in the ingesters. Doing it in the ingesters is // more efficient as it is earlier in the process, but since that one is per user, // we still need to do it here after all the results are merged. - if limitPerMetric > 0 && len(ms) >= limitPerMetric { + if limitPerMetric > 0 && len(ms) >= int(limitPerMetric) { continue } if !ok { - if limit >= 0 && len(metrics) >= limit { + if limit >= 0 && len(metrics) >= int(limit) { break } // Most metrics will only hold 1 copy of the same metadata.