Skip to content

Adding 'not_optimized_regex_matchers_count' field on the query stats #6696

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions pkg/frontend/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func (f *Handler) reportQueryStats(r *http.Request, source, userID string, query
numPeakSamples := stats.LoadPeakSamples()
numChunkBytes := stats.LoadFetchedChunkBytes()
numDataBytes := stats.LoadFetchedDataBytes()
notOptimizedRegexMatchers := stats.LoadNotOptimizedRegexMatchers()
numStoreGatewayTouchedPostings := stats.LoadStoreGatewayTouchedPostings()
numStoreGatewayTouchedPostingBytes := stats.LoadStoreGatewayTouchedPostingBytes()
splitQueries := stats.LoadSplitQueries()
Expand Down Expand Up @@ -431,6 +432,7 @@ func (f *Handler) reportQueryStats(r *http.Request, source, userID string, query
"status_code", statusCode,
"response_size", contentLength,
"samples_scanned", numScannedSamples,
"not_optimized_regex_matchers_count", notOptimizedRegexMatchers,
}, stats.LoadExtraFields()...)

if numStoreGatewayTouchedPostings > 0 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func (q querier) Select(ctx context.Context, sortSeries bool, sp *storage.Select
}
startT := time.Now()
defer func() {
stats.StoreMaxNotOptimizedRegexMatchers(notOptimizedRegexMatchersCount(matchers))
stats.AddQueryStorageWallTime(time.Since(startT))
}()

Expand Down Expand Up @@ -610,6 +611,16 @@ func (u useBeforeTimestampQueryable) UseQueryable(_ time.Time, queryMinT, _ int6
return queryMinT < u.ts
}

func notOptimizedRegexMatchersCount(matchers []*labels.Matcher) uint64 {
n := uint64(0)
for _, matcher := range matchers {
if matcher.GetRegexString() != "" && !matcher.IsRegexOptimized() {
n++
}
}
return n
}

// Returns QueryableWithFilter, that is used only if query starts before given timestamp.
// If timestamp is zero (time.IsZero), queryable is always used.
func UseBeforeTimestampQueryable(queryable storage.Queryable, ts time.Time) QueryableWithFilter {
Expand Down
18 changes: 18 additions & 0 deletions pkg/querier/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ func (s *QueryStats) AddQueryStorageWallTime(t time.Duration) {
atomic.AddInt64((*int64)(&s.QueryStorageWallTime), int64(t))
}

func (s *QueryStats) StoreMaxNotOptimizedRegexMatchers(count uint64) {
if s == nil {
return
}

// For this stats we get the MAX, instead of adding the counts
// as for split queries we will call this method multiples times.
atomic.StoreUint64(&s.NotOptimizedRegexCount, max(count, s.LoadNotOptimizedRegexMatchers()))
}

func (s *QueryStats) LoadNotOptimizedRegexMatchers() uint64 {
if s == nil {
return 0
}
return atomic.LoadUint64(&s.NotOptimizedRegexCount)
}

// LoadQueryStorageWallTime returns current query storage wall time.
func (s *QueryStats) LoadQueryStorageWallTime() time.Duration {
if s == nil {
Expand Down Expand Up @@ -390,6 +407,7 @@ func (s *QueryStats) Merge(other *QueryStats) {
s.AddFetchedChunkBytes(other.LoadFetchedChunkBytes())
s.AddFetchedDataBytes(other.LoadFetchedDataBytes())
s.AddFetchedSamples(other.LoadFetchedSamples())
s.StoreMaxNotOptimizedRegexMatchers(other.LoadNotOptimizedRegexMatchers())
s.AddFetchedChunks(other.LoadFetchedChunks())
s.AddStoreGatewayTouchedPostings(other.LoadStoreGatewayTouchedPostings())
s.AddStoreGatewayTouchedPostingBytes(other.LoadStoreGatewayTouchedPostingBytes())
Expand Down
120 changes: 82 additions & 38 deletions pkg/querier/stats/stats.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/querier/stats/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ message Stats {
// The highest count of samples considered while evaluating a query.
// Equal to PeakSamples in https://github.com/prometheus/prometheus/blob/main/util/stats/query_stats.go
uint64 peak_samples = 14;
// The total number of not optimized regex matchers
uint64 not_optimized_regex_count = 15;
}
3 changes: 3 additions & 0 deletions pkg/querier/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestStats_Merge(t *testing.T) {
stats1.AddFetchedSamples(109)
stats1.AddScannedSamples(100)
stats1.AddPeakSamples(100)
stats1.StoreMaxNotOptimizedRegexMatchers(2)
stats1.AddExtraFields("a", "b")
stats1.AddExtraFields("a", "b")

Expand All @@ -234,6 +235,7 @@ func TestStats_Merge(t *testing.T) {
stats1.AddStoreGatewayTouchedPostingBytes(301)
stats2.AddFetchedChunks(102)
stats2.AddFetchedSamples(103)
stats2.StoreMaxNotOptimizedRegexMatchers(3)
stats2.AddPeakSamples(105)
stats2.AddScannedSamples(105)
stats2.AddExtraFields("c", "d")
Expand All @@ -251,6 +253,7 @@ func TestStats_Merge(t *testing.T) {
assert.Equal(t, uint64(105), stats1.LoadPeakSamples())
assert.Equal(t, uint64(401), stats1.LoadStoreGatewayTouchedPostings())
assert.Equal(t, uint64(601), stats1.LoadStoreGatewayTouchedPostingBytes())
assert.Equal(t, uint64(3), stats1.LoadNotOptimizedRegexMatchers())
checkExtraFields(t, []interface{}{"a", "b", "c", "d"}, stats1.LoadExtraFields())
})

Expand Down
1 change: 1 addition & 0 deletions pkg/ruler/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func RecordAndReportRuleQueryMetrics(qf rules.QueryFunc, userID string, evalMetr
"fetched_samples_count", queryStats.FetchedSamplesCount,
"fetched_chunks_bytes", queryStats.FetchedChunkBytes,
"fetched_data_bytes", queryStats.FetchedDataBytes,
"not_optimized_regex_matchers_count", queryStats.NotOptimizedRegexCount,
)
logMessage = append(logMessage, queryStats.LoadExtraFields()...)
level.Info(util_log.WithContext(ctx, logger)).Log(logMessage...)
Expand Down
Loading