insert_distributed_trace_headers

For general information on how the Python agent API calls relate to each other, see the API guide.

newrelic.agent.insert_distributed_trace_headers(list_of_headers)
This method is used for inserting headers used to connect transactions within a distributed trace.

Requirements

Python agent version 5.6.0.135 or higher.

Distributed tracing must be enabled.

Description

This API requires distributed tracing to be enabled.

For instructions on how to use this call, along with its partner call accept_distributed_trace_headers, see Enable distributed tracing with agent APIs.

This call is used to implement distributed tracing. It inserts headers into a list that can be read by the receiving application with the accept_distributed_trace_headers method.

Parameters

Parameter Description

headers

list

Required. A list of headers. This list will be mutated by the call (headers will be inserted into the list in the form of (header_name, header_value)).

Return value(s)

Returns None. The input list will be updated by a call to this function.

Example(s)

In order to maintain proper ordering of spans in a trace, you must generate the payload in the context of the span that sends it.

Create a distributed trace payload inside a background task

An example of using insert_distributed_trace_headers in creating an external trace from within single a background task:

@newrelic.agent.background_task()
def main(url):
    with newrelic.agent.ExternalTrace('my_external_library',
            url, method='GET'):
        # Generate the payload in the context of the ExternalTrace
        # span that sends it
        headers = []
        newrelic.agent.insert_distributed_trace_headers(headers)
        response = my_external_library._get(url, headers=headers)

    data = _process_response(response)

Create a distributed trace payload inside an external trace

An example of using insert_distributed_trace_headers in creating an external trace:

def _bind_url(url, *args, **kwargs):
    # _bind_url is called with the args and kwargs sent to the `get`
    # method below
    return url

@newrelic.agent.external_trace('my_external_library', _bind_url, method='GET')
def get(url):
    headers = []
    newrelic.agent.insert_distributed_trace_headers(headers)
    return my_external_library._get(url, headers=headers)

For more help

Recommendations for learning more: