• /
  • EnglishEspañol日本語한국어Português
  • Inicia sesiónComenzar ahora

message_transaction (Python agent API)

Syntax

newrelic.agent.message_transaction(library, destination_type, destination_name, application, routing_key=None, exchange_type=None, headers=None, queue_name=None, reply_to=None, correlation_id=None)

Report message functions as transactions.

Requirements

Agent version 2.88.0.72 or higher.

Description

This decorator returns a partial of MessageTransactionWrapper that can be used as a decorator for a messaging function. When used, the returned decorator records a message transaction and its message-related attributes.

If the decorator will not work in your application, you can use one of the following:

  • The context manager: The context manager form is MessageTransaction. It takes the same parameters as the decorator.
  • The wrapper: The wrapper form is MessageTransactionWrapper. 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_transaction. This applies the MessageTransactionWrapper 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 when these different calls should be used, see Different call formats. See Examples for call examples.

Parameters

Parameters for message_transaction and MessageTransaction

newrelic.agent.message_transaction(library, destination_type, destination_name, application, routing_key=None, exchange_type=None, headers=None, queue_name=None, reply_to=None, correlation_id=None)
newrelic.agent.MessageTransaction(library, destination_type, destination_name, application, routing_key=None, exchange_type=None, headers=None, queue_name=None, reply_to=None, correlation_id=None)

The decorator and context manager use these parameters:

Parameter

Description

library

string or function

Required. The name (or type) of message broker in use. 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.

application

Application

Required. An application instance, as returned by application.

routing_key

string or function

Optional. The routing key of the message.

exchange_type

string or function

Optional. The exchange type of the message.

headers

dictionary or function

Optional. The headers of the message.

queue_name

string or function

Optional. The queue name property of the message.

reply_to

string or function

Optional. The replyTo property of the message.

correlation_id

string or function

Optional. The correlationID property of the message.

Parameters for MessageTransactionWrapper

newrelic.agent.MessageTransactionWrapper(wrapped, library, destination_type, destination_name, application, routing_key=None, exchange_type=None, headers=None, queue_name=None, reply_to=None, correlation_id=None)

The MessageTransactionWrapper takes all of the same parameters as the decorator in addition to this wrapped parameter:

Parameter

Description

wrapped

function

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

Parameters for wrap_message_transaction

newrelic.agent.wrap_message_transaction(module, object_path, library, destination_type, destination_name, application, routing_key=None, exchange_type=None, headers=None, queue_name=None, reply_to=None, correlation_id=None)

This 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 values

The decorator message_transaction returns a MessageTransactionWrapper partial.

Examples

message_transaction example

An example of the decorator:

mt = message_transaction('library', 'Exchange', 'x', routing_key='foo.bar')
@mt
def foo():
pass

MessageTransaction example

An example using the context manager:

def callback(method, properties, body):
with MessageTransaction('library', 'Exchange', 'x', application=app):
pass

MessageTransactionWrapper example

An example using the wrapper:

basic_consume_wrapper = newrelic.agent.MessageTransactionWrapper(basic_consume_register_callback, 'library', 'Queue', 'x')
method_frame, header_frame, body = basic_consume_wrapper('queue')

wrap_message_transaction example

An example using the path-based wrapper:

wrap_message_transaction('module', 'Foo.bar', 'library', 'Exchange', 'x')
Copyright © 2024 New Relic Inc.

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