From 04888bf6edb8a44b6fbc0148ef6a439c37b27fd9 Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Mon, 30 Sep 2024 10:39:57 -0700 Subject: [PATCH] add query user and query source to "executing query" log lines Signed-off-by: Callum Styan --- pkg/logql/engine.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pkg/logql/engine.go b/pkg/logql/engine.go index c44ee06c16948..e8f590fa819ed 100644 --- a/pkg/logql/engine.go +++ b/pkg/logql/engine.go @@ -225,6 +225,32 @@ func (q *query) resultLength(res promql_parser.Value) int { } } +// findSource looks for a query source in the QueryTags. +func findSource(tags string) string { + source := strings.SplitN(tags, "Source=", 2) + if len(source) == 2 { + source = strings.Split(source[1], ",") + return source[0] + } + // look for source lowercase since that's what we insert for grafana alert + source = strings.SplitN(tags, "source=", 2) + if len(source) == 2 { + source = strings.Split(source[1], ",") + return source[0] + } + return "" +} + +// findUser looks for a query user in the QueryTags. +func findUser(tags string) string { + user := strings.SplitN(tags, "user=", 2) + if len(user) == 2 { + user = strings.Split(user[1], ",") + return user[0] + } + return "" +} + // Exec Implements `Query`. It handles instrumentation & defers to Eval. func (q *query) Exec(ctx context.Context) (logqlmodel.Result, error) { sp, ctx := opentracing.StartSpanFromContext(ctx, "query.Exec") @@ -240,11 +266,15 @@ func (q *query) Exec(ctx context.Context) (logqlmodel.Result, error) { ) if q.logExecQuery { + // check for a query source + tags := httpreq.ExtractQueryTagsFromContext(ctx) + source := findSource(tags) + user := findUser(tags) queryHash := util.HashedQuery(q.params.QueryString()) if GetRangeType(q.params) == InstantType { - level.Info(logutil.WithContext(ctx, q.logger)).Log("msg", "executing query", "type", "instant", "query", q.params.QueryString(), "query_hash", queryHash) + level.Info(logutil.WithContext(ctx, q.logger)).Log("msg", "executing query", "type", "instant", "query", q.params.QueryString(), "query_hash", queryHash, "source", source, "user", user) } else { - 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) + 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, "source", source, "user", user) } }