-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmetrics_receiver_honor_timestamp_test.go
532 lines (496 loc) · 18.2 KB
/
metrics_receiver_honor_timestamp_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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package prometheusreceiver
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
var (
timeNow = time.Now()
ts1 = timeNow.Add(10 * time.Second).UnixMilli()
ts2 = timeNow.Add(20 * time.Second).UnixMilli()
ts3 = timeNow.Add(30 * time.Second).UnixMilli()
ts4 = timeNow.Add(40 * time.Second).UnixMilli()
ts5 = timeNow.Add(50 * time.Second).UnixMilli()
ts6 = timeNow.Add(60 * time.Second).UnixMilli()
ts7 = timeNow.Add(70 * time.Second).UnixMilli()
ts8 = timeNow.Add(80 * time.Second).UnixMilli()
ts9 = timeNow.Add(90 * time.Second).UnixMilli()
ts10 = timeNow.Add(100 * time.Second).UnixMilli()
ts11 = timeNow.Add(110 * time.Second).UnixMilli()
ts12 = timeNow.Add(120 * time.Second).UnixMilli()
ts13 = timeNow.Add(130 * time.Second).UnixMilli()
ts14 = timeNow.Add(140 * time.Second).UnixMilli()
ts15 = timeNow.Add(150 * time.Second).UnixMilli()
)
var onlyOnce sync.Once
// honorTimestampsPage2 has lower values for metrics than honorTimestampsPage1.
// So, the start_timestamp should get reset.
var honorTimestampsPage1 = `
# HELP go_threads Number of OS threads created
# TYPE go_thread gauge
go_threads 19 %v
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 100 %v
http_requests_total{method="post",code="400"} 5 %v
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 1000 %v
http_request_duration_seconds_bucket{le="0.5"} 1500 %v
http_request_duration_seconds_bucket{le="1"} 2000 %v
http_request_duration_seconds_bucket{le="+Inf"} 2500 %v
http_request_duration_seconds_sum 5000 %v
http_request_duration_seconds_count 2500 %v
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 1 %v
rpc_duration_seconds{quantile="0.9"} 5 %v
rpc_duration_seconds{quantile="0.99"} 8 %v
rpc_duration_seconds_sum 5000 %v
rpc_duration_seconds_count 1000 %v
`
var honorTimestampsPage2 = `
# HELP go_threads Number of OS threads created
# TYPE go_thread gauge
go_threads 18 %v
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 99 %v
http_requests_total{method="post",code="400"} 3 %v
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 900 %v
http_request_duration_seconds_bucket{le="0.5"} 1400 %v
http_request_duration_seconds_bucket{le="1"} 1900 %v
http_request_duration_seconds_bucket{le="+Inf"} 2400 %v
http_request_duration_seconds_sum 4950 %v
http_request_duration_seconds_count 2400 %v
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 1 %v
rpc_duration_seconds{quantile="0.9"} 6 %v
rpc_duration_seconds{quantile="0.99"} 8 %v
rpc_duration_seconds_sum 4980 %v
rpc_duration_seconds_count 900 %v
`
// honorTimestampsPage3 has higher value than previous scrape.
// So, start_timestamp should not be reset for honorTimestampsPage3
var honorTimestampsPage3 = `
# HELP go_threads Number of OS threads created
# TYPE go_thread gauge
go_threads 19 %v
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 100 %v
http_requests_total{method="post",code="400"} 5 %v
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 1000 %v
http_request_duration_seconds_bucket{le="0.5"} 1500 %v
http_request_duration_seconds_bucket{le="1"} 2000 %v
http_request_duration_seconds_bucket{le="+Inf"} 2500 %v
http_request_duration_seconds_sum 5000 %v
http_request_duration_seconds_count 2500 %v
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 1 %v
rpc_duration_seconds{quantile="0.9"} 5 %v
rpc_duration_seconds{quantile="0.99"} 8 %v
rpc_duration_seconds_sum 5000 %v
rpc_duration_seconds_count 1000 %v
`
// TestHonorTimeStampsWithTrue validates honor_timestamp configuration
// where all metricFamilies (and each datapoint) in the testdata has explicit timestamps.
// TestHonorTimeStampsWithTrue does not check for scenario where timestamp is not provided
// to all metric families in a scrape- as those situations are rare though valid.
// TestHonorTimeStampsWithTrue has testdata such that
// For valid data- Start_timestamps should not be ahead of point_timestamps.
// TestHonorTimeStampsWithTrue validates:
// - For initial scrape, start_timestamp is same as point timestamp,
// - Start_timestamp should be the explicit timestamp of first-time a particular metric is seen
// - Start_timestamp should get reset if current scrape has lower value than previous scrape
func TestHonorTimeStampsWithTrue(t *testing.T) {
setMetricsTimestamp()
targets := []*testData{
{
name: "target1",
pages: []mockPrometheusResponse{
{code: 200, data: honorTimestampsPage1},
{code: 200, data: honorTimestampsPage2},
{code: 200, data: honorTimestampsPage3},
},
validateFunc: verifyHonorTimeStampsTrue,
},
}
testComponent(t, targets, nil)
}
// TestHonorTimeStampsWithFalse validates that with honor_timestamp config set to false,
// valid testdata provided with explicit timestamps does not get honored.
func TestHonorTimeStampsWithFalse(t *testing.T) {
setMetricsTimestamp()
targets := []*testData{
{
name: "target1",
pages: []mockPrometheusResponse{
{code: 200, data: honorTimestampsPage1},
{code: 200, data: honorTimestampsPage2},
},
validateFunc: verifyHonorTimeStampsFalse,
},
}
testComponent(t, targets, nil, func(cfg *PromConfig) {
for _, scrapeConfig := range cfg.ScrapeConfigs {
scrapeConfig.HonorTimestamps = false
}
})
}
func setMetricsTimestamp() {
onlyOnce.Do(func() {
honorTimestampsPage1 = fmt.Sprintf(honorTimestampsPage1,
ts1, // timestamp for gauge
ts2, ts3, // timestamp for counter
ts4, ts4, ts4, ts4, ts4, ts4, // timestamp for histogram
ts5, ts5, ts5, ts5, ts5, // timestamp for summary
)
honorTimestampsPage2 = fmt.Sprintf(honorTimestampsPage2,
ts6, // timestamp for gauge
ts7, ts8, // timestamp for counter
ts9, ts9, ts9, ts9, ts9, ts9, // timestamp for histogram
ts10, ts10, ts10, ts10, ts10, // timestamp for summary
)
honorTimestampsPage3 = fmt.Sprintf(honorTimestampsPage3,
ts11, // timestamp for gauge
ts12, ts13, // timestamp for counter
ts14, ts14, ts14, ts14, ts14, ts14, // timestamp for histogram
ts15, ts15, ts15, ts15, ts15, // timestamp for summary
)
})
}
func verifyHonorTimeStampsTrue(t *testing.T, td *testData, resourceMetrics []pmetric.ResourceMetrics) {
verifyNumValidScrapeResults(t, td, resourceMetrics)
m1 := resourceMetrics[0]
// m1 has 4 metrics + 5 internal scraper metrics
assert.Equal(t, 9, metricsCount(m1))
wantAttributes := td.attributes
e1 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pmetric.MetricTypeGauge),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts1))),
compareDoubleValue(19),
},
},
}),
assertMetricPresent("http_requests_total",
compareMetricType(pmetric.MetricTypeSum),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts2))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts2))),
compareDoubleValue(100),
compareAttributes(map[string]string{"method": "post", "code": "200"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts3))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts3))),
compareDoubleValue(5),
compareAttributes(map[string]string{"method": "post", "code": "400"}),
},
},
}),
assertMetricPresent("http_request_duration_seconds",
compareMetricType(pmetric.MetricTypeHistogram),
compareMetricUnit(""),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts4))),
compareHistogramTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts4))),
compareHistogram(2500, 5000, []float64{0.05, 0.5, 1}, []uint64{1000, 500, 500, 500}),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pmetric.MetricTypeSummary),
compareMetricUnit(""),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts5))),
compareSummaryTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts5))),
compareSummary(1000, 5000, [][]float64{{0.01, 1}, {0.9, 5}, {0.99, 8}}),
},
},
}),
}
doCompare(t, "scrape-honorTimestamp-1", wantAttributes, m1, e1)
m2 := resourceMetrics[1]
// m1 has 4 metrics + 5 internal scraper metrics
assert.Equal(t, 9, metricsCount(m2))
e2 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pmetric.MetricTypeGauge),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts6))),
compareDoubleValue(18),
},
},
}),
assertMetricPresent("http_requests_total",
compareMetricType(pmetric.MetricTypeSum),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts7))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts7))),
compareDoubleValue(99),
compareAttributes(map[string]string{"method": "post", "code": "200"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts8))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts8))),
compareDoubleValue(3),
compareAttributes(map[string]string{"method": "post", "code": "400"}),
},
},
}),
assertMetricPresent("http_request_duration_seconds",
compareMetricType(pmetric.MetricTypeHistogram),
compareMetricUnit(""),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts9))),
compareHistogramTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts9))),
compareHistogram(2400, 4950, []float64{0.05, 0.5, 1}, []uint64{900, 500, 500, 500}),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pmetric.MetricTypeSummary),
compareMetricUnit(""),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts10))),
compareSummaryTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts10))),
compareSummary(900, 4980, [][]float64{{0.01, 1}, {0.9, 6}, {0.99, 8}}),
},
},
}),
}
doCompare(t, "scrape-honorTimestamp-2", wantAttributes, m2, e2)
m3 := resourceMetrics[2]
// m1 has 4 metrics + 5 internal scraper metrics
assert.Equal(t, 9, metricsCount(m3))
e3 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pmetric.MetricTypeGauge),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts11))),
compareDoubleValue(19),
},
},
}),
assertMetricPresent("http_requests_total",
compareMetricType(pmetric.MetricTypeSum),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts7))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts12))),
compareDoubleValue(100),
compareAttributes(map[string]string{"method": "post", "code": "200"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts8))),
compareTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts13))),
compareDoubleValue(5),
compareAttributes(map[string]string{"method": "post", "code": "400"}),
},
},
}),
assertMetricPresent("http_request_duration_seconds",
compareMetricType(pmetric.MetricTypeHistogram),
compareMetricUnit(""),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts9))),
compareHistogramTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts14))),
compareHistogram(2500, 5000, []float64{0.05, 0.5, 1}, []uint64{1000, 500, 500, 500}),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pmetric.MetricTypeSummary),
compareMetricUnit(""),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts10))),
compareSummaryTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(ts15))),
compareSummary(1000, 5000, [][]float64{{0.01, 1}, {0.9, 5}, {0.99, 8}}),
},
},
}),
}
doCompare(t, "scrape-honorTimestamp-3", wantAttributes, m3, e3)
}
func verifyHonorTimeStampsFalse(t *testing.T, td *testData, resourceMetrics []pmetric.ResourceMetrics) {
verifyNumValidScrapeResults(t, td, resourceMetrics)
m1 := resourceMetrics[0]
// m1 has 4 metrics + 5 internal scraper metrics
assert.Equal(t, 9, metricsCount(m1))
wantAttributes := td.attributes
metrics1 := m1.ScopeMetrics().At(0).Metrics()
ts1 := getTS(metrics1)
e1 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pmetric.MetricTypeGauge),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(19),
},
},
}),
assertMetricPresent("http_requests_total",
compareMetricType(pmetric.MetricTypeSum),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(ts1),
compareTimestamp(ts1),
compareDoubleValue(100),
compareAttributes(map[string]string{"method": "post", "code": "200"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(ts1),
compareTimestamp(ts1),
compareDoubleValue(5),
compareAttributes(map[string]string{"method": "post", "code": "400"}),
},
},
}),
assertMetricPresent("http_request_duration_seconds",
compareMetricType(pmetric.MetricTypeHistogram),
compareMetricUnit(""),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(ts1),
compareHistogramTimestamp(ts1),
compareHistogram(2500, 5000, []float64{0.05, 0.5, 1}, []uint64{1000, 500, 500, 500}),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pmetric.MetricTypeSummary),
compareMetricUnit(""),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(ts1),
compareSummaryTimestamp(ts1),
compareSummary(1000, 5000, [][]float64{{0.01, 1}, {0.9, 5}, {0.99, 8}}),
},
},
}),
}
doCompare(t, "scrape-honorTimestamp-1", wantAttributes, m1, e1)
m2 := resourceMetrics[1]
// m2 has 4 metrics + 5 internal scraper metrics
assert.Equal(t, 9, metricsCount(m2))
metricsScrape2 := m2.ScopeMetrics().At(0).Metrics()
ts2 := getTS(metricsScrape2)
e2 := []testExpectation{
assertMetricPresent("go_threads",
compareMetricType(pmetric.MetricTypeGauge),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts2),
compareDoubleValue(18),
},
},
}),
assertMetricPresent("http_requests_total",
compareMetricType(pmetric.MetricTypeSum),
compareMetricUnit(""),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(ts2),
compareTimestamp(ts2),
compareDoubleValue(99),
compareAttributes(map[string]string{"method": "post", "code": "200"}),
},
},
{
numberPointComparator: []numberPointComparator{
compareStartTimestamp(ts2),
compareTimestamp(ts2),
compareDoubleValue(3),
compareAttributes(map[string]string{"method": "post", "code": "400"}),
},
},
}),
assertMetricPresent("http_request_duration_seconds",
compareMetricType(pmetric.MetricTypeHistogram),
compareMetricUnit(""),
[]dataPointExpectation{
{
histogramPointComparator: []histogramPointComparator{
compareHistogramStartTimestamp(ts2),
compareHistogramTimestamp(ts2),
compareHistogram(2400, 4950, []float64{0.05, 0.5, 1}, []uint64{900, 500, 500, 500}),
},
},
}),
assertMetricPresent("rpc_duration_seconds",
compareMetricType(pmetric.MetricTypeSummary),
compareMetricUnit(""),
[]dataPointExpectation{
{
summaryPointComparator: []summaryPointComparator{
compareSummaryStartTimestamp(ts2),
compareSummaryTimestamp(ts2),
compareSummary(900, 4980, [][]float64{{0.01, 1}, {0.9, 6}, {0.99, 8}}),
},
},
}),
}
doCompare(t, "scrape-honorTimestamp-2", wantAttributes, m2, e2)
}