-
-
Notifications
You must be signed in to change notification settings - Fork 884
feat: experimental tracing logger #4406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| Nitro can instrument its request lifecycle through Node [diagnostics channels](https://nodejs.org/api/diagnostics_channel.html) — no OpenTelemetry SDK required. This example turns instrumentation on and enables the built-in console logger, which groups every h3, srvx and unstorage span into a per-request timeline (waterfall). | ||
|
|
||
| ## Enabling tracing | ||
|
|
||
| ```ts [nitro.config.ts] | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| // Instrument the h3, srvx and unstorage tracing channels. | ||
| tracingChannel: true, | ||
|
|
||
| experimental: { | ||
| // Log every completed span to the console (built-in, dependency-free sink). | ||
| tracingLogger: true, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| `tracingChannel: true` wires up the producers (it accepts `{ h3, srvx, unstorage }` to trace a subset). `experimental.tracingLogger` adds a built-in sink that `console.log`s each completed span — a dependency-free alternative to a vendor exporter, handy for local development. | ||
|
|
||
| ## Try it | ||
|
|
||
| ```sh | ||
| npm run dev | ||
| # then, in another terminal: | ||
| curl http://localhost:3000/ | ||
| curl http://localhost:3000/users/42 | ||
| ``` | ||
|
|
||
| Each request prints a timeline of its spans to the console — the middleware, the matched route, and every storage operation, positioned and sized by when they ran and how long they took: | ||
|
|
||
| ``` | ||
| ▶ GET / 4.10ms (4 spans) | ||
| middleware GET / █······················· 0.12ms h3.handler_type=middleware http.route=/ | ||
| GET / ·███████████████████····· 2.49ms h3.handler_type=route http.route=/ | ||
| getItem ···██···················· 0.18ms db.operation=getItem db.system=memory unstorage.keys_count=1 | ||
| setItem ·····██·················· 0.16ms db.operation=setItem db.system=memory unstorage.keys_count=1 | ||
| ``` | ||
|
|
||
| The header line (`▶`) is the request itself — its method, path and total time; the rows below are the spans that ran within it. Note the dynamic route is named by its matched template — `GET /users/:id`, not `/users/42` — keeping span names low-cardinality per the OpenTelemetry HTTP conventions. | ||
|
|
||
| The request boundary comes from Nitro's `request`/`response` runtime hooks, so grouping works identically in `vite dev` and in a production build. Spans are grouped per request via async context, so timelines for concurrent requests stay separate, and a failed span is marked `✖` with its error message. | ||
|
|
||
| ## What gets traced | ||
|
|
||
| | Channel | Span | Emitted by | | ||
| | --- | --- | --- | | ||
| | `h3.request` | each matched route and middleware | `server/routes/*`, `server/middleware/*` | | ||
| | `unstorage.*` | each storage operation (`getItem`, `setItem`, …) | `useStorage()` in `server/routes/index.ts` | | ||
| | `srvx.request` | the whole request, with response status (production server) | the srvx server layer | | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { defineConfig } from "nitro"; | ||
|
|
||
| export default defineConfig({ | ||
| serverDir: true, | ||
|
|
||
| // Instrument the h3, srvx and unstorage tracing channels. | ||
| tracingChannel: true, | ||
|
|
||
| experimental: { | ||
| // Log every completed span to the console (built-in, dependency-free sink). | ||
| tracingLogger: true, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "nitro build", | ||
| "dev": "nitro dev", | ||
| "preview": "node .output/server/index.mjs" | ||
| }, | ||
| "devDependencies": { | ||
| "nitro": "latest" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { defineMiddleware } from "nitro"; | ||
|
|
||
| // A trivial middleware — each request produces its own `middleware` span. | ||
| export default defineMiddleware((event) => { | ||
| event.context.requestedAt = Date.now(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineHandler } from "nitro"; | ||
| import { useStorage } from "nitro/storage"; | ||
|
|
||
| // Reads and writes storage so the request emits `unstorage.*` spans (CLIENT) | ||
| // alongside the `srvx.request`, `middleware` and route (`h3.request`) spans. | ||
| export default defineHandler(async () => { | ||
| const storage = useStorage(); | ||
| const hits = ((await storage.getItem<number>("hits")) ?? 0) + 1; | ||
| await storage.setItem("hits", hits); | ||
| return { message: "Hello from the Nitro tracing demo", hits }; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineHandler } from "nitro"; | ||
|
|
||
| // A dynamic route — spans are named by the matched route template | ||
| // (`GET /users/:id`), not the concrete path, to keep cardinality low. | ||
| export default defineHandler((event) => ({ | ||
| user: { id: event.context.params!.id }, | ||
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "extends": "nitro/tsconfig" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { defineConfig } from "vite"; | ||
| import { nitro } from "nitro/vite"; | ||
|
|
||
| export default defineConfig({ plugins: [nitro()] }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.