Public APIedit

The Elastic APM Python agent has several public APIs. Most of the public API functionality is not needed when using one of our supported frameworks, but they allow customized usage.

Client APIedit

The public Client API consists of several methods on the Client class. This API can be used to track exceptions and log messages, as well as to mark the beginning and end of transactions.

Instantiationedit

To create a Client instance, import it and call its constructor:

from elasticapm import Client

client = Client({'APP_NAME': 'example'}, **defaults)
  • config: A dictionary, with key/value configuration. For the possible configuration keys, see Configuration.
  • **defaults: default values for configuration. These can be omitted in most cases, and take the least precedence.

framework integrations like Django and Flask instantiate the client automatically.

Errorsedit

Client.capture_exception()edit

Captures an exception object:

try:
    x = int("five")
except ValueError:
    client.capture_exception()
  • exc_info: A (type, value, traceback) tuple as returned by sys.exc_info(). If not provided, it will be captured automatically.
  • date: A datetime.datetime object representing the occurrence time of the error. If left empty, it defaults to datetime.datetime.utcnow().
  • context: A dictionary with contextual information. This dictionary must follow the Context schema definition.
  • custom: A dictionary of custom data you want to attach to the event.

Returns the id of the error as a string.

Client.capture_message()edit

Captures a message with optional added contextual data. Example:

client.capture_message('Billing process succeeded.')
  • message: The message as a string.
  • param_message: Alternatively, a parametrized message as a dictionary. The dictionary contains two values: message, and params. This allows the APM server to group messages together that share the same parametrized message. Example:

    client.capture_message(param_message={
        'message': 'Billing process for %s succeeded. Amount: %s',
        'params': (customer.id, order.total_amount),
    })
  • stack: If set to True (the default), a stacktrace from the call site will be captured.
  • exc_info: A (type, value, traceback) tuple as returned by sys.exc_info(). If not provided, it will be captured automatically, if capture_message() was called in an except block.
  • date: A datetime.datetime object representing the occurrence time of the error. If left empty, it defaults to datetime.datetime.utcnow().
  • context: A dictionary with contextual information. This dictionary must follow the Context schema definition.
  • custom: A dictionary of custom data you want to attach to the event.

Returns the id of the message as a string.

Either the message or the param_message argument is required.

Transactionsedit

Client.begin_transaction()edit

Begin tracking a transaction. Should be called e.g. at the beginning of a request or when starting a background task. Example:

client.begin_transaction('processors')
  • transaction_type: (required) A string describing the type of the transaction, e.g. 'request' or 'celery'.
Client.end_transaction()edit

End tracking the transaction. Should be called e.g. at the end of a request or when ending a background task. Example:

client.end_transaction('myapp.billing_process', processor.status)
  • name: (required) A string describing the name of the transaction, e.g. process_order. This is typically the name of the view/controller that handles the request, or the route name.
  • result: (required) A string describing the result of the transaction. This is typically the HTTP status code, or e.g. 'success' for a background task.

Other APIsedit

elasticapm.instrument()edit

Instruments libraries automatically. This includes a wide range of standard library and 3rd party modules. A list of instrumented modules can be found in elasticapm.intrumentation.register. This function should be called as early as possibly in the startup of your application. For supported frameworks, this is called automatically. Example:

import elasticapm

elasticapm.instrument()

elasticapm.set_transaction_name()edit

Override the name of the current transaction. For supported frameworks, the transaction name is determined automatically, and can be overridden using this function. Example:

import elasticapm

elasticapm.set_transaction_name('myapp.billing_process')
  • name: (required) A string describing name of the transaction

elasticapm.set_transaction_data()edit

Attach custom contextual data to current transaction. Supported frameworks will automatically attach information about the HTTP request and the logged in user. You can attach further data using this function. Example:

import elasticapm

elasticapm.set_transaction_data({'billing_amount': product.price * item_count})
  • data: (required) A dictionary with the data to be attached. This should be a flat key/value dict object.
  • _key: The key to use for this dict object. Defaults to custom.

    This should only be overridden by framework integrations.