Skip to content

Commit 49133bf

Browse files
authored
Merge branch 'main' into fix-console-scroll
2 parents 390587a + 69cb697 commit 49133bf

File tree

10 files changed

+456
-22
lines changed

10 files changed

+456
-22
lines changed

apps/svelte.dev/content/docs/cli/20-commands/20-sv-add.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ You can select multiple space-separated add-ons from [the list below](#Official-
2828

2929
<!-- TODO: it'd be nice for this to live on the "add-ons" page, but we first need svelte.dev to support making pages from headings -->
3030

31+
- [`devtools-json`](devtools-json)
3132
- [`drizzle`](drizzle)
3233
- [`eslint`](eslint)
3334
- [`lucia`](lucia)
@@ -39,4 +40,3 @@ You can select multiple space-separated add-ons from [the list below](#Official-
3940
- [`sveltekit-adapter`](sveltekit-adapter)
4041
- [`tailwindcss`](tailwind)
4142
- [`vitest`](vitest)
42-
- [`devtools-json`](devtools-json)

apps/svelte.dev/content/docs/kit/10-getting-started/30-project-structure.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ my-project/
2020
│ ├ error.html
2121
│ ├ hooks.client.js
2222
│ ├ hooks.server.js
23-
│ └ service-worker.js
23+
| ├ service-worker.js
24+
│ └ tracing.server.js
2425
├ static/
2526
│ └ [your static assets]
2627
├ tests/
@@ -55,6 +56,8 @@ The `src` directory contains the meat of your project. Everything except `src/ro
5556
- `hooks.client.js` contains your client [hooks](hooks)
5657
- `hooks.server.js` contains your server [hooks](hooks)
5758
- `service-worker.js` contains your [service worker](service-workers)
59+
- `instrumentation.server.js` contains your [observability](observability) setup and instrumentation code
60+
- Requires adapter support. If your adapter supports it, it is guarnteed to run prior to loading and running your application code.
5861

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

apps/svelte.dev/content/docs/kit/25-build-and-deploy/99-writing-adapters.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export default function (options) {
3535
// Return `true` if the route with the given `config` can use `read`
3636
// from `$app/server` in production, return `false` if it can't.
3737
// Or throw a descriptive error describing how to configure the deployment
38+
},
39+
tracing: () => {
40+
// Return `true` if this adapter supports loading `tracing.server.js`.
41+
// Return `false if it can't, or throw a descriptive error.
3842
}
3943
}
4044
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
3+
title: Observability
4+
---
5+
6+
<blockquote class="since note">
7+
<p>Available since 2.29</p>
8+
</blockquote>
9+
10+
> [!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!
11+
12+
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](https://opentelemetry.io) spans for the following:
13+
14+
- [`handle`](hooks#Server-hooks-handle) hook (`handle` functions running in a [`sequence`](@sveltejs-kit-hooks#sequence) will show up as children of each other and the root handle hook)
15+
- [`load`](load) functions (includes universal `load` functions when they're run on the server)
16+
- [Form actions](form-actions)
17+
- [Remote functions](remote-functions)
18+
19+
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.
20+
21+
To enable both of these features, add the following to your `svelte.config.js`:
22+
23+
```js
24+
/// file: svelte.config.js
25+
export default {
26+
kit: {
27+
+++experimental: {
28+
tracing: {
29+
server: true
30+
},
31+
instrumentation: {
32+
server: true
33+
}
34+
}+++
35+
}
36+
};
37+
```
38+
39+
> [!NOTE] 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.
40+
41+
## Agumenting SvelteKit's builtin tracing
42+
43+
SvelteKit provides access to the `root` span and the `current` span on the request event. The root span is the one associated with your root `handle` function, and the current span could be associated with `handle`, `load`, a form action, or a remote function, depending on the context. You can annotate these spans with any attributes you wish to record:
44+
45+
```js
46+
/// file: $lib/authenticate.ts
47+
48+
// @filename: ambient.d.ts
49+
declare module '$lib/auth-core' {
50+
export function getAuthenticatedUser(): Promise<{ id: string }>
51+
}
52+
53+
// @filename: index.js
54+
// ---cut---
55+
import { getRequestEvent } from '$app/server';
56+
import { getAuthenticatedUser } from '$lib/auth-core';
57+
58+
async function authenticate() {
59+
const user = await getAuthenticatedUser();
60+
const event = getRequestEvent();
61+
event.tracing.root.setAttribute('userId', user.id);
62+
}
63+
```
64+
65+
## Development quickstart
66+
67+
To view your first trace, you'll need to set up a local collector. We'll use [Jaeger](https://www.jaegertracing.io/docs/getting-started/) in this example, as they provide an easy-to-use quickstart command. Once your collector is running locally:
68+
69+
- Turn on the experimental flag mentioned above in your `svelte.config.js` file
70+
- Use your package manager to install the dependencies you'll need
71+
```sh
72+
npm i @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-oltp-proto import-in-the-middle
73+
```
74+
- Create `src/instrumentation.server.ts` with the following:
75+
76+
```ts
77+
import { NodeSDK } from '@opentelemetry/sdk-node';
78+
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
79+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
80+
import { createAddHookMessageChannel } from 'import-in-the-middle';
81+
import { register } from 'module';
82+
83+
const { registerOptions } = createAddHookMessageChannel();
84+
register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions);
85+
86+
const sdk = new NodeSDK({
87+
serviceName: 'test-sveltekit-tracing',
88+
traceExporter: new OTLPTraceExporter(),
89+
instrumentations: [getNodeAutoInstrumentations()]
90+
});
91+
92+
sdk.start();
93+
```
94+
95+
Any server-side requests will now begin generating traces, which you can view in Jaeger's web console at [localhost:16686](http://localhost:16686).

0 commit comments

Comments
 (0)