-
Notifications
You must be signed in to change notification settings - Fork 1
/
otelpyroscope_test.go
69 lines (60 loc) · 1.62 KB
/
otelpyroscope_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
package otelpyroscope
import (
"context"
"runtime/pprof"
"testing"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
)
func Test_tracerProvider(t *testing.T) {
otel.SetTracerProvider(NewTracerProvider(trace.NewTracerProvider()))
tracer := otel.Tracer("")
labels := make(map[string]string)
ctx, spanR := tracer.Start(context.Background(), "RootSpan")
pprof.ForLabels(ctx, func(key, value string) bool {
labels[key] = value
return true
})
spanID, ok := labels[spanIDLabelName]
if !ok {
t.Fatal("span ID label not found")
}
if len(spanID) != 16 {
t.Fatalf("invalid span ID: %q", spanID)
}
name, ok := labels[spanNameLabelName]
if !ok {
t.Fatal("span name label not found")
}
if name != "RootSpan" {
t.Fatalf("invalid span name: %q", name)
}
// Nested child span has the same labels.
ctx, spanA := tracer.Start(ctx, "SpanA")
pprof.ForLabels(ctx, func(key, value string) bool {
if v, ok := labels[key]; !ok || v != value {
t.Fatalf("nested span labels mismatch: %q=%q", key, value)
}
return true
})
spanA.End()
spanR.End()
// Child span created after the root span end using its context.
ctx, spanB := tracer.Start(ctx, "SpanB")
pprof.ForLabels(ctx, func(key, value string) bool {
if v, ok := labels[key]; !ok || v != value {
t.Fatalf("nested span labels mismatch: %q=%q", key, value)
}
return true
})
spanB.End()
// A new root span.
ctx, spanC := tracer.Start(context.Background(), "SpanC")
pprof.ForLabels(ctx, func(key, value string) bool {
if v, ok := labels[key]; !ok || v == value {
t.Fatalf("unexpected match: %q=%q", key, value)
}
return true
})
spanC.End()
}