diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index 164dcbd09561..ea9148ee814b 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -46,67 +46,70 @@ func TestPrometheusExporter(t *testing.T) { name: "counter", expectedFile: "testdata/counter.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), attribute.Key("E").Bool(true), attribute.Key("F").Int(42), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Float64Counter( "foo", instrument.WithDescription("a simple counter"), instrument.WithUnit("ms"), ) require.NoError(t, err) - counter.Add(ctx, 5, attrs...) - counter.Add(ctx, 10.3, attrs...) - counter.Add(ctx, 9, attrs...) + counter.Add(ctx, 5, opt) + counter.Add(ctx, 10.3, opt) + counter.Add(ctx, 9, opt) - attrs2 := []attribute.KeyValue{ + attrs2 := attribute.NewSet( attribute.Key("A").String("D"), attribute.Key("C").String("B"), attribute.Key("E").Bool(true), attribute.Key("F").Int(42), - } - counter.Add(ctx, 5, attrs2...) + ) + counter.Add(ctx, 5, instrument.WithAttributes(attrs2)) }, }, { name: "gauge", expectedFile: "testdata/gauge.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), - } + ) + opt := instrument.WithAttributes(attrs) gauge, err := meter.Float64UpDownCounter( "bar", instrument.WithDescription("a fun little gauge"), instrument.WithUnit("1"), ) require.NoError(t, err) - gauge.Add(ctx, 1.0, attrs...) - gauge.Add(ctx, -.25, attrs...) + gauge.Add(ctx, 1.0, opt) + gauge.Add(ctx, -.25, opt) }, }, { name: "histogram", expectedFile: "testdata/histogram.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), - } + ) + opt := instrument.WithAttributes(attrs) histogram, err := meter.Float64Histogram( "histogram_baz", instrument.WithDescription("a very nice histogram"), instrument.WithUnit("By"), ) require.NoError(t, err) - histogram.Record(ctx, 23, attrs...) - histogram.Record(ctx, 7, attrs...) - histogram.Record(ctx, 101, attrs...) - histogram.Record(ctx, 105, attrs...) + histogram.Record(ctx, 23, opt) + histogram.Record(ctx, 7, opt) + histogram.Record(ctx, 101, opt) + histogram.Record(ctx, 105, opt) }, }, { @@ -114,7 +117,7 @@ func TestPrometheusExporter(t *testing.T) { expectedFile: "testdata/sanitized_labels.txt", options: []Option{WithoutUnits()}, recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( // exact match, value should be overwritten attribute.Key("A.B").String("X"), attribute.Key("A.B").String("Q"), @@ -122,7 +125,8 @@ func TestPrometheusExporter(t *testing.T) { // unintended match due to sanitization, values should be concatenated attribute.Key("C.D").String("Y"), attribute.Key("C/D").String("Z"), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Float64Counter( "foo", instrument.WithDescription("a sanitary counter"), @@ -130,37 +134,38 @@ func TestPrometheusExporter(t *testing.T) { instrument.WithUnit("By"), ) require.NoError(t, err) - counter.Add(ctx, 5, attrs...) - counter.Add(ctx, 10.3, attrs...) - counter.Add(ctx, 9, attrs...) + counter.Add(ctx, 5, opt) + counter.Add(ctx, 10.3, opt) + counter.Add(ctx, 9, opt) }, }, { name: "invalid instruments are renamed", expectedFile: "testdata/sanitized_names.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), - } + ) + opt := instrument.WithAttributes(attrs) // Valid. gauge, err := meter.Float64UpDownCounter("bar", instrument.WithDescription("a fun little gauge")) require.NoError(t, err) - gauge.Add(ctx, 100, attrs...) - gauge.Add(ctx, -25, attrs...) + gauge.Add(ctx, 100, opt) + gauge.Add(ctx, -25, opt) // Invalid, will be renamed. gauge, err = meter.Float64UpDownCounter("invalid.gauge.name", instrument.WithDescription("a gauge with an invalid name")) require.NoError(t, err) - gauge.Add(ctx, 100, attrs...) + gauge.Add(ctx, 100, opt) counter, err := meter.Float64Counter("0invalid.counter.name", instrument.WithDescription("a counter with an invalid name")) require.NoError(t, err) - counter.Add(ctx, 100, attrs...) + counter.Add(ctx, 100, opt) histogram, err := meter.Float64Histogram("invalid.hist.name", instrument.WithDescription("a histogram with an invalid name")) require.NoError(t, err) - histogram.Record(ctx, 23, attrs...) + histogram.Record(ctx, 23, opt) }, }, { @@ -168,17 +173,18 @@ func TestPrometheusExporter(t *testing.T) { emptyResource: true, expectedFile: "testdata/empty_resource.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), attribute.Key("E").Bool(true), attribute.Key("F").Int(42), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter")) require.NoError(t, err) - counter.Add(ctx, 5, attrs...) - counter.Add(ctx, 10.3, attrs...) - counter.Add(ctx, 9, attrs...) + counter.Add(ctx, 5, opt) + counter.Add(ctx, 10.3, opt) + counter.Add(ctx, 9, opt) }, }, { @@ -189,17 +195,18 @@ func TestPrometheusExporter(t *testing.T) { }, expectedFile: "testdata/custom_resource.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), attribute.Key("E").Bool(true), attribute.Key("F").Int(42), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter")) require.NoError(t, err) - counter.Add(ctx, 5, attrs...) - counter.Add(ctx, 10.3, attrs...) - counter.Add(ctx, 9, attrs...) + counter.Add(ctx, 5, opt) + counter.Add(ctx, 10.3, opt) + counter.Add(ctx, 9, opt) }, }, { @@ -207,17 +214,18 @@ func TestPrometheusExporter(t *testing.T) { options: []Option{WithoutTargetInfo()}, expectedFile: "testdata/without_target_info.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), attribute.Key("E").Bool(true), attribute.Key("F").Int(42), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter")) require.NoError(t, err) - counter.Add(ctx, 5, attrs...) - counter.Add(ctx, 10.3, attrs...) - counter.Add(ctx, 9, attrs...) + counter.Add(ctx, 5, opt) + counter.Add(ctx, 10.3, opt) + counter.Add(ctx, 9, opt) }, }, { @@ -225,18 +233,19 @@ func TestPrometheusExporter(t *testing.T) { options: []Option{WithoutScopeInfo()}, expectedFile: "testdata/without_scope_info.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), - } + ) + opt := instrument.WithAttributes(attrs) gauge, err := meter.Int64UpDownCounter( "bar", instrument.WithDescription("a fun little gauge"), instrument.WithUnit("1"), ) require.NoError(t, err) - gauge.Add(ctx, 2, attrs...) - gauge.Add(ctx, -1, attrs...) + gauge.Add(ctx, 2, opt) + gauge.Add(ctx, -1, opt) }, }, { @@ -244,18 +253,19 @@ func TestPrometheusExporter(t *testing.T) { options: []Option{WithoutScopeInfo(), WithoutTargetInfo()}, expectedFile: "testdata/without_scope_and_target_info.txt", recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { - attrs := []attribute.KeyValue{ + attrs := attribute.NewSet( attribute.Key("A").String("B"), attribute.Key("C").String("D"), - } + ) + opt := instrument.WithAttributes(attrs) counter, err := meter.Int64Counter( "bar", instrument.WithDescription("a fun little counter"), instrument.WithUnit("By"), ) require.NoError(t, err) - counter.Add(ctx, 2, attrs...) - counter.Add(ctx, 1, attrs...) + counter.Add(ctx, 2, opt) + counter.Add(ctx, 1, opt) }, }, } @@ -368,7 +378,8 @@ func TestMultiScopes(t *testing.T) { instrument.WithUnit("ms"), instrument.WithDescription("meter foo counter")) assert.NoError(t, err) - fooCounter.Add(ctx, 100, attribute.String("type", "foo")) + s := attribute.NewSet(attribute.String("type", "foo")) + fooCounter.Add(ctx, 100, instrument.WithAttributes(s)) barCounter, err := provider.Meter("meterbar", otelmetric.WithInstrumentationVersion("v0.1.0")). Int64Counter( @@ -376,7 +387,8 @@ func TestMultiScopes(t *testing.T) { instrument.WithUnit("ms"), instrument.WithDescription("meter bar counter")) assert.NoError(t, err) - barCounter.Add(ctx, 200, attribute.String("type", "bar")) + s = attribute.NewSet(attribute.String("type", "bar")) + barCounter.Add(ctx, 200, instrument.WithAttributes(s)) file, err := os.Open("testdata/multi_scopes.txt") require.NoError(t, err) @@ -387,6 +399,12 @@ func TestMultiScopes(t *testing.T) { } func TestDuplicateMetrics(t *testing.T) { + ab := attribute.NewSet(attribute.String("A", "B")) + withAB := instrument.WithAttributes(ab) + typeBar := attribute.NewSet(attribute.String("type", "bar")) + withTypeBar := instrument.WithAttributes(typeBar) + typeFoo := attribute.NewSet(attribute.String("type", "foo")) + withTypeFoo := instrument.WithAttributes(typeFoo) testCases := []struct { name string customResouceAttrs []attribute.KeyValue @@ -401,13 +419,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter counter foo")) assert.NoError(t, err) - fooA.Add(ctx, 100, attribute.String("A", "B")) + fooA.Add(ctx, 100, withAB) fooB, err := meterB.Int64Counter("foo", instrument.WithUnit("By"), instrument.WithDescription("meter counter foo")) assert.NoError(t, err) - fooB.Add(ctx, 100, attribute.String("A", "B")) + fooB.Add(ctx, 100, withAB) }, possibleExpectedFiles: []string{"testdata/no_conflict_two_counters.txt"}, }, @@ -418,13 +436,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter gauge foo")) assert.NoError(t, err) - fooA.Add(ctx, 100, attribute.String("A", "B")) + fooA.Add(ctx, 100, withAB) fooB, err := meterB.Int64UpDownCounter("foo", instrument.WithUnit("By"), instrument.WithDescription("meter gauge foo")) assert.NoError(t, err) - fooB.Add(ctx, 100, attribute.String("A", "B")) + fooB.Add(ctx, 100, withAB) }, possibleExpectedFiles: []string{"testdata/no_conflict_two_updowncounters.txt"}, }, @@ -435,13 +453,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter histogram foo")) assert.NoError(t, err) - fooA.Record(ctx, 100, attribute.String("A", "B")) + fooA.Record(ctx, 100, withAB) fooB, err := meterB.Int64Histogram("foo", instrument.WithUnit("By"), instrument.WithDescription("meter histogram foo")) assert.NoError(t, err) - fooB.Record(ctx, 100, attribute.String("A", "B")) + fooB.Record(ctx, 100, withAB) }, possibleExpectedFiles: []string{"testdata/no_conflict_two_histograms.txt"}, }, @@ -452,13 +470,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter a bar")) assert.NoError(t, err) - barA.Add(ctx, 100, attribute.String("type", "bar")) + barA.Add(ctx, 100, withTypeBar) barB, err := meterB.Int64Counter("bar", instrument.WithUnit("By"), instrument.WithDescription("meter b bar")) assert.NoError(t, err) - barB.Add(ctx, 100, attribute.String("type", "bar")) + barB.Add(ctx, 100, withTypeBar) }, possibleExpectedFiles: []string{ "testdata/conflict_help_two_counters_1.txt", @@ -472,13 +490,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter a bar")) assert.NoError(t, err) - barA.Add(ctx, 100, attribute.String("type", "bar")) + barA.Add(ctx, 100, withTypeBar) barB, err := meterB.Int64UpDownCounter("bar", instrument.WithUnit("By"), instrument.WithDescription("meter b bar")) assert.NoError(t, err) - barB.Add(ctx, 100, attribute.String("type", "bar")) + barB.Add(ctx, 100, withTypeBar) }, possibleExpectedFiles: []string{ "testdata/conflict_help_two_updowncounters_1.txt", @@ -492,13 +510,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter a bar")) assert.NoError(t, err) - barA.Record(ctx, 100, attribute.String("A", "B")) + barA.Record(ctx, 100, withAB) barB, err := meterB.Int64Histogram("bar", instrument.WithUnit("By"), instrument.WithDescription("meter b bar")) assert.NoError(t, err) - barB.Record(ctx, 100, attribute.String("A", "B")) + barB.Record(ctx, 100, withAB) }, possibleExpectedFiles: []string{ "testdata/conflict_help_two_histograms_1.txt", @@ -512,13 +530,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter bar")) assert.NoError(t, err) - bazA.Add(ctx, 100, attribute.String("type", "bar")) + bazA.Add(ctx, 100, withTypeBar) bazB, err := meterB.Int64Counter("bar", instrument.WithUnit("ms"), instrument.WithDescription("meter bar")) assert.NoError(t, err) - bazB.Add(ctx, 100, attribute.String("type", "bar")) + bazB.Add(ctx, 100, withTypeBar) }, options: []Option{WithoutUnits()}, possibleExpectedFiles: []string{"testdata/conflict_unit_two_counters.txt"}, @@ -530,13 +548,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter gauge bar")) assert.NoError(t, err) - barA.Add(ctx, 100, attribute.String("type", "bar")) + barA.Add(ctx, 100, withTypeBar) barB, err := meterB.Int64UpDownCounter("bar", instrument.WithUnit("ms"), instrument.WithDescription("meter gauge bar")) assert.NoError(t, err) - barB.Add(ctx, 100, attribute.String("type", "bar")) + barB.Add(ctx, 100, withTypeBar) }, options: []Option{WithoutUnits()}, possibleExpectedFiles: []string{"testdata/conflict_unit_two_updowncounters.txt"}, @@ -548,13 +566,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter histogram bar")) assert.NoError(t, err) - barA.Record(ctx, 100, attribute.String("A", "B")) + barA.Record(ctx, 100, withAB) barB, err := meterB.Int64Histogram("bar", instrument.WithUnit("ms"), instrument.WithDescription("meter histogram bar")) assert.NoError(t, err) - barB.Record(ctx, 100, attribute.String("A", "B")) + barB.Record(ctx, 100, withAB) }, options: []Option{WithoutUnits()}, possibleExpectedFiles: []string{"testdata/conflict_unit_two_histograms.txt"}, @@ -566,13 +584,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter foo")) assert.NoError(t, err) - counter.Add(ctx, 100, attribute.String("type", "foo")) + counter.Add(ctx, 100, withTypeFoo) gauge, err := meterA.Int64UpDownCounter("foo_total", instrument.WithUnit("By"), instrument.WithDescription("meter foo")) assert.NoError(t, err) - gauge.Add(ctx, 200, attribute.String("type", "foo")) + gauge.Add(ctx, 200, withTypeFoo) }, options: []Option{WithoutUnits()}, possibleExpectedFiles: []string{ @@ -587,13 +605,13 @@ func TestDuplicateMetrics(t *testing.T) { instrument.WithUnit("By"), instrument.WithDescription("meter gauge foo")) assert.NoError(t, err) - fooA.Add(ctx, 100, attribute.String("A", "B")) + fooA.Add(ctx, 100, withAB) fooHistogramA, err := meterA.Int64Histogram("foo", instrument.WithUnit("By"), instrument.WithDescription("meter histogram foo")) assert.NoError(t, err) - fooHistogramA.Record(ctx, 100, attribute.String("A", "B")) + fooHistogramA.Record(ctx, 100, withAB) }, possibleExpectedFiles: []string{ "testdata/conflict_type_histogram_and_updowncounter_1.txt",