@@ -138,7 +138,9 @@ def test_active_tracing(self):
138
138
self .assertEqual (len (spans ), 1 )
139
139
span = spans [0 ]
140
140
self .assertEqual (span .name , os .environ [_HANDLER ])
141
- self .assertEqual (span .get_span_context ().trace_id , MOCK_XRAY_TRACE_ID )
141
+ self .assertNotEqual (
142
+ span .get_span_context ().trace_id , MOCK_XRAY_TRACE_ID
143
+ )
142
144
self .assertEqual (span .kind , SpanKind .SERVER )
143
145
self .assertSpanHasAttributes (
144
146
span ,
@@ -149,11 +151,7 @@ def test_active_tracing(self):
149
151
)
150
152
151
153
parent_context = span .parent
152
- self .assertEqual (
153
- parent_context .trace_id , span .get_span_context ().trace_id
154
- )
155
- self .assertEqual (parent_context .span_id , MOCK_XRAY_PARENT_SPAN_ID )
156
- self .assertTrue (parent_context .is_remote )
154
+ self .assertEqual (None , parent_context )
157
155
158
156
test_env_patch .stop ()
159
157
@@ -165,11 +163,8 @@ class TestCase:
165
163
context : Dict
166
164
expected_traceid : int
167
165
expected_parentid : int
168
- xray_traceid : str
169
166
expected_state_value : str = None
170
167
expected_trace_state_len : int = 0
171
- disable_aws_context_propagation : bool = False
172
- disable_aws_context_propagation_envvar : str = ""
173
168
174
169
def custom_event_context_extractor (lambda_event ):
175
170
return get_global_textmap ().extract (lambda_event ["foo" ]["headers" ])
@@ -188,42 +183,9 @@ def custom_event_context_extractor(lambda_event):
188
183
expected_parentid = MOCK_W3C_PARENT_SPAN_ID ,
189
184
expected_trace_state_len = 3 ,
190
185
expected_state_value = MOCK_W3C_TRACE_STATE_VALUE ,
191
- xray_traceid = MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED ,
192
- ),
193
- TestCase (
194
- name = "custom_extractor_not_sampled_xray" ,
195
- custom_extractor = custom_event_context_extractor ,
196
- context = {
197
- "foo" : {
198
- "headers" : {
199
- TraceContextTextMapPropagator ._TRACEPARENT_HEADER_NAME : MOCK_W3C_TRACE_CONTEXT_SAMPLED ,
200
- TraceContextTextMapPropagator ._TRACESTATE_HEADER_NAME : f"{ MOCK_W3C_TRACE_STATE_KEY } ={ MOCK_W3C_TRACE_STATE_VALUE } ,foo=1,bar=2" ,
201
- }
202
- }
203
- },
204
- expected_traceid = MOCK_W3C_TRACE_ID ,
205
- expected_parentid = MOCK_W3C_PARENT_SPAN_ID ,
206
- expected_trace_state_len = 3 ,
207
- expected_state_value = MOCK_W3C_TRACE_STATE_VALUE ,
208
- xray_traceid = MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED ,
209
- ),
210
- TestCase (
211
- name = "custom_extractor_sampled_xray" ,
212
- custom_extractor = custom_event_context_extractor ,
213
- context = {
214
- "foo" : {
215
- "headers" : {
216
- TraceContextTextMapPropagator ._TRACEPARENT_HEADER_NAME : MOCK_W3C_TRACE_CONTEXT_SAMPLED ,
217
- TraceContextTextMapPropagator ._TRACESTATE_HEADER_NAME : f"{ MOCK_W3C_TRACE_STATE_KEY } ={ MOCK_W3C_TRACE_STATE_VALUE } ,foo=1,bar=2" ,
218
- }
219
- }
220
- },
221
- expected_traceid = MOCK_XRAY_TRACE_ID ,
222
- expected_parentid = MOCK_XRAY_PARENT_SPAN_ID ,
223
- xray_traceid = MOCK_XRAY_TRACE_CONTEXT_SAMPLED ,
224
186
),
225
187
TestCase (
226
- name = "custom_extractor_sampled_xray_disable_aws_propagation " ,
188
+ name = "custom_extractor " ,
227
189
custom_extractor = custom_event_context_extractor ,
228
190
context = {
229
191
"foo" : {
@@ -233,47 +195,24 @@ def custom_event_context_extractor(lambda_event):
233
195
}
234
196
}
235
197
},
236
- disable_aws_context_propagation = True ,
237
- expected_traceid = MOCK_W3C_TRACE_ID ,
238
- expected_parentid = MOCK_W3C_PARENT_SPAN_ID ,
239
- expected_trace_state_len = 3 ,
240
- expected_state_value = MOCK_W3C_TRACE_STATE_VALUE ,
241
- xray_traceid = MOCK_XRAY_TRACE_CONTEXT_SAMPLED ,
242
- ),
243
- TestCase (
244
- name = "no_custom_extractor_xray_disable_aws_propagation_via_env_var" ,
245
- custom_extractor = None ,
246
- context = {
247
- "headers" : {
248
- TraceContextTextMapPropagator ._TRACEPARENT_HEADER_NAME : MOCK_W3C_TRACE_CONTEXT_SAMPLED ,
249
- TraceContextTextMapPropagator ._TRACESTATE_HEADER_NAME : f"{ MOCK_W3C_TRACE_STATE_KEY } ={ MOCK_W3C_TRACE_STATE_VALUE } ,foo=1,bar=2" ,
250
- }
251
- },
252
- disable_aws_context_propagation = False ,
253
- disable_aws_context_propagation_envvar = "true" ,
254
198
expected_traceid = MOCK_W3C_TRACE_ID ,
255
199
expected_parentid = MOCK_W3C_PARENT_SPAN_ID ,
256
200
expected_trace_state_len = 3 ,
257
201
expected_state_value = MOCK_W3C_TRACE_STATE_VALUE ,
258
- xray_traceid = MOCK_XRAY_TRACE_CONTEXT_SAMPLED ,
259
202
),
260
203
]
261
204
for test in tests :
262
205
test_env_patch = mock .patch .dict (
263
206
"os.environ" ,
264
207
{
265
208
** os .environ ,
266
- # NOT Active Tracing
267
- _X_AMZN_TRACE_ID : test .xray_traceid ,
268
- OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION : test .disable_aws_context_propagation_envvar ,
269
209
# NOT using the X-Ray Propagator
270
210
OTEL_PROPAGATORS : "tracecontext" ,
271
211
},
272
212
)
273
213
test_env_patch .start ()
274
214
AwsLambdaInstrumentor ().instrument (
275
- event_context_extractor = test .custom_extractor ,
276
- disable_aws_context_propagation = test .disable_aws_context_propagation ,
215
+ event_context_extractor = test .custom_extractor
277
216
)
278
217
mock_execute_lambda (test .context )
279
218
spans = self .memory_exporter .get_finished_spans ()
@@ -301,6 +240,65 @@ def custom_event_context_extractor(lambda_event):
301
240
AwsLambdaInstrumentor ().uninstrument ()
302
241
test_env_patch .stop ()
303
242
243
+ def test_links_from_lambda_event (self ):
244
+ @dataclass
245
+ class TestCase :
246
+ name : str
247
+ context : Dict
248
+ expected_link_trace_id : int
249
+ xray_traceid : str
250
+ disable_aws_context_propagation : bool = False
251
+ disable_aws_context_propagation_envvar : str = ""
252
+
253
+ tests = [
254
+ TestCase (
255
+ name = "enabled_aws_context_propagation" ,
256
+ context = {},
257
+ expected_link_trace_id = MOCK_XRAY_TRACE_ID ,
258
+ xray_traceid = MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED ,
259
+ ),
260
+ TestCase (
261
+ name = "disable_aws_context_propagation" ,
262
+ context = {},
263
+ expected_link_trace_id = None ,
264
+ xray_traceid = MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED ,
265
+ disable_aws_context_propagation = True ,
266
+ ),
267
+ ]
268
+ for test in tests :
269
+ test_env_patch = mock .patch .dict (
270
+ "os.environ" ,
271
+ {
272
+ ** os .environ ,
273
+ # NOT Active Tracing
274
+ _X_AMZN_TRACE_ID : test .xray_traceid ,
275
+ OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION : test .disable_aws_context_propagation_envvar ,
276
+ # NOT using the X-Ray Propagator
277
+ OTEL_PROPAGATORS : "tracecontext" ,
278
+ },
279
+ )
280
+ test_env_patch .start ()
281
+ AwsLambdaInstrumentor ().instrument (
282
+ disable_aws_context_propagation = test .disable_aws_context_propagation ,
283
+ )
284
+ mock_execute_lambda (test .context )
285
+ spans = self .memory_exporter .get_finished_spans ()
286
+ assert spans
287
+ self .assertEqual (len (spans ), 1 )
288
+ span = spans [0 ]
289
+
290
+ if test .expected_link_trace_id == None :
291
+ self .assertEqual (0 , len (span .links ))
292
+ else :
293
+ link = span .links [0 ]
294
+ self .assertEqual (
295
+ link .context .trace_id , test .expected_link_trace_id
296
+ )
297
+
298
+ self .memory_exporter .clear ()
299
+ AwsLambdaInstrumentor ().uninstrument ()
300
+ test_env_patch .stop ()
301
+
304
302
def test_lambda_no_error_with_invalid_flush_timeout (self ):
305
303
test_env_patch = mock .patch .dict (
306
304
"os.environ" ,
0 commit comments