File: filter_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (46 lines) | stat: -rw-r--r-- 1,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package exemplar // import "go.opentelemetry.io/otel/sdk/metric/exemplar"

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"

	"go.opentelemetry.io/otel/trace"
)

func TestTraceBasedFilter(t *testing.T) {
	t.Run("Int64", testTraceBasedFilter[int64])
	t.Run("Float64", testTraceBasedFilter[float64])
}

func testTraceBasedFilter[N int64 | float64](t *testing.T) {
	ctx := context.Background()

	assert.False(t, TraceBasedFilter(ctx), "non-sampled context should not be offered")
	assert.True(t, TraceBasedFilter(sample(ctx)), "sampled context should be offered")
}

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)
}

func TestAlwaysOnFilter(t *testing.T) {
	t.Run("Int64", testAlwaysOnFiltered[int64])
	t.Run("Float64", testAlwaysOnFiltered[float64])
}

func testAlwaysOnFiltered[N int64 | float64](t *testing.T) {
	ctx := context.Background()

	assert.True(t, AlwaysOnFilter(ctx), "non-sampled context should not be offered")
	assert.True(t, AlwaysOnFilter(sample(ctx)), "sampled context should be offered")
}