-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctxtrace_test.go
132 lines (108 loc) · 3.96 KB
/
ctxtrace_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ctxtrace
import (
"context"
"net/http/httptest"
"testing"
"time"
"github.com/openzipkin/zipkin-go/propagation/b3"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/metadata"
)
const (
dummyRequestID = "Foo"
)
func TestPackMetadata(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
ctx = context.WithValue(ctx, traceCtxMarker{}, TraceData{
RequestID: dummyRequestID,
})
ctx = NewOutgoingContextWithData(ctx)
outGoingMD, _ := metadata.FromOutgoingContext(ctx)
if assert.NotNil(t, outGoingMD) {
if assert.Contains(t, outGoingMD, headerRequestID) {
if assert.Len(t, outGoingMD[headerRequestID], 1) {
assert.Equal(t, dummyRequestID, outGoingMD[headerRequestID][0])
}
}
}
}
func TestAddOtelSpanContextToContext_NotSmapled(t *testing.T) {
r := httptest.NewRequest("GET", "/foo", nil)
r.Header.Set(headerRequestID, dummyRequestID)
r.Header.Set(b3.ParentSpanID, "0716f381a10c2a9b")
r.Header.Set(b3.Sampled, "0")
r.Header.Set(b3.SpanID, "b2f181687dd7ca60")
r.Header.Set(b3.TraceID, "df1af326541277a75e451e1c03b7e893")
ctx := context.Background()
data, err := ExtractHTTP(r)
assert.Nil(t, err)
ctx = addOtelSpanContextToContext(ctx, data)
spanContext := trace.SpanContextFromContext(ctx)
assert.NotNil(t, spanContext)
assert.Equal(t, spanContext.TraceFlags(), trace.TraceFlags(0))
assert.Equal(t, spanContext.SpanID().String(), data.TraceSpan.ID.String())
assert.Equal(t, spanContext.TraceID().String(), data.TraceSpan.TraceID.String())
}
func TestAddOtelSpanContextToContext_Smapled(t *testing.T) {
r := httptest.NewRequest("GET", "/foo", nil)
r.Header.Set(headerRequestID, dummyRequestID)
r.Header.Set(b3.ParentSpanID, "0716f381a10c2a9b")
r.Header.Set(b3.Sampled, "1")
r.Header.Set(b3.SpanID, "b2f181687dd7ca60")
r.Header.Set(b3.TraceID, "df1af326541277a75e451e1c03b7e893")
ctx := context.Background()
data, err := ExtractHTTP(r)
assert.Nil(t, err)
ctx = addOtelSpanContextToContext(ctx, data)
spanContext := trace.SpanContextFromContext(ctx)
assert.NotNil(t, spanContext)
assert.Equal(t, spanContext.TraceFlags(), trace.TraceFlags(1))
assert.Equal(t, spanContext.SpanID().String(), data.TraceSpan.ID.String())
assert.Equal(t, spanContext.TraceID().String(), data.TraceSpan.TraceID.String())
}
func TestAddOtelSpanContextToContext_InvalidParent(t *testing.T) {
r := httptest.NewRequest("GET", "/foo", nil)
r.Header.Set(headerRequestID, dummyRequestID)
r.Header.Set(b3.ParentSpanID, "")
r.Header.Set(b3.Sampled, "1")
r.Header.Set(b3.SpanID, "")
r.Header.Set(b3.TraceID, "")
ctx := context.Background()
data, err := ExtractHTTP(r)
assert.Nil(t, err)
ctx = addOtelSpanContextToContext(ctx, data)
// If there's no spanContext in the ctx, default EmptySpanContext is returned
spanContext := trace.SpanContextFromContext(ctx)
assert.NotNil(t, spanContext)
assert.False(t, spanContext.IsValid())
assert.Equal(t, spanContext, trace.SpanContext{})
}
func TestExtractHTTP(t *testing.T) {
r := httptest.NewRequest("GET", "/foo", nil)
r.Header.Set(headerRequestID, dummyRequestID)
r.Header.Set(b3.ParentSpanID, "0716f381a10c2a9b")
r.Header.Set(b3.Sampled, "0")
r.Header.Set(b3.SpanID, "b2f181687dd7ca60")
r.Header.Set(b3.TraceID, "df1af326541277a75e451e1c03b7e893")
data, err := ExtractHTTP(r)
assert.Nil(t, err)
assert.NotNil(t, data.TraceSpan)
assert.Equal(t, dummyRequestID, data.RequestID)
ctx := context.Background()
ctx = ExtractHTTPToContext(ctx, r)
ctx = NewOutgoingContextWithData(ctx)
outGoingMD, _ := metadata.FromOutgoingContext(ctx)
if assert.NotNil(t, outGoingMD) {
if assert.Contains(t, outGoingMD, headerRequestID) {
if assert.Len(t, outGoingMD[headerRequestID], 1) {
assert.Equal(t, dummyRequestID, outGoingMD[headerRequestID][0])
}
}
assert.Contains(t, outGoingMD, b3.SpanID)
assert.Contains(t, outGoingMD, b3.Sampled)
assert.Contains(t, outGoingMD, b3.TraceID)
assert.Contains(t, outGoingMD, b3.ParentSpanID)
}
}