Skip to content

Commit 2c60463

Browse files
authored
feat(ingester): Add "source" pprof label to all query methods (#7494)
* feat(ingester): Add "source" pprof label to all query methods Add GetSource() to requestmeta and use it as a pprof label in all ingester pprof.Do sites (QueryStream, LabelValues, LabelNames, MetricsForLabelMatchers, MetricsMetadata, QueryExemplars). The ruler already sets source="ruler" via ContextWithRequestSource; queries without a source show as "unknown". Usage: go tool pprof -tagfocus="source:ruler" profile.pb.gz go tool pprof -tagfocus="source:unknown" profile.pb.gz Signed-off-by: Daniel Blando <ddeluigg@amazon.com> * changelog Signed-off-by: Daniel Blando <ddeluigg@amazon.com> --------- Signed-off-by: Daniel Blando <ddeluigg@amazon.com>
1 parent dcc04af commit 2c60463

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [ENHANCEMENT] Upgrade gRPC from v1.71.2 to v1.79.3 to address CVE-2026-33186. #7460
2121
* [ENHANCEMENT] Query Frontend: Add `query_too_expensive` reason to QFE and `reason` field to query stats. #7479
2222
* [ENHANCEMENT] Distributor: Add HMAC-SHA256 stream authentication for `PushStream` via `-distributor.sign-write-requests-keys`. #7475
23+
* [ENHANCEMENT] Instrument Ingester CPU profile with source for read APIs. #7494
2324
* [BUGFIX] Querier: Fix queryWithRetry and labelsWithRetry returning (nil, nil) on cancelled context by propagating ctx.Err(). #7370
2425
* [BUGFIX] Metrics Helper: Fix non-deterministic bucket order in merged histograms by sorting buckets after map iteration, matching Prometheus client library behavior. #7380
2526
* [BUGFIX] Distributor: Return HTTP 401 Unauthorized when tenant ID resolution fails in the Prometheus Remote Write 2.0 path. #7389

pkg/ingester/ingester.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import (
6464
"github.com/cortexproject/cortex/pkg/util/limiter"
6565
logutil "github.com/cortexproject/cortex/pkg/util/log"
6666
util_math "github.com/cortexproject/cortex/pkg/util/math"
67+
"github.com/cortexproject/cortex/pkg/util/requestmeta"
6768
"github.com/cortexproject/cortex/pkg/util/resource"
6869
"github.com/cortexproject/cortex/pkg/util/services"
6970
"github.com/cortexproject/cortex/pkg/util/spanlogger"
@@ -1835,7 +1836,7 @@ func (i *Ingester) QueryExemplars(ctx context.Context, req *client.ExemplarQuery
18351836
}
18361837

18371838
// Set pprof labels for profiling
1838-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
1839+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
18391840
resp, err = i.queryExemplars(ctx, userID, req)
18401841
})
18411842
return resp, err
@@ -1905,7 +1906,7 @@ func (i *Ingester) LabelValues(ctx context.Context, req *client.LabelValuesReque
19051906
}
19061907

19071908
// Set pprof labels for profiling
1908-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
1909+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
19091910
var cleanup func()
19101911
resp, cleanup, err = i.labelsValuesCommon(ctx, req)
19111912
defer cleanup()
@@ -1924,7 +1925,7 @@ func (i *Ingester) LabelValuesStream(req *client.LabelValuesRequest, stream clie
19241925
}
19251926

19261927
// Set pprof labels for profiling
1927-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
1928+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
19281929
var resp *client.LabelValuesResponse
19291930
var cleanup func()
19301931
resp, cleanup, err = i.labelsValuesCommon(ctx, req)
@@ -2021,7 +2022,7 @@ func (i *Ingester) LabelNames(ctx context.Context, req *client.LabelNamesRequest
20212022
}
20222023

20232024
// Set pprof labels for profiling
2024-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2025+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
20252026
var cleanup func()
20262027
resp, cleanup, err = i.labelNamesCommon(ctx, req)
20272028
defer cleanup()
@@ -2040,7 +2041,7 @@ func (i *Ingester) LabelNamesStream(req *client.LabelNamesRequest, stream client
20402041
}
20412042

20422043
// Set pprof labels for profiling
2043-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2044+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
20442045
var resp *client.LabelNamesResponse
20452046
var cleanup func()
20462047
resp, cleanup, err = i.labelNamesCommon(ctx, req)
@@ -2137,7 +2138,7 @@ func (i *Ingester) MetricsForLabelMatchers(ctx context.Context, req *client.Metr
21372138
}
21382139

21392140
// Set pprof labels for profiling
2140-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2141+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
21412142
result = &client.MetricsForLabelMatchersResponse{}
21422143
var cleanup func()
21432144
cleanup, err = i.metricsForLabelMatchersCommon(ctx, req, func(l labels.Labels) error {
@@ -2161,7 +2162,7 @@ func (i *Ingester) MetricsForLabelMatchersStream(req *client.MetricsForLabelMatc
21612162
}
21622163

21632164
// Set pprof labels for profiling
2164-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2165+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
21652166
result := &client.MetricsForLabelMatchersStreamResponse{}
21662167

21672168
var cleanup func()
@@ -2301,7 +2302,7 @@ func (i *Ingester) MetricsMetadata(ctx context.Context, req *client.MetricsMetad
23012302
}
23022303

23032304
// Set pprof labels for profiling
2304-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2305+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
23052306
userMetadata := i.getUserMetadata(userID)
23062307

23072308
if userMetadata == nil {
@@ -2443,7 +2444,7 @@ func (i *Ingester) QueryStream(req *client.QueryRequest, stream client.Ingester_
24432444
}
24442445

24452446
// Set pprof labels for profiling
2446-
pprof.Do(ctx, pprof.Labels("user", userID), func(ctx context.Context) {
2447+
pprof.Do(ctx, pprof.Labels("user", userID, "source", requestmeta.GetSource(ctx)), func(ctx context.Context) {
24472448
err = i.queryStream(ctx, userID, req, stream, spanlog)
24482449
})
24492450

pkg/util/requestmeta/source.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ func RequestFromRuler(ctx context.Context) bool {
2525
}
2626
return metadataMap[RequestSourceKey] == SourceRuler
2727
}
28+
29+
// GetSource returns the request source from context, or "unknown" if not set.
30+
func GetSource(ctx context.Context) string {
31+
metadataMap := MapFromContext(ctx)
32+
if metadataMap == nil {
33+
return "unknown"
34+
}
35+
if source := metadataMap[RequestSourceKey]; source != "" {
36+
return source
37+
}
38+
return "unknown"
39+
}

0 commit comments

Comments
 (0)