15
15
from unittest import TestCase
16
16
17
17
from opentelemetry .baggage import get_all , set_baggage
18
+ from opentelemetry .context import Context
18
19
from opentelemetry .propagators .ot_trace import (
19
20
OT_BAGGAGE_PREFIX ,
20
21
OT_SAMPLED_HEADER ,
24
25
)
25
26
from opentelemetry .sdk .trace import _Span
26
27
from opentelemetry .trace import (
27
- INVALID_SPAN_CONTEXT ,
28
- INVALID_SPAN_ID ,
29
28
INVALID_TRACE_ID ,
30
29
SpanContext ,
31
30
TraceFlags ,
@@ -275,65 +274,44 @@ def test_extract_trace_id_span_id_sampled_false(self):
275
274
get_current_span ().get_span_context ().trace_flags , TraceFlags
276
275
)
277
276
278
- def test_extract_malformed_trace_id (self ):
279
- """Test extraction with malformed trace_id"""
280
-
281
- span_context = get_current_span (
282
- self .ot_trace_propagator .extract (
283
- {
284
- OT_TRACE_ID_HEADER : "abc123!" ,
285
- OT_SPAN_ID_HEADER : "e457b5a2e4d86bd1" ,
286
- OT_SAMPLED_HEADER : "false" ,
287
- },
288
- )
289
- ).get_span_context ()
290
-
291
- self .assertEqual (span_context , INVALID_SPAN_CONTEXT )
292
-
293
- def test_extract_malformed_span_id (self ):
294
- """Test extraction with malformed span_id"""
295
-
296
- span_context = get_current_span (
297
- self .ot_trace_propagator .extract (
298
- {
299
- OT_TRACE_ID_HEADER : "64fe8b2a57d3eff7" ,
300
- OT_SPAN_ID_HEADER : "abc123!" ,
301
- OT_SAMPLED_HEADER : "false" ,
302
- },
303
- )
304
- ).get_span_context ()
305
-
306
- self .assertEqual (span_context , INVALID_SPAN_CONTEXT )
307
-
308
- def test_extract_invalid_trace_id (self ):
309
- """Test extraction with invalid trace_id"""
310
-
311
- span_context = get_current_span (
312
- self .ot_trace_propagator .extract (
313
- {
314
- OT_TRACE_ID_HEADER : INVALID_TRACE_ID ,
315
- OT_SPAN_ID_HEADER : "e457b5a2e4d86bd1" ,
316
- OT_SAMPLED_HEADER : "false" ,
317
- },
318
- )
319
- ).get_span_context ()
320
-
321
- self .assertEqual (span_context , INVALID_SPAN_CONTEXT )
322
-
323
- def test_extract_invalid_span_id (self ):
324
- """Test extraction with invalid span_id"""
325
-
326
- span_context = get_current_span (
327
- self .ot_trace_propagator .extract (
328
- {
329
- OT_TRACE_ID_HEADER : "64fe8b2a57d3eff7" ,
330
- OT_SPAN_ID_HEADER : INVALID_SPAN_ID ,
331
- OT_SAMPLED_HEADER : "false" ,
332
- },
333
- )
334
- ).get_span_context ()
335
-
336
- self .assertEqual (span_context , INVALID_SPAN_CONTEXT )
277
+ def test_extract_invalid_trace_header_to_explict_ctx (self ):
278
+ invalid_headers = [
279
+ ("abc123!" , "e457b5a2e4d86bd1" ), # malformed trace id
280
+ ("64fe8b2a57d3eff7" , "abc123!" ), # malformed span id
281
+ ("0" * 32 , "e457b5a2e4d86bd1" ), # invalid trace id
282
+ ("64fe8b2a57d3eff7" , "0" * 16 ), # invalid span id
283
+ ]
284
+ for trace_id , span_id in invalid_headers :
285
+ with self .subTest (trace_id = trace_id , span_id = span_id ):
286
+ orig_ctx = Context ({"k1" : "v1" })
287
+
288
+ ctx = self .ot_trace_propagator .extract (
289
+ {
290
+ OT_TRACE_ID_HEADER : trace_id ,
291
+ OT_SPAN_ID_HEADER : span_id ,
292
+ OT_SAMPLED_HEADER : "false" ,
293
+ },
294
+ orig_ctx ,
295
+ )
296
+ self .assertDictEqual (orig_ctx , ctx )
297
+
298
+ def test_extract_invalid_trace_header_to_implicit_ctx (self ):
299
+ invalid_headers = [
300
+ ("abc123!" , "e457b5a2e4d86bd1" ), # malformed trace id
301
+ ("64fe8b2a57d3eff7" , "abc123!" ), # malformed span id
302
+ ("0" * 32 , "e457b5a2e4d86bd1" ), # invalid trace id
303
+ ("64fe8b2a57d3eff7" , "0" * 16 ), # invalid span id
304
+ ]
305
+ for trace_id , span_id in invalid_headers :
306
+ with self .subTest (trace_id = trace_id , span_id = span_id ):
307
+ ctx = self .ot_trace_propagator .extract (
308
+ {
309
+ OT_TRACE_ID_HEADER : trace_id ,
310
+ OT_SPAN_ID_HEADER : span_id ,
311
+ OT_SAMPLED_HEADER : "false" ,
312
+ }
313
+ )
314
+ self .assertDictEqual (Context (), ctx )
337
315
338
316
def test_extract_baggage (self ):
339
317
"""Test baggage extraction"""
@@ -359,11 +337,13 @@ def test_extract_baggage(self):
359
337
self .assertEqual (baggage ["abc" ], "abc" )
360
338
self .assertEqual (baggage ["def" ], "def" )
361
339
362
- def test_extract_empty (self ):
363
- "Test extraction when no headers are present"
340
+ def test_extract_empty_to_explicit_ctx (self ):
341
+ """Test extraction when no headers are present"""
342
+ orig_ctx = Context ({"k1" : "v1" })
343
+ ctx = self .ot_trace_propagator .extract ({}, orig_ctx )
364
344
365
- span_context = get_current_span (
366
- self .ot_trace_propagator .extract ({})
367
- ).get_span_context ()
345
+ self .assertDictEqual (orig_ctx , ctx )
368
346
369
- self .assertEqual (span_context , INVALID_SPAN_CONTEXT )
347
+ def test_extract_empty_to_implicit_ctx (self ):
348
+ ctx = self .ot_trace_propagator .extract ({})
349
+ self .assertDictEqual (Context (), ctx )
0 commit comments