forked from bukalapak/prometheus-aggregator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
106 lines (100 loc) · 2.85 KB
/
parser_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
package main
import (
"strings"
"testing"
a "github.com/stretchr/testify/assert"
)
func Test_SampleParser_Parse_Success(t *testing.T) {
cases := map[string]struct {
in string
exp []sample
}{
"counters with shared labels": {
`service=srvA1;host=hostA;phpVersion=5.6
name_of_1_metric_total|c|labelA=labelValueA;label2=labelValue2|12.345
name_of_2_metric_total|c|56
name_of_3_metric|g|7.3`,
[]sample{
{
name: "name_of_1_metric_total", kind: sampleCounter,
labels: map[string]string{"service": "srvA1", "host": "hostA", "phpVersion": "5.6", "labelA": "labelValueA", "label2": "labelValue2"},
value: 12.345,
},
{
name: "name_of_2_metric_total", kind: sampleCounter,
labels: map[string]string{"service": "srvA1", "host": "hostA", "phpVersion": "5.6"},
value: 56,
},
{
name: "name_of_3_metric", kind: sampleGauge,
labels: map[string]string{"service": "srvA1", "host": "hostA", "phpVersion": "5.6"},
value: 7.3,
},
},
},
"counters withouth shared labels": {
`name_of_1_metric_total|c|labelA=labelValueA;label2=labelValue2|12.345
name_of_2_metric_total|c|56
name_of_3_metric|g|labelA=labelValueA;label2=labelValue2|7.3
name_of_3_metric|g|17.3`,
[]sample{
{
name: "name_of_1_metric_total", kind: sampleCounter,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 12.345,
},
{
name: "name_of_2_metric_total", kind: sampleCounter,
labels: map[string]string{},
value: 56,
},
{
name: "name_of_3_metric", kind: sampleGauge,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 7.3,
},
{
name: "name_of_3_metric", kind: sampleGauge,
labels: map[string]string{},
value: 17.3,
},
},
},
"histogram, linear buckets": {
`name_of_1_metric_seconds|hl|3.3;2.0;5|labelA=labelValueA;label2=labelValue2|12.345`,
[]sample{
{
name: "name_of_1_metric_seconds", kind: sampleHistogramLinear,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 12.345,
histogramDef: []string{"3.3", "2.0", "5"},
},
},
},
"histogram": {
`name_of_2_metric_seconds|h|2.0;2.2;5;7|labelA=labelValueA;label2=labelValue2|12.345`,
[]sample{
{
name: "name_of_2_metric_seconds", kind: sampleHistogram,
labels: map[string]string{"labelA": "labelValueA", "label2": "labelValue2"},
value: 12.345,
histogramDef: []string{"2.0", "2.2", "5", "7"},
},
},
},
}
for k, tc := range cases {
r := strings.NewReader(tc.in)
got, err := parseSample(r)
if !a.NoError(t, err, k) {
continue
}
for i := 0; i < len(tc.exp); i++ {
if len(got) < i+1 {
t.Errorf("[%s] Missing sample no. %d", k, i)
continue
}
a.Equal(t, tc.exp[i], *got[i], k)
}
}
}