11from requests_mock import Adapter
22from nose .tools import eq_ , assert_in , assert_not_in
33
4+ from ddtrace import config
5+
46from .test_requests import BaseRequestTestCase
57
68
@@ -24,10 +26,26 @@ def headers_not_here(self, tracer, request):
2426 assert_not_in ('x-datadog-parent-id' , headers )
2527 return True
2628
29+ def test_propagation_default (self ):
30+ # ensure by default, distributed tracing is disabled
31+ adapter = Adapter ()
32+ self .session .mount ('mock' , adapter )
33+
34+ with self .tracer .trace ('root' ):
35+ def matcher (request ):
36+ return self .headers_not_here (self .tracer , request )
37+ adapter .register_uri ('GET' , 'mock://datadog/foo' , additional_matcher = matcher , text = 'bar' )
38+ resp = self .session .get ('mock://datadog/foo' )
39+ eq_ (200 , resp .status_code )
40+ eq_ ('bar' , resp .text )
41+
2742 def test_propagation_true (self ):
43+ # [Backward compatibility]: ensure users can switch the distributed
44+ # tracing flag using the `Session` attribute
45+ cfg = config .get_from (self .session )
46+ cfg ['distributed_tracing' ] = True
2847 adapter = Adapter ()
2948 self .session .mount ('mock' , adapter )
30- self .session .distributed_tracing = True
3149
3250 with self .tracer .trace ('root' ) as root :
3351 def matcher (request ):
@@ -45,6 +63,46 @@ def matcher(request):
4563 eq_ (root .span_id , req .parent_id )
4664
4765 def test_propagation_false (self ):
66+ # [Backward compatibility]: ensure users can switch the distributed
67+ # tracing flag using the `Session` attribute
68+ cfg = config .get_from (self .session )
69+ cfg ['distributed_tracing' ] = False
70+ adapter = Adapter ()
71+ self .session .mount ('mock' , adapter )
72+
73+ with self .tracer .trace ('root' ):
74+ def matcher (request ):
75+ return self .headers_not_here (self .tracer , request )
76+ adapter .register_uri ('GET' , 'mock://datadog/foo' , additional_matcher = matcher , text = 'bar' )
77+ resp = self .session .get ('mock://datadog/foo' )
78+ eq_ (200 , resp .status_code )
79+ eq_ ('bar' , resp .text )
80+
81+ def test_propagation_true_legacy (self ):
82+ # [Backward compatibility]: ensure users can switch the distributed
83+ # tracing flag using the `Session` attribute
84+ adapter = Adapter ()
85+ self .session .mount ('mock' , adapter )
86+ self .session .distributed_tracing = True
87+
88+ with self .tracer .trace ('root' ) as root :
89+ def matcher (request ):
90+ return self .headers_here (self .tracer , request , root )
91+ adapter .register_uri ('GET' , 'mock://datadog/foo' , additional_matcher = matcher , text = 'bar' )
92+ resp = self .session .get ('mock://datadog/foo' )
93+ eq_ (200 , resp .status_code )
94+ eq_ ('bar' , resp .text )
95+
96+ spans = self .tracer .writer .spans
97+ root , req = spans
98+ eq_ ('root' , root .name )
99+ eq_ ('requests.request' , req .name )
100+ eq_ (root .trace_id , req .trace_id )
101+ eq_ (root .span_id , req .parent_id )
102+
103+ def test_propagation_false_legacy (self ):
104+ # [Backward compatibility]: ensure users can switch the distributed
105+ # tracing flag using the `Session` attribute
48106 adapter = Adapter ()
49107 self .session .mount ('mock' , adapter )
50108 self .session .distributed_tracing = False
0 commit comments