-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp_test.go
84 lines (67 loc) · 1.62 KB
/
amqp_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
81
82
83
84
package amqptrace
import (
"context"
"strings"
"sync"
"testing"
"github.com/streadway/amqp"
"go.opentelemetry.io/otel/api/trace"
export "go.opentelemetry.io/otel/sdk/export/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func TestInject(t *testing.T) {
ctx := context.Background()
ctx, span := testMockStart(ctx)
h := amqp.Table{}
Inject(ctx, h)
v, ok := h["traceparent"]
if !ok {
t.Fatalf("failed to Inject")
}
s, ok := v.(string)
if !ok {
t.Fatalf("invalid trace id")
}
ss := strings.Split(s, "-")
if len(ss) != 4 {
t.Fatalf("invalid trace id")
}
if span.SpanContext().TraceID.String() != ss[1] {
t.Fatalf("invalid trace id")
}
}
func TestExtract(t *testing.T) {
ctx := context.Background()
ctx, span := testMockStart(ctx)
h := amqp.Table{}
Inject(ctx, h)
_, spanCtx := Extract(ctx, h)
if span.SpanContext().TraceID.String() != spanCtx.TraceID.String() {
t.Fatalf("failed to Extract")
}
}
func testMockStart(ctx context.Context) (context.Context, trace.Span) {
exp := &testExporter{spanMap: make(map[string]*export.SpanData)}
tp, _ := sdktrace.NewProvider(
sdktrace.WithSyncer(exp),
sdktrace.WithConfig(sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
}))
tracer := tp.Tracer("amqptrace/client")
ctx, span := tracer.Start(
ctx,
"testamqp",
trace.WithSpanKind(trace.SpanKindClient),
)
ctx = trace.ContextWithSpan(ctx, span)
return ctx, span
}
type testExporter struct {
mu sync.Mutex
spanMap map[string]*export.SpanData
}
func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) {
t.mu.Lock()
defer t.mu.Unlock()
t.spanMap[s.Name] = s
}