Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions instana/instrumentation/urllib3.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
from __future__ import absolute_import
import opentracing.ext.tags as ext
import instana
from instana.log import logger
import opentracing
import opentracing.ext.tags as ext
import wrapt


try:
import urllib3
import urllib3 # noqa

def collect(instance, args, kwargs):
try:
kvs = {}

kvs['host'] = instance.host
kvs['port'] = instance.port

@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
if len(args) is 2:
kvs['method'] = args[0]
kvs['path'] = args[1]
else:
kvs['method'] = kwargs['method']
kvs['path'] = kwargs['url']

if type(instance) is urllib3.connectionpool.HTTPSConnectionPool:
kvs['url'] = 'https://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
else:
kvs['url'] = 'http://%s:%d%s' % (kvs['host'], kvs['port'], kvs['path'])
except Exception as e:
logger.debug(e)
return kvs
else:
return kvs

@wrapt.patch_function_wrapper('urllib3', 'HTTPConnectionPool.urlopen')
def urlopen_with_instana(wrapped, instance, args, kwargs):
context = instana.internal_tracer.current_context()

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

try:
span = instana.internal_tracer.start_span("urllib3", child_of=context)
span.set_tag(ext.HTTP_URL, args[1])
span.set_tag(ext.HTTP_METHOD, args[0])

kvs = collect(instance, args, kwargs)
span.set_tag(ext.HTTP_URL, kvs['url'])
span.set_tag(ext.HTTP_METHOD, kvs['method'])

instana.internal_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
rv = wrapped(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ basictracer>=2.2.0
autowrapt>=1.0
flask>=0.12.2
urllib3>=1.9
requests>=2.11.1
47 changes: 47 additions & 0 deletions tests/test_urllib3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import
from nose.tools import assert_equals
from instana import internal_tracer as tracer
import requests
import urllib3


Expand Down Expand Up @@ -149,3 +150,49 @@ def test_client_error(self):

assert_equals(second_span.t, first_span.t)
assert_equals(second_span.p, first_span.s)

def test_requestspkg_get(self):
span = tracer.start_span("test")
r = requests.get('http://127.0.0.1:5000/', timeout=2)
span.finish()

spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
first_span = spans[1]
second_span = spans[0]

assert(r)
assert_equals(200, r.status_code)
assert_equals("test", first_span.data.sdk.name)
assert_equals("urllib3", second_span.n)
assert_equals(200, second_span.data.http.status)
assert_equals("http://127.0.0.1:5000/", second_span.data.http.url)
assert_equals("GET", second_span.data.http.method)

assert_equals(None, second_span.error)
assert_equals(None, second_span.ec)

assert_equals(second_span.t, first_span.t)
assert_equals(second_span.p, first_span.s)

def test_requestspkg_put(self):
span = tracer.start_span("test")
r = requests.put('http://127.0.0.1:5000/notfound')
span.finish()

spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
first_span = spans[1]
second_span = spans[0]

assert_equals(404, r.status_code)
assert_equals("test", first_span.data.sdk.name)
assert_equals("urllib3", second_span.n)
assert_equals(404, second_span.data.http.status)
assert_equals("http://127.0.0.1:5000/notfound", second_span.data.http.url)
assert_equals("PUT", second_span.data.http.method)
assert_equals(None, second_span.error)
assert_equals(None, second_span.ec)

assert_equals(second_span.t, first_span.t)
assert_equals(second_span.p, first_span.s)