@gatojs/observability workspace package (packages/observability). It gives you a logger built on the OpenTelemetry (OTel) logs API, an OTLP/HTTP exporter for logs and traces, and a withSpan helper. The core also sets security headers on every response and exposes a database healthcheck.
The logger
src/shared/lib/logger.ts
logger has debug, info, warn and error methods that take a message and optional attributes:
Where entries go
Both conditions are read once, when the logger module is imported, so changing them requires a restart.
Attributes and error types
On export, attributes go throughnormalizeAttributes:
- an
Errorbecomes three attributes:<key>.type,<key>.messageand<key>.stacktrace; - a
Datebecomes an ISO string, an object becomes a JSON string; nullandundefinedare dropped.
<key>.type comes from the error’s name when it’s a non-empty string other than "Error", otherwise from the constructor name. Production builds minify class names, so a custom error class only keeps a readable type if it assigns its name as a string literal:
this.name = this.constructor.name or new.target.name would turn into a minified name such as d. npm run lint enforces this: a no-restricted-syntax rule flags those patterns and flags any class that extends Error directly without its own name. Test files, e2e/ and .d.ts files are exempt.
A subclass of DomainError without its own name inherits "DomainError". If you want to tell your errors apart in logs, give each subclass its own name.
Log messages
Core messages are in English with an area prefix:[MAILER], [USERS], [AUTH], [ORG]. Follow the same convention in your features. The exception is handleCommandError, which logs the command’s default (Polish) error message without a prefix, at warn for DomainError and error for anything else.
LOG_LEVEL does nothing
LOG_LEVEL is in the env schema and .env.example sets it to info, but nothing reads it. The logger doesn’t filter by level: debug entries are printed and exported just like error. Filter by severity in your log backend instead.
OpenTelemetry export
register() in src/instrumentation.ts runs once when the server starts, in the Node.js runtime only. After the environment check, it sets up export only when an endpoint is configured:
src/instrumentation.ts
Any collector that accepts OTLP over HTTP with JSON works: an OpenTelemetry Collector, Grafana Cloud, Honeycomb, Axiom and similar services.
How export behaves:
- Logs and spans are sent in batches. Log records go out every 5 seconds by default.
- The kit doesn’t flush on shutdown. On
SIGTERM(a redeploy or restart), Next.js exits immediately, so entries from the last few seconds may be lost. - Traces use
NodeTracerProviderwith anAsyncLocalStoragecontext manager and W3C propagation, so nested spans get the right parent. - There is no automatic instrumentation: Prisma queries and HTTP requests don’t produce spans. A span exists only where your code opens one.
- No metrics provider is registered.
meterfromcreateTelemetryworks, but measurements go nowhere.
Tracing your code with withSpan
createTelemetry(serviceName) returns tracer, meter and withSpan(name, attributes, fn):
withSpan opens an active span, runs fn and returns its result as a Promise, even for a synchronous function. On success it sets status OK. If fn throws or rejects, it records the exception, sets status ERROR and rethrows the same value. The span always ends.
Security headers
next.config.ts adds the headers from src/shared/lib/security-headers.ts to every response (source: "/(.*)"), including /api/* routes and proxy redirects.
There is no
Content-Security-Policy header. If your app needs one, add it to security-headers.ts and test it against the pages and third-party scripts you use.Healthcheck
src/app/api/healthcheck/route.ts
What it tells you, and what it doesn’t:
- Public. The proxy skips
/api, and the handler checks no session. The response contains no data. - Database only. A
200means the process passed the startup env check, is running and can reach the database. A server with invalid env never answers at all, because it exits on startup. - Doesn’t detect values that pass validation but don’t work (a wrong Resend key, an
APP_URLon another domain), an empty database without tables (SELECT 1still works), or a redirect loop caused by a badHOSTNAME, since/apibypasses the proxy. - Database errors aren’t logged. The
catchreturns503silently; check the database’s own logs. - Timing. A closed port fails immediately; an unreachable host waits up to 10 seconds (
connectionTimeoutMillisinsrc/shared/lib/prisma.ts).
Related
Environment variables
Full reference, including the OTel variables.
Docker
The production image and its runtime settings.