Source: https://gres.monicz.dev/guide/observability/

# Observability

Gres reports spans and metrics through [OpenTelemetry](https://opentelemetry.io/docs/languages/python/) following its [database client semantic conventions](https://opentelemetry.io/docs/specs/semconv/db/), and logs server notices via Python's `logging` module.

## Enable tracing and metrics

Install the `opentelemetry` extra and pass `tracer_provider`, `meter_provider`, or both to `Connection` or `Pool`:

```python
# Partial: configure exporters and readers as your application already does.
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.trace import TracerProvider
from gres import Pool

pool = Pool(tracer_provider=TracerProvider(), meter_provider=MeterProvider())
```

Gres never installs global providers or manages exporter lifecycles; resources without providers emit no telemetry.

Operation spans and `db.client.operation.duration` record `db.system.name` (`postgresql`), `db.namespace` (database name), `gres.operation.name` (method name, e.g. `fetch_one`), and `server.address`/`server.port` for single-host configurations (omitted for multi-host). Failures add `error.type` (e.g. `gres.errors.UniqueViolation`) and, for server errors, `db.response.status_code` (SQLSTATE). SQL text, bound values, and server error messages are never recorded.

## Metrics

| Metric | Instrument | Measures |
| --- | --- | --- |
| `db.client.operation.duration` | Histogram, seconds | Public query and `COPY` operations (for streams, includes time held open by the consumer). |
| `gres.stream.first_result.duration` | Histogram, seconds | Stream entry until the first converted row or Arrow batch is delivered. |
| `gres.pool.phase.duration` | Histogram, seconds | Physical pool phase (`wait`, `create`, `reset`, or `close` via `gres.pool.phase`). `reset` counts resets awaited by a borrower; single-call queued resets are not recorded. |
| `gres.cleanup.wait_timeouts` | Counter | Expired cleanup waits, by `gres.cleanup.action` (`connection`, `arrow_producer`, `pool`, `pool_startup`, `pool_connection`). |
| `gres.wire.bytes` | Counter, bytes | PostgreSQL protocol bytes on session connections, by `network.io.direction`. |
| `gres.wire.rows` | Counter | `DataRow` messages accepted on query and stream paths. |
| `gres.statement_cache.events` | Counter | Statement cache hits, misses, evictions, bypasses, and stale names, by `gres.cache.event`. |
| `gres.type_cache.lookups` | Counter | Requested PostgreSQL type and record-layout identifiers. |
| `gres.transaction.completions` | Counter | Top-level transaction finish requests, by `gres.transaction.outcome`. |
| `gres.cancel.requests` | Counter | Cancellation requests, by `gres.cancel.outcome`. |

- `db.client.operation.duration` advertises semantic-convention buckets from 1 ms to 10 s (overridable via meter views). Compare against `gres.stream.first_result.duration` to separate server latency from consumer pacing; filter `gres.pool.phase.duration` by `gres.pool.phase="wait"` to measure pool contention.
- `gres.cleanup.wait_timeouts` counts timed-out waits (e.g. a blocked Arrow producer can trigger both connection and pool waits); check `pool.stats.pending_cleanup` for active count.
- `gres.wire.bytes` covers startup, catalog queries, `COPY`, and pool resets (excluding TLS overhead and cancel sockets). `gres.wire.rows` counts `DataRow` headers before conversion (excluding `COPY` and DML row counts).
- `gres.statement_cache.events` tracks [automatic preparation](https://gres.monicz.dev/guide/queries/#prepared-statements) and tracked transaction-pooler statements (`stale` = rejected by server). `gres.type_cache.lookups` counts identifier lookups, not round trips.
- `gres.transaction.completions` includes SQLAlchemy commits/rollbacks and excludes savepoints and raw SQL.

## Log server notices

Gres logs PostgreSQL notices (`RAISE NOTICE`, login warnings, `IF NOT EXISTS` notices, etc.) on the `gres.notice` logger mapped by severity:

| Server severity | Python logging level |
| --- | --- |
| `WARNING` | `WARNING` |
| `NOTICE`, `INFO` | `INFO` |
| `LOG`, `DEBUG` | `DEBUG` |

`WARNING` records reach stderr by default; enable `INFO` on `gres.notice` to capture ordinary notices. Each log record includes `severity`, `sqlstate`, `detail`, and `hint` attributes (notice text can contain server data; filter if sensitive). At most 32 notices are logged per operation, followed by a summary record with `notice_count`.
