Skip to content

Put query field at the end of query stats log #5622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pkg/frontend/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
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 All @@ -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...)
Expand Down Expand Up @@ -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
}

Expand Down