18
18
import opentracing
19
19
20
20
import opentelemetry .ext .opentracing_shim as opentracingshim
21
- from opentelemetry import trace
21
+ from opentelemetry import propagators , trace
22
+ from opentelemetry .context .propagation .httptextformat import HTTPTextFormat
22
23
from opentelemetry .ext .opentracing_shim import util
23
24
from opentelemetry .sdk .trace import Tracer
24
25
@@ -40,6 +41,17 @@ def setUpClass(cls):
40
41
41
42
trace .set_preferred_tracer_implementation (lambda T : Tracer ())
42
43
44
+ # Save current propagator to be restored on teardown.
45
+ cls ._previous_propagator = propagators .get_global_httptextformat ()
46
+
47
+ # Set mock propagator for testing.
48
+ propagators .set_global_httptextformat (MockHTTPTextFormat )
49
+
50
+ @classmethod
51
+ def tearDownClass (cls ):
52
+ # Restore previous propagator.
53
+ propagators .set_global_httptextformat (cls ._previous_propagator )
54
+
43
55
def test_shim_type (self ):
44
56
# Verify shim is an OpenTracing tracer.
45
57
self .assertIsInstance (self .shim , opentracing .Tracer )
@@ -453,3 +465,91 @@ def test_span_on_error(self):
453
465
self .assertEqual (
454
466
scope .span .unwrap ().events [0 ].attributes ["error.kind" ], Exception
455
467
)
468
+
469
+ def test_inject_http_headers (self ):
470
+ """Test `inject()` method for Format.HTTP_HEADERS."""
471
+
472
+ otel_context = trace .SpanContext (trace_id = 1220 , span_id = 7478 )
473
+ context = opentracingshim .SpanContextShim (otel_context )
474
+
475
+ headers = {}
476
+ self .shim .inject (context , opentracing .Format .HTTP_HEADERS , headers )
477
+ self .assertEqual (headers [MockHTTPTextFormat .TRACE_ID_KEY ], str (1220 ))
478
+ self .assertEqual (headers [MockHTTPTextFormat .SPAN_ID_KEY ], str (7478 ))
479
+
480
+ def test_inject_text_map (self ):
481
+ """Test `inject()` method for Format.TEXT_MAP."""
482
+
483
+ otel_context = trace .SpanContext (trace_id = 1220 , span_id = 7478 )
484
+ context = opentracingshim .SpanContextShim (otel_context )
485
+
486
+ # Verify Format.TEXT_MAP
487
+ text_map = {}
488
+ self .shim .inject (context , opentracing .Format .TEXT_MAP , text_map )
489
+ self .assertEqual (text_map [MockHTTPTextFormat .TRACE_ID_KEY ], str (1220 ))
490
+ self .assertEqual (text_map [MockHTTPTextFormat .SPAN_ID_KEY ], str (7478 ))
491
+
492
+ def test_inject_binary (self ):
493
+ """Test `inject()` method for Format.BINARY."""
494
+
495
+ otel_context = trace .SpanContext (trace_id = 1220 , span_id = 7478 )
496
+ context = opentracingshim .SpanContextShim (otel_context )
497
+
498
+ # Verify exception for non supported binary format.
499
+ with self .assertRaises (opentracing .UnsupportedFormatException ):
500
+ self .shim .inject (context , opentracing .Format .BINARY , bytearray ())
501
+
502
+ def test_extract_http_headers (self ):
503
+ """Test `extract()` method for Format.HTTP_HEADERS."""
504
+
505
+ carrier = {
506
+ MockHTTPTextFormat .TRACE_ID_KEY : 1220 ,
507
+ MockHTTPTextFormat .SPAN_ID_KEY : 7478 ,
508
+ }
509
+
510
+ ctx = self .shim .extract (opentracing .Format .HTTP_HEADERS , carrier )
511
+ self .assertEqual (ctx .unwrap ().trace_id , 1220 )
512
+ self .assertEqual (ctx .unwrap ().span_id , 7478 )
513
+
514
+ def test_extract_text_map (self ):
515
+ """Test `extract()` method for Format.TEXT_MAP."""
516
+
517
+ carrier = {
518
+ MockHTTPTextFormat .TRACE_ID_KEY : 1220 ,
519
+ MockHTTPTextFormat .SPAN_ID_KEY : 7478 ,
520
+ }
521
+
522
+ ctx = self .shim .extract (opentracing .Format .TEXT_MAP , carrier )
523
+ self .assertEqual (ctx .unwrap ().trace_id , 1220 )
524
+ self .assertEqual (ctx .unwrap ().span_id , 7478 )
525
+
526
+ def test_extract_binary (self ):
527
+ """Test `extract()` method for Format.BINARY."""
528
+
529
+ # Verify exception for non supported binary format.
530
+ with self .assertRaises (opentracing .UnsupportedFormatException ):
531
+ self .shim .extract (opentracing .Format .BINARY , bytearray ())
532
+
533
+
534
+ class MockHTTPTextFormat (HTTPTextFormat ):
535
+ """Mock propagator for testing purposes."""
536
+
537
+ TRACE_ID_KEY = "mock-traceid"
538
+ SPAN_ID_KEY = "mock-spanid"
539
+
540
+ @classmethod
541
+ def extract (cls , get_from_carrier , carrier ):
542
+ trace_id_list = get_from_carrier (carrier , cls .TRACE_ID_KEY )
543
+ span_id_list = get_from_carrier (carrier , cls .SPAN_ID_KEY )
544
+
545
+ if not trace_id_list or not span_id_list :
546
+ return trace .INVALID_SPAN_CONTEXT
547
+
548
+ return trace .SpanContext (
549
+ trace_id = int (trace_id_list [0 ]), span_id = int (span_id_list [0 ])
550
+ )
551
+
552
+ @classmethod
553
+ def inject (cls , context , set_in_carrier , carrier ):
554
+ set_in_carrier (carrier , cls .TRACE_ID_KEY , str (context .trace_id ))
555
+ set_in_carrier (carrier , cls .SPAN_ID_KEY , str (context .span_id ))
0 commit comments