Skip to content

Commit

Permalink
fix: report correct status code for metric and log queries in metrics…
Browse files Browse the repository at this point in the history
….go (#12102)
  • Loading branch information
ashwanthgoli authored Sep 19, 2024
1 parent a475153 commit 900751c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
1 change: 0 additions & 1 deletion pkg/logql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ func (q *query) Exec(ctx context.Context) (logqlmodel.Result, error) {
sp.LogKV(statResult.KVList()...)

status, _ := server.ClientHTTPStatusAndError(err)

if q.record {
RecordRangeAndInstantQueryMetrics(ctx, q.logger, q.params, strconv.Itoa(status), statResult, data)
}
Expand Down
30 changes: 5 additions & 25 deletions pkg/querier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ func (q *QuerierAPI) LabelHandler(ctx context.Context, req *logproto.LabelReques
sp.LogKV(statResult.KVList()...)
}

status := 200
if err != nil {
status, _ = serverutil.ClientHTTPStatusAndError(err)
}

status, _ := serverutil.ClientHTTPStatusAndError(err)
logql.RecordLabelQueryMetrics(ctx, util_log.Logger, *req.Start, *req.End, req.Name, req.Query, strconv.Itoa(status), statResult)

return resp, err
Expand Down Expand Up @@ -277,11 +273,7 @@ func (q *QuerierAPI) SeriesHandler(ctx context.Context, req *logproto.SeriesRequ
sp.LogKV(statResult.KVList()...)
}

status := 200
if err != nil {
status, _ = serverutil.ClientHTTPStatusAndError(err)
}

status, _ := serverutil.ClientHTTPStatusAndError(err)
logql.RecordSeriesQueryMetrics(ctx, util_log.Logger, req.Start, req.End, req.Groups, strconv.Itoa(status), req.GetShards(), statResult)

return resp, statResult, err
Expand All @@ -308,11 +300,7 @@ func (q *QuerierAPI) IndexStatsHandler(ctx context.Context, req *loghttp.RangeQu
sp.LogKV(statResult.KVList()...)
}

status := 200
if err != nil {
status, _ = serverutil.ClientHTTPStatusAndError(err)
}

status, _ := serverutil.ClientHTTPStatusAndError(err)
logql.RecordStatsQueryMetrics(ctx, util_log.Logger, req.Start, req.End, req.Query, strconv.Itoa(status), statResult)

return resp, err
Expand Down Expand Up @@ -340,11 +328,7 @@ func (q *QuerierAPI) IndexShardsHandler(ctx context.Context, req *loghttp.RangeQ
sp.LogKV(statResult.KVList()...)
}

status := 200
if err != nil {
status, _ = serverutil.ClientHTTPStatusAndError(err)
}

status, _ := serverutil.ClientHTTPStatusAndError(err)
logql.RecordShardsQueryMetrics(
ctx, util_log.Logger, req.Start, req.End, req.Query, targetBytesPerShard, strconv.Itoa(status), resLength, statResult,
)
Expand Down Expand Up @@ -377,11 +361,7 @@ func (q *QuerierAPI) VolumeHandler(ctx context.Context, req *logproto.VolumeRequ
sp.LogKV(statResult.KVList()...)
}

status := 200
if err != nil {
status, _ = serverutil.ClientHTTPStatusAndError(err)
}

status, _ := serverutil.ClientHTTPStatusAndError(err)
logql.RecordVolumeQueryMetrics(ctx, util_log.Logger, req.From.Time(), req.Through.Time(), req.GetQuery(), uint32(req.GetLimit()), time.Duration(req.GetStep()), strconv.Itoa(status), statResult)

return resp, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func WriteError(err error, w http.ResponseWriter) {
// ClientHTTPStatusAndError returns error and http status that is "safe" to return to client without
// exposing any implementation details.
func ClientHTTPStatusAndError(err error) (int, error) {
if err == nil {
return http.StatusOK, nil
}

var (
queryErr storage_errors.QueryError
promErr promql.ErrStorage
Expand Down

0 comments on commit 900751c

Please sign in to comment.