Skip to content

Commit b0a4283

Browse files
committed
Hook lower in urllib3 for greater coverage
* Fixes situation where requests package calls won't be traced * Add tests that include calls via requests package
1 parent 857d08b commit b0a4283

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

instana/instrumentation/urllib3.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
from __future__ import absolute_import
2-
import opentracing.ext.tags as ext
32
import instana
3+
from instana.log import logger
44
import opentracing
5+
import opentracing.ext.tags as ext
56
import wrapt
67

78

89
try:
9-
import urllib3
10+
import urllib3 # noqa
11+
12+
def collect(instance, args, kwargs):
13+
try:
14+
kvs = {}
15+
16+
kvs['host'] = instance.host
17+
kvs['port'] = instance.port
1018

11-
@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
19+
if len(args) is 2:
20+
kvs['method'] = args[0]
21+
kvs['path'] = args[1]
22+
else:
23+
kvs['method'] = kwargs['method']
24+
kvs['path'] = kwargs['url']
25+
26+
if type(instance) is urllib3.connectionpool.HTTPSConnectionPool:
27+
kvs['url'] = 'https://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
28+
else:
29+
kvs['url'] = 'http://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
30+
except Exception as e:
31+
logger.debug(e)
32+
return kvs
33+
else:
34+
return kvs
35+
36+
@wrapt.patch_function_wrapper('urllib3', 'HTTPConnectionPool.urlopen')
1237
def urlopen_with_instana(wrapped, instance, args, kwargs):
1338
context = instana.internal_tracer.current_context()
1439

@@ -18,8 +43,10 @@ def urlopen_with_instana(wrapped, instance, args, kwargs):
1843

1944
try:
2045
span = instana.internal_tracer.start_span("urllib3", child_of=context)
21-
span.set_tag(ext.HTTP_URL, args[1])
22-
span.set_tag(ext.HTTP_METHOD, args[0])
46+
47+
kvs = collect(instance, args, kwargs)
48+
span.set_tag(ext.HTTP_URL, kvs['url'])
49+
span.set_tag(ext.HTTP_METHOD, kvs['method'])
2350

2451
instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
2552
rv = wrapped(*args, **kwargs)

test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ basictracer>=2.2.0
55
autowrapt>=1.0
66
flask>=0.12.2
77
urllib3>=1.9
8+
requests>=2.11.1

tests/test_urllib3.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from nose.tools import assert_equals
33
from instana import internal_tracer as tracer
4+
import requests
45
import urllib3
56

67

@@ -149,3 +150,49 @@ def test_client_error(self):
149150

150151
assert_equals(second_span.t, first_span.t)
151152
assert_equals(second_span.p, first_span.s)
153+
154+
def test_requestspkg_get(self):
155+
span = tracer.start_span("test")
156+
r = requests.get('http://127.0.0.1:5000/', timeout=2)
157+
span.finish()
158+
159+
spans = self.recorder.queued_spans()
160+
assert_equals(2, len(spans))
161+
first_span = spans[1]
162+
second_span = spans[0]
163+
164+
assert(r)
165+
assert_equals(200, r.status_code)
166+
assert_equals("test", first_span.data.sdk.name)
167+
assert_equals("urllib3", second_span.n)
168+
assert_equals(200, second_span.data.http.status)
169+
assert_equals("http://127.0.0.1:5000/", second_span.data.http.url)
170+
assert_equals("GET", second_span.data.http.method)
171+
172+
assert_equals(None, second_span.error)
173+
assert_equals(None, second_span.ec)
174+
175+
assert_equals(second_span.t, first_span.t)
176+
assert_equals(second_span.p, first_span.s)
177+
178+
def test_requestspkg_put(self):
179+
span = tracer.start_span("test")
180+
r = requests.put('http://127.0.0.1:5000/notfound')
181+
span.finish()
182+
183+
spans = self.recorder.queued_spans()
184+
assert_equals(2, len(spans))
185+
first_span = spans[1]
186+
second_span = spans[0]
187+
188+
assert_equals(404, r.status_code)
189+
assert_equals("test", first_span.data.sdk.name)
190+
assert_equals("urllib3", second_span.n)
191+
assert_equals(404, second_span.data.http.status)
192+
assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url)
193+
assert_equals("PUT", second_span.data.http.method)
194+
assert_equals(None, second_span.error)
195+
assert_equals(None, second_span.ec)
196+
197+
assert_equals(second_span.t, first_span.t)
198+
assert_equals(second_span.p, first_span.s)

0 commit comments

Comments
 (0)