Minimal, barebone SvelteKit app (same tooling as ../frontend — pnpm, adapter-node,
TypeScript — but without auth/UI libraries) demonstrating SvelteKit's built-in
server observability feature wired up to a
local Grafana LGTM stack via docker compose.
svelte.config.jsturns on the experimentalkit.experimental.tracing.serverandkit.experimental.instrumentation.serverflags, which make SvelteKit emit OpenTelemetry spans forhandle,load, form actions, etc.src/instrumentation.server.tsis loaded by SvelteKit before any app code. It boots an OpenTelemetry Node SDK with auto-instrumentations and OTLP/HTTP exporters for traces, logs and metrics, all pointed atOTEL_EXPORTER_OTLP_ENDPOINT(defaults tohttp://localhost:4318, which docker compose overrides to theotel-lgtmservice).src/hooks.server.tsshows how to annotate the root span with a custom attribute (http.route) usingevent.tracing.root.src/routes/+page.server.tsandsrc/routes/api/hello/+server.tseach generate a request so you have two distinct traces to look at.src/routes/api/login/+server.tsis a simulated login endpoint showing manual instrumentation withtracer.startActiveSpan(nestedvalidate-credentials/create-sessionspans, with an error status on invalid credentials), a correlated log record via@opentelemetry/api-logs(shows up in Loki with the request'strace_id/span_id), and alogin.attemptscounter metric (shows up in Prometheus/Mimir).docker-compose.ymlrunsgrafana/otel-lgtm(Grafana + Tempo + Loki + Mimir/Prometheus, all in one image with an OTLP receiver built in) alongside the built app.
docker compose up --buildThen:
- Open the app at http://localhost:3001 and click around, and try the login form (this generates traces, logs and metrics).
- Open Grafana at http://localhost:3000 (default login
admin/admin). - Go to Explore:
- Pick the Tempo data source and search for traces from the
sveltekit-observability-exampleservice. - Pick the Loki data source and query
{service_name="sveltekit-observability-example"}to see the login logs, correlated with their trace. - Pick the Prometheus data source and query
login_attempts_totalto see the counter split byapp_login_success.
- Pick the Tempo data source and search for traces from the
pnpm install
# start just the observability stack
docker run --rm -p 3000:3000 -p 4317:4317 -p 4318:4318 grafana/otel-lgtm
pnpm devThe dev server reads OTEL_EXPORTER_OTLP_ENDPOINT from .env (copy .env.example), which
defaults to http://localhost:4318.