Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Changed

- `db.client.connection.pool.name` previously appeared only on the `db.client.connection.count` gauge; it now also propagates to spans, the `db.client.operation.duration` / `db.client.response.returned_rows` histograms, and the rest of the `db.client.connection.*` family via the shared connection-attribute set. The `count` gauge's attribute set is unchanged ([#32](https://github.com/chmodas/sqlx-otel/pull/32)).
- `db.query.text` now collapses inter-token whitespace runs to a single space and trims leading/trailing whitespace for both `QueryTextMode::Full` (default) and `QueryTextMode::Obfuscated`. Whitespace inside string literals, quoted identifiers, dollar-quoted bodies, and comments is preserved verbatim. Multi-line SQL written for source-level readability now renders as a single readable line in OTel exports without the embedded `\n` and indentation runs that came from source-level formatting. `QueryTextMode::Off` is unchanged ([#34](https://github.com/chmodas/sqlx-otel/pull/34)).

### Fixed

Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,28 @@ See [`QueryAnnotations`](https://docs.rs/sqlx-otel/latest/sqlx_otel/struct.Query

| Mode | Behaviour |
|------------------|----------------------------------------------------------------------------------------------------|
| `Full` (default) | Capture the parameterised query as-is. Safe because SQLx uses bind parameters. |
| `Full` (default) | Capture the parameterised query. Safe because SQLx uses bind parameters. |
| `Obfuscated` | Replace literal values (string, numeric, hex, boolean, dollar-quoted) with `?` in `db.query.text`. |
| `Off` | Do not capture `db.query.text`. |

`Obfuscated` is useful when SQL is constructed via string interpolation rather than bind parameters – the structure of the query is preserved while sensitive literal values are redacted. Comments, identifiers (quoted or otherwise), operators, and `NULL` are kept verbatim.

Both `Full` and `Obfuscated` collapse inter-token whitespace runs to a single space and trim leading/trailing whitespace before emitting `db.query.text`, so multi-line SQL written for source-level readability renders cleanly in OTel exports. Whitespace **inside** string literals, quoted identifiers, dollar-quoted bodies, and comments is preserved verbatim. For example, an UPSERT split across multiple lines for readability:

```sql
INSERT INTO items (id, name, qty)
VALUES (?1, ?2, ?3)
ON CONFLICT (id) DO UPDATE SET
name = excluded.name,
qty = excluded.qty
```

emits as a single readable line in `db.query.text`:

```
INSERT INTO items (id, name, qty) VALUES (?1, ?2, ?3) ON CONFLICT (id) DO UPDATE SET name = excluded.name, qty = excluded.qty
```

## Reference

### Span attributes
Expand All @@ -152,7 +168,7 @@ Set on every `Executor` method (`execute`, `fetch`, `fetch_all`, `fetch_one`, `f
| `network.protocol.name` | Wire protocol; defaults per backend, overridable on builder | When applicable |
| `network.transport` | OSI L4 transport (`"tcp"`, `"unix"`, `"pipe"`, `"inproc"`) | When set via builder |
| `db.client.connection.pool.name` | Pool identifier set via `with_pool_name` | When set via builder |
| `db.query.text` | The SQL query string | Unless `QueryTextMode::Off` |
| `db.query.text` | The SQL query string with inter-token whitespace collapsed | Unless `QueryTextMode::Off` |
| `db.operation.name` | Database operation (e.g. `SELECT`) | When annotated |
| `db.collection.name` | Target table or collection | When annotated |
| `db.query.summary` | Low-cardinality query summary | When annotated |
Expand Down
25 changes: 19 additions & 6 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ use opentelemetry_semantic_conventions::attribute;
///
/// Configured via [`PoolBuilder::with_query_text_mode`](crate::PoolBuilder::with_query_text_mode).
///
/// # Whitespace normalisation
///
/// For both [`Full`](Self::Full) and [`Obfuscated`](Self::Obfuscated) the emitted text has
/// inter-token whitespace runs collapsed to a single ASCII space and leading/trailing
/// whitespace trimmed. Whitespace **inside** string literals, quoted identifiers,
/// dollar-quoted bodies, and comments is preserved verbatim. Multi-line SQL written across
/// several Rust source lines therefore renders as a single readable line in `OTel` exports
/// without the embedded `\n` and indentation runs that come from source-level formatting.
/// [`Off`](Self::Off) is unaffected (no attribute is emitted).
///
/// # When to choose what
///
/// - **[`Full`](Self::Full)** (default) – appropriate when all SQL flows through `SQLx`
Expand All @@ -13,8 +23,8 @@ use opentelemetry_semantic_conventions::attribute;
/// - **[`Obfuscated`](Self::Obfuscated)** – appropriate when SQL is built via string
/// interpolation (`format!`, query concatenation, dynamic identifiers) and may contain
/// literal values. Structure is preserved; literals (string, numeric, hex, boolean, and
/// `PostgreSQL` dollar-quoted) are replaced with `?`. Comments, whitespace, identifiers
/// (quoted or otherwise), operators, and `NULL` are kept verbatim.
/// `PostgreSQL` dollar-quoted) are replaced with `?`. Comments, identifiers (quoted or
/// otherwise), operators, and `NULL` are kept verbatim.
/// - **[`Off`](Self::Off)** – appropriate when the query text is itself sensitive
/// (proprietary schemas, query shapes that reveal business logic) or when query-text
/// cardinality must be eliminated entirely.
Expand All @@ -25,13 +35,16 @@ use opentelemetry_semantic_conventions::attribute;
/// them manually via the active span using the OpenTelemetry API.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum QueryTextMode {
/// Capture the parameterised query text as-is. This is the default because `SQLx`
/// queries use bind parameters (`$1`, `?`), so literal values are not present in the
/// query string.
/// Capture the parameterised query text. This is the default because `SQLx` queries
/// use bind parameters (`$1`, `?`), so literal values are not present in the query
/// string. Inter-token whitespace is collapsed to a single space and leading/trailing
/// whitespace is trimmed; whitespace inside literals, identifiers, and comments is
/// preserved verbatim.
#[default]
Full,
/// Replace literal values in the query text with `?`. Useful when queries are built
/// via string interpolation rather than bind parameters.
/// via string interpolation rather than bind parameters. The same whitespace
/// normalisation as [`Full`](Self::Full) is applied after redaction.
Obfuscated,
/// Do not capture `db.query.text` at all.
Off,
Expand Down
Loading
Loading