@@ -104,3 +104,40 @@ Note that you don't need to do anything to send the data -- the `Client` object
104104will handle that before the script exits. Additionally, the `Client` object should
105105be treated as a singleton -- you should only create one instance and store/pass
106106around that instance for all transaction handling.
107+
108+ [float]
109+ [[instrumenting-custom-code-distributed-tracing]]
110+ ==== Distributed Tracing
111+
112+ When instrumenting custom code across multiple services, you should propagate
113+ the TraceParent where possible. This allows Elastic APM to bundle the various
114+ transactions into a single distributed trace. The Python Agent will
115+ automatically add TraceParent information to the headers of outgoing HTTP
116+ requests, which can then be used on the receiving end to add that TraceParent
117+ information to new manually-created transactions.
118+
119+ Additionally, the Python Agent provides utilities for propagating the
120+ TraceParent in string format.
121+
122+ [source,python]
123+ ----
124+ import elasticapm
125+
126+ client = elasticapm.Client(service_name="foo", server_url="https://example.com:8200")
127+
128+ # Retrieve the current TraceParent as a string, requires active transaction
129+ traceparent_string = elasticapm.get_trace_parent_header()
130+
131+ # Create a TraceParent object from a string and use it for a new transaction
132+ parent = elasticapm.trace_parent_from_string(traceparent_string)
133+ client.begin_transaction(transaction_type="script", trace_parent=parent)
134+ # Do some work
135+ client.end_transaction(name=__name__, result="success")
136+
137+ # Create a TraceParent object from a dictionary of headers, provided
138+ # automatically by the sending service if it is using an Elastic APM Agent.
139+ parent = elasticapm.trace_parent_from_headers(headers_dict)
140+ client.begin_transaction(transaction_type="script", trace_parent=parent)
141+ # Do some work
142+ client.end_transaction(name=__name__, result="success")
143+ ----
0 commit comments