# Observability (/docs/starter/modules/observability)

Choose with `--observability otel`, `--observability sentry`, or `--observability none` (default). On [a React + Vite frontend with an API](../../cli/frontend-and-api.mdx#modules-on-these-stacks), the Hono and FastAPI APIs implement every option: see [A frontend and its API](#a-frontend-and-its-api).

Server errors and traces go to the tool you chose. Errors that escape a page, route, server action, or server function are sent with the request's method and path, and so are errors the app handles and reports with `reportError` from `@clubedge/core`, such as a failed webhook, email, or background job.

Every request is traced, and so are calls through the email, storage, and payments composition roots, as spans such as `email.send` and `storage.upload`. Wrap your own work with `traced`:

```ts title="Any server code"
import { traced } from "@clubedge/core";

await traced("name", () => work());
```

Until the tool is configured nothing is sent, and errors keep going to the server log.

<Tabs items={['OpenTelemetry', 'Sentry']} groupId="observability" persist>
  <Tab value="OpenTelemetry">
    Set `OTEL_EXPORTER_OTLP_ENDPOINT` to an OpenTelemetry collector or to a vendor's OTLP/HTTP endpoint (Grafana Cloud, Honeycomb, Datadog, and others accept it), with any API key in `OTEL_EXPORTER_OTLP_HEADERS` as `key=value` pairs. Traces go to `/v1/traces` and errors to `/v1/logs` as error log records, and each error is also recorded on the span it happened in. `OTEL_SERVICE_NAME` defaults to the project's service id. With Next.js, the spans Next.js and pg-boss create join the same traces.

    To see traces during development, run [Jaeger](https://www.jaegertracing.io), set `OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318`, and open [http://localhost:16686](http://localhost:16686):

    ```sh
    docker run --rm -p 16686:16686 -p 4318:4318 jaegertracing/jaeger:latest
    ```
  </Tab>
  <Tab value="Sentry">
    Create a Node.js project in [Sentry](https://sentry.io) and set `SENTRY_DSN` to its DSN. `SENTRY_TRACES_SAMPLE_RATE` is the share of requests traced, from 0 to 1 (default 0.1; use 1 while trying it out), and `SENTRY_ENVIRONMENT` labels events (default `NODE_ENV`). The SDK sends no user details, cookies, query strings, or request bodies. With Next.js, the spans Next.js and pg-boss create join the same traces.

    ### Source maps [#source-maps]

    To see your own code in browser stack traces, give the build `SENTRY_AUTH_TOKEN` (an auth token with the `project:releases` scope), `SENTRY_ORG`, and `SENTRY_PROJECT`. The build then makes source maps for the browser bundle, uploads them to Sentry, and deletes them, so they are never served. Without the token the build makes no source maps. Set the three variables where the build runs: in CI, or as build-time variables on your platform.
  </Tab>
</Tabs>

## Browser errors [#browser-errors]

Uncaught errors in the browser are reported too, with the page's path and no query string. With OpenTelemetry, the browser sends them to `/api/client-errors`, which reports them like server errors and accepts 30 reports a minute per address. With Sentry, Sentry's browser SDK sends them directly; it is downloaded only when `SENTRY_DSN` is set. In both cases the browser reads its settings from `/api/client-errors` at runtime, so builds never bake them in.

On Vercel and Netlify, a function can stop right after it responds, so spans still waiting in a batch at that moment can be lost.

## A frontend and its API [#a-frontend-and-its-api]

With the Hono API (Starter v0.25.0 or newer) and the FastAPI API (Starter v0.26.0 or newer), the API reports its errors and traces its requests. Set the variables in `apps/api/.env.local`, and on the API's service in production. `OTEL_SERVICE_NAME` defaults to the API's service id. The API stops taking requests on `SIGTERM` and sends what is still buffered before it exits.

With FastAPI, every error the API logs at the `ERROR` level, such as with `logger.exception(...)`, reaches the tool, with the `extra` it was logged with as its attributes. With Sentry, create a Python project instead of a Node.js one; its integrations trace what FastAPI, httpx, and Redis do in the same traces.

The React + Vite frontend reports browser errors as above: it reads its settings from the API's `/api/client-errors` at runtime, through the frontend, so `SENTRY_DSN` is set on the API only. The React + Vite build does not upload source maps to Sentry yet, so browser stack traces point into the minified bundle.

## Another tool [#another-tool]

Observability goes through the provider-neutral `ObservabilityProvider` interface in `packages/observability` and the telemetry hooks in `@clubedge/core`. To use another tool, implement that interface in a new package and install it from `apps/web/src/server/observability.ts` (`apps/api/src/server/observability.ts` with the Hono API); error reports and spans across the app stay the same.

With the FastAPI API, the `Observability` protocol is in `apps/api/src/clubedge_api/telemetry.py`, and `tracing.py` traces each request. Implement the protocol and start it from `apps/api/src/clubedge_api/observability.py`.
