Skip to content
Open
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
31 changes: 23 additions & 8 deletions src/instana/instrumentation/httpx.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Rafal-Chrzanowski-IBM, thanks for the PR!
According to our spec, we aren't supposed to raise exceptions that are caused by our instrumentation code. But I understand that we shouldn't suppress the exceptions coming from the underlying client library as well. Could you please re-raise the exception only for the original wrapped call as in the snippet I've shared below?

Please update the code (for both the methods) and verify from your end if it fixes the issue. We can then review the PR again, thanks!

            try:
                request = args[0]
                _set_request_span_attributes(span, request)  # Has its own try-except
                tracer.inject(span.context, Format.HTTP_HEADERS, request.headers)
            except Exception:
                logger.debug("httpx handle_request_with_instana:", exc_info=True)
            
            try:
                response = wrapped(*args, **kwargs)
            except Exception as e:
                span.record_exception(e)
                raise
            
            _set_response_span_attributes(span, response)  # Has its own try-except

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @GSVarsha, thank you for taking a look so quickly. I updated the code according to your suggestions. Please let me know whether I should make any further changes to the PR. Thank you.

Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,22 @@ def handle_request_with_instana(
) as span:
try:
request = args[0]
_set_request_span_attributes(span, request)
_set_request_span_attributes(span, request) # Has its own try-except
tracer.inject(span.context, Format.HTTP_HEADERS, request.headers)
except Exception:
logger.exception(
"httpx handle_request_with_instana pre-request:", exc_info=True
)

try:
response = wrapped(*args, **kwargs)
_set_response_span_attributes(span, response)
except Exception as e:
span.record_exception(e)
else:
return response
raise

_set_response_span_attributes(span, response) # Has its own try-except

return response

@wrapt.patch_function_wrapper("httpx", "AsyncHTTPTransport.handle_async_request")
async def handle_async_request_with_instana(
Expand All @@ -111,15 +118,23 @@ async def handle_async_request_with_instana(
) as span:
try:
request = args[0]
_set_request_span_attributes(span, request)
_set_request_span_attributes(span, request) # Has its own try-except
tracer.inject(span.context, Format.HTTP_HEADERS, request.headers)
except Exception:
logger.exception(
"httpx handle_async_request_with_instana pre-request:",
exc_info=True,
)

try:
response = await wrapped(*args, **kwargs)
_set_response_span_attributes(span, response)
except Exception as e:
span.record_exception(e)
else:
return response
raise

_set_response_span_attributes(span, response) # Has its own try-except

return response

logger.debug("Instrumenting httpx")

Expand Down
Loading