Skip to content

Commit aa32471

Browse files
committed
refactor(receiver/prometheusreceiver): use type and unit as identifier in test
I'd like to fix a technical dept from open-telemetry#28663 where it didn't allow receiving both classic and native histogram for the same metric name. To test the implementation, the assertions must not assume that the metric name alone is identifying in OpenTelemetry. No new tests added, I just refactored to make name+type+unit the identifier. Also I made it implicit that doCompare fails on metrics not expected - excluding the 5 scrape metrics. Signed-off-by: György Krajcsovits <[email protected]>
1 parent c232180 commit aa32471

10 files changed

+1016
-618
lines changed

receiver/prometheusreceiver/metrics_receiver_helper_test.go

Lines changed: 90 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"net/http/httptest"
1515
"net/url"
16+
"strings"
1617
"sync"
1718
"sync/atomic"
1819
"testing"
@@ -396,7 +397,6 @@ func isExtraScrapeMetrics(m pmetric.Metric) bool {
396397
}
397398

398399
type (
399-
metricTypeComparator func(*testing.T, pmetric.Metric)
400400
numberPointComparator func(*testing.T, pmetric.NumberDataPoint)
401401
histogramPointComparator func(*testing.T, pmetric.HistogramDataPoint)
402402
summaryPointComparator func(*testing.T, pmetric.SummaryDataPoint)
@@ -410,19 +410,32 @@ type dataPointExpectation struct {
410410
exponentialHistogramComparator []exponentialHistogramComparator
411411
}
412412

413+
type metricChecker func(*testing.T, pmetric.Metric)
414+
415+
type metricExpectation struct {
416+
name string
417+
mtype pmetric.MetricType
418+
munit string
419+
dataPointExpectations []dataPointExpectation
420+
extraExpectation func(*testing.T, pmetric.Metric)
421+
}
422+
413423
type testExpectation func(*testing.T, pmetric.ResourceMetrics)
414424

415-
func doCompare(t *testing.T, name string, want pcommon.Map, got pmetric.ResourceMetrics, expectations []testExpectation) {
416-
doCompareNormalized(t, name, want, got, expectations, false)
425+
// doCompare is a helper function to compare the expected metrics with the actual metrics
426+
// name is the test name
427+
// want is the map of expected attributes
428+
// got is the actual metrics
429+
// metricExpections is the list of expected metrics, exluding the internal scrape metrics
430+
func doCompare(t *testing.T, name string, want pcommon.Map, got pmetric.ResourceMetrics, metricExpectations []metricExpectation) {
431+
doCompareNormalized(t, name, want, got, metricExpectations, false)
417432
}
418433

419-
func doCompareNormalized(t *testing.T, name string, want pcommon.Map, got pmetric.ResourceMetrics, expectations []testExpectation, normalizedNames bool) {
434+
func doCompareNormalized(t *testing.T, name string, want pcommon.Map, got pmetric.ResourceMetrics, metricExpectations []metricExpectation, normalizedNames bool) {
420435
t.Run(name, func(t *testing.T) {
421436
assert.Equal(t, expectedScrapeMetricCount, countScrapeMetricsRM(got, normalizedNames))
422437
assertExpectedAttributes(t, want, got)
423-
for _, e := range expectations {
424-
e(t, got)
425-
}
438+
assertExpectedMetrics(t, metricExpectations, got)
426439
})
427440
}
428441

@@ -437,75 +450,114 @@ func assertExpectedAttributes(t *testing.T, want pcommon.Map, got pmetric.Resour
437450
}
438451
}
439452

440-
func assertMetricPresent(name string, metricTypeExpectations metricTypeComparator, metricUnitExpectations metricTypeComparator, dataPointExpectations []dataPointExpectation) testExpectation {
441-
return func(t *testing.T, rm pmetric.ResourceMetrics) {
442-
allMetrics := getMetrics(rm)
443-
var present bool
444-
for _, m := range allMetrics {
445-
if name != m.Name() {
453+
func assertExpectedMetrics(t *testing.T, metricExpectations []metricExpectation, got pmetric.ResourceMetrics) {
454+
allMetrics := getMetrics(got)
455+
for _, me := range metricExpectations {
456+
id := fmt.Sprintf("name '%s' type '%s' unit '%s'", me.name, me.mtype.String(), me.munit)
457+
pos := -1
458+
for k, m := range allMetrics {
459+
if me.name != m.Name() || me.mtype != m.Type() || me.munit != m.Unit() {
446460
continue
447461
}
448462

449-
present = true
450-
metricTypeExpectations(t, m)
451-
metricUnitExpectations(t, m)
452-
for i, de := range dataPointExpectations {
463+
require.True(t, pos == -1, "metric %s is not unique", id)
464+
pos = k
465+
466+
for i, de := range me.dataPointExpectations {
453467
switch m.Type() {
454468
case pmetric.MetricTypeGauge:
455469
for _, npc := range de.numberPointComparator {
456-
require.Len(t, dataPointExpectations, m.Gauge().DataPoints().Len(), "Expected number of data-points in Gauge metric '%s' does not match to testdata", name)
470+
require.Len(t, me.dataPointExpectations, m.Gauge().DataPoints().Len(), "Expected number of data-points in Gauge metric '%s' does not match to testdata", id)
457471
npc(t, m.Gauge().DataPoints().At(i))
458472
}
459473
case pmetric.MetricTypeSum:
460474
for _, npc := range de.numberPointComparator {
461-
require.Len(t, dataPointExpectations, m.Sum().DataPoints().Len(), "Expected number of data-points in Sum metric '%s' does not match to testdata", name)
475+
require.Len(t, me.dataPointExpectations, m.Sum().DataPoints().Len(), "Expected number of data-points in Sum metric '%s' does not match to testdata", id)
462476
npc(t, m.Sum().DataPoints().At(i))
463477
}
464478
case pmetric.MetricTypeHistogram:
465479
for _, hpc := range de.histogramPointComparator {
466-
require.Len(t, dataPointExpectations, m.Histogram().DataPoints().Len(), "Expected number of data-points in Histogram metric '%s' does not match to testdata", name)
480+
require.Len(t, me.dataPointExpectations, m.Histogram().DataPoints().Len(), "Expected number of data-points in Histogram metric '%s' does not match to testdata", id)
467481
hpc(t, m.Histogram().DataPoints().At(i))
468482
}
469483
case pmetric.MetricTypeSummary:
470484
for _, spc := range de.summaryPointComparator {
471-
require.Len(t, dataPointExpectations, m.Summary().DataPoints().Len(), "Expected number of data-points in Summary metric '%s' does not match to testdata", name)
485+
require.Len(t, me.dataPointExpectations, m.Summary().DataPoints().Len(), "Expected number of data-points in Summary metric '%s' does not match to testdata", id)
472486
spc(t, m.Summary().DataPoints().At(i))
473487
}
474488
case pmetric.MetricTypeExponentialHistogram:
475489
for _, ehc := range de.exponentialHistogramComparator {
476-
require.Len(t, dataPointExpectations, m.ExponentialHistogram().DataPoints().Len(), "Expected number of data-points in Exponential Histogram metric '%s' does not match to testdata", name)
490+
require.Len(t, me.dataPointExpectations, m.ExponentialHistogram().DataPoints().Len(), "Expected number of data-points in Exponential Histogram metric '%s' does not match to testdata", id)
477491
ehc(t, m.ExponentialHistogram().DataPoints().At(i))
478492
}
479493
case pmetric.MetricTypeEmpty:
480494
}
481495
}
496+
497+
if me.extraExpectation != nil {
498+
me.extraExpectation(t, m)
499+
}
482500
}
483-
require.True(t, present, "expected metric '%s' is not present", name)
501+
require.True(t, pos>=0, "expected metric %s is not present", id)
502+
allMetrics = append(allMetrics[:pos], allMetrics[pos+1:]...)
503+
}
504+
505+
remainingMetrics := []string{}
506+
for _, m := range allMetrics {
507+
remainingMetrics = append(remainingMetrics, fmt.Sprintf("%s(%s,%s)", m.Name(), m.Type().String(), m.Unit()))
484508
}
509+
510+
require.Len(t, allMetrics, expectedScrapeMetricCount, "not all metrics were validated: %v", strings.Join(remainingMetrics, ", "))
485511
}
486512

487-
func assertMetricAbsent(name string) testExpectation {
513+
func assertMetricPresent(name string, mtype pmetric.MetricType, munit string, dataPointExpectations []dataPointExpectation) testExpectation {
488514
return func(t *testing.T, rm pmetric.ResourceMetrics) {
515+
id := fmt.Sprintf("name '%s' type '%s' unit '%s'", name, mtype.String(), munit)
516+
489517
allMetrics := getMetrics(rm)
518+
var present bool
490519
for _, m := range allMetrics {
491-
assert.NotEqual(t, name, m.Name(), "Metric is present, but was expected absent")
492-
}
493-
}
494-
}
495-
496-
func compareMetricType(typ pmetric.MetricType) metricTypeComparator {
497-
return func(t *testing.T, metric pmetric.Metric) {
498-
assert.Equal(t, typ.String(), metric.Type().String(), "Metric type does not match")
499-
}
500-
}
520+
if name != m.Name() || mtype != m.Type() || munit != m.Unit() {
521+
continue
522+
}
501523

502-
func compareMetricUnit(unit string) metricTypeComparator {
503-
return func(t *testing.T, metric pmetric.Metric) {
504-
assert.Equal(t, unit, metric.Unit(), "Metric unit does not match")
524+
present = true
525+
for i, de := range dataPointExpectations {
526+
switch m.Type() {
527+
case pmetric.MetricTypeGauge:
528+
for _, npc := range de.numberPointComparator {
529+
require.Len(t, dataPointExpectations, m.Gauge().DataPoints().Len(), "Expected number of data-points in Gauge metric '%s' does not match to testdata", id)
530+
npc(t, m.Gauge().DataPoints().At(i))
531+
}
532+
case pmetric.MetricTypeSum:
533+
for _, npc := range de.numberPointComparator {
534+
require.Len(t, dataPointExpectations, m.Sum().DataPoints().Len(), "Expected number of data-points in Sum metric '%s' does not match to testdata", id)
535+
npc(t, m.Sum().DataPoints().At(i))
536+
}
537+
case pmetric.MetricTypeHistogram:
538+
for _, hpc := range de.histogramPointComparator {
539+
require.Len(t, dataPointExpectations, m.Histogram().DataPoints().Len(), "Expected number of data-points in Histogram metric '%s' does not match to testdata", id)
540+
hpc(t, m.Histogram().DataPoints().At(i))
541+
}
542+
case pmetric.MetricTypeSummary:
543+
for _, spc := range de.summaryPointComparator {
544+
require.Len(t, dataPointExpectations, m.Summary().DataPoints().Len(), "Expected number of data-points in Summary metric '%s' does not match to testdata", id)
545+
spc(t, m.Summary().DataPoints().At(i))
546+
}
547+
case pmetric.MetricTypeExponentialHistogram:
548+
for _, ehc := range de.exponentialHistogramComparator {
549+
require.Len(t, dataPointExpectations, m.ExponentialHistogram().DataPoints().Len(), "Expected number of data-points in Exponential Histogram metric '%s' does not match to testdata", id)
550+
ehc(t, m.ExponentialHistogram().DataPoints().At(i))
551+
}
552+
case pmetric.MetricTypeEmpty:
553+
}
554+
}
555+
}
556+
require.True(t, present, "expected metric %s is not present", id)
505557
}
506558
}
507559

508-
func compareMetricIsMonotonic(isMonotonic bool) metricTypeComparator {
560+
func compareMetricIsMonotonic(isMonotonic bool) metricChecker {
509561
return func(t *testing.T, metric pmetric.Metric) {
510562
assert.Equal(t, pmetric.MetricTypeSum.String(), metric.Type().String(), "IsMonotonic only exists for sums")
511563
assert.Equal(t, isMonotonic, metric.Sum().IsMonotonic(), "IsMonotonic does not match")

0 commit comments

Comments
 (0)