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) } }