| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/exemplar" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/trace" |
| 13 |
) |
| 14 |
|
| 15 |
func TestTraceBasedFilter(t *testing.T) { |
| 16 |
t.Run("Int64", testTraceBasedFilter[int64]) |
| 17 |
t.Run("Float64", testTraceBasedFilter[float64]) |
| 18 |
} |
| 19 |
|
| 20 |
func testTraceBasedFilter[N int64 | float64](t *testing.T) { |
| 21 |
ctx := t.Context() |
| 22 |
|
| 23 |
assert.False(t, TraceBasedFilter(ctx), "non-sampled context should not be offered") |
| 24 |
assert.True(t, TraceBasedFilter(sample(ctx)), "sampled context should be offered") |
| 25 |
} |
| 26 |
|
| 27 |
func sample(parent context.Context) context.Context { |
| 28 |
sc := trace.NewSpanContext(trace.SpanContextConfig{ |
| 29 |
TraceID: trace.TraceID{0x01}, |
| 30 |
SpanID: trace.SpanID{0x01}, |
| 31 |
TraceFlags: trace.FlagsSampled, |
| 32 |
}) |
| 33 |
return trace.ContextWithSpanContext(parent, sc) |
| 34 |
} |
| 35 |
|
| 36 |
func TestAlwaysOnFilter(t *testing.T) { |
| 37 |
t.Run("Int64", testAlwaysOnFiltered[int64]) |
| 38 |
t.Run("Float64", testAlwaysOnFiltered[float64]) |
| 39 |
} |
| 40 |
|
| 41 |
func testAlwaysOnFiltered[N int64 | float64](t *testing.T) { |
| 42 |
ctx := t.Context() |
| 43 |
|
| 44 |
assert.True(t, AlwaysOnFilter(ctx), "non-sampled context should not be offered") |
| 45 |
assert.True(t, AlwaysOnFilter(sample(ctx)), "sampled context should be offered") |
| 46 |
} |