Skip to content

Commit

Permalink
expand unit testing for exemplar filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Oct 4, 2024
1 parent 8a10f73 commit 020cf0f
Showing 1 changed file with 92 additions and 6 deletions.
98 changes: 92 additions & 6 deletions sdk/metric/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.opentelemetry.io/otel/sdk/metric/exemplar"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
)

type reader struct {
Expand Down Expand Up @@ -194,10 +195,95 @@ func TestWithView(t *testing.T) {
assert.Len(t, c.views, 2)
}

func TestWithExemplarFilter(t *testing.T) {
c := newConfig([]Option{WithExemplarFilter(
exemplar.AlwaysOffFilter,
)})
assert.NotNil(t, c.exemplarFilter)
assert.False(t, c.exemplarFilter(context.Background()))
func TestWithExemplarFilterOff(t *testing.T) {
for _, tc := range []struct {
desc string
opts []Option
env string
expectFilterSampled bool
expectFilterNotSampled bool
}{
{
desc: "default",
expectFilterSampled: true,
expectFilterNotSampled: false,
},
{
desc: "always on option",
opts: []Option{WithExemplarFilter(exemplar.AlwaysOnFilter)},
expectFilterSampled: true,
expectFilterNotSampled: true,
},
{
desc: "always off option",
opts: []Option{WithExemplarFilter(exemplar.AlwaysOffFilter)},
expectFilterSampled: false,
expectFilterNotSampled: false,
},
{
desc: "trace based option",
opts: []Option{WithExemplarFilter(exemplar.TraceBasedFilter)},
expectFilterSampled: true,
expectFilterNotSampled: false,
},
{
desc: "last option takes precedence",
opts: []Option{
WithExemplarFilter(exemplar.AlwaysOffFilter),
WithExemplarFilter(exemplar.AlwaysOnFilter),
},
expectFilterSampled: true,
expectFilterNotSampled: true,
},
{
desc: "always_off env",
env: "always_off",
expectFilterSampled: false,
expectFilterNotSampled: false,
},
{
desc: "always_on env",
env: "always_on",
expectFilterSampled: true,
expectFilterNotSampled: true,
},
{
desc: "trace_based env",
env: "trace_based",
expectFilterSampled: true,
expectFilterNotSampled: false,
},
{
desc: "wrong env",
env: "foo_bar",
expectFilterSampled: true,
expectFilterNotSampled: false,
},
{
desc: "option takes precedence over env var",
env: "always_off",
opts: []Option{WithExemplarFilter(exemplar.AlwaysOnFilter)},
expectFilterSampled: true,
expectFilterNotSampled: true,
},
} {
t.Run(tc.desc, func(t *testing.T) {
if tc.env != "" {
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", tc.env)
}
c := newConfig(tc.opts)
assert.NotNil(t, c.exemplarFilter)
assert.Equal(t, tc.expectFilterNotSampled, c.exemplarFilter(context.Background()))
assert.Equal(t, tc.expectFilterSampled, c.exemplarFilter(sample(context.Background())))
})
}
}

func sample(parent context.Context) context.Context {
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{0x01},
SpanID: trace.SpanID{0x01},
TraceFlags: trace.FlagsSampled,
})
return trace.ContextWithSpanContext(parent, sc)
}

0 comments on commit 020cf0f

Please sign in to comment.