Observability
Available since 2.29
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!
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 server-side OpenTelemetry spans for the following:
handlehook (handlefunctions running in asequencewill show up as children of each other and the root handle hook)loadfunctions (includes universalloadfunctions when they’re run on the server)- Form actions
- Remote functions
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/instrumentation.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, providing your deployment platform supports it and your adapter is aware of it.
To enable both of these features, add the following to your svelte.config.js:
export default {
kit: {
experimental: {
tracing: {
server: boolean;
};
instrumentation: {
server: boolean;
};
};
}
kit: {
experimental: {
tracing: {
server: boolean;
};
instrumentation: {
server: boolean;
};
}experimental: {
tracing: {
server: boolean;
}tracing: {
server: booleanserver: true
},
instrumentation: {
server: boolean;
}instrumentation: {
server: booleanserver: true
}
}
}
};Tracing — and more significantly, observability 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.
Development quickstart
To view your first trace, you’ll need to set up a local collector. We’ll use Jaeger in this example, as they provide an easy-to-use quickstart command. Once your collector is running locally:
- Turn on the experimental flag mentioned above in your
svelte.config.jsfile - Use your package manager to install the dependencies you’ll need
npm i @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-oltp-proto import-in-the-middle - Create
src/instrumentation.server.tswith the following:
import { import NodeSDKNodeSDK } from '@opentelemetry/sdk-node';
import { import getNodeAutoInstrumentationsgetNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { import OTLPTraceExporterOTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { import createAddHookMessageChannelcreateAddHookMessageChannel } from 'import-in-the-middle';
import { function register<Data = any>(specifier: string | URL, parentURL?: string | URL, options?: Module.RegisterOptions<Data>): void (+1 overload)Register a module that exports hooks that customize Node.js module
resolution and loading behavior. See
Customization hooks.
register } from 'module';
const { const registerOptions: anyregisterOptions } = import createAddHookMessageChannelcreateAddHookMessageChannel();
register<any>(specifier: string | URL, parentURL?: string | URL, options?: Module.RegisterOptions<any> | undefined): void (+1 overload)Register a module that exports hooks that customize Node.js module
resolution and loading behavior. See
Customization hooks.
register('import-in-the-middle/hook.mjs', import.meta.ImportMeta.url: stringThe absolute file: URL of the module.
url, const registerOptions: anyregisterOptions);
const const sdk: anysdk = new import NodeSDKNodeSDK({
serviceName: stringserviceName: 'test-sveltekit-tracing',
traceExporter: anytraceExporter: new import OTLPTraceExporterOTLPTraceExporter(),
instrumentations: any[]instrumentations: [import getNodeAutoInstrumentationsgetNodeAutoInstrumentations()]
});
const sdk: anysdk.start();Any server-side requests will now begin generating traces, which you can view in Jaeger’s web console at localhost:16686.
Edit this page on GitHub llms.txt