> ## Documentation Index
> Fetch the complete documentation index at: https://latchkey.getgalaxy.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Latchkey observability and audit

> The audit log, server and SDK metrics, the decision log and ExplainAccess.

Latchkey records every management write in the audit log, every deny in the
decision log, and exports metrics from both the server and the SDK.

## Audit log

Every management write lands in the audit log with its actor, the state
before and after, and the new revision. Membership changes are stored as
diffs. The table is partitioned by month and indexed by tenant, time and
actor. A default partition catches any row whose month has
no partition yet, so an audit write never fails. At start and once a day the
server keeps the next two months in place with `ensure_audit_partitions()`.
It also gives any other month with rows in the default partition its own,
such as the old months `latchkey export` copies in, and moves those rows
into their month.

`latchkey audit prune` removes old entries. On Postgres it drops whole
months with owner credentials. On SQLite it deletes old rows and needs write
access to `latchkey.db`. See
[the CLI](https://latchkey.getgalaxy.io/pages/guides/usage/cli.md#operator-commands). Retention is a Helm
value, 400 days by default.

`AuditService.ListAuditEntries` reads the log, newest first. The admin UI
and `latchkey audit` read it this way. A tenant's Admin
reads that tenant's entries, checked in the same transaction as the read, so
a revoked Admin reads nothing more. An operator reads the instance's entries,
the writes to domains and kinds, by naming no tenant. Neither reads the
other's. Entries can be narrowed to those since a time, by one actor, or for
one entity, the part of an operation before the first dot, such as `binding`
for `binding.create`.

Each page also counts every matching entry in the retained log, and the
entity filter is not indexed, so a long retention makes pages slower. A page
holds at most 1000 entries. An entry's time is when its transaction began on
Postgres, so a write that commits slowly can appear behind entries already
read. Following the log with `since`, starting each read a little before the
newest entry seen and skipping the ids already read, is best effort: a write
whose transaction ran longer than that overlap can be missed. To collect every
entry, also read the whole retained log again from time to time.

## Health probes

The server answers the Kubernetes probes on its API port.

| Path | Answers 200 when |
| - | - |
| `/livez` | The process is running |
| `/startupz` | The snapshot is loaded and the listener is open |
| `/readyz` | Started, and not shutting down |

Readiness does not check the database. Replicas answer from their snapshot,
so a database outage would otherwise pull every replica out of service at
once. The [revision rule](https://latchkey.getgalaxy.io/pages/guides/concepts/architecture.md) handles an
outage instead.

On `SIGTERM` the server fails readiness, keeps serving for 5 seconds while
Kubernetes stops routing to it, then waits up to 10 seconds for requests in
flight. A second signal exits at once.

## Metrics and traces

The server records these through the OpenTelemetry API.

| Metric | What it measures | Attributes |
| - | - | - |
| `latchkey.server.check.duration` | Seconds a `Check` or `CheckBatch` call took in its handler. A call refused at authentication or by its access rule is counted in `check.errors` and not timed. | None |
| `latchkey.server.check.decisions` | Decisions made | `kind`, `action`, `allowed` |
| `latchkey.server.check.errors` | `Check` and `CheckBatch` calls that failed, or that denied because a requested revision never arrived | `caller`, `reason` |
| `latchkey.server.snapshot.reload.duration` | Seconds a snapshot load from the database took | `scope`, one of `full`, `tenant` or `kinds` |
| `latchkey.server.snapshot.changes` | Changes made through other replicas that this one picked up | `source`, one of `notify`, `poll` or `resync` |
| `latchkey.server.snapshot.age` | Seconds since the least recently confirmed tenant was confirmed against the database | None |
| `latchkey.server.snapshot.tenants` | Tenants the snapshot holds | None |
| `latchkey.server.jit.subjects` | Just-in-time users saved for the first time | None |

No metric has a tenant attribute, so the number of series does not grow
with the number of tenants. `caller` is the id of a configured owner or
operator, `other` for anyone else, or `unauthenticated`. `reason` is the
Connect code, or `revision_timeout`. Errors from one caller point at a
misconfigured product.

`snapshot.age` is what to alert on. Checks in the stalest tenant answer
`UNAVAILABLE` once it passes `LATCHKEY_MAX_STALENESS`.

On Postgres nearly every change should arrive with source `notify`. A poll
that overlaps a notification can count the same change, so an occasional
`poll` means nothing. A count of `poll` that keeps pace with `notify`, or
grows with no `notify` at all, is worth a look. Notifications may not be
reaching the replica, as behind a connection pooler in transaction mode.

The server binary exports them over OTLP when
`OTEL_EXPORTER_OTLP_ENDPOINT` or `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` is
set, and reads the other standard `OTEL_*` variables. See
[Configuration](https://latchkey.getgalaxy.io/pages/guides/deployment/configuration.md#metrics). In
embedded mode they go to the global meter provider the product sets.

The SDK exports, through the OpenTelemetry API, check latency
(`latchkey.sdk.check.duration`), timeouts (`latchkey.sdk.check.timeouts`),
requests denied because Latchkey could not be asked
(`latchkey.sdk.check.unavailable`), decision cache lookups by hit
(`latchkey.sdk.cache.lookups`) and registration failures
(`latchkey.sdk.registration.failures`). It also emits spans for the
interceptor, the check and the parent resolver. With no provider set they
cost nothing.

## Decision log

The SDK logs every deny, and in shadow mode every decision, through `slog`
with the mode, the request, the tenant, the subject, each check's kind,
action and resource id, and the code. A product can bridge `slog` to
OpenTelemetry logs. These are not Postgres rows, so decision volume never
competes with the policy tables. A configurable sample of allows comes after
V1.

## Explaining access

`ExplainAccess` answers why a subject can or cannot do something. It lists
the bindings that reach the subject and flags a subject id that matched
nothing. `latchkey explain` is its command line form.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.