Skip to content

Commit e44935f

Browse files
committed
Test uses trace context from context object.
1 parent b5e9c50 commit e44935f

1 file changed

Lines changed: 72 additions & 2 deletions

File tree

internal/trace/context_test.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414
"io/ioutil"
1515
"testing"
1616

17+
"github.com/DataDog/datadog-lambda-go/internal/extension"
1718
"github.com/aws/aws-xray-sdk-go/header"
18-
1919
"github.com/aws/aws-xray-sdk-go/xray"
20-
2120
"github.com/stretchr/testify/assert"
2221
)
2322

@@ -45,6 +44,20 @@ func mockLambdaXRayTraceContext(ctx context.Context, traceID, parentID string, s
4544
return context.WithValue(ctx, xray.LambdaTraceHeaderKey, headerString)
4645
}
4746

47+
func mockTraceContext(traceID, parentID, samplingPriority string) context.Context {
48+
ctx := context.Background()
49+
if traceID != "" {
50+
ctx = context.WithValue(ctx, extension.DdTraceId, traceID)
51+
}
52+
if parentID != "" {
53+
ctx = context.WithValue(ctx, extension.DdParentId, parentID)
54+
}
55+
if samplingPriority != "" {
56+
ctx = context.WithValue(ctx, extension.DdSamplingPriority, samplingPriority)
57+
}
58+
return ctx
59+
}
60+
4861
func loadRawJSON(t *testing.T, filename string) *json.RawMessage {
4962
bytes, err := ioutil.ReadFile(filename)
5063
if err != nil {
@@ -117,6 +130,63 @@ func TestGetDatadogTraceContextForMissingData(t *testing.T) {
117130
assert.False(t, ok)
118131
}
119132

133+
func TestGetDatadogTraceContextFromContextObject(t *testing.T) {
134+
testcases := []struct {
135+
traceID string
136+
parentID string
137+
samplingPriority string
138+
expectTC TraceContext
139+
expectOk bool
140+
}{
141+
{
142+
"trace",
143+
"parent",
144+
"sampling",
145+
TraceContext{
146+
"x-datadog-trace-id": "trace",
147+
"x-datadog-parent-id": "parent",
148+
"x-datadog-sampling-priority": "sampling",
149+
},
150+
true,
151+
},
152+
{
153+
"",
154+
"parent",
155+
"sampling",
156+
TraceContext{},
157+
false,
158+
},
159+
{
160+
"trace",
161+
"",
162+
"sampling",
163+
TraceContext{},
164+
false,
165+
},
166+
{
167+
"trace",
168+
"parent",
169+
"",
170+
TraceContext{
171+
"x-datadog-trace-id": "trace",
172+
"x-datadog-parent-id": "parent",
173+
"x-datadog-sampling-priority": "1",
174+
},
175+
true,
176+
},
177+
}
178+
179+
ev := loadRawJSON(t, "../testdata/non-proxy-no-headers.json")
180+
for _, test := range testcases {
181+
t.Run(test.traceID+test.parentID+test.samplingPriority, func(t *testing.T) {
182+
ctx := mockTraceContext(test.traceID, test.parentID, test.samplingPriority)
183+
tc, ok := getTraceContext(ctx, getHeadersFromEventHeaders(ctx, *ev))
184+
assert.Equal(t, test.expectTC, tc)
185+
assert.Equal(t, test.expectOk, ok)
186+
})
187+
}
188+
}
189+
120190
func TestConvertXRayTraceID(t *testing.T) {
121191
output, err := convertXRayTraceIDToDatadogTraceID(mockXRayTraceID)
122192
assert.NoError(t, err)

0 commit comments

Comments
 (0)