Skip to content

Commit

Permalink
put query log at the end to avoid truncating other fields
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 committed Oct 27, 2023
1 parent f608df2 commit 1557afd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/frontend/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ func (f *Handler) reportSlowQuery(r *http.Request, queryString url.Values, query
"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...)
}
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -355,6 +355,7 @@ func (f *Handler) reportQueryStats(r *http.Request, userID string, queryString u
} else {
level.Info(util_log.WithContext(r.Context(), f.log)).Log(logMessage...)
}
logMessage = append(logMessage, formatQueryString(queryString)...)

var reason string
if statusCode == http.StatusTooManyRequests {
Expand Down Expand Up @@ -404,9 +405,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
}

Expand Down

0 comments on commit 1557afd

Please sign in to comment.