|
| 1 | +--- |
| 2 | +title: Observability |
| 3 | +--- |
| 4 | + |
| 5 | +<blockquote class="since note"> |
| 6 | + <p>Available since 2.28</p> |
| 7 | +</blockquote> |
| 8 | + |
| 9 | +> [!NOTE] This feature is experimental. Expect bugs and breaking changes in minor versions (though we'll do our best to keep those to an absolute minimum). Please provide feedback! |
| 10 | +
|
| 11 | +Sometimes, you may need to observe how your application is behaving in order to improve performance or find the root cause of a pesky bug. To help with this, SvelteKit can emit serverside [OpenTelemetry](https://opentelemetry.io) spans for the following: |
| 12 | + |
| 13 | +- `handle` |
| 14 | +- `sequence` (`sequence`d `handle` functions will show up as children of each other and the root handle hook) |
| 15 | +- `load` (includes univeral `load`s when they're run on the server) |
| 16 | +- Form actions |
| 17 | +- Remote Functions |
| 18 | + |
| 19 | +To enable these spans, add the following to your `svelte.config.js`'s `kit` configuration: |
| 20 | + |
| 21 | +```ts |
| 22 | +{ |
| 23 | + experimental: { |
| 24 | + tracing: { |
| 25 | + server: true |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Just telling SvelteKit to emit spans won't get you far, though -- you need to actually collect them somewhere to be able to view them. SvelteKit provides `src/tracing.server.ts` as a place to write your tracing setup and instrumentation code. It's guaranteed to be run prior to your application code being imported. |
| 32 | + |
| 33 | +> [!NOTE] Tracing -- and more significantly, tracing instrumentation -- can have a nontrivial overhead. Before you go all-in on tracing, consider whether or not you really need it, or if it might be more appropriate to turn it on in development and preview environments only. |
| 34 | +
|
| 35 | +## Development Quickstart |
| 36 | + |
| 37 | +To view your first trace, you'll need to set up a local collector. We recommend [Jaeger](https://www.jaegertracing.io/docs/2.7/getting-started/), as they provide an easy-to-use quickstart command. Once your collector is running locally: |
| 38 | + |
| 39 | +- Turn on the experemental flag from above in your `svelte.config.js` |
| 40 | +- Use your package manager to install `@opentelemetry/sdk-node`, `opentelemetry/auto-instrumentations-node`, `@opentelemetry/exporter-trace-otlp-proto`, and `import-in-the-middle` |
| 41 | +- Create `src/tracing.server.ts` with the following: |
| 42 | + |
| 43 | +```ts |
| 44 | +import { NodeSDK } from '@opentelemetry/sdk-node'; |
| 45 | +import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; |
| 46 | +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; |
| 47 | +import { createAddHookMessageChannel } from 'import-in-the-middle'; |
| 48 | +import { register } from 'module'; |
| 49 | + |
| 50 | +const { registerOptions } = createAddHookMessageChannel(); |
| 51 | +register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions); |
| 52 | + |
| 53 | +const sdk = new NodeSDK({ |
| 54 | + serviceName: 'test-sveltekit-tracing', |
| 55 | + traceExporter: new OTLPTraceExporter(), |
| 56 | + instrumentations: [getNodeAutoInstrumentations()] |
| 57 | +}); |
| 58 | + |
| 59 | +sdk.start(); |
| 60 | +``` |
| 61 | + |
| 62 | +Any serverside requests will now begin generating traces, which you can view in Jaeger's web console at http://localhost:16686. |
0 commit comments