message_trace

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

newrelic.agent.message_trace(library, operation, destination_type, destination_name, params={})
Report messaging functions as transaction trace segments.

Requirements

Agent version 2.88.0.72 or higher.

Description

message_transaction (and its associated calls) report message functions as transactions. message_trace is used to add more detail to your transaction traces in the form of additional segments. message_trace returns a partial of MessageTraceWrapper that can be used as a decorator for a message function.

The message_trace decorator can be used on generators and coroutines with agent version 2.102.0.85 or higher. Timing of these objects begins when consumption starts, and ends when the object is exhausted or goes out of scope. This is a change from earlier versions where the metric represented the time taken to create the generator or coroutine object itself.

If you cannot use the decorator in your application, you can use one of these other call formats:

  • The context manager: The context manager form is MessageTrace. It takes the same parameters as the decorator.
  • The wrapper: The wrapper form is MessageTraceWrapper. It can be used to return a wrapped function without the use of a decorator.
  • The path-based wrapper: The path-based wrapper form is wrap_message_trace. This applies the MessageTraceWrapper to a given object through monkey patching. This takes the same parameters as the decorator plus an additional module and object_path parameter.

For an explanation of the uses of these different call formats, see Different call formats. See Examples for call examples.

Parameters

newrelic.agent.message_trace(library, operation, destination_type, destination_name, params={})

The message_trace decorator uses these parameters:

Parameter Description

library

string or function

Required. The name (or type) of the type of message broker in use. Pass either a string which defines it or a function which returns it.

operation

string or function

Required. Either Produce or Consume as indicated by the operation occurring in the traced function. Pass either a string which defines it or a function which returns it.

destination_type

string or function

Required. The type of destination targeted by the operation. Pass either a string which defines it or a function which returns it. This is typically Exchange or Queue.

destination_name

string or function

Required. The name of the destination being targeted by the operation. Pass either a string which defines it or a function which returns it.

params

dictionary

Optional. Additional details pertaining to the operation. These are typically routing_key, correlation_id, reply_to, queue_name, or headers.
newrelic.agent.MessageTrace(library, transaction, operation, destination_type, destination_name, params={})

The MessageTrace context manager takes all of the parameters taken by message_trace and this parameter:

Parameter Description

transaction

object or string

Required. Object corresponding to current transaction. See current_transaction.

newrelic.agent.MessageTraceWrapper(wrapped, library, operation, destination_type, destination_name, params={})
     

The MessageTraceWrapper takes all of the same parameters as the decorator in addition to an initial wrapped parameter:

Parameter Description

wrapped

function

Required. The messaging function to attribute to the message broker time.

newrelic.agent.wrap_message_trace(module, object_path, library, operation, destination_type, destination_name, params={})

The wrap_message_trace takes all of the parameters that the decorator does in addition to a module parameter and an object_path parameter:

Parameter Description

module

object

Required. The module containing the object to be wrapped.

object_path

string

Required. The path to the object to be wrapped.

Return value(s)

message_trace returns a MessageTraceWrapper() partial.

Example(s)

An example of using message_trace:

@message_trace('library', 'Consume', 'Exchange', 'x')
def foo():
    pass

An example of using message_trace decorator with a native coroutine:

@message_trace('library', 'Consume', 'Exchange', 'x')
async def foo():
    pass

An example of using the MessageTrace context manager:

def basic_get(queue, no_ack=False):
    with MessageTrace(transaction, 'library', 'Consume', 'Exchange', 'x'):
        pass

An example of using the MessageTraceWrapper:

wrapped_basic_get = newrelic.agent.MessageTraceWrapper(basic_get, 'library', 'Consume', 'Queue', 'x')

method_frame, header_frame, body = wrapped_basic_get('queue')

An example of using the wrap_message_trace path-based wrapper:

wrap_message_trace('module', 'Foo.bar', 'library', 'Produce', 'Exchange', 'x')

For more help

Recommendations for learning more: