# Monitoring

> Diagnostics endpoints, metrics and what to alert on.

---

LLMS index: [llms.txt](/llms.txt)

---

This page covers health probes, Prometheus metrics, Watch client supervision, graceful shutdown, and primary switchover boundaries for the single-primary tenant gateway. When `pgetcd` backs Kubernetes, PostgreSQL and the gateway must be able to start without depending on that Kubernetes API server.

## Diagnostics endpoints {#health}

The diagnostics listener (`diagnostics_listen`, port `2381` in examples) requires the same mutual TLS client certificate (`spiffe://pgetcd/tenant/<tenant>`) as the gRPC listener. All endpoints accept `GET` only and disable HTTP response caching.

| Endpoint | Response |
| --- | --- |
| `/livez` | `ok` (HTTP 200) if the gateway process is alive and serving |
| `/readyz`, `/health` | `{"health":true}` (HTTP 200) when ready, or `{"health":false}` (HTTP 503) when not ready |
| `/version` | Product version (`0.1.0`), advertised etcd version (`3.6.0`), SQL ABI (`1`), and resource profiles |
| `/metrics` | Prometheus text exposition format with fixed metric names and no client-controlled labels |
{.wide}

Query readiness with `curl`:

```bash
curl --cacert /path/to/server-ca.crt \
  --cert /path/to/tenant-client.crt \
  --key /path/to/tenant-client.key \
  https://127.0.0.1:2381/readyz
```

`/livez` checks only that the gateway HTTP handler responds. `/readyz` and `/health` also verify request admission, primary database connectivity, persistent identity, and Lease background worker readiness inside a fresh certified transaction that must complete its synchronous `COMMIT` within 1 second.

> [!WARNING]
> Since `v0.1.0-rc2` (commit `81691bc`), `etcdctl endpoint health` reports healthy, and the readiness check uses a reserved slot, so client load that fills `pool_size` does not withdraw readiness. The gateway logs `tenant readiness withdrawn: <reason>` and `tenant readiness granted` on each change; alert on the withdrawn line. In `v0.1.0-rc1`, `endpoint health` failed and the readiness check shared the client slots.

## Prometheus metrics {#metrics}

All exported metrics on `/metrics` are gauges with no tenant keys, values, or credentials:

| Metric | Meaning |
| --- | --- |
| `pgetcd_ready` | Runtime readiness flag (`1` when ready, `0` when withdrawn) |
| `pgetcd_publication_inflight` | Active requests currently holding a publication slot |
| `pgetcd_publication_capacity` | Configured publication slot capacity (`pool_size`) |
| `pgetcd_pool_connections`, `pgetcd_pool_idle_connections` | Total and idle connections in the PostgreSQL pool |
| `pgetcd_watch_streams` | Currently admitted gRPC Watch streams |
| `pgetcd_watch_senders`, `pgetcd_watch_receivers` | Active Watch sender and receiver goroutines |
| `pgetcd_watch_tail_inflight` | In-flight `_tail_v2` polling transactions |
| `pgetcd_watch_serialized_reserved_bytes` | Reserved serialized Watch buffer bytes against `watch_max_tenant_bytes` |
{.wide}

What to watch and alert on:

- **`pgetcd_ready == 0`**: Alert immediately. Inspect `diag.status` in PostgreSQL (`not_ready_reasons`) or check whether `pgetcd_publication_inflight` reached `pgetcd_publication_capacity`.
- **`pgetcd_publication_inflight` approaching `pgetcd_publication_capacity`**: Indicates slow SQL, lock contention on `etcd.meta`, or `COMMIT` waiting on synchronous standby replication. A slot remains occupied until its `COMMIT` finishes.
- **`pgetcd_watch_serialized_reserved_bytes` approaching `watch_max_tenant_bytes`**: Indicates slow Watch consumers or high write volume; streams that exceed their buffer or tenant quota receive `ResourceExhausted`.

## Watch client supervision {#watch}

- Watch consumers should persist a resume revision only after processing all events in a complete revision.
- A Watch `Create` response confirms stream registration, not event progress.
- If a Watch stream breaks with `Unavailable`, reconnect starting at `last_processed_revision + 1`.
- If the requested revision has been logically compacted (`CompactRevision > 0`), read a fresh `Range` snapshot and open a new Watch at `header.revision + 1` (see [Use etcdctl](/docs/etcd/tutorial/etcdctl/#watch)).

## Graceful shutdown {#shutdown}

On `SIGINT` or `SIGTERM`, the gateway withdraws readiness, stops admitting new requests, closes Watch streams, and waits up to a shared 10-second deadline for in-flight publication transactions and gRPC transports to drain. When the 10-second deadline expires, it stops the gRPC and HTTP servers forcibly.

In-flight `COMMIT` calls hold their publication slots until they return. A completed gateway shutdown timeout does not prove that every PostgreSQL backend transaction rolled back; the gateway suppresses responses for any unresolved `COMMIT` and never replays it.

## Primary switchover, backup, and restore {#recovery}

All KV rows, event history, revision manifests, history anchors, Leases, and publication barriers live in PostgreSQL tables under schema `etcd`.

- **Manual primary switchover**: To preserve all acknowledged writes across a primary failure, PostgreSQL must run with `synchronous_commit = on` and a synchronous standby. After promoting the synchronous standby, point `PGETCD_DSN` at the new primary and restart `pgetcd`. In the `v0.1.0-rc2` switchover smoke test (commit `81691bc`, 2026-11-08), restarting the gateways after promotion took `1.03 s` and kept all `52/52` acknowledged writes. This checks the procedure only; it does not provide fencing.
- **Database restore**: Startup and periodic metadata checks verify `database_name`, `database_oid`, `cluster_id`, `member_id`, and `restore_generation`. Keep a restored database isolated until you verify these identities. Automated failover fencing and offline PITR protocol recovery (`pgetcd-restore`) are post-MVP roadmap items (see [HA roadmap](/docs/etcd/design/ha-roadmap/) and [Offline protocol recovery](/docs/etcd/design/recovery/)).

---

Backlinks:

- [Operations](/docs/etcd/operations/)
