forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_test.go
More file actions
363 lines (341 loc) · 8.96 KB
/
helper_test.go
File metadata and controls
363 lines (341 loc) · 8.96 KB
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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheusremotewriteexporter
import (
"strconv"
"testing"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/consumer/pdata"
common "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlp "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
)
// Test_validateMetrics checks validateMetrics return true if a type and temporality combination is valid, false
// otherwise.
func Test_validateMetrics(t *testing.T) {
// define a single test
type combTest struct {
name string
metric *otlp.Metric
want bool
}
tests := []combTest{}
// append true cases
for k, validMetric := range validMetrics1 {
name := "valid_" + k
tests = append(tests, combTest{
name,
validMetric,
true,
})
}
// append nil case
tests = append(tests, combTest{"invalid_nil", nil, false})
for k, invalidMetric := range invalidMetrics {
name := "valid_" + k
tests = append(tests, combTest{
name,
invalidMetric,
false,
})
}
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := validateMetrics(tt.metric)
assert.Equal(t, tt.want, got)
})
}
}
// Test_addSample checks addSample updates the map it receives correctly based on the sample and Label
// set it receives.
// Test cases are two samples belonging to the same TimeSeries, two samples belong to different TimeSeries, and nil
// case.
func Test_addSample(t *testing.T) {
type testCase struct {
metric *otlp.Metric
sample prompb.Sample
labels []prompb.Label
}
tests := []struct {
name string
orig map[string]*prompb.TimeSeries
testCase []testCase
want map[string]*prompb.TimeSeries
}{
{
"two_points_same_ts_same_metric",
map[string]*prompb.TimeSeries{},
[]testCase{
{validMetrics1[validDoubleGauge],
getSample(floatVal1, msTime1),
promLbs1,
},
{
validMetrics1[validDoubleGauge],
getSample(floatVal2, msTime2),
promLbs1,
},
},
twoPointsSameTs,
},
{
"two_points_different_ts_same_metric",
map[string]*prompb.TimeSeries{},
[]testCase{
{validMetrics1[validIntGauge],
getSample(float64(intVal1), msTime1),
promLbs1,
},
{validMetrics1[validIntGauge],
getSample(float64(intVal1), msTime2),
promLbs2,
},
},
twoPointsDifferentTs,
},
}
t.Run("nil_case", func(t *testing.T) {
tsMap := map[string]*prompb.TimeSeries{}
addSample(tsMap, nil, nil, nil)
assert.Exactly(t, tsMap, map[string]*prompb.TimeSeries{})
})
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
addSample(tt.orig, &tt.testCase[0].sample, tt.testCase[0].labels, tt.testCase[0].metric)
addSample(tt.orig, &tt.testCase[1].sample, tt.testCase[1].labels, tt.testCase[1].metric)
assert.Exactly(t, tt.want, tt.orig)
})
}
}
// Test_timeSeries checks timeSeriesSignature returns consistent and unique signatures for a distinct label set and
// metric type combination.
func Test_timeSeriesSignature(t *testing.T) {
tests := []struct {
name string
lbs []prompb.Label
metric *otlp.Metric
want string
}{
{
"int64_signature",
promLbs1,
validMetrics1[validIntGauge],
strconv.Itoa(int(pdata.MetricDataTypeIntGauge)) + lb1Sig,
},
{
"histogram_signature",
promLbs2,
validMetrics1[validIntHistogram],
strconv.Itoa(int(pdata.MetricDataTypeIntHistogram)) + lb2Sig,
},
{
"unordered_signature",
getPromLabels(label22, value22, label21, value21),
validMetrics1[validIntHistogram],
strconv.Itoa(int(pdata.MetricDataTypeIntHistogram)) + lb2Sig,
},
// descriptor type cannot be nil, as checked by validateMetrics
{
"nil_case",
nil,
validMetrics1[validIntHistogram],
strconv.Itoa(int(pdata.MetricDataTypeIntHistogram)),
},
}
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualValues(t, tt.want, timeSeriesSignature(tt.metric, &tt.lbs))
})
}
}
// Test_createLabelSet checks resultant label names are sanitized and label in extra overrides label in labels if
// collision happens. It does not check whether labels are not sorted
func Test_createLabelSet(t *testing.T) {
tests := []struct {
name string
orig []common.StringKeyValue
externalLabels map[string]string
extras []string
want []prompb.Label
}{
{
"labels_clean",
lbs1,
map[string]string{},
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label31, value31, label32, value32),
},
{
"labels_duplicate_in_extras",
lbs1,
map[string]string{},
[]string{label11, value31},
getPromLabels(label11, value31, label12, value12),
},
{
"labels_dirty",
lbs1Dirty,
map[string]string{},
[]string{label31 + dirty1, value31, label32, value32},
getPromLabels(label11+"_", value11, "key_"+label12, value12, label31+"_", value31, label32, value32),
},
{
"no_original_case",
nil,
nil,
[]string{label31, value31, label32, value32},
getPromLabels(label31, value31, label32, value32),
},
{
"empty_extra_case",
lbs1,
map[string]string{},
[]string{"", ""},
getPromLabels(label11, value11, label12, value12, "", ""),
},
{
"single_left_over_case",
lbs1,
map[string]string{},
[]string{label31, value31, label32},
getPromLabels(label11, value11, label12, value12, label31, value31),
},
{
"valid_external_labels",
lbs1,
exlbs1,
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label41, value41, label31, value31, label32, value32),
},
{
"overwritten_external_labels",
lbs1,
exlbs2,
[]string{label31, value31, label32, value32},
getPromLabels(label11, value11, label12, value12, label31, value31, label32, value32),
},
}
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatch(t, tt.want, createLabelSet(tt.orig, tt.externalLabels, tt.extras...))
})
}
}
// Tes_getPromMetricName checks if OTLP metric names are converted to Cortex metric names correctly.
// Test cases are empty namespace, monotonic metrics that require a total suffix, and metric names that contains
// invalid characters.
func Test_getPromMetricName(t *testing.T) {
tests := []struct {
name string
metric *otlp.Metric
ns string
want string
}{
{
"nil_case",
nil,
ns1,
"",
},
{
"normal_case",
validMetrics1[validDoubleGauge],
ns1,
"test_ns_" + validDoubleGauge,
},
{
"empty_namespace",
validMetrics1[validDoubleGauge],
"",
validDoubleGauge,
},
{
"total_suffix",
validMetrics1[validIntSum],
ns1,
"test_ns_" + validIntSum + counterSuffix,
},
{
"dirty_string",
validMetrics2[validIntGaugeDirty],
"7" + ns1,
"key_7test_ns__" + validIntGauge + "_",
},
}
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, getPromMetricName(tt.metric, tt.ns))
})
}
}
// Test_batchTimeSeries checks batchTimeSeries return the correct number of requests
// depending on byte size.
func Test_batchTimeSeries(t *testing.T) {
// First we will instantiate a dummy TimeSeries instance to pass into both the export call and compare the http request
labels := getPromLabels(label11, value11, label12, value12, label21, value21, label22, value22)
sample1 := getSample(floatVal1, msTime1)
sample2 := getSample(floatVal2, msTime2)
sample3 := getSample(floatVal3, msTime3)
ts1 := getTimeSeries(labels, sample1, sample2)
ts2 := getTimeSeries(labels, sample1, sample2, sample3)
tsMap1 := getTimeseriesMap([]*prompb.TimeSeries{})
tsMap2 := getTimeseriesMap([]*prompb.TimeSeries{ts1})
tsMap3 := getTimeseriesMap([]*prompb.TimeSeries{ts1, ts2})
tests := []struct {
name string
tsMap map[string]*prompb.TimeSeries
maxBatchByteSize int
numExpectedRequests int
returnErr bool
}{
{
"no_timeseries",
tsMap1,
100,
-1,
true,
},
{
"normal_case",
tsMap2,
300,
1,
false,
},
{
"two_requests",
tsMap3,
300,
2,
false,
},
}
// run tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
requests, err := batchTimeSeries(tt.tsMap, tt.maxBatchByteSize)
if tt.returnErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.numExpectedRequests, len(requests))
})
}
}