---
title: "Queries Module"
url: https://develop.sentry.dev/sdk/telemetry/traces/modules/queries/
---

# Queries Module | Sentry Docs

This document uses key words such as "MUST", "SHOULD", and "MAY" as defined in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt) to indicate requirement levels.

The SDK should auto-instrument all database queries, and each query to the database should result in a span. This includes queries made manually, or via ORM. The Queries module supports SQL databases like PostgreSQL and MySQL, as well as MongoDB. It does not support other databases like Neo4j or ClickHouse. Regardless of that, the SDK should instrument database queries as fully and correctly as possible to ensure correct behavior and future compatibility.

## [Span Attributes](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#span-attributes)

| Attribute     | Description                                                                                                                                                   | Notes                                                                                                                                         |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `op`          | Prefixed with `"db"` (e.g., `"db"`, `"db.query"`, `"db.sql.query"`) [1](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#user-content-fn-1) | Required                                                                                                                                      |
| `description` | The parameterized query (e.g., `SELECT * FROM users where id = ?`)                                                                                            | Required, see [Query Parameterization](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#query-parameterization) for details |
| `data`        | A key-value mapping of span attributes. (e.g., `{"db.system": "postgresql", "db.name": "prod-2"}`)                                                            | Required for full experience. See [Span Data](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#span-data) for details       |

### [Query Parameterization](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#query-parameterization)

Parameterization replaces each literal value in a query with a placeholder, so Sentry can group the same query with different values. See [Data Scrubbing](https://develop.sentry.dev/sdk/foundations/data-scrubbing.md#parameterization).

If the database driver supplies the parameterized query, the SDK **MUST** use it unchanged. Sentry supports the placeholders `?`, `%s`, `:c0` (e.g., `:c0`, `:c1`), and `$1` (e.g., `$1`, `$2`).

If the driver does not supply a parameterized query, the SDK **SHOULD** replace all literals with `?`. For example, the query

```sql
SELECT * FROM users WHERE id = 17;
```

becomes:

```sql
SELECT * FROM users WHERE id = ?;
```

If the SDK does not replace the literals, the span description **MUST NOT** contain the query. The SDK uses the first available value of this list instead:

1. `db.query.summary`
2. `{db.operation.name} {target}`
3. `{target}`, where the target is `db.collection.name`, `db.stored_procedure.name`, `db.namespace`, or `server.address`
4. `db.system.name`

If the SDK does not replace the literals, the SDK **MAY** set `db.query.text` to the query as it is. Server-side scrubbing applies to it (see [Server-Side Scrubbing](https://develop.sentry.dev/sdk/foundations/data-scrubbing.md#server-side-scrubbing)).

The SDK **SHOULD** set `db.query.summary` when the driver supplies it. The SDK **MAY** generate the summary from the parameterized query. The summary contains the operation and the target names, for example `SELECT users`.

For MongoDB, the description should contain valid JSON. If replacing values with a placeholder, the string `"?"` should be used.

```json
{ "find": "documents", "filter": { "wordCount": { "$gte": "?" } } }
```

### [Span Data](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#span-data)

For full support, each database span should have a `data` attribute. `data` is itself a key-value lookup of attributes. Refer to [Database Span Data Conventions](https://develop.sentry.dev/sdk/performance/span-data-conventions.md#database) for a full list of attributes that database spans should have. We recommend that the SDK provide *all* of those attributes for every span. However, the *minimal* requirement is that the SDK provides the `db.system` attribute.

#### [MongoDB Data](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#mongodb-data)

MongoDB support requires a few extra data attributes. In addition to a `db.system` value of `"mongodb"`, the `db.operation` and `db.collection.name` attributes are also required to contain the command and collection name respectively.

## [SQL Instrumentation Example](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#sql-instrumentation-example)

Consider a hypothetical Python ORM:

```python
from magicORM import ORM
from .models import User, Product

ORM.connect_postgres(host="127.0.0.1", port="7777", database="production")
return User.find(17).find_related(Product)
```

Assume it makes this SQL query:

```sql
SELECT * FROM projects
WHERE projects_user = 17;
```

This should result in the following span:

```json
{
  "description": "SELECT * FROM projects WHERE projects_user = ?",
  "op": "db",
  "data": {
    "db.system": "postgresql",
    "db.operation": "SELECT",
    "db.name": "production",
    "server.address": "127.0.0.1",
    "server.port": "7777"
  }
}
```

## [Footnotes](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#footnote-label)

1. Some operations are accepted by Sentry, but not indexed by the Queries module. This includes `db.sql.room`, `db.redis`, and some others. Do not attempt to trick the system by providing an incorrect span operation! [↩](https://develop.sentry.dev/sdk/telemetry/traces/modules/queries.md#user-content-fnref-1)
