Skip to content

Commit

Permalink
test filtered attributes in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Apr 3, 2024
1 parent e527b4c commit 525c53d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,12 @@ func TestShutdownExporter(t *testing.T) {
}

func TestExemplars(t *testing.T) {
attrsOpt := otelmetric.WithAttributes(
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
attribute.Key("E").Bool(true),
attribute.Key("F").Int(42),
)
for _, tc := range []struct {
name string
recordMetrics func(ctx context.Context, meter otelmetric.Meter)
Expand All @@ -912,7 +918,7 @@ func TestExemplars(t *testing.T) {
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
counter, err := meter.Float64Counter("foo")
require.NoError(t, err)
counter.Add(ctx, 9)
counter.Add(ctx, 9, attrsOpt)
},
expectedExemplarValue: 9,
},
Expand All @@ -921,7 +927,7 @@ func TestExemplars(t *testing.T) {
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
hist, err := meter.Int64Histogram("foo")
require.NoError(t, err)
hist.Record(ctx, 9)
hist.Record(ctx, 9, attrsOpt)
},
expectedExemplarValue: 9,
},
Expand All @@ -947,6 +953,14 @@ func TestExemplars(t *testing.T) {
provider := metric.NewMeterProvider(
metric.WithReader(exporter),
metric.WithResource(res),
metric.WithView(metric.NewView(
metric.Instrument{Name: "*"},
metric.Stream{
// filter out all attributes so they are added as filtered
// attributes to the exemplar
AttributeFilter: attribute.NewAllowKeysFilter(),
},
)),
)
meter := provider.Meter("meter", otelmetric.WithInstrumentationVersion("v0.1.0"))

Expand Down Expand Up @@ -987,6 +1001,10 @@ func TestExemplars(t *testing.T) {
expectedLabels := map[string]string{
traceIDExemplarKey: "01000000000000000000000000000000",
spanIDExemplarKey: "0100000000000000",
"A": "B",
"C": "D",
"E": "true",
"F": "42",
}
require.Equal(t, len(expectedLabels), len(exemplar.GetLabel()))
for _, label := range exemplar.GetLabel() {
Expand Down

0 comments on commit 525c53d

Please sign in to comment.