-
Notifications
You must be signed in to change notification settings - Fork 137
/
serializers_test.go
151 lines (125 loc) · 4.36 KB
/
serializers_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
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
package main
import (
"math"
"testing"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
)
func NewWriteRequest() *prompb.WriteRequest {
return &prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{
{Name: "__name__", Value: "foo"},
{Name: "labelfoo", Value: "label-bar"},
},
Samples: []prompb.Sample{
{Timestamp: 0, Value: 456},
{Timestamp: 10000, Value: math.Inf(1)},
},
},
},
}
}
func TestSerializeEmptyTimeseriesToJSON(t *testing.T) {
request := &prompb.WriteRequest{}
serializer, err := NewJSONSerializer()
assert.Nil(t, err)
data, err := Serialize(serializer, request)
assert.Nil(t, err)
assert.Len(t, data, 0)
assert.NotNil(t, data)
}
func TestSerializeToJSON(t *testing.T) {
serializer, err := NewJSONSerializer()
assert.Nil(t, err)
writeRequest := NewWriteRequest()
output, err := Serialize(serializer, writeRequest)
assert.Len(t, output["metrics"], 2)
assert.Nil(t, err)
expectedSamples := []string{
"{\"value\":\"456\",\"timestamp\":\"1970-01-01T00:00:00Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
"{\"value\":\"+Inf\",\"timestamp\":\"1970-01-01T00:00:10Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
}
for i, metric := range output["metrics"] {
assert.JSONEqf(t, expectedSamples[i], string(metric[:]), "wrong json serialization found")
}
}
func TestSerializeEmptyTimeseriesToAvroJSON(t *testing.T) {
request := &prompb.WriteRequest{}
serializer, err := NewAvroJSONSerializer("schemas/metric.avsc")
assert.Nil(t, err)
data, err := Serialize(serializer, request)
assert.Nil(t, err)
assert.Len(t, data, 0)
assert.NotNil(t, data)
}
func TestSerializeToAvro(t *testing.T) {
serializer, err := NewAvroJSONSerializer("schemas/metric.avsc")
assert.Nil(t, err)
writeRequest := NewWriteRequest()
output, err := Serialize(serializer, writeRequest)
assert.Len(t, output["metrics"], 2)
assert.Nil(t, err)
expectedSamples := []string{
"{\"value\":\"456\",\"timestamp\":\"1970-01-01T00:00:00Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
"{\"value\":\"+Inf\",\"timestamp\":\"1970-01-01T00:00:10Z\",\"name\":\"foo\",\"labels\":{\"__name__\":\"foo\",\"labelfoo\":\"label-bar\"}}",
}
for i, metric := range output["metrics"] {
assert.JSONEqf(t, expectedSamples[i], string(metric[:]), "wrong json serialization found")
}
}
func TestTemplatedTopic(t *testing.T) {
var err error
topicTemplate, err = parseTopicTemplate("{{ index . \"labelfoo\" | replace \"bar\" \"foo\" | substring 6 -1 }}")
assert.Nil(t, err)
serializer, err := NewJSONSerializer()
assert.Nil(t, err)
writeRequest := NewWriteRequest()
output, err := Serialize(serializer, writeRequest)
for k := range output {
assert.Equal(t, "foo", k, "templated topic failed")
}
}
func TestFilter(t *testing.T) {
rulesText := `['foo{y="2"}','foo', 'bar{x="1"}',
'up{x="1",y="2"}', 'baz{key="valu
e1;value2"}','bar{y="2"}']`
rules, _ := parseMatchList(rulesText)
for _, mf := range rules {
match[mf.GetName()] = mf
}
type TestCase struct {
Name string
Labels map[string]string
Expect bool
}
testList := []TestCase{
{Name: "foo", Labels: map[string]string{"z": "3"}, Expect: true},
{Name: "bar", Labels: map[string]string{"x": "1"}, Expect: true},
{Name: "bar", Labels: map[string]string{"x": "2"}, Expect: false},
{Name: "bar", Labels: map[string]string{"y": "2"}, Expect: true},
{Name: "bar", Labels: map[string]string{"y": "1"}, Expect: false},
{Name: "up", Labels: map[string]string{"x": "1", "y": "2"}, Expect: true},
{Name: "up", Labels: map[string]string{"x": "1", "y": "2", "z": "3"}, Expect: true},
{Name: "up", Labels: map[string]string{"x": "2", "y": "1"}, Expect: false},
{Name: "go", Labels: map[string]string{"x": "1", "y": "2"}, Expect: false},
}
for _, tcase := range testList {
assert.Equal(t, tcase.Expect, filter(tcase.Name, tcase.Labels))
}
}
func BenchmarkSerializeToAvroJSON(b *testing.B) {
serializer, _ := NewAvroJSONSerializer("schemas/metric.avsc")
writeRequest := NewWriteRequest()
for n := 0; n < 20000; n++ {
Serialize(serializer, writeRequest)
}
}
func BenchmarkSerializeToJSON(b *testing.B) {
serializer, _ := NewJSONSerializer()
writeRequest := NewWriteRequest()
for n := 0; n < 20000; n++ {
Serialize(serializer, writeRequest)
}
}