From 1dfd5f900e4a001656c5a8961268a45fcb57674f Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Fri, 27 Oct 2023 21:58:56 -0700 Subject: [PATCH] Put query field at the end of query stats log (#5622) * put query log at the end to avoid truncating other fields Signed-off-by: Ben Ye * fix lint Signed-off-by: Ben Ye * fix lint again Signed-off-by: Ben Ye --------- Signed-off-by: Ben Ye --- pkg/frontend/transport/handler.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/frontend/transport/handler.go b/pkg/frontend/transport/handler.go index 7d953d1e25..080712056e 100644 --- a/pkg/frontend/transport/handler.go +++ b/pkg/frontend/transport/handler.go @@ -271,17 +271,18 @@ func formatGrafanaStatsFields(r *http.Request) []interface{} { // reportSlowQuery reports slow queries. func (f *Handler) reportSlowQuery(r *http.Request, queryString url.Values, queryResponseTime time.Duration) { - logMessage := append([]interface{}{ + logMessage := []interface{}{ "msg", "slow query detected", "method", r.Method, "host", r.Host, "path", r.URL.Path, "time_taken", queryResponseTime.String(), - }, formatQueryString(queryString)...) + } grafanaFields := formatGrafanaStatsFields(r) if len(grafanaFields) > 0 { logMessage = append(logMessage, grafanaFields...) } + logMessage = append(logMessage, formatQueryString(queryString)...) level.Info(util_log.WithContext(r.Context(), f.log)).Log(logMessage...) } @@ -327,7 +328,6 @@ func (f *Handler) reportQueryStats(r *http.Request, userID string, queryString u "response_size", contentLength, }, stats.LoadExtraFields()...) - logMessage = append(logMessage, formatQueryString(queryString)...) grafanaFields := formatGrafanaStatsFields(r) if len(grafanaFields) > 0 { logMessage = append(logMessage, grafanaFields...) @@ -351,6 +351,9 @@ func (f *Handler) reportQueryStats(r *http.Request, userID string, queryString u } else { logMessage = append(logMessage, "error", s.Message()) } + } + logMessage = append(logMessage, formatQueryString(queryString)...) + if error != nil { level.Error(util_log.WithContext(r.Context(), f.log)).Log(logMessage...) } else { level.Info(util_log.WithContext(r.Context(), f.log)).Log(logMessage...) @@ -404,9 +407,18 @@ func (f *Handler) parseRequestQueryString(r *http.Request, bodyBuf bytes.Buffer) } func formatQueryString(queryString url.Values) (fields []interface{}) { + var queryFields []string for k, v := range queryString { + // If `query` or `match[]` field exists, we always put it as the last field. + if k == "query" || k == "match[]" { + queryFields = []string{fmt.Sprintf("param_%s", k), strings.Join(v, ",")} + continue + } fields = append(fields, fmt.Sprintf("param_%s", k), strings.Join(v, ",")) } + if len(queryFields) > 0 { + fields = append(fields, queryFields) + } return fields }