Skip to content

Commit cc0eef5

Browse files
feat: Initial tracing setup (peer deps + utils)
1 parent 6ce43ea commit cc0eef5

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

packages/kit/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"vitefu": "^1.0.6"
3434
},
3535
"devDependencies": {
36+
"@opentelemetry/api": "^1.0.0",
3637
"@playwright/test": "catalog:",
3738
"@sveltejs/vite-plugin-svelte": "^5.0.1",
3839
"@types/connect": "^3.4.38",
@@ -47,10 +48,16 @@
4748
"vitest": "catalog:"
4849
},
4950
"peerDependencies": {
51+
"@opentelemetry/api": "^1.0.0",
5052
"@sveltejs/vite-plugin-svelte": "^3.0.0 || ^4.0.0-next.1 || ^5.0.0",
5153
"svelte": "^4.0.0 || ^5.0.0-next.0",
5254
"vite": "^5.0.3 || ^6.0.0"
5355
},
56+
"peerDependenciesMeta": {
57+
"@opentelemetry/api": {
58+
"optional": true
59+
}
60+
},
5461
"bin": {
5562
"svelte-kit": "svelte-kit.js"
5663
},
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @import { Tracer } from '@opentelemetry/api' */
2+
import { trace } from '@opentelemetry/api';
3+
import { noop_tracer } from './noop-tracer.js';
4+
5+
/**
6+
* @param {Object} [options={}] - Configuration options
7+
* @param {boolean} [options.is_enabled=false] - Whether tracing is enabled
8+
* @param {Tracer} [options.tracer] - Custom tracer instance
9+
* @returns {Tracer} The tracer instance
10+
*/
11+
export function get_tracer({ is_enabled = false, tracer } = {}) {
12+
if (!is_enabled) {
13+
return noop_tracer;
14+
}
15+
16+
if (tracer) {
17+
return tracer;
18+
}
19+
20+
return trace.getTracer('sveltekit');
21+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** @import { Tracer, Span, SpanContext } from '@opentelemetry/api' */
2+
3+
/**
4+
* Tracer implementation that does nothing (null object).
5+
* @type {Tracer}
6+
*/
7+
export const noop_tracer = {
8+
/**
9+
* @returns {Span}
10+
*/
11+
startSpan() {
12+
return noop_span;
13+
},
14+
15+
/**
16+
* @param {unknown} _name
17+
* @param {unknown} arg_1
18+
* @param {unknown} [arg_2]
19+
* @param {Function} [arg_3]
20+
* @returns {unknown}
21+
*/
22+
startActiveSpan(_name, arg_1, arg_2, arg_3) {
23+
if (typeof arg_1 === 'function') {
24+
return arg_1(noop_span);
25+
}
26+
if (typeof arg_2 === 'function') {
27+
return arg_2(noop_span);
28+
}
29+
if (typeof arg_3 === 'function') {
30+
return arg_3(noop_span);
31+
}
32+
}
33+
};
34+
35+
/**
36+
* @type {Span}
37+
*/
38+
const noop_span = {
39+
spanContext() {
40+
return noop_span_context;
41+
},
42+
setAttribute() {
43+
return this;
44+
},
45+
setAttributes() {
46+
return this;
47+
},
48+
addEvent() {
49+
return this;
50+
},
51+
setStatus() {
52+
return this;
53+
},
54+
updateName() {
55+
return this;
56+
},
57+
end() {
58+
return this;
59+
},
60+
isRecording() {
61+
return false;
62+
},
63+
recordException() {
64+
return this;
65+
}
66+
};
67+
68+
/**
69+
* @type {SpanContext}
70+
*/
71+
const noop_span_context = {
72+
traceId: '',
73+
spanId: '',
74+
traceFlags: 0
75+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/** @import { SpanAttributes, Span, Tracer } from '@opentelemetry/api' */
2+
import { SpanStatusCode } from '@opentelemetry/api';
3+
import { HttpError, Redirect } from '../../control.js';
4+
5+
/**
6+
* @template T
7+
* @param {Object} options
8+
* @param {string} options.name
9+
* @param {Tracer} options.tracer
10+
* @param {SpanAttributes} options.attributes
11+
* @param {function(Span): Promise<T>} options.fn
12+
* @returns {Promise<T>}
13+
*/
14+
export function record_span({ name, tracer, attributes, fn }) {
15+
return tracer.startActiveSpan(name, { attributes }, async (span) => {
16+
try {
17+
const result = await fn(span);
18+
span.end();
19+
return result;
20+
} catch (error) {
21+
if (error instanceof HttpError) {
22+
span.setAttributes({
23+
[`${name}.result.type`]: 'known_error',
24+
[`${name}.result.status`]: error.status,
25+
[`${name}.result.message`]: error.body.message
26+
});
27+
span.recordException({
28+
name: 'HttpError',
29+
message: error.body.message
30+
});
31+
span.setStatus({
32+
code: SpanStatusCode.ERROR,
33+
message: error.body.message
34+
});
35+
} else if (error instanceof Redirect) {
36+
span.setAttributes({
37+
[`${name}.result.type`]: 'redirect',
38+
[`${name}.result.status`]: error.status,
39+
[`${name}.result.location`]: error.location
40+
});
41+
} else if (error instanceof Error) {
42+
span.setAttributes({
43+
[`${name}.result.type`]: 'unknown_error'
44+
});
45+
span.recordException({
46+
name: error.name,
47+
message: error.message,
48+
stack: error.stack
49+
});
50+
span.setStatus({
51+
code: SpanStatusCode.ERROR,
52+
message: error.message
53+
});
54+
} else {
55+
span.setAttributes({
56+
[`${name}.result.type`]: 'unknown_error'
57+
});
58+
span.setStatus({ code: SpanStatusCode.ERROR });
59+
}
60+
span.end();
61+
62+
throw error;
63+
}
64+
});
65+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Telemetry configuration.
3+
*/
4+
// This is meant to be both flexible for custom app requirements (metadata)
5+
// and extensible for standardization (example: functionId, more to come).
6+
export type TelemetrySettings = {
7+
/**
8+
* Enable or disable telemetry. Disabled by default while experimental.
9+
*/
10+
enabled?: boolean;
11+
};

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)