@@ -99,7 +99,85 @@ def test_wrap_httplib_request(self):
9999 self .assertEqual (expected_attributes ,
100100 mock_tracer .span .attributes )
101101 self .assertEqual (expected_name , mock_tracer .span .name )
102- self .assertEqual (span_module .SpanKind .CLIENT , mock_tracer .span .span_kind )
102+ self .assertEqual (span_module .SpanKind .CLIENT , mock_tracer .span .span_kind )
103+
104+ def test_wrap_httplib_request_blacklist_ok (self ):
105+ mock_span = mock .Mock ()
106+ span_id = '1234'
107+ mock_span .span_id = span_id
108+ mock_tracer = MockTracer (mock_span )
109+ mock_request_func = mock .Mock ()
110+ mock_request_func .__name__ = 'request'
111+
112+ patch_tracer = mock .patch (
113+ 'opencensus.trace.ext.requests.trace.execution_context.'
114+ 'get_opencensus_tracer' ,
115+ return_value = mock_tracer )
116+ patch_attr = mock .patch (
117+ 'opencensus.trace.ext.requests.trace.execution_context.'
118+ 'get_opencensus_attr' ,
119+ return_value = None )
120+
121+ wrapped = trace .wrap_httplib_request (mock_request_func )
122+
123+ mock_self = mock .Mock ()
124+ method = 'GET'
125+ url = 'http://localhost:8080'
126+ body = None
127+ headers = {}
128+
129+ with patch_tracer , patch_attr :
130+ wrapped (mock_self , method , url , body , headers )
131+
132+ expected_attributes = {
133+ 'http.url' : url ,
134+ 'http.method' : method }
135+ expected_name = '[httplib]request'
136+
137+ mock_request_func .assert_called_with (
138+ mock_self , method , url , body , {
139+ 'traceparent' : '00-123-456-01' ,
140+ }
141+ )
142+
143+ def test_wrap_httplib_request_blacklist_nok (self ):
144+ mock_span = mock .Mock ()
145+ span_id = '1234'
146+ mock_span .span_id = span_id
147+ mock_tracer = MockTracer (mock_span )
148+ mock_request_func = mock .Mock ()
149+ mock_request_func .__name__ = 'request'
150+
151+ patch_tracer = mock .patch (
152+ 'opencensus.trace.ext.requests.trace.execution_context.'
153+ 'get_opencensus_tracer' ,
154+ return_value = mock_tracer )
155+ patch_attr = mock .patch (
156+ 'opencensus.trace.ext.requests.trace.execution_context.'
157+ 'get_opencensus_attr' ,
158+ return_value = ['localhost:8080' ])
159+
160+ wrapped = trace .wrap_httplib_request (mock_request_func )
161+
162+ mock_self = mock .Mock ()
163+ mock_self ._dns_host = 'localhost'
164+ mock_self .port = '8080'
165+ method = 'GET'
166+ url = 'http://{}:{}' .format (mock_self ._dns_host , mock_self .port )
167+ body = None
168+ headers = {}
169+
170+ with patch_tracer , patch_attr :
171+ wrapped (mock_self , method , url , body , headers )
172+
173+ expected_attributes = {
174+ 'http.url' : url ,
175+ 'http.method' : method }
176+ expected_name = '[httplib]request'
177+
178+ mock_request_func .assert_called_with (
179+ mock_self , method , url , body , {}
180+ )
103181
104182 def test_wrap_httplib_response (self ):
105183 mock_span = mock .Mock ()
0 commit comments