Skip to content

Commit 3dd8cf1

Browse files
cstyanjeschkies
authored andcommitted
feat: add query user and query source to "executing query" log lines (grafana#14320)
Signed-off-by: Callum Styan <[email protected]>
1 parent f0e33c7 commit 3dd8cf1

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

pkg/logql/engine.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,21 @@ func (q *query) Exec(ctx context.Context) (logqlmodel.Result, error) {
241241

242242
if q.logExecQuery {
243243
queryHash := util.HashedQuery(q.params.QueryString())
244+
245+
logValues := []interface{}{
246+
"msg", "executing query",
247+
"query", q.params.QueryString(),
248+
"query_hash", queryHash,
249+
}
250+
tags := httpreq.ExtractQueryTagsFromContext(ctx)
251+
tagValues := tagsToKeyValues(tags)
244252
if GetRangeType(q.params) == InstantType {
245-
level.Info(logutil.WithContext(ctx, q.logger)).Log("msg", "executing query", "type", "instant", "query", q.params.QueryString(), "query_hash", queryHash)
253+
logValues = append(logValues, "type", "instant")
246254
} else {
247-
level.Info(logutil.WithContext(ctx, q.logger)).Log("msg", "executing query", "type", "range", "query", q.params.QueryString(), "length", q.params.End().Sub(q.params.Start()), "step", q.params.Step(), "query_hash", queryHash)
255+
logValues = append(logValues, "type", "range", "length", q.params.End().Sub(q.params.Start()), "step", q.params.Step())
248256
}
257+
logValues = append(logValues, tagValues...)
258+
level.Info(logutil.WithContext(ctx, q.logger)).Log(logValues...)
249259
}
250260

251261
rangeType := GetRangeType(q.params)
@@ -265,6 +275,7 @@ func (q *query) Exec(ctx context.Context) (logqlmodel.Result, error) {
265275
sp.LogKV(statResult.KVList()...)
266276

267277
status, _ := server.ClientHTTPStatusAndError(err)
278+
268279
if q.record {
269280
RecordRangeAndInstantQueryMetrics(ctx, q.logger, q.params, strconv.Itoa(status), statResult, data)
270281
}

pkg/logql/engine_test.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"math"
9-
"regexp"
109
"strings"
1110
"testing"
1211
"time"
@@ -2618,23 +2617,14 @@ func TestHashingStability(t *testing.T) {
26182617
expectedQueryHash := util.HashedQuery(test.qs)
26192618

26202619
// check that both places will end up having the same query hash, even though they're emitting different log lines.
2621-
require.Regexp(t,
2622-
regexp.MustCompile(
2623-
fmt.Sprintf(
2624-
`level=info org_id=fake msg="executing query" type=range query=.* length=5s step=1m0s query_hash=%d.*`, expectedQueryHash,
2625-
),
2626-
),
2627-
queryWithEngine(),
2628-
)
2629-
2630-
require.Regexp(t,
2631-
regexp.MustCompile(
2632-
fmt.Sprintf(
2633-
`level=info org_id=fake latency=slow query=".*" query_hash=%d query_type=metric range_type=range.*\n`, expectedQueryHash,
2634-
),
2635-
),
2636-
queryDirectly(),
2637-
)
2620+
withEngine := queryWithEngine()
2621+
require.Contains(t, withEngine, fmt.Sprintf("query_hash=%d", expectedQueryHash))
2622+
require.Contains(t, withEngine, "step=1m0s")
2623+
2624+
directly := queryDirectly()
2625+
require.Contains(t, directly, fmt.Sprintf("query_hash=%d", expectedQueryHash))
2626+
require.Contains(t, directly, "length=5s")
2627+
require.Contains(t, directly, "latency=slow")
26382628
}
26392629
}
26402630

0 commit comments

Comments
 (0)