-
Notifications
You must be signed in to change notification settings - Fork 0
/
spanprocessor_test.go
80 lines (68 loc) · 2.07 KB
/
spanprocessor_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
78
79
80
package otelfuncmeta
import (
"context"
"strings"
"testing"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func basicTracerProvider(t *testing.T, e *testExporter) *sdktrace.TracerProvider {
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(NewSpanProcessor()),
sdktrace.WithSyncer(e),
)
return tp
}
type testExporter struct {
spans []sdktrace.ReadOnlySpan
}
func (e *testExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
e.spans = append(e.spans, spans...)
return nil
}
func (e *testExporter) Shutdown(_ context.Context) error {
return nil
}
func TestSpanProcessor(t *testing.T) {
e := &testExporter{}
tp := basicTracerProvider(t, e)
tr := tp.Tracer("NilExporter")
_, span := tr.Start(context.Background(), "foo")
span.End()
if len(e.spans) != 1 {
t.Fatalf("expected 1 span, got %d", len(e.spans))
}
attrs := e.spans[0].Attributes()
if len(attrs) != 4 {
t.Fatalf("expected 4 attribute, got %d", len(attrs))
}
if attrs[0].Key != "function.pc" {
t.Fatalf("expected function.pc, got %s", attrs[0].Key)
}
// Intentionally not testing the value of the program counter as it is
// likely to change across Go versions.
if attrs[1].Key != "function.name" {
t.Fatalf("expected function.name, got %s", attrs[1].Key)
}
if attrs[1].Value.AsString() != "github.com/polarsignals/otelfuncmeta.TestSpanProcessor" {
t.Fatalf("expected TestSpanProcessor, got %s", attrs[1].Value.AsString())
}
if attrs[2].Key != "function.file" {
t.Fatalf("expected function.file, got %s", attrs[2].Key)
}
if !strings.HasSuffix(attrs[2].Value.AsString(), "spanprocessor_test.go") {
t.Fatalf("expected to end with spanprocessor_test.go, got %s", attrs[2].Value.AsString())
}
if attrs[3].Key != "function.line" {
t.Fatalf("expected function.line, got %s", attrs[3].Key)
}
if attrs[3].Value.AsInt64() != 39 {
t.Fatalf("expected 38, got %d", attrs[3].Value.AsInt64())
}
}
func BenchmarkFuncMetadata(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _, _, _ = funcMetadata(1)
}
}