From 5e6907db43f16f107c854bace92bf3552ba2892d Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Mon, 2 Dec 2024 19:27:39 -0800 Subject: [PATCH] log grafana headers along with query request before query execution (#6391) --- pkg/frontend/transport/handler.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/frontend/transport/handler.go b/pkg/frontend/transport/handler.go index b4ecca59ee..9a2292bb46 100644 --- a/pkg/frontend/transport/handler.go +++ b/pkg/frontend/transport/handler.go @@ -228,13 +228,7 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Log request if f.cfg.QueryStatsEnabled { queryString = f.parseRequestQueryString(r, buf) - logMessage := append([]interface{}{ - "msg", "query request", - "component", "query-frontend", - "method", r.Method, - "path", r.URL.Path, - }, formatQueryString(queryString)...) - level.Info(util_log.WithContext(r.Context(), f.log)).Log(logMessage...) + f.logQueryRequest(r, queryString) } startTime := time.Now() @@ -306,6 +300,23 @@ func formatGrafanaStatsFields(r *http.Request) []interface{} { return fields } +// logQueryRequest logs query request before query execution. +func (f *Handler) logQueryRequest(r *http.Request, queryString url.Values) { + logMessage := []interface{}{ + "msg", "query request", + "component", "query-frontend", + "method", r.Method, + "path", r.URL.Path, + } + 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...) +} + // reportSlowQuery reports slow queries. func (f *Handler) reportSlowQuery(r *http.Request, queryString url.Values, queryResponseTime time.Duration) { logMessage := []interface{}{