in elasticapm/instrumentation/packages/urllib.py [0:0]
def call(self, module, method, wrapped, instance, args, kwargs):
request_object = args[1] if len(args) > 1 else kwargs["req"]
method = request_object.get_method()
host = request_host(request_object)
url = sanitize_url(request_object.get_full_url())
signature = method.upper() + " " + host
transaction = execution_context.get_transaction()
with capture_span(
signature,
span_type="external",
span_subtype="http",
extra={"http": {"url": url}},
leaf=True,
) as span:
# if urllib 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
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)
)
self._set_disttracing_headers(request_object, trace_parent, transaction)
if leaf_span:
leaf_span.dist_tracing_propagated = True
response = wrapped(*args, **kwargs)
if response:
if span.context:
span.context["http"]["status_code"] = response.status
span.set_success() if response.status < 400 else span.set_failure()
return response