-
Notifications
You must be signed in to change notification settings - Fork 12
/
remote_write_test.go
325 lines (314 loc) · 11.1 KB
/
remote_write_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
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
package remotewrite
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
"github.com/golang/protobuf/proto"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/require"
)
func TestEvaluateTemplate(t *testing.T) {
testcases := []struct {
template string
value int
result string
expectedError string
}{
{template: "something ${series_id} else", value: 12, result: "something 12 else"},
{template: "something ${series_id else", expectedError: "unsupported template"},
{template: "something ${series_id/6} else", value: 12, result: "something 2 else"},
{template: "something ${series_id/6 else", expectedError: "closing bracket"},
{template: "something ${series_id%6} else", value: 12, result: "something 0 else"},
{template: "something ${series_id%6 else", expectedError: "closing bracket"},
{template: "something ${series_id*6} else", expectedError: "unsupported template"},
{template: "something else", result: "something else"},
}
for _, testcase := range testcases {
testcase := testcase
t.Run(fmt.Sprintf("template=%q,value=%d", testcase.template, testcase.value), func(t *testing.T) {
compiled, err := compileTemplate(testcase.template)
if testcase.expectedError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), testcase.expectedError)
return
}
require.NoError(t, err)
result := string(compiled.AppendByte(nil, testcase.value))
require.Equal(t, testcase.result, result)
})
}
}
func TestGenerateFromTemplates(t *testing.T) {
type args struct {
minValue int
maxValue int
timestamp int64
minSeriesID int
maxSeriesID int
labelsTemplate map[string]string
}
type want struct {
valueMin float64
valueMax float64
series []prompb.TimeSeries
}
tests := []struct {
name string
args args
want want
}{
{
name: "11th batch of 5",
args: args{
minValue: 123,
maxValue: 133,
timestamp: 123456789,
minSeriesID: 50,
maxSeriesID: 55,
labelsTemplate: map[string]string{
"__name__": "k6_generated_metric_${series_id}",
"series_id": "${series_id}",
"cardinality_1e1": "${series_id/10}",
"cardinality_1e3": "${series_id/1000}",
"cardinality_2": "${series_id%2}",
"cardinality_10": "${series_id%10}",
},
},
want: want{
valueMin: 123,
valueMax: 133,
series: []prompb.TimeSeries{
{
Labels: []prompb.Label{
{Name: "__name__", Value: "k6_generated_metric_50"},
{Name: "cardinality_10", Value: "0"},
{Name: "cardinality_1e1", Value: "5"},
{Name: "cardinality_1e3", Value: "0"},
{Name: "cardinality_2", Value: "0"},
{Name: "series_id", Value: "50"},
},
Samples: []prompb.Sample{{Timestamp: 123456789}},
}, {
Labels: []prompb.Label{
{Name: "__name__", Value: "k6_generated_metric_51"},
{Name: "cardinality_10", Value: "1"},
{Name: "cardinality_1e1", Value: "5"},
{Name: "cardinality_1e3", Value: "0"},
{Name: "cardinality_2", Value: "1"},
{Name: "series_id", Value: "51"},
},
Samples: []prompb.Sample{{Timestamp: 123456789}},
}, {
Labels: []prompb.Label{
{Name: "__name__", Value: "k6_generated_metric_52"},
{Name: "cardinality_10", Value: "2"},
{Name: "cardinality_1e1", Value: "5"},
{Name: "cardinality_1e3", Value: "0"},
{Name: "cardinality_2", Value: "0"},
{Name: "series_id", Value: "52"},
},
Samples: []prompb.Sample{{Timestamp: 123456789}},
}, {
Labels: []prompb.Label{
{Name: "__name__", Value: "k6_generated_metric_53"},
{Name: "cardinality_10", Value: "3"},
{Name: "cardinality_1e1", Value: "5"},
{Name: "cardinality_1e3", Value: "0"},
{Name: "cardinality_2", Value: "1"},
{Name: "series_id", Value: "53"},
},
Samples: []prompb.Sample{{Timestamp: 123456789}},
}, {
Labels: []prompb.Label{
{Name: "__name__", Value: "k6_generated_metric_54"},
{Name: "cardinality_10", Value: "4"},
{Name: "cardinality_1e1", Value: "5"},
{Name: "cardinality_1e3", Value: "0"},
{Name: "cardinality_2", Value: "0"},
{Name: "series_id", Value: "54"},
},
Samples: []prompb.Sample{{Timestamp: 123456789}},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().Unix()))
compiled, err := compileLabelTemplates(tt.args.labelsTemplate)
require.NoError(t, err)
buf := generateFromPrecompiledTemplates(r, tt.args.minValue, tt.args.maxValue, tt.args.timestamp, tt.args.minSeriesID, tt.args.maxSeriesID, compiled)
req := new(prompb.WriteRequest)
require.NoError(t, proto.Unmarshal(buf.Bytes(), req))
got := req.Timeseries
require.NoError(t, err)
if len(got) != len(tt.want.series) {
t.Errorf("Differing length, want: %d, got: %d", len(tt.want.series), len(got))
}
for seriesId := range got {
if !reflect.DeepEqual(got[seriesId].Labels, tt.want.series[seriesId].Labels) {
t.Errorf("Unexpected labels in series %d, want: %v, got: %v", seriesId, tt.want.series[seriesId].Labels, got[seriesId].Labels)
}
if got[seriesId].Samples[0].Timestamp != tt.want.series[seriesId].Samples[0].Timestamp {
t.Errorf("Unexpected timestamp in series %d, want: %d, got: %d", seriesId, tt.want.series[seriesId].Samples[0].Timestamp, got[seriesId].Samples[0].Timestamp)
}
if got[seriesId].Samples[0].Value < tt.want.valueMin || got[seriesId].Samples[0].Value > tt.want.valueMax {
t.Errorf("Unexpected value in series %d, want: %f-%f, got: %f", seriesId, tt.want.valueMin, tt.want.valueMax, got[seriesId].Samples[0].Value)
}
}
})
}
}
// this test that the prompb stream marshalling implementation produces the same result as the upstream one
func TestStreamEncoding(t *testing.T) {
seed := time.Now().Unix()
t.Logf("seed=%d", seed)
r := rand.New(rand.NewSource(seed))
timestamp := int64(valueBetween(r, 10, 100)) // timestamp
r = rand.New(rand.NewSource(seed)) // reset
minValue := 10
maxValue := 100000
// this is the upstream encoding. It is purposefully this "handwritten"
d, _ := proto.Marshal(&prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: (timestamp),
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 7 thing"},
{Name: "forth", Value: "some 15 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 1 thing"},
{Name: "third", Value: "some 1 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 8 thing"},
{Name: "forth", Value: "some 16 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 1 thing"},
{Name: "third", Value: "some 0 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 8 thing"},
{Name: "forth", Value: "some 17 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 1 thing"},
{Name: "third", Value: "some 1 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 9 thing"},
{Name: "forth", Value: "some 18 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 1 thing"},
{Name: "third", Value: "some 0 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 9 thing"},
{Name: "forth", Value: "some 19 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 1 thing"},
{Name: "third", Value: "some 1 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 10 thing"},
{Name: "forth", Value: "some 20 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 2 thing"},
{Name: "third", Value: "some 0 thing"},
},
},
{
Samples: []prompb.Sample{{
Value: valueBetween(r, minValue, maxValue),
Timestamp: timestamp,
}},
Labels: []prompb.Label{
{Name: "fifth", Value: "some 10 thing"},
{Name: "forth", Value: "some 21 thing"},
{Name: "here", Value: "else"},
{Name: "here2", Value: "else2"},
{Name: "sixth", Value: "some 2 thing"},
{Name: "third", Value: "some 1 thing"},
},
},
},
})
r = rand.New(rand.NewSource(seed)) // reset
template, err := compileLabelTemplates(map[string]string{
"here": "else",
"here2": "else2",
"third": "some ${series_id%2} thing",
"forth": "some ${series_id} thing",
"fifth": "some ${series_id/2} thing",
"sixth": "some ${series_id/10} thing",
})
require.NoError(t, err)
buf := generateFromPrecompiledTemplates(r, minValue, maxValue, timestamp, 15, 22, template)
b := buf.Bytes()
require.Equal(t, d, b)
}
func BenchmarkWriteFor(b *testing.B) {
tsBuf := new(bytes.Buffer)
template, err := compileLabelTemplates(map[string]string{
"__name__": "k6_generated_metric_${series_id/1000}", // Name of the series.
"series_id": "${series_id}", // Each value of this label will match 1 series.
"cardinality_1e1": "${series_id/10}", // Each value of this label will match 10 series.
"cardinality_1e2": "${series_id/100}", // Each value of this label will match 100 series.
"cardinality_1e3": "${series_id/1000}", // Each value of this label will match 1000 series.
"cardinality_1e4": "${series_id/10000}", // Each value of this label will match 10000 series.
"cardinality_1e5": "${series_id/100000}", // Each value of this label will match 100000 series.
"cardinality_1e6": "${series_id/1000000}", // Each value of this label will match 1000000 series.
"cardinality_1e7": "${series_id/10000000}", // Each value of this label will match 10000000 series.
"cardinality_1e8": "${series_id/100000000}", // Each value of this label will match 100000000 series.
"cardinality_1e9": "${series_id/1000000000}", // Each value of this label will match 1000000000 series.
})
require.NoError(b, err)
template.writeFor(tsBuf, 15, 15, 234)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
template.writeFor(tsBuf, 15, i, 234)
}
}