Skip to content

Commit c0df08b

Browse files
committed
Add instrumentation dir with urllib3 instrumentation
1 parent e8aa850 commit c0df08b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

instana/instrumentation/__init__.py

Whitespace-only changes.

instana/instrumentation/urllib3.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import opentracing.ext.tags as ext
2+
import opentracing
3+
import wrapt
4+
5+
6+
@wrapt.patch_function_wrapper('urllib3', 'PoolManager.urlopen')
7+
def urlopen_with_instana(wrapped, instance, args, kwargs):
8+
try:
9+
span = opentracing.global_tracer.start_span("urllib3")
10+
span.set_tag(ext.HTTP_URL, args[1])
11+
span.set_tag(ext.HTTP_METHOD, args[0])
12+
13+
opentracing.global_tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, kwargs["headers"])
14+
15+
rv = wrapped(*args, **kwargs)
16+
span.set_tag(ext.HTTP_STATUS_CODE, rv.status)
17+
if 500 <= rv.status <= 511:
18+
span.set_tag("error", True)
19+
ec = span.tags.get('ec', 0)
20+
span.set_tag("ec", ec+1)
21+
22+
except Exception as e:
23+
print("found error:", e)
24+
span.set_tag("error", True)
25+
ec = span.tags.get('ec', 0)
26+
span.set_tag("ec", ec+1)
27+
raise
28+
else:
29+
span.finish()
30+
return rv

0 commit comments

Comments
 (0)