---
title: database_trace (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/databasetrace-python-agent-api
---

## Syntax

```py
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](https://docs.newrelic.com/docs/accounts-partnerships/education/getting-started-new-relic/glossary#transaction-trace) in the form of additional segments. Any calls reported with `database_trace` will appear on the [APM Databases page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/databases-slow-queries-page). `database_trace` returns a [partial](https://docs.python.org/3.12/library/functools.html#functools.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](https://docs.newrelic.com/docs/python-agent-api-different-call-forms). See [Examples](#examples) for call examples.

## Parameters

### Parameters for decorator [#main-parameters]

```py
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 [#context-mgr-parameters]

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

Parameters for the context manager includes all of the [parameters](#main-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

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

Parameters for the wrapper include all [parameters](#main-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 [#path-wrap-parameters]

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

Parameters include all [parameters](#main-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 [#trace_example]

An example of using the `database_trace` decorator:

```py
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:

```py
import newrelic.agent

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

### Context manager example [#context-mgr-example]

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

```py
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 [#trace-wrap-example]

An example of using the `DatabaseTraceWrapper`:

```py
import newrelic.agent

def get(id):
    ...


get = newrelic.agent.DatabaseTraceWrapper(
    get, sql="SELECT * FROM users WHERE id = 1"
)
```
