This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathprometheus_test.go
611 lines (540 loc) · 16.9 KB
/
prometheus_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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright 2017, OpenCensus 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 prometheus
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"go.opencensus.io/resource"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type mSlice []*stats.Int64Measure
func (measures *mSlice) createAndAppend(name, desc, unit string) {
m := stats.Int64(name, desc, unit)
*measures = append(*measures, m)
}
type vCreator []*view.View
func (vc *vCreator) createAndAppend(name, description string, keys []tag.Key, measure stats.Measure, agg *view.Aggregation) {
v := &view.View{
Name: name,
Description: description,
TagKeys: keys,
Measure: measure,
Aggregation: agg,
}
*vc = append(*vc, v)
}
func TestMetricsEndpointOutput(t *testing.T) {
exporter, err := NewExporter(Options{})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
names := []string{"foo", "bar", "baz"}
var measures mSlice
for _, name := range names {
measures.createAndAppend("tests/"+name, name, "")
}
var vc vCreator
for _, m := range measures {
vc.createAndAppend(m.Name(), m.Description(), nil, m, view.Count())
}
if err := view.Register(vc...); err != nil {
t.Fatalf("failed to create views: %v", err)
}
defer view.Unregister(vc...)
for _, m := range measures {
stats.Record(context.Background(), m.M(1))
}
srv := httptest.NewServer(exporter)
defer srv.Close()
var i int
var output string
for {
time.Sleep(10 * time.Millisecond)
if i == 10 {
t.Fatal("no output at /metrics (100ms wait)")
}
i++
resp, err := http.Get(srv.URL)
if err != nil {
t.Fatalf("failed to get /metrics: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
resp.Body.Close()
output = string(body)
if output != "" {
break
}
}
if strings.Contains(output, "collected before with the same name and label values") {
t.Fatal("metric name and labels being duplicated but must be unique")
}
if strings.Contains(output, "error(s) occurred") {
t.Fatal("error reported by prometheus registry")
}
for _, name := range names {
if !strings.Contains(output, "tests_"+name+" 1") {
t.Fatalf("measurement missing in output: %v", name)
}
}
}
func TestCumulativenessFromHistograms(t *testing.T) {
exporter, err := NewExporter(Options{})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
m := stats.Float64("tests/bills", "payments by denomination", stats.UnitDimensionless)
v := &view.View{
Name: "cash/register",
Description: "this is a test",
Measure: m,
// Intentionally used repeated elements in the ascending distribution.
// to ensure duplicate distribution items are handles.
Aggregation: view.Distribution(1, 5, 5, 5, 5, 10, 20, 50, 100, 250),
}
if err = view.Register(v); err != nil {
t.Fatalf("Register error: %v", err)
}
defer view.Unregister(v)
// Give the reporter ample time to process registration
// <-time.After(10 * reportPeriod)
values := []float64{0.25, 245.67, 12, 1.45, 199.9, 7.69, 187.12}
// We want the results that look like this:
// 1: [0.25] | 1 + prev(i) = 1 + 0 = 1
// 5: [1.45] | 1 + prev(i) = 1 + 1 = 2
// 10: [7.69] | 1 + prev(i) = 1 + 2 = 3
// 20: [12] | 1 + prev(i) = 1 + 3 = 4
// 50: [] | 0 + prev(i) = 0 + 4 = 4
// 100: [] | 0 + prev(i) = 0 + 4 = 4
// 250: [187.12, 199.9, 245.67] | 3 + prev(i) = 3 + 4 = 7
wantLines := []string{
`cash_register_bucket{le="1"} 1`,
`cash_register_bucket{le="5"} 2`,
`cash_register_bucket{le="10"} 3`,
`cash_register_bucket{le="20"} 4`,
`cash_register_bucket{le="50"} 4`,
`cash_register_bucket{le="100"} 4`,
`cash_register_bucket{le="250"} 7`,
`cash_register_bucket{le="+Inf"} 7`,
`cash_register_sum 654.0799999999999`, // Summation of the input values
`cash_register_count 7`,
}
ctx := context.Background()
ms := make([]stats.Measurement, 0, len(values))
for _, value := range values {
mx := m.M(value)
ms = append(ms, mx)
}
stats.Record(ctx, ms...)
// Give the recorder ample time to process recording
// <-time.After(10 * reportPeriod)
cst := httptest.NewServer(exporter)
defer cst.Close()
res, err := http.Get(cst.URL)
if err != nil {
t.Fatalf("http.Get error: %v", err)
}
blob, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("Read body error: %v", err)
}
str := strings.Trim(string(blob), "\n")
lines := strings.Split(str, "\n")
nonComments := make([]string, 0, len(lines))
for _, line := range lines {
if !strings.Contains(line, "#") {
nonComments = append(nonComments, line)
}
}
got := strings.Join(nonComments, "\n")
want := strings.Join(wantLines, "\n")
if got != want {
t.Fatalf("\ngot:\n%s\n\nwant:\n%s\n", got, want)
}
}
func TestHistogramUnorderedBucketBounds(t *testing.T) {
exporter, err := NewExporter(Options{})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
m := stats.Float64("tests/bills", "payments by denomination", stats.UnitDimensionless)
v := &view.View{
Name: "cash/register",
Description: "this is a test",
Measure: m,
// Intentionally used unordered and duplicated elements in the distribution
// to ensure unordered bucket bounds are handled.
Aggregation: view.Distribution(10, 5, 1, 1, 50, 5, 20, 100, 250),
}
if err = view.Register(v); err != nil {
t.Fatalf("Register error: %v", err)
}
defer view.Unregister(v)
// Give the reporter ample time to process registration
// <-time.After(10 * reportPeriod)
values := []float64{0.25, 245.67, 12, 1.45, 199.9, 7.69, 187.12}
// We want the results that look like this:
// 1: [0.25] | 1 + prev(i) = 1 + 0 = 1
// 5: [1.45] | 1 + prev(i) = 1 + 1 = 2
// 10: [7.69] | 1 + prev(i) = 1 + 2 = 3
// 20: [12] | 1 + prev(i) = 1 + 3 = 4
// 50: [] | 0 + prev(i) = 0 + 4 = 4
// 100: [] | 0 + prev(i) = 0 + 4 = 4
// 250: [187.12, 199.9, 245.67] | 3 + prev(i) = 3 + 4 = 7
wantLines := []string{
`cash_register_bucket{le="1"} 1`,
`cash_register_bucket{le="5"} 2`,
`cash_register_bucket{le="10"} 3`,
`cash_register_bucket{le="20"} 4`,
`cash_register_bucket{le="50"} 4`,
`cash_register_bucket{le="100"} 4`,
`cash_register_bucket{le="250"} 7`,
`cash_register_bucket{le="+Inf"} 7`,
`cash_register_sum 654.0799999999999`, // Summation of the input values
`cash_register_count 7`,
}
ctx := context.Background()
ms := make([]stats.Measurement, 0, len(values))
for _, value := range values {
mx := m.M(value)
ms = append(ms, mx)
}
stats.Record(ctx, ms...)
// Give the recorder ample time to process recording
// <-time.After(10 * reportPeriod)
cst := httptest.NewServer(exporter)
defer cst.Close()
res, err := http.Get(cst.URL)
if err != nil {
t.Fatalf("http.Get error: %v", err)
}
blob, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("Read body error: %v", err)
}
str := strings.Trim(string(blob), "\n")
lines := strings.Split(str, "\n")
nonComments := make([]string, 0, len(lines))
for _, line := range lines {
if !strings.Contains(line, "#") {
nonComments = append(nonComments, line)
}
}
got := strings.Join(nonComments, "\n")
want := strings.Join(wantLines, "\n")
if got != want {
t.Fatalf("\ngot:\n%s\n\nwant:\n%s\n", got, want)
}
}
func TestConstLabelsAndResource(t *testing.T) {
testCases := []struct {
name string
constLabels prometheus.Labels
resource *resource.Resource
want string
}{{
name: "neither const labels nor resource",
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{method="issue961"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{method="issue961"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{method="issue961"} 1
`,
}, {
name: "const labels only",
constLabels: prometheus.Labels{"service": "spanner"},
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{method="issue961",service="spanner"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{method="issue961",service="spanner"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{method="issue961",service="spanner"} 1
`,
}, {
name: "resource only",
resource: &resource.Resource{Type: "test resource", Labels: map[string]string{"region": "us-east"}},
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{method="issue961",region="us-east"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{method="issue961",region="us-east"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{method="issue961",region="us-east"} 1
`,
}, {
name: "both const labels and resource",
constLabels: prometheus.Labels{"service": "spanner"},
resource: &resource.Resource{Type: "test resource", Labels: map[string]string{"region": "us-east"}},
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{method="issue961",region="us-east",service="spanner"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{method="issue961",region="us-east",service="spanner"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{method="issue961",region="us-east",service="spanner"} 1
`,
}, {
name: "const labels and resource overlap",
constLabels: prometheus.Labels{"service": "spanner"},
resource: &resource.Resource{Type: "test resource", Labels: map[string]string{"service": "bigtable"}},
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{method="issue961",service="bigtable"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{method="issue961",service="bigtable"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{method="issue961",service="bigtable"} 1
`,
}, {
name: "const labels and resource with overlap and non-overlap",
constLabels: prometheus.Labels{"service": "spanner", "account": "test"},
resource: &resource.Resource{Type: "test resource", Labels: map[string]string{"service": "bigtable", "region": "us-east"}},
want: `# HELP tests_bar bar
# TYPE tests_bar counter
tests_bar{account="test",method="issue961",region="us-east",service="bigtable"} 1
# HELP tests_baz baz
# TYPE tests_baz counter
tests_baz{account="test",method="issue961",region="us-east",service="bigtable"} 1
# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{account="test",method="issue961",region="us-east",service="bigtable"} 1
`,
}}
measureLabel, _ := tag.NewKey("method")
for _, testCase := range testCases {
exporter, err := NewExporter(Options{
ConstLabels: testCase.constLabels,
})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
names := []string{"foo", "bar", "baz"}
var measures mSlice
for _, name := range names {
measures.createAndAppend("tests/"+name, name, "")
}
var vc vCreator
for _, m := range measures {
vc.createAndAppend(m.Name(), m.Description(), []tag.Key{measureLabel}, m, view.Count())
}
meter := view.NewMeter()
meter.SetResource(testCase.resource)
meter.Start()
if err := meter.Register(vc...); err != nil {
t.Fatalf("failed to create views: %v", err)
}
defer meter.Unregister(vc...)
ctx, _ := tag.New(context.Background(), tag.Upsert(measureLabel, "issue961"))
for _, m := range measures {
opt := stats.WithRecorder(meter)
stats.RecordWithOptions(ctx, opt, stats.WithMeasurements(m.M(1)))
}
srv := httptest.NewServer(exporter)
defer srv.Close()
var i int
var output string
for {
time.Sleep(10 * time.Millisecond)
if i == 10 {
t.Fatal("no output at /metrics (100ms wait)")
}
i++
resp, err := http.Get(srv.URL)
if err != nil {
t.Fatalf("failed to get /metrics: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
resp.Body.Close()
output = string(body)
if output != "" {
break
}
}
if strings.Contains(output, "collected before with the same name and label values") {
t.Fatal("metric name and labels being duplicated but must be unique")
}
if strings.Contains(output, "error(s) occurred") {
t.Fatal("error reported by prometheus registry")
}
if diff := cmp.Diff(testCase.want, output); diff != "" {
t.Errorf("Unexpected prometheus output for test {%s} (-want +got):\n%s", testCase.name, diff)
}
meter.Unregister(vc...)
}
}
func TestViewMeasureWithoutTag(t *testing.T) {
exporter, err := NewExporter(Options{})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
m := stats.Int64("tests/foo", "foo", stats.UnitDimensionless)
k1, _ := tag.NewKey("key/1")
k2, _ := tag.NewKey("key/2")
k3, _ := tag.NewKey("key/3")
k4, _ := tag.NewKey("key/4")
k5, _ := tag.NewKey("key/5")
randomKey, _ := tag.NewKey("issue659")
v := &view.View{
Name: m.Name(),
Description: m.Description(),
TagKeys: []tag.Key{k2, k5, k3, k1, k4}, // Ensure view has a tag
Measure: m,
Aggregation: view.Count(),
}
if err := view.Register(v); err != nil {
t.Fatalf("failed to create views: %v", err)
}
defer view.Unregister(v)
// Make a measure without some tags in the view.
ctx1, _ := tag.New(context.Background(), tag.Upsert(k4, "issue659"), tag.Upsert(randomKey, "value"), tag.Upsert(k2, "issue659"))
stats.Record(ctx1, m.M(1))
ctx2, _ := tag.New(context.Background(), tag.Upsert(k5, "issue659"), tag.Upsert(k3, "issue659"), tag.Upsert(k1, "issue659"))
stats.Record(ctx2, m.M(2))
srv := httptest.NewServer(exporter)
defer srv.Close()
var i int
var output string
for {
time.Sleep(10 * time.Millisecond)
if i == 10 {
t.Fatal("no output at /metrics (100ms wait)")
}
i++
resp, err := http.Get(srv.URL)
if err != nil {
t.Fatalf("failed to get /metrics: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
resp.Body.Close()
output = string(body)
if output != "" {
break
}
}
if strings.Contains(output, "collected before with the same name and label values") {
t.Fatal("metric name and labels being duplicated but must be unique")
}
if strings.Contains(output, "error(s) occurred") {
t.Fatal("error reported by prometheus registry")
}
want := `# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo{key_1="",key_2="issue659",key_3="",key_4="issue659",key_5=""} 1
tests_foo{key_1="issue659",key_2="",key_3="issue659",key_4="",key_5="issue659"} 1
`
if output != want {
t.Fatalf("output differed from expected output: %s want: %s", output, want)
}
}
func TestShareDefaultRegistry(t *testing.T) {
_, err := NewExporter(Options{
Registerer: prometheus.DefaultRegisterer,
Gatherer: prometheus.DefaultGatherer,
})
if err != nil {
t.Fatalf("failed to create prometheus exporter: %v", err)
}
m := stats.Int64("tests/foo", "foo", stats.UnitDimensionless)
v := &view.View{
Name: m.Name(),
Description: m.Description(),
Measure: m,
Aggregation: view.Count(),
}
if err := view.Register(v); err != nil {
t.Fatalf("failed to create views: %v", err)
}
defer view.Unregister(v)
stats.Record(context.Background(), m.M(1))
// counter, prometheus way
c := prometheus.NewCounter(prometheus.CounterOpts{Name: "prom_counter", Help: "Prometheus Counter"})
prometheus.MustRegister(c)
c.Add(1)
// Use prometheus handler
srv := httptest.NewServer(promhttp.Handler())
defer srv.Close()
var i int
var output string
for {
time.Sleep(10 * time.Millisecond)
if i == 10 {
t.Fatal("no output at /metrics (100ms wait)")
}
i++
resp, err := http.Get(srv.URL)
if err != nil {
t.Fatalf("failed to get /metrics: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read body: %v", err)
}
resp.Body.Close()
output = string(body)
if output != "" {
break
}
}
if strings.Contains(output, "collected before with the same name and label values") {
t.Fatal("metric name and labels being duplicated but must be unique")
}
if strings.Contains(output, "error(s) occurred") {
t.Fatal("error reported by prometheus registry")
}
wantOc := `# HELP tests_foo foo
# TYPE tests_foo counter
tests_foo 1
`
if !strings.Contains(output, wantOc) {
t.Errorf("output does not contain opencensus counter. Output: %s want: %s", output, wantOc)
}
wantP := `# HELP prom_counter Prometheus Counter
# TYPE prom_counter counter
prom_counter 1
`
if !strings.Contains(output, wantP) {
t.Errorf("output does not contain opencensus counter. Output: %s want: %s", output, wantP)
}
}