-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromql.go
376 lines (293 loc) · 7.84 KB
/
promql.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package promql
import (
"fmt"
"strings"
)
type Stringer interface {
String() string
}
type Node interface {
Stringer
Self() string
Children() []Node
}
// --- constant node
type ConstantStringNode struct {
constant string
}
func NewConstantStringNode(constantString string) ConstantStringNode {
return ConstantStringNode{constant: constantString}
}
func (m ConstantStringNode) String() string {
return m.Self()
}
func (m ConstantStringNode) Self() string {
return m.constant
}
func (m ConstantStringNode) Children() []Node {
return nil
}
var _ Node = (*ConstantStringNode)(nil)
// --- time series selector, see https://prometheus.io/docs/prometheus/latest/querying/basics/#time-series-selectors
type TSSelector struct {
name string
labels []Label
duration string // 可选, 比如5m
offset string // 可选,比如offset 5m
}
var _ Node = (*TSSelector)(nil)
func (m TSSelector) String() string {
return m.Self()
}
func (m TSSelector) Self() string {
if m.name == "" && len(m.labels) == 0 {
panic("metric name and labels cannot be both empty")
}
s := m.name
if len(m.labels) != 0 {
labelStrings := make([]string, 0, len(m.labels))
for _, label := range m.labels {
labelStrings = append(labelStrings, label.String())
}
s += fmt.Sprintf("{%s}", strings.Join(labelStrings, ", "))
}
if m.duration != "" {
s += fmt.Sprintf("[%s]", m.duration)
}
if m.offset != "" {
s += fmt.Sprintf(" offset %s", m.offset)
}
return s
}
func (m TSSelector) Children() []Node {
return nil
}
func NewTSSelector(name string, labels ...Label) TSSelector {
return TSSelector{
name: name,
labels: labels,
}
}
func (m TSSelector) Labels(labels ...Label) TSSelector {
m.labels = append(m.labels, labels...)
return m
}
func (m TSSelector) Duration(duration string) TSSelector {
m.duration = duration
return m
}
func (m TSSelector) Offset(offset string) TSSelector {
m.offset = offset
return m
}
type Label struct {
Key string
Value string
Matcher string // = != =~ !~
}
func NewLabel(key, matcher, value string) Label {
return Label{Key: key, Value: value, Matcher: matcher}
}
func (l Label) String() string {
return fmt.Sprintf(`%s%s"%s"`, l.Key, l.Matcher, l.Value)
}
// --- query functions, see https://prometheus.io/docs/prometheus/latest/querying/functions
type Func struct {
fun string
parameters []Node // 长度不定, 1, 2,等
}
func NewFunc(fun string, parameters ...Node) Func {
return Func{fun: fun, parameters: parameters}
}
func (f Func) Parameters(params ...Node) Func {
f.parameters = append(f.parameters, params...)
return f
}
var _ Node = (*Func)(nil)
func (f Func) String() string {
params := make([]string, 0, len(f.parameters))
for _, p := range f.parameters {
params = append(params, p.String())
}
return fmt.Sprintf("%s(%s)", f.Self(), strings.Join(params, ", "))
}
func (f Func) Self() string {
return f.fun
}
func (f Func) Children() []Node {
return f.parameters
}
// --- 二元操作符, binary operators, see https://prometheus.io/docs/prometheus/latest/querying/operators/#binary-operators
type BinaryOp struct {
operator string // + - * / == != > < >= <= and or unless
left, right Node // operands
matcher *VectorMatcher // 可选
}
func NewBinaryOp(operator string) BinaryOp {
return BinaryOp{operator: operator}
}
func (bo BinaryOp) Operands(left, right Node) BinaryOp {
bo.left = left
bo.right = right
return bo
}
func (bo BinaryOp) Left(l Node) BinaryOp {
bo.left = l
return bo
}
func (bo BinaryOp) Right(r Node) BinaryOp {
bo.right = r
return bo
}
func (bo BinaryOp) Matcher(vm VectorMatcher) BinaryOp {
bo.matcher = &vm
return bo
}
var _ Node = (*BinaryOp)(nil)
func (bo BinaryOp) String() string {
return fmt.Sprintf("%s %s %s", bo.left.String(), bo.Self(), bo.right.String())
}
func (bo BinaryOp) Self() string {
s := bo.operator
if bo.matcher != nil {
s += " " + bo.matcher.String()
}
return s
}
func (bo BinaryOp) Children() []Node {
return []Node{bo.left, bo.right}
}
// --- See https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching
type VectorMatcher struct {
keyword string // on/ignoring
labels []string
group *GroupModifier // 可选
}
func NewVectorMatcher(keyword string, labels ...string) VectorMatcher {
return VectorMatcher{keyword: keyword, labels: labels}
}
func NewOnVectorMatcher(labels ...string) VectorMatcher {
return VectorMatcher{keyword: "on", labels: labels}
}
func NewIgnoringVectorMatcher(labels ...string) VectorMatcher {
return VectorMatcher{keyword: "ignoring", labels: labels}
}
func (vm VectorMatcher) String() string {
s := fmt.Sprintf("%s(%s)", vm.keyword, strings.Join(vm.labels, ", "))
if vm.group != nil {
s += " " + vm.group.String()
}
return s
}
func (vm VectorMatcher) Labels(labels ...string) VectorMatcher {
vm.labels = append(vm.labels, labels...)
return vm
}
func (vm VectorMatcher) GroupLeft(labels ...string) VectorMatcher {
vm.group = &GroupModifier{left: true, labels: labels}
return vm
}
func (vm VectorMatcher) GroupRight(labels ...string) VectorMatcher {
vm.group = &GroupModifier{left: false, labels: labels}
return vm
}
type GroupModifier struct {
left bool
labels []string
}
func (gm GroupModifier) String() string {
var group string
if gm.left {
group = "group_left"
} else {
group = "group_right"
}
if len(gm.labels) == 0 {
return group
}
return fmt.Sprintf("%s(%s)", group, strings.Join(gm.labels, ", "))
}
// --- 聚合操作符, see https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators
type AggregationOp struct {
operator string // sum, min, max, avg, topk, count, quantile ...
operand Node // aggregate a single instant vector
clause *AggregationClause // 可选,比如 by(code)
parameter *Scalar // only required for count_values, quantile, topk and bottomk.
}
func NewAggregationOp(operator string) AggregationOp {
return AggregationOp{operator: operator}
}
func (ao AggregationOp) Operand(operand Node) AggregationOp {
ao.operand = operand
return ao
}
func (ao AggregationOp) By(labels ...string) AggregationOp {
ao.clause = &AggregationClause{keyword: "by", labels: labels}
return ao
}
func (ao AggregationOp) Without(labels ...string) AggregationOp {
ao.clause = &AggregationClause{keyword: "without", labels: labels}
return ao
}
func (ao AggregationOp) WithParameter(param Scalar) AggregationOp {
ao.parameter = ¶m
return ao
}
var _ Node = (*AggregationOp)(nil)
func (ao AggregationOp) Self() string {
s := ao.operator
if ao.clause != nil {
s += " " + ao.clause.String()
}
return s
}
func (ao AggregationOp) String() string {
if ao.parameter != nil {
return fmt.Sprintf("%s (%f, %s)", ao.Self(), *ao.parameter, ao.operand.String())
}
return fmt.Sprintf("%s (%s)", ao.Self(), ao.operand.String())
}
func (ao AggregationOp) Children() []Node {
return []Node{ao.operand}
}
type AggregationClause struct {
keyword string // by, without
labels []string
}
func (ac AggregationClause) String() string {
return fmt.Sprintf("%s (%s)", ac.keyword, strings.Join(ac.labels, ", "))
}
// --- Wrap origin node with parenthesis
type Parenthesis struct {
Node
}
func (p Parenthesis) String() string {
return fmt.Sprintf("(%s)", p.Node.String())
}
// --- Scalar, see https://prometheus.io/docs/prometheus/latest/querying/basics/#float-literals
type Scalar Node
// --- 浮点数表示的标量
type Float float64
var _ Scalar = (*Float)(nil)
func (f Float) String() string {
return f.Self()
}
// TODO: 处理显示的精度
func (f Float) Self() string {
return fmt.Sprintf("%.4f", f)
}
func (f Float) Children() []Node {
return nil
}
// --- 整型表示的标量
type Int int
var _ Scalar = (*Int)(nil)
func (i Int) String() string {
return i.Self()
}
func (i Int) Self() string {
return fmt.Sprintf("%d", i)
}
func (i Int) Children() []Node {
return nil
}