add_custom_span_attribute (Python agent API)

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

newrelic.agent.add_custom_span_attribute(key, value)
Adds a custom attribute to a span event.

Description

This call records a custom attribute (a key/value pair attached to your span event).

Attributes may be found in New Relic Distributed Tracing or in New Relic APM if a transaction trace is created by the parent transaction. Attributes can also be found and queried in New Relic Insights.

Before you create custom attributes, review New Relic's list of reserved terms used by NRQL and Insights.

Parameters

Parameter Description

key

string

Required. The key name. Only the first 255 characters are retained.

value

string, integer, float, boolean

Required. The string value to add to the current span event. Only the first 255 characters are retained.

Return value(s)

Returns True if attribute was added successfully. 

Example(s)

Adding custom attributes to background task

An example of adding custom attributes to a background task:

@newrelic.agent.background_task()
def send_request():
	with newrelic.agent.FunctionTrace(name='Trace'):
    	newrelic.agent.add_custom_span_attribute('attribute', 'value')

Using custom span attribute to troubleshoot

You can also use custom attributes to troubleshoot performance issues. For example, you might see occasional slow response times from a pool of memcache instances, but you don't know what instance is causing the problem. You might add an attribute to the span indicating the server, like so:

# Set server_ip to be the current server processing the transaction

newrelic.agent.add_custom_span_attribute("memcache_query_frontend_lookup", server_ip)

For more help

Recommendations for learning more: