To link transactions across multiple services into a single distributed trace, you must propagate the TraceParent. The Python Agent automatically adds TraceParent to outgoing HTTP request headers.
To manually continue a trace in a new service, use the following utilities:
elasticapm.get_trace_parent_header(): Retrieves the current TraceParent as a string (requires an active transaction).elasticapm.trace_parent_from_string(traceparent_string): Creates a TraceParent object from a string.elasticapm.trace_parent_from_headers(headers_dict): Creates a TraceParent object from a dictionary of headers (e.g., headers received from an incoming HTTP request).
Pass the resulting TraceParent object to client.begin_transaction(..., trace_parent=parent).
import elasticapm
client = elasticapm.Client(service_name="foo", server_url="https://example.com:8200")
# Retrieve the current TraceParent as a string, requires active transaction
traceparent_string = elasticapm.get_trace_parent_header()
# Create a TraceParent object from a string and use it for a new transaction
parent = elasticapm.trace_parent_from_string(traceparent_string)
client.begin_transaction(transaction_type="script", trace_parent=parent)
# Do some work
client.end_transaction(name=__name__, result="success")
# Create a TraceParent object from a dictionary of headers, provided
# automatically by the sending service if it is using an Elastic APM Agent.
parent = elasticapm.trace_parent_from_headers(headers_dict)
client.begin_transaction(transaction_type="script", trace_parent=parent)
# Do some work
client.end_transaction(name=__name__, result="success")