-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer_test.go
77 lines (67 loc) · 2.22 KB
/
tracer_test.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ddspanname
import (
"context"
"testing"
"github.com/stretchr/testify/require"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func TestTracer(t *testing.T) {
tests := []struct {
instrumentName string // default is "ddspanname.instrument"
operationName string // default is "ddspanname.operation"
spanName string // default is "ddspanname.span"
options []TracerOption
expectedSpanName string
expectedOperationName string
expectedResourceName string
}{
{
expectedSpanName: "ddspanname.span",
expectedOperationName: "ddspanname.operation",
expectedResourceName: "ddspanname.span",
},
{
options: []TracerOption{
WithTracerOperationNameFormatter(func(ctx context.Context, operationName string) string {
return operationName + "Custom"
}),
},
expectedSpanName: "ddspanname.span",
expectedOperationName: "ddspanname.operationCustom",
expectedResourceName: "ddspanname.span",
},
{
options: []TracerOption{
WithTracerSpanNameFormatter(func(ctx context.Context, operationName string, spanName string) string {
return spanName + "Custom"
}),
},
expectedSpanName: "ddspanname.spanCustom",
expectedOperationName: "ddspanname.operation",
expectedResourceName: "ddspanname.spanCustom",
},
}
for _, test := range tests {
if test.instrumentName == "" {
test.instrumentName = "ddspanname.instrument"
}
if test.operationName == "" {
test.operationName = "ddspanname.operation"
}
if test.spanName == "" {
test.spanName = "ddspanname.span"
}
sr := tracetest.NewSpanRecorder()
tracerProvider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
tracer := tracerProvider.Tracer(test.instrumentName)
ddtracer := NewTracer(tracer, test.operationName, test.options...)
_, span := ddtracer.Start(context.Background(), test.spanName)
span.End()
spans := sr.Ended()
require.Len(t, spans, 1)
require.Equal(t, test.expectedResourceName, spans[0].Name())
require.Contains(t, spans[0].Attributes(), DDOperationNameKey.String(test.expectedOperationName))
require.Contains(t, spans[0].Attributes(), DDResourceNameKey.String(test.expectedResourceName))
}
}