Skip to content

Commit 5954ae4

Browse files
changeset and docs
1 parent b526831 commit 5954ae4

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

.changeset/early-taxis-make.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@sveltejs/kit': minor
33
---
44

5-
feat: OpenTelemetry tracing for `handle`, `sequence`, form actions, and `load` functions running on the server
5+
feat: OpenTelemetry tracing for `handle`, `sequence`, form actions, remote functions, and `load` functions running on the server

.changeset/whole-bananas-sort.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': minor
3+
'@sveltejs/adapter-netlify': minor
4+
'@sveltejs/adapter-vercel': minor
5+
'@sveltejs/adapter-node': minor
6+
'@sveltejs/kit': minor
7+
---
8+
9+
feat: add `tracing.server.ts` for tracing instrumentation and setup

documentation/docs/10-getting-started/30-project-structure.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ my-project/
1919
│ ├ error.html
2020
│ ├ hooks.client.js
2121
│ ├ hooks.server.js
22-
│ └ service-worker.js
22+
| ├ service-worker.js
23+
│ └ tracing.server.js
2324
├ static/
2425
│ └ [your static assets]
2526
├ tests/
@@ -54,6 +55,7 @@ The `src` directory contains the meat of your project. Everything except `src/ro
5455
- `hooks.client.js` contains your client [hooks](hooks)
5556
- `hooks.server.js` contains your server [hooks](hooks)
5657
- `service-worker.js` contains your [service worker](service-workers)
58+
- `tracing.server.js` contains your [tracing](observability) setup and instrumentation code
5759

5860
(Whether the project contains `.js` or `.ts` files depends on whether you opt to use TypeScript when you create your project.)
5961

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)