Skip to content

Commit

Permalink
Fix event hook response processing regression.
Browse files Browse the repository at this point in the history
  • Loading branch information
umaannamalai authored and a-feld committed Jan 21, 2021
1 parent 2856c37 commit 20f1a72
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions newrelic/hooks/external_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
from collections import abc

from newrelic.api.external_trace import ExternalTrace
from newrelic.api.time_trace import current_trace
from newrelic.api.transaction import current_transaction
from newrelic.common.object_wrapper import wrap_function_wrapper


def newrelic_event_hook(response):
tracer = current_trace()
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)
tracer = getattr(response.request, "_nr_trace", None)
if tracer is not None:
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)


async def newrelic_event_hook_async(response):
tracer = current_trace()
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)
tracer = getattr(response.request, "_nr_trace", None)
if tracer is not None:
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)


def newrelic_first_gen(l, is_async=False):
Expand Down Expand Up @@ -76,35 +76,29 @@ def sync_send_wrapper(wrapped, instance, args, kwargs):

with ExternalTrace("httpx", str(request.url), request.method) as tracer:
if hasattr(tracer, "generate_request_headers"):
request._nr_trace = tracer
outgoing_headers = tracer.generate_request_headers(tracer.transaction)
for header_name, header_value in outgoing_headers:
# User headers should override our CAT headers
if header_name not in request.headers:
request.headers[header_name] = header_value

response = wrapped(*args, **kwargs)
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)

return response
return wrapped(*args, **kwargs)


async def async_send_wrapper(wrapped, instance, args, kwargs):
request = bind_request(*args, **kwargs)

with ExternalTrace("httpx", str(request.url), request.method) as tracer:
if hasattr(tracer, "generate_request_headers"):
request._nr_trace = tracer
outgoing_headers = tracer.generate_request_headers(tracer.transaction)
for header_name, header_value in outgoing_headers:
# User headers should override our CAT headers
if header_name not in request.headers:
request.headers[header_name] = header_value

response = await wrapped(*args, **kwargs)
headers = dict(getattr(response, "headers", ())).items()
tracer.process_response(getattr(response, "status_code", None), headers)

return response
return await wrapped(*args, **kwargs)


@property
Expand Down

0 comments on commit 20f1a72

Please sign in to comment.