@@ -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+
4861func loadRawJSON (t * testing.T , filename string ) * json.RawMessage {
4962 bytes , err := ioutil .ReadFile (filename )
5063 if err != nil {
@@ -60,7 +73,7 @@ func TestGetDatadogTraceContextForTraceMetadataNonProxyEvent(t *testing.T) {
6073 ctx := mockLambdaXRayTraceContext (context .Background (), mockXRayTraceID , mockXRayEntityID , true )
6174 ev := loadRawJSON (t , "../testdata/apig-event-with-headers.json" )
6275
63- headers , ok := getTraceContext (getHeadersFromEventHeaders (ctx , * ev ))
76+ headers , ok := getTraceContext (ctx , getHeadersFromEventHeaders (ctx , * ev ))
6477 assert .True (t , ok )
6578
6679 expected := TraceContext {
@@ -75,7 +88,7 @@ func TestGetDatadogTraceContextForTraceMetadataWithMixedCaseHeaders(t *testing.T
7588 ctx := mockLambdaXRayTraceContext (context .Background (), mockXRayTraceID , mockXRayEntityID , true )
7689 ev := loadRawJSON (t , "../testdata/non-proxy-with-mixed-case-headers.json" )
7790
78- headers , ok := getTraceContext (getHeadersFromEventHeaders (ctx , * ev ))
91+ headers , ok := getTraceContext (ctx , getHeadersFromEventHeaders (ctx , * ev ))
7992 assert .True (t , ok )
8093
8194 expected := TraceContext {
@@ -90,7 +103,7 @@ func TestGetDatadogTraceContextForTraceMetadataWithMissingSamplingPriority(t *te
90103 ctx := mockLambdaXRayTraceContext (context .Background (), mockXRayTraceID , mockXRayEntityID , true )
91104 ev := loadRawJSON (t , "../testdata/non-proxy-with-missing-sampling-priority.json" )
92105
93- headers , ok := getTraceContext (getHeadersFromEventHeaders (ctx , * ev ))
106+ headers , ok := getTraceContext (ctx , getHeadersFromEventHeaders (ctx , * ev ))
94107 assert .True (t , ok )
95108
96109 expected := TraceContext {
@@ -105,18 +118,75 @@ func TestGetDatadogTraceContextForInvalidData(t *testing.T) {
105118 ctx := mockLambdaXRayTraceContext (context .Background (), mockXRayTraceID , mockXRayEntityID , true )
106119 ev := loadRawJSON (t , "../testdata/invalid.json" )
107120
108- _ , ok := getTraceContext (getHeadersFromEventHeaders (ctx , * ev ))
121+ _ , ok := getTraceContext (ctx , getHeadersFromEventHeaders (ctx , * ev ))
109122 assert .False (t , ok )
110123}
111124
112125func TestGetDatadogTraceContextForMissingData (t * testing.T ) {
113126 ctx := mockLambdaXRayTraceContext (context .Background (), mockXRayTraceID , mockXRayEntityID , true )
114127 ev := loadRawJSON (t , "../testdata/non-proxy-no-headers.json" )
115128
116- _ , ok := getTraceContext (getHeadersFromEventHeaders (ctx , * ev ))
129+ _ , ok := getTraceContext (ctx , getHeadersFromEventHeaders (ctx , * ev ))
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+
120190func TestConvertXRayTraceID (t * testing.T ) {
121191 output , err := convertXRayTraceIDToDatadogTraceID (mockXRayTraceID )
122192 assert .NoError (t , err )
0 commit comments