Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contextual information and query statistics to log #5604

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## master / unreleased
* [CHANGE] Ruler: Add contextual info and query statistics to log
* [FEATURE] Ruler: Add support for disabling rule groups. #5521
* [FEATURE] Added the flag `-alertmanager.alerts-gc-interval` to configure alert manager alerts Garbage collection interval. #5550
* [FEATURE] Ruler: Add support for Limit field on RuleGroup. #5528
Expand Down
34 changes: 29 additions & 5 deletions pkg/ruler/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"time"

"github.com/cortexproject/cortex/pkg/util/validation"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -26,7 +24,9 @@ import (

"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/querier"
"github.com/cortexproject/cortex/pkg/querier/stats"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/cortexproject/cortex/pkg/util/validation"
)

// Pusher is an ingester server that accepts pushes.
Expand Down Expand Up @@ -214,6 +214,7 @@ func RecordAndReportRuleQueryMetrics(qf rules.QueryFunc, queryTime prometheus.Co
}

return func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) {
queryStats, ctx := stats.ContextWithEmptyStats(ctx)
// If we've been passed a counter we want to record the wall time spent executing this request.
timer := prometheus.NewTimer(nil)
defer func() {
Expand All @@ -224,9 +225,29 @@ func RecordAndReportRuleQueryMetrics(qf rules.QueryFunc, queryTime prometheus.Co
logMessage := []interface{}{
"msg", "query stats",
"component", "ruler",
"cortex_ruler_query_seconds_total", querySeconds,
"query", qs,
}
if origin := ctx.Value(promql.QueryOrigin{}); origin != nil {
queryLabels := origin.(map[string]interface{})
rgMap := queryLabels["ruleGroup"].(map[string]string)
logMessage = append(logMessage,
"rule_group", rgMap["name"],
"namespace", rgMap["file"],
)
}
ruleDetail := rules.FromOriginContext(ctx)
logMessage = append(logMessage,
"rule", ruleDetail.Name,
rajagopalanand marked this conversation as resolved.
Show resolved Hide resolved
"rule_kind", ruleDetail.Kind,
"query", qs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be huge sometimes, and it will repetitive. We can add it for now. But later if we have log problems, we can remove it.

"cortex_ruler_query_seconds_total", querySeconds,
"query_wall_time_seconds", queryStats.WallTime,
"fetched_series_count", queryStats.FetchedSeriesCount,
"fetched_chunks_count", queryStats.FetchedChunksCount,
"fetched_samples_count", queryStats.FetchedSamplesCount,
"fetched_chunks_bytes", queryStats.FetchedChunkBytes,
"fetched_data_bytes", queryStats.FetchedDataBytes,
)
logMessage = append(logMessage, queryStats.LoadExtraFields()...)
level.Info(util_log.WithContext(ctx, logger)).Log(logMessage...)
}()

Expand Down Expand Up @@ -295,10 +316,13 @@ func DefaultTenantManagerFactory(cfg Config, p Pusher, q storage.Queryable, engi
totalWrites := totalWritesVec.WithLabelValues(userID)
failedWrites := failedWritesVec.WithLabelValues(userID)

engineQueryFunc := EngineQueryFunc(engine, q, overrides, userID)
metricsQueryFunc := MetricsQueryFunc(engineQueryFunc, totalQueries, failedQueries)

return rules.NewManager(&rules.ManagerOptions{
Appendable: NewPusherAppendable(p, userID, overrides, totalWrites, failedWrites),
Queryable: q,
QueryFunc: RecordAndReportRuleQueryMetrics(MetricsQueryFunc(EngineQueryFunc(engine, q, overrides, userID), totalQueries, failedQueries), queryTime, logger),
QueryFunc: RecordAndReportRuleQueryMetrics(metricsQueryFunc, queryTime, logger),
Context: user.InjectOrgID(ctx, userID),
ExternalURL: cfg.ExternalURL.URL,
NotifyFunc: SendAlerts(notifier, cfg.ExternalURL.URL.String()),
Expand Down
Loading