From 5e6e50b0359cd9dfbaaf29ebdda5e8c821c9fad2 Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Sun, 15 Dec 2024 17:57:06 -0800 Subject: [PATCH] add a log line `request timings` that could be propagated to usage insights within grafana cloud for grafana cloud logs users Signed-off-by: Callum Styan --- pkg/ruler/evaluator_local.go | 6 ++++++ pkg/ruler/evaluator_remote.go | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/ruler/evaluator_local.go b/pkg/ruler/evaluator_local.go index 00faabd7ea499..8ed2351118b75 100644 --- a/pkg/ruler/evaluator_local.go +++ b/pkg/ruler/evaluator_local.go @@ -5,7 +5,9 @@ import ( "fmt" "time" + "github.com/go-kit/kit/log/level" "github.com/go-kit/log" + "github.com/grafana/loki/v3/pkg/util" "github.com/grafana/loki/v3/pkg/logproto" "github.com/grafana/loki/v3/pkg/logql" @@ -49,5 +51,9 @@ func (l *LocalEvaluator) Eval(ctx context.Context, qs string, now time.Time) (*l return nil, err } + // Retrieve rule details from context + ruleName, ruleType := GetRuleDetailsFromContext(ctx) + + level.Info(l.logger).Log("msg", "request timings", "insight", "true", "source", "loki_ruler", "rule_name", ruleName, "rule_type", ruleType, "total", res.Statistics.Summary.ExecTime, "total_bytes", res.Statistics.Summary.TotalBytesProcessed, "query_hash", util.HashedQuery(qs)) return &res, nil } diff --git a/pkg/ruler/evaluator_remote.go b/pkg/ruler/evaluator_remote.go index c5db5801f6e41..694b26efe3dac 100644 --- a/pkg/ruler/evaluator_remote.go +++ b/pkg/ruler/evaluator_remote.go @@ -281,7 +281,9 @@ func (r *RemoteEvaluator) query(ctx context.Context, orgID, query string, ts tim level.Debug(log).Log("msg", "rule evaluation succeeded") r.metrics.successfulEvals.WithLabelValues(orgID).Inc() - return r.decodeResponse(ctx, resp, orgID) + dr, err := r.decodeResponse(ctx, resp, orgID) + level.Info(log).Log("msg", "request timings", "insight", "true", "source", "loki_ruler", "rule_name", ruleName, "rule_type", ruleType, "total", dr.Statistics.Summary.ExecTime, "total_bytes", dr.Statistics.Summary.TotalBytesProcessed, "query_hash", util.HashedQuery(query)) + return dr, err } func (r *RemoteEvaluator) decodeResponse(ctx context.Context, resp *httpgrpc.HTTPResponse, orgID string) (*logqlmodel.Result, error) {