Skip to content

Commit 88a6d8b

Browse files
committed
Start adding tests featuring unit
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 166e88e commit 88a6d8b

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

expfmt/openmetrics_create_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,70 @@ foos_total 42.0
417417
},
418418
out: `# HELP name doc string
419419
# TYPE name counter
420+
`,
421+
},
422+
// 9: No metric plus unit.
423+
{
424+
in: &dto.MetricFamily{
425+
Name: proto.String("name_seconds"),
426+
Help: proto.String("doc string"),
427+
Type: dto.MetricType_COUNTER.Enum(),
428+
Unit: proto.String("seconds"),
429+
Metric: []*dto.Metric{},
430+
},
431+
out: `# HELP name_seconds doc string
432+
# TYPE name_seconds unknown
433+
# UNIT name_seconds seconds
434+
`,
435+
},
436+
// 10: Histogram plus unit.
437+
{
438+
in: &dto.MetricFamily{
439+
Name: proto.String("request_duration_microseconds"),
440+
Help: proto.String("The response latency."),
441+
Type: dto.MetricType_HISTOGRAM.Enum(),
442+
Unit: proto.String("microseconds"),
443+
Metric: []*dto.Metric{
444+
&dto.Metric{
445+
Histogram: &dto.Histogram{
446+
SampleCount: proto.Uint64(2693),
447+
SampleSum: proto.Float64(1756047.3),
448+
Bucket: []*dto.Bucket{
449+
&dto.Bucket{
450+
UpperBound: proto.Float64(100),
451+
CumulativeCount: proto.Uint64(123),
452+
},
453+
&dto.Bucket{
454+
UpperBound: proto.Float64(120),
455+
CumulativeCount: proto.Uint64(412),
456+
},
457+
&dto.Bucket{
458+
UpperBound: proto.Float64(144),
459+
CumulativeCount: proto.Uint64(592),
460+
},
461+
&dto.Bucket{
462+
UpperBound: proto.Float64(172.8),
463+
CumulativeCount: proto.Uint64(1524),
464+
},
465+
&dto.Bucket{
466+
UpperBound: proto.Float64(math.Inf(+1)),
467+
CumulativeCount: proto.Uint64(2693),
468+
},
469+
},
470+
},
471+
},
472+
},
473+
},
474+
out: `# HELP request_duration_microseconds The response latency.
475+
# TYPE request_duration_microseconds histogram
476+
# UNIT request_duration_microseconds microseconds
477+
request_duration_microseconds_bucket{le="100.0"} 123
478+
request_duration_microseconds_bucket{le="120.0"} 412
479+
request_duration_microseconds_bucket{le="144.0"} 592
480+
request_duration_microseconds_bucket{le="172.8"} 1524
481+
request_duration_microseconds_bucket{le="+Inf"} 2693
482+
request_duration_microseconds_sum 1.7560473e+06
483+
request_duration_microseconds_count 2693
420484
`,
421485
},
422486
}

expfmt/text_create_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,56 @@ request_duration_microseconds_count 2693
330330
out: `# HELP name doc string
331331
# TYPE name counter
332332
name -Inf
333+
`,
334+
},
335+
// 7: Histogram with unit
336+
{
337+
in: &dto.MetricFamily{
338+
Name: proto.String("request_duration_microseconds"),
339+
Help: proto.String("The response latency."),
340+
Type: dto.MetricType_HISTOGRAM.Enum(),
341+
Unit: proto.String("microseconds"),
342+
Metric: []*dto.Metric{
343+
&dto.Metric{
344+
Histogram: &dto.Histogram{
345+
SampleCount: proto.Uint64(2693),
346+
SampleSum: proto.Float64(1756047.3),
347+
Bucket: []*dto.Bucket{
348+
&dto.Bucket{
349+
UpperBound: proto.Float64(100),
350+
CumulativeCount: proto.Uint64(123),
351+
},
352+
&dto.Bucket{
353+
UpperBound: proto.Float64(120),
354+
CumulativeCount: proto.Uint64(412),
355+
},
356+
&dto.Bucket{
357+
UpperBound: proto.Float64(144),
358+
CumulativeCount: proto.Uint64(592),
359+
},
360+
&dto.Bucket{
361+
UpperBound: proto.Float64(172.8),
362+
CumulativeCount: proto.Uint64(1524),
363+
},
364+
&dto.Bucket{
365+
UpperBound: proto.Float64(math.Inf(+1)),
366+
CumulativeCount: proto.Uint64(2693),
367+
},
368+
},
369+
},
370+
},
371+
},
372+
},
373+
out: `# HELP request_duration_microseconds The response latency.
374+
# TYPE request_duration_microseconds histogram
375+
# UNIT request_duration_microseconds microseconds
376+
request_duration_microseconds_bucket{le="100"} 123
377+
request_duration_microseconds_bucket{le="120"} 412
378+
request_duration_microseconds_bucket{le="144"} 592
379+
request_duration_microseconds_bucket{le="172.8"} 1524
380+
request_duration_microseconds_bucket{le="+Inf"} 2693
381+
request_duration_microseconds_sum 1.7560473e+06
382+
request_duration_microseconds_count 2693
333383
`,
334384
},
335385
}

expfmt/text_parse_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,110 @@ request_duration_microseconds_count 2693
385385
},
386386
},
387387
},
388+
// 5: The histogram with unit.
389+
{
390+
in: `
391+
# HELP request_duration_microseconds The response latency.
392+
# TYPE request_duration_microseconds histogram
393+
# UNIT request_duration_microseconds microseconds
394+
request_duration_microseconds_bucket{le="100"} 123
395+
request_duration_microseconds_bucket{le="120"} 412
396+
request_duration_microseconds_bucket{le="144"} 592
397+
request_duration_microseconds_bucket{le="172.8"} 1524
398+
request_duration_microseconds_bucket{le="+Inf"} 2693
399+
request_duration_microseconds_sum 1.7560473e+06
400+
request_duration_microseconds_count 2693
401+
`,
402+
out: []*dto.MetricFamily{
403+
{
404+
Name: proto.String("request_duration_microseconds"),
405+
Help: proto.String("The response latency."),
406+
Type: dto.MetricType_HISTOGRAM.Enum(),
407+
Unit: proto.String("microseconds"),
408+
Metric: []*dto.Metric{
409+
&dto.Metric{
410+
Histogram: &dto.Histogram{
411+
SampleCount: proto.Uint64(2693),
412+
SampleSum: proto.Float64(1756047.3),
413+
Bucket: []*dto.Bucket{
414+
&dto.Bucket{
415+
UpperBound: proto.Float64(100),
416+
CumulativeCount: proto.Uint64(123),
417+
},
418+
&dto.Bucket{
419+
UpperBound: proto.Float64(120),
420+
CumulativeCount: proto.Uint64(412),
421+
},
422+
&dto.Bucket{
423+
UpperBound: proto.Float64(144),
424+
CumulativeCount: proto.Uint64(592),
425+
},
426+
&dto.Bucket{
427+
UpperBound: proto.Float64(172.8),
428+
CumulativeCount: proto.Uint64(1524),
429+
},
430+
&dto.Bucket{
431+
UpperBound: proto.Float64(math.Inf(+1)),
432+
CumulativeCount: proto.Uint64(2693),
433+
},
434+
},
435+
},
436+
},
437+
},
438+
},
439+
},
440+
},
441+
// 6: A counter with unit
442+
{
443+
in: `
444+
# HELP name something to do with time
445+
# TYPE name counter
446+
# UNIT name seconds
447+
name{labelname="val1",basename="basevalue"} 50.0
448+
name{labelname="val2",basename="basevalue"} 0.24 2
449+
`,
450+
out: []*dto.MetricFamily{
451+
{
452+
Name: proto.String("name"),
453+
Help: proto.String("something to do with time"),
454+
Type: dto.MetricType_COUNTER.Enum(),
455+
Unit: proto.String("seconds"),
456+
Metric: []*dto.Metric{
457+
&dto.Metric{
458+
Label: []*dto.LabelPair{
459+
&dto.LabelPair{
460+
Name: proto.String("labelname"),
461+
Value: proto.String("val1"),
462+
},
463+
&dto.LabelPair{
464+
Name: proto.String("basename"),
465+
Value: proto.String("basevalue"),
466+
},
467+
},
468+
Counter: &dto.Counter{
469+
Value: proto.Float64(50),
470+
},
471+
},
472+
&dto.Metric{
473+
Label: []*dto.LabelPair{
474+
&dto.LabelPair{
475+
Name: proto.String("labelname"),
476+
Value: proto.String("val2"),
477+
},
478+
&dto.LabelPair{
479+
Name: proto.String("basename"),
480+
Value: proto.String("basevalue"),
481+
},
482+
},
483+
Counter: &dto.Counter{
484+
Value: proto.Float64(.24),
485+
},
486+
TimestampMs: proto.Int64(2),
487+
},
488+
},
489+
},
490+
},
491+
},
388492
}
389493

390494
for i, scenario := range scenarios {

0 commit comments

Comments
 (0)