• /
  • EnglishEspañol日本語한국어Português
  • ログイン今すぐ開始

create_distributed_trace_payload (Python agent API)

Syntax

newrelic.agent.create_distributed_trace_payload()

This method is used for generating payloads used to connect transactions within a distributed trace.

Requirements

Python agent version 4.2.0.100 or higher.

注意

This API has been deprecated. Please use newrelic.agent.insert_distributed_trace_headers instead.

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_payload, see Enable distributed tracing with agent APIs.

This call is used to implement distributed tracing. It generates a payload that is read by the receiving application with the accept_distributed_trace_payload method.

Return values

When successful, returns a DistributedTracePayload object, which inherits from Python's built in dict type and has the following additional methods:

  • text: Generate a JSON encoded string version of the payload.
  • http_safe: Generate a base64 encoded string version of the payload.

When unsuccessful, returns None. This can happen when distributed tracing is not enabled or when called from outside an active transaction.

Examples

重要

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 create_distributed_trace_payload in creating two external traces from within single a background task:

@newrelic.agent.background_task()
def main(url):
header_key = newrelic.agent.ExternalTrace.cat_distributed_trace_key
with newrelic.agent.ExternalTrace('my_external_library', url, method='GET'):
# Generate the payload in the context of the ExternalTrace
# span that sends it
payload = newrelic.agent.create_distributed_trace_payload()
headers = {header_key: payload.http_safe()}
response = my_external_library._get(url, headers=headers)
data = _process_response(response)
with newrelic.agent.ExternalTrace('my_external_library', url, method='POST'):
# Generate the payload in the context of the ExternalTrace
# span that sends it
payload = newrelic.agent.create_distributed_trace_payload()
headers = {header_key: payload.http_safe()}
response = my_external_library._post(url, data=data, headers=headers)

Create a distributed trace payload inside an external trace

An example of using create_distributed_trace_payload 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):
payload = newrelic.agent.create_distributed_trace_payload()
header_key = newrelic.agent.ExternalTrace.cat_distributed_trace_key
headers = {header_key: payload.http_safe()}
return my_external_library._get(url, headers=headers)
Copyright © 2024 New Relic株式会社。

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.