-
Notifications
You must be signed in to change notification settings - Fork 5
/
metricquery_test.go
112 lines (106 loc) · 2.81 KB
/
metricquery_test.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
package ddqp
import (
"testing"
"github.com/alecthomas/repr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_MetricQuery(t *testing.T) {
parser := NewMetricQueryParser()
tests := []struct {
name string
query string
wantErr bool
printAST bool // For debugging, can opt in to print AST
}{
// Simple passing example. Guaranteed to have all parts of a query
{
name: "simple query",
query: "sum:namespace.metric.name{foo:bar} by {foo}",
wantErr: false,
printAST: false,
},
// Simple failing example. Guaranteed to fail because missing the aggregator
{
name: "fail due to no aggregator",
query: "namespace.metric.name{foo:bar, baz:bang} by {foo,bar}",
wantErr: true,
printAST: false,
},
{
name: "filter by asterisk",
query: "sum:namespace.metric.name{*} by {foo,bar}",
wantErr: false,
printAST: false,
},
{
name: "filer by partial asterisk",
query: "sum:namespace.metric.name{foo:bar-*} by {foo,bar}",
wantErr: false,
printAST: false,
},
{
name: "test underscores in metric name",
query: "sum:namespace.metric_name{foo:bar} by {baz}",
wantErr: false,
printAST: false,
},
{
name: "test hyphens in filters and groupings",
query: "sum:prometheus_metric_source{foo:bar-bar, baz:bang} by {fizz-buzz,bang}",
wantErr: false,
printAST: false,
},
{
name: "test numbers in the metric name",
query: "sum:prometheus_metric_source_1{foo:bar-bar, baz:bang} by {fizz-buzz,bang}",
wantErr: false,
printAST: false,
},
{
name: "test numbers in the filters and grouping",
query: "sum:prometheus_metric_source_1{foo:bar-bar-1, baz:bang_2} by {fizz-buzz3,bang}",
wantErr: false,
printAST: false,
},
{
name: "test docs example query",
query: "avg:system.cpu.user{env:staging AND (availability-zone:us-east-1a OR availability-zone:us-east-1c)} by {availability-zone}",
wantErr: false,
printAST: false,
},
{
name: "test less than condition in count",
query: "count(v: v<=1):metric.name{foo:bar}",
wantErr: false,
printAST: false,
},
{
name: "test greater than condition in count",
query: "count(v: v>=1.53):metric.name{foo:bar}",
wantErr: false,
printAST: false,
},
{
name: "test equal than condition in count",
query: "count(v: v>=100):metric.name{foo:bar}",
wantErr: false,
printAST: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ast, err := parser.Parse(tt.query)
if (err != nil) != tt.wantErr {
require.NoError(t, err)
}
if tt.printAST {
repr.Println(ast)
}
// Check to make sure we're able to restringify
if !tt.wantErr {
assert.Equal(t, tt.query, ast.String())
}
})
}
}