feat(api): ship logs and HTTP metrics to OpenObserve

Replace Pino with LogTape and add OpenTelemetry metrics, both pushed to
OpenObserve over OTLP/HTTP when OPENOBSERVE_URL is set (console only otherwise).

- lib/logger.ts: console sink (pretty in dev, JSON lines in production) plus an
  OpenObserve sink via @logtape/otel. Each request runs inside
  withContext({ requestId }), so every record logged during it carries the id.
- The request middleware logs method, path, route pattern, status, durationMs,
  userId and user agent, at error/warning/info by status (/api/health at debug).
- lib/metrics.ts: http.server.request.duration (histogram, 5 ms to 10 s) and
  http.server.active_requests, attributed by method, route pattern and status,
  exported every METRICS_EXPORT_INTERVAL_MS (15 s).
- lib/openobserve.ts holds the shared endpoint and Basic auth settings; logs and
  metrics are flushed on SIGINT/SIGTERM.
- docs/openobserve-dashboard.md explains the dashboard and its queries, and
  docs/openobserve-dashboard.json can be imported as is.
- New variables are declared in turbo.json, the .env examples and the compose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-11-06 11:59:17 -03:00
parent 3411b4b145
commit dd9cf4ce43
21 changed files with 1191 additions and 102 deletions

View File

@@ -15,7 +15,7 @@ The `health` module is the reference example.
- Monorepo orchestrated with **Turborepo** (see the managed block at the end of this file)
- Backend: **Bun** + **Hono**, **Prisma 7** against **Postgres** (via `@prisma/adapter-pg`), **Zod** for
validation, **Pino** for logging, **BetterAuth** for auth
validation, **LogTape** for logging (shipped to **OpenObserve**), **BetterAuth** for auth
- Frontend: **React + Vite + TypeScript**, **Tailwind v4**, UI components from **shadcn/ui**
- Frontend routing: **TanStack Router** (file-based) · server state: **TanStack Query** ·
forms: **React Hook Form + Zod**
@@ -197,9 +197,26 @@ export class CreateAlert {
(`problemResponse`, `notFoundHandler`, `errorHandler`) in `apps/api/src/lib/problem-details.ts`.
- Zod is the only validation library, and Zod 4 is what is installed. Define schemas next to the feature that
uses them (`schema.ts`); share them through `@superlist/shared` only when more than one module needs them.
- **Logging: Pino** — one shared logger in `apps/api/src/lib/logger.ts`, reachable from handlers as
`c.get('logger')` (already enriched with the request id); never `console.log` in the backend. Level comes from
`LOG_LEVEL`, defaulting to `debug` (pretty) outside production and `info` (JSON) in production.
- **Logging: LogTape** — root logger (`superlist` category) exported from `apps/api/src/lib/logger.ts`, reachable
from handlers as `c.get('logger')`; never `console.log` in the backend. The call shape is
`logger.info('message {prop}', { prop })` (message first, properties second; pass errors as `{ error }`).
`configureLogging()` runs once in `src/index.ts` (tests never call it, so logging is silent there) and
`dispose()` flushes on SIGINT/SIGTERM. Sinks: console (`@logtape/pretty` outside production, JSON lines in
production) plus OpenObserve over OTLP/HTTP (`@logtape/otel`, `/api/<org>/v1/logs`, Basic auth, `stream-name`
header) when `OPENOBSERVE_URL` is set (`OPENOBSERVE_ORG`, `OPENOBSERVE_STREAM`, `OPENOBSERVE_USER`,
`OPENOBSERVE_PASSWORD`). Level comes from `LOG_LEVEL` (`debug` in dev, `info` in production; `warn` is accepted
for `warning`). The request middleware in `app.ts` wraps each request in `withContext({ requestId })`, so every
record logged during it carries the request id, and logs one `superlist.http` record per request with
`method`, `path`, `route` (the Hono pattern, e.g. `/api/lists/:listId`), `status`, `durationMs`, `userId` and
`userAgent` — `error` for 5xx, `warning` for 4xx, `debug` for `/api/health`, `info` otherwise. SSE routes
return their `Response` immediately, so their duration is the time to open the stream.
- **Metrics: OpenTelemetry SDK** (`apps/api/src/lib/metrics.ts`), pushed to OpenObserve over OTLP/HTTP
(`/api/<org>/v1/metrics`, cumulative temporality) every `METRICS_EXPORT_INTERVAL_MS` (15 s) when
`OPENOBSERVE_URL` is set; otherwise the global meter is a no-op. The same request middleware records
`http.server.request.duration` (histogram, seconds; attributes `http.request.method`, `http.route`,
`http.response.status_code`) and `http.server.active_requests`. Always use the route pattern
(`routePath(c, -1)`), never the raw path, as an attribute. The OpenObserve settings live in
`lib/openobserve.ts`, shared by logs and metrics. Dashboard guide: `docs/openobserve-dashboard.md` (importable JSON: `docs/openobserve-dashboard.json`).
- Unexpected throws are turned into Problem Details by `app.onError`; unmatched routes by `app.notFound`
(both in `apps/api/src/lib/problem-details.ts`).
- `createApp()` middleware order matters: `secureHeaders` → `cors` (origins from `CORS_ORIGIN`, credentials on)