-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathstats.go
66 lines (56 loc) · 2 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: AGPL-3.0-only
package querymiddleware
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
)
type queryStatsMiddleware struct {
nonAlignedQueries prometheus.Counter
regexpMatcherCount prometheus.Counter
regexpMatcherOptimizedCount prometheus.Counter
next Handler
}
func newQueryStatsMiddleware(reg prometheus.Registerer) Middleware {
nonAlignedQueries := promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_query_frontend_non_step_aligned_queries_total",
Help: "Total queries sent that are not step aligned.",
})
regexpMatcherCount := promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_query_frontend_regexp_matcher_count",
Help: "Total number of regexp matchers",
})
regexpMatcherOptimizedCount := promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_query_frontend_regexp_matcher_optimized_count",
Help: "Total number of optimized regexp matchers",
})
return MiddlewareFunc(func(next Handler) Handler {
return &queryStatsMiddleware{
nonAlignedQueries: nonAlignedQueries,
regexpMatcherCount: regexpMatcherCount,
regexpMatcherOptimizedCount: regexpMatcherOptimizedCount,
next: next,
}
})
}
func (s queryStatsMiddleware) Do(ctx context.Context, req Request) (Response, error) {
if !isRequestStepAligned(req) {
s.nonAlignedQueries.Inc()
}
if expr, err := parser.ParseExpr(req.GetQuery()); err == nil {
for _, selectors := range parser.ExtractSelectors(expr) {
for _, matcher := range selectors {
if matcher.Type != labels.MatchRegexp && matcher.Type != labels.MatchNotRegexp {
continue
}
s.regexpMatcherCount.Inc()
if matcher.IsRegexOptimized() {
s.regexpMatcherOptimizedCount.Inc()
}
}
}
}
return s.next.Do(ctx, req)
}