Skip to content

Commit 735ae43

Browse files
committed
improve docs
1 parent 0e397e3 commit 735ae43

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
# Svelte Kit OpenTelemetry Middleware
2+
[![Documentation][docs_badge]][docs]
3+
[![Latest Release][release_badge]][release]
4+
[![License][license_badge]][license]
25

36
A streamlined OpenTelemetry Middleware for SvelteKit
47

8+
![Svelte Trace](./trace.png)
9+
10+
11+
12+
## Getting Started
13+
14+
Install the packages
15+
16+
```bash
17+
npm i @baselime/node-opentelemetry @baselime/sveltekit-opentelemetry-middleware
18+
```
19+
20+
Add the following code to your `root.server.{ts|js}`
21+
522
```javascript
623
import { withOpenTelemetry } from '@baselime/sveltekit-opentelemetry-middleware'
724
import { BaselimeSDK } from '@baselime/node-opentelemetry';
@@ -13,3 +30,30 @@ export const handle = withOpenTelemetry(async ({ event, resolve }) => {
1330
});
1431
```
1532

33+
To configure the BaselimeSDK visit the [documentation](https://baselime.io/docs/sending-data/languages/node.js/)
34+
35+
## Options
36+
37+
The withOpenTelemetry function takes the following options
38+
39+
| Parameter | Type | Description |
40+
|-----------------------|-----------|----------------------------------------------------------|
41+
| captureRequestBody | boolean | Capture the request body in the span. (Optional) |
42+
| captureResponseBody | boolean | Capture the response body in the span. (Optional) |
43+
| requestIdHeader | string | The header to look for the request id in. By default it will check x-request-id and x-vercel
44+
45+
## License
46+
47+
© Baselime Limited, 2023
48+
49+
Distributed under MIT License (`The MIT License`).
50+
51+
See [LICENSE](LICENSE) for more information.
52+
53+
54+
[docs]: https://baselime.io/docs/
55+
[docs_badge]: https://img.shields.io/badge/docs-reference-blue.svg?style=flat-square
56+
[release]: https://github.com/baselime/node-opentelemetry/releases/latest
57+
[release_badge]: https://img.shields.io/github/release/baselime/node-opentelemetry.svg?style=flat-square&ghcache=unused
58+
[license]: https://opensource.org/licenses/MIT
59+
[license_badge]: https://img.shields.io/github/license/baselime/node-opentelemetry.svg?color=blue&style=flat-square&ghcache=unused

example/package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"type": "module",
1919
"dependencies": {
20-
"@baselime/node-opentelemetry": "^0.3.6"
20+
"@baselime/node-opentelemetry": "^0.3.6",
21+
"axios": "^1.6.7"
2122
}
2223
}

example/src/hooks.server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import type { Handle } from '@sveltejs/kit';
22
import { withOpenTelemetry } from '../../src/index'
3-
import { BaselimeSDK } from '@baselime/node-opentelemetry';
3+
import { BaselimeSDK, BetterHttpInstrumentation } from '@baselime/node-opentelemetry';
44

55
new BaselimeSDK({
66
collectorUrl: 'https://otel.baselime.cc/v1',
77
serverless: true,
8-
service: "svelte-demo"
8+
service: "svelte-demo",
9+
instrumentations: [
10+
new BetterHttpInstrumentation()
11+
]
912
}).start();
1013

1114
export const handle: Handle= withOpenTelemetry(async ({ event, resolve }) => {

example/src/routes/sverdle/+page.server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { fail } from '@sveltejs/kit';
22
import { Game } from './game';
3-
4-
export const load = ({ cookies }) => {
3+
import axios from 'axios';
4+
export const load = async ({ cookies }) => {
55
const game = new Game(cookies.get('sverdle'));
66

7+
await axios.get('https://google.com')
8+
79
return {
810
/**
911
* The player's guessed words so far

example/src/routes/sverdle/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
});
5454
}
5555
56+
5657
/**
5758
* Modify the game state without making a trip to the server,
5859
* if client-side JavaScript is enabled

src/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import type { Handle } from '@sveltejs/kit';
22
import { trace, propagation, ROOT_CONTEXT } from '@opentelemetry/api';
33
import { flatten } from 'flat';
44

5+
56
type TraceOptions = {
67
captureRequestBody?: boolean;
78
captureResponseBody?: boolean;
9+
requestIdHeader?: string;
810
}
911

1012
const possibleRequestIdHeaders = [
@@ -13,15 +15,24 @@ const possibleRequestIdHeaders = [
1315
];
1416

1517

18+
/**
19+
*
20+
* @param fn {Handle} The handle hook runs every time the SvelteKit server receives a request and determines the response. It receives an event object representing the request and a function called resolve, which renders the route and generates a Response. This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing routes programmatically, for example).
21+
* @param opts.captureRequestBody {boolean} Capture the request body in the span.
22+
* @param opts.captureResponseBody {boolean} Capture the response body in the span.
23+
* @param opts.requestIdHeader {string} The header to look for the request id in. By default it will check x-request-id and x-vercel-id.
24+
* @returns {Handle}
25+
*/
1626
export function withOpenTelemetry(fn: Handle, opts?: TraceOptions): Handle {
1727
opts = opts || {};
18-
const tracer = trace.getTracer('svelte-kit');
28+
const tracer = trace.getTracer('@baselime/sveltekit-opentelemetry-middleware');
1929
return async function (args) {
2030
const name = `${args.event.request.method} ${args.event.route.id}`;
21-
22-
const traceParent = propagation.extract(ROOT_CONTEXT, args.event.request.headers);
23-
2431
let requestId: string | undefined = undefined;
32+
33+
if(opts.requestIdHeader) {
34+
possibleRequestIdHeaders.push(opts.requestIdHeader.toLowerCase());
35+
}
2536
for(const [key, value] of args.event.request.headers.entries()) {
2637
if(possibleRequestIdHeaders.includes(key.toLowerCase())) {
2738
requestId = value;
@@ -54,7 +65,7 @@ export function withOpenTelemetry(fn: Handle, opts?: TraceOptions): Handle {
5465
platform: args.event.platform
5566
}
5667
})
57-
}, traceParent, async (span) => {
68+
}, async (span) => {
5869

5970
const res = await fn(args);
6071

trace.png

24 KB
Loading

0 commit comments

Comments
 (0)