Skip to content

Commit d5dd7a8

Browse files
GSVarshapvital
authored andcommitted
feat: Add logic for handling async httpx requests
Signed-off-by: Varsha GS <varsha.gs@ibm.com>
1 parent 6c1822f commit d5dd7a8

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/instana/instrumentation/httpx.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ def handle_request_with_instana(
9393
span.record_exception(e)
9494
else:
9595
return response
96+
97+
@wrapt.patch_function_wrapper("httpx", "AsyncHTTPTransport.handle_async_request")
98+
async def handle_async_request_with_instana(
99+
wrapped: Callable[..., "httpx.AsyncHTTPTransport.handle_async_request"],
100+
instance: httpx.AsyncHTTPTransport,
101+
args: Tuple[int, str, Tuple[Any, ...]],
102+
kwargs: Dict[str, Any],
103+
) -> httpx.Response:
104+
# If we're not tracing, just return
105+
if tracing_is_off():
106+
return await wrapped(*args, **kwargs)
107+
108+
tracer, parent_span, _ = get_tracer_tuple()
109+
parent_context = parent_span.get_span_context() if parent_span else None
110+
111+
with tracer.start_as_current_span(
112+
"httpx", span_context=parent_context, kind=SpanKind.CLIENT
113+
) as span:
114+
try:
115+
request = args[0]
116+
_set_request_span_attributes(span, request)
117+
tracer.inject(span.context, Format.HTTP_HEADERS, request.headers)
118+
119+
response = await wrapped(*args, **kwargs)
120+
_set_response_span_attributes(span, response)
121+
except Exception as e:
122+
span.record_exception(e)
123+
else:
124+
return response
96125

97126
logger.debug("Instrumenting httpx")
98127
except ImportError:

0 commit comments

Comments
 (0)