in elasticapm/instrumentation/packages/httpx/sync/httpcore.py [0:0]
def call(self, module, method, wrapped, instance, args, kwargs):
url, method, headers = utils.get_request_data(args, kwargs)
scheme, host, port, target = url
if port != default_ports.get(scheme):
host += ":" + str(port)
signature = "%s %s" % (method.upper(), host)
url = "%s://%s%s" % (scheme, host, target)
transaction = execution_context.get_transaction()
with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}},
leaf=True,
) as span:
# if httpcore has been called in a leaf span, this span might be a DroppedSpan.
leaf_span = span
while isinstance(leaf_span, DroppedSpan):
leaf_span = leaf_span.parent
if headers is not None:
# It's possible that there are only dropped spans, e.g. if we started dropping spans.
# In this case, the transaction.id is used
parent_id = leaf_span.id if leaf_span else transaction.id
trace_parent = transaction.trace_parent.copy_from(
span_id=parent_id, trace_options=TracingOptions(recorded=True)
)
utils.set_disttracing_headers(headers, trace_parent, transaction)
if leaf_span:
leaf_span.dist_tracing_propagated = True
response = wrapped(*args, **kwargs)
status_code = utils.get_status(response)
if status_code:
if span.context:
span.context["http"]["status_code"] = status_code
span.set_success() if status_code < 400 else span.set_failure()
return response