/
promql_rate.go
190 lines (167 loc) · 5.83 KB
/
promql_rate.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package checks
import (
"context"
"fmt"
"time"
"golang.org/x/exp/slices"
"github.com/cloudflare/pint/internal/discovery"
"github.com/cloudflare/pint/internal/output"
"github.com/cloudflare/pint/internal/parser"
"github.com/cloudflare/pint/internal/parser/utils"
"github.com/cloudflare/pint/internal/promapi"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
promParser "github.com/prometheus/prometheus/promql/parser"
)
// FIXME: add native histograms
const (
RateCheckName = "promql/rate"
RateCheckDetails = `Using [rate](https://prometheus.io/docs/prometheus/latest/querying/functions/#rate) and [irate](https://prometheus.io/docs/prometheus/latest/querying/functions/#irate) function comes with a few requirements:
- The metric you calculate (i)rate from must be a counter or native histograms.
- The time window of the (i)rate function must have at least 2 samples.
The type of your metric is defined by the application that exports that metric.
The number of samples depends on how often your application is being scraped by Prometheus.
Each scrape produces a sample, so if your application is scrape every minute then the minimal time window you can use is two minutes.`
)
func NewRateCheck(prom *promapi.FailoverGroup) RateCheck {
return RateCheck{prom: prom, minIntervals: 2}
}
type RateCheck struct {
prom *promapi.FailoverGroup
minIntervals int
}
func (c RateCheck) Meta() CheckMeta {
return CheckMeta{
States: []discovery.ChangeType{
discovery.Noop,
discovery.Added,
discovery.Modified,
discovery.Moved,
},
IsOnline: true,
}
}
func (c RateCheck) String() string {
return fmt.Sprintf("%s(%s)", RateCheckName, c.prom.Name())
}
func (c RateCheck) Reporter() string {
return RateCheckName
}
func (c RateCheck) Check(ctx context.Context, _ discovery.Path, rule parser.Rule, entries []discovery.Entry) (problems []Problem) {
expr := rule.Expr()
if expr.SyntaxError != nil {
return problems
}
cfg, err := c.prom.Config(ctx, 0)
if err != nil {
text, severity := textAndSeverityFromError(err, c.Reporter(), c.prom.Name(), Bug)
problems = append(problems, Problem{
Lines: expr.Value.Lines,
Reporter: c.Reporter(),
Text: text,
Severity: severity,
})
return problems
}
done := &completedList{}
for _, problem := range c.checkNode(ctx, expr.Query, entries, cfg, done) {
problems = append(problems, Problem{
Lines: expr.Value.Lines,
Reporter: c.Reporter(),
Text: problem.text,
Details: problem.details,
Severity: problem.severity,
})
}
return problems
}
func (c RateCheck) checkNode(ctx context.Context, node *parser.PromQLNode, entries []discovery.Entry, cfg *promapi.ConfigResult, done *completedList) (problems []exprProblem) {
if n, ok := node.Expr.(*promParser.Call); ok && (n.Func.Name == "rate" || n.Func.Name == "irate" || n.Func.Name == "deriv") {
for _, arg := range n.Args {
m, ok := arg.(*promParser.MatrixSelector)
if !ok {
continue
}
if m.Range < cfg.Config.Global.ScrapeInterval*time.Duration(c.minIntervals) {
p := exprProblem{
expr: node.Expr.String(),
text: fmt.Sprintf("Duration for `%s()` must be at least %d x scrape_interval, %s is using `%s` scrape_interval.",
n.Func.Name, c.minIntervals, promText(c.prom.Name(), cfg.URI), output.HumanizeDuration(cfg.Config.Global.ScrapeInterval)),
details: RateCheckDetails,
severity: Bug,
}
problems = append(problems, p)
}
if n.Func.Name == "deriv" {
continue
}
if s, ok := m.VectorSelector.(*promParser.VectorSelector); ok {
if slices.Contains(done.values, s.Name) {
continue
}
done.values = append(done.values, s.Name)
metadata, err := c.prom.Metadata(ctx, s.Name)
if err != nil {
text, severity := textAndSeverityFromError(err, c.Reporter(), c.prom.Name(), Bug)
problems = append(problems, exprProblem{
expr: s.Name,
text: text,
severity: severity,
})
continue
}
for _, m := range metadata.Metadata {
if m.Type != v1.MetricTypeCounter && m.Type != v1.MetricTypeUnknown {
problems = append(problems, exprProblem{
expr: s.Name,
text: fmt.Sprintf("`%s()` should only be used with counters but `%s` is a %s according to metrics metadata from %s.",
n.Func.Name, s.Name, m.Type, promText(c.prom.Name(), metadata.URI)),
details: RateCheckDetails,
severity: Bug,
})
}
}
for _, e := range entries {
if e.PathError != nil {
continue
}
if e.Rule.Error.Err != nil {
continue
}
if e.Rule.RecordingRule != nil && e.Rule.RecordingRule.Expr.SyntaxError == nil && e.Rule.RecordingRule.Record.Value == s.Name {
for _, sm := range utils.HasOuterSum(e.Rule.RecordingRule.Expr.Query) {
if sv, ok := sm.Expr.(*promParser.VectorSelector); ok {
metadata, err := c.prom.Metadata(ctx, sv.Name)
if err != nil {
text, severity := textAndSeverityFromError(err, c.Reporter(), c.prom.Name(), Bug)
problems = append(problems, exprProblem{
expr: sv.Name,
text: text,
severity: severity,
})
continue
}
for _, m := range metadata.Metadata {
if m.Type == v1.MetricTypeCounter {
problems = append(problems, exprProblem{
expr: node.Expr.String(),
text: fmt.Sprintf("`rate(sum(counter))` chain detected, `%s` is called here on results of `%s`, calling `rate()` on `sum()` results will return bogus results, always `sum(rate(counter))`, never `rate(sum(counter))`.",
node.Expr, sm),
severity: Bug,
})
}
}
}
}
}
}
}
}
}
for _, child := range node.Children {
problems = append(problems, c.checkNode(ctx, child, entries, cfg, done)...)
}
return problems
}
type completedList struct {
values []string
}