• /
  • EnglishEspañolFrançais日本語한국어Português
  • Log inStart now

database_trace (Python agent API)

|View as Markdown

Syntax

newrelic.agent.database_trace(sql, dbapi2_module=None, async_wrapper=None)

Used to instrument calls to databases.

Description

database_trace is used to add more detail to your transaction traces in the form of additional segments. Any calls reported with database_trace will appear on the APM Databases page. database_trace returns a partial of DatabaseTraceWrapper that can be used as a decorator for a function to time calls to your database.

The database_trace decorator can be used on generators and coroutines with agent version 2.102.0.85 or higher.

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 DatabaseTrace.
  • The wrapper: The wrapper form is DatabaseTraceWrapper. 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_database_trace. This applies the DatabaseTraceWrapper 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

Parameters for decorator

newrelic.agent.database_trace(sql, dbapi2_module=None, async_wrapper=None)

This call includes these parameters:

Parameter

Description

sql

string or function

Required. The database query or a function that accepts the same parameters as the function being wrapped and returns the database query.

Parameters for context manager

newrelic.agent.DatabaseTrace(sql, dbapi2_module=None, async_wrapper=None)

Parameters for the context manager includes all of the parameters from database_trace plus the additional host, port_path_or_id, and database_name parameters.

Parameter

Description

sql

string

Required. The database query.

Wrapper parameters

newrelic.agent.DatabaseTraceWrapper(wrapped, sql, dbapi2_module=None, async_wrapper=None)

Parameters for the wrapper include all parameters for database_trace and a wrapped parameter:

Parameter

Description

wrapped

function

Required. The function being wrapped.

sql

string or function

Required. The database query or a function that accepts the same parameters as the function being wrapped and returns the database query.

Path-based wrapping parameters

newrelic.agent.wrap_database_trace(module, object_path, sql, dbapi2_module=None, async_wrapper=None)

Parameters include all parameters for database_trace and these parameters:

Parameter

Description

module

object

Required. The module containing the function to be instrumented.

object_path

string

Required. The path to the location of the function.

sql

string or function

Required. The database query or a function that accepts the same parameters as the function being wrapped and returns the database query.

Return values

database_trace returns a DatabaseTraceWrapper() partial.

Examples

database_trace example

An example of using the database_trace decorator:

import newrelic.agent
@newrelic.agent.database_trace(sql="SELECT * FROM users WHERE id = 1")
def get_user(id):
...

An example of using the database_trace decorator with native coroutines:

import newrelic.agent
@newrelic.agent.database_trace(sql="SELECT * FROM users WHERE id = 1")
async def get_user(id):
...

Context manager example

An example of using the DatabaseTrace context manager: This will give timings for how long it takes to do `custom_action`.

import newrelic.agent
def complex_query(a, b, c):
with Connection(host, port, db) as conn:
with newrelic.agent.DatabaseTrace(
sql="SELECT * FROM users WHERE id = 1",
):
conn.custom_action()

Wrapper example

An example of using the DatabaseTraceWrapper:

import newrelic.agent
def get(id):
...
get = newrelic.agent.DatabaseTraceWrapper(
get, sql="SELECT * FROM users WHERE id = 1"
)
Copyright © 2026 New Relic Inc.

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