Skip to content

Commit

Permalink
add query user and query source to "executing query" log lines
Browse files Browse the repository at this point in the history
Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan committed Sep 30, 2024
1 parent cf1d4a3 commit 04888bf
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pkg/logql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 04888bf

Please sign in to comment.