Skip to content

Commit 7f18fc0

Browse files
make server tracing file separate from tracing config
1 parent 65abef2 commit 7f18fc0

File tree

9 files changed

+100
-82
lines changed

9 files changed

+100
-82
lines changed

packages/adapter-cloudflare/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ export default function (options = {}) {
113113
ASSETS: assets_binding
114114
}
115115
});
116-
builder.trace({
117-
entrypoint: worker_dest,
118-
tracing: `${builder.getServerDirectory()}/tracing.server.js`,
119-
tla: false
120-
});
116+
if (builder.hasServerTracingFile()) {
117+
builder.trace({
118+
entrypoint: worker_dest,
119+
tracing: `${builder.getServerDirectory()}/tracing.server.js`,
120+
tla: false
121+
});
122+
}
121123

122124
// _headers
123125
if (existsSync('_headers')) {

packages/adapter-netlify/index.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ async function generate_edge_functions({ builder }) {
206206
outfile: '.netlify/edge-functions/tracing.server.js',
207207
...esbuild_config
208208
});
209-
builder.trace({
210-
entrypoint: '.netlify/edge-functions/render.js',
211-
tracing: '.netlify/edge-functions/tracing.server.js',
212-
tla: false
213-
});
209+
if (builder.hasServerTracingFile()) {
210+
builder.trace({
211+
entrypoint: '.netlify/edge-functions/render.js',
212+
tracing: '.netlify/edge-functions/tracing.server.js',
213+
tla: false
214+
});
215+
}
214216

215217
writeFileSync('.netlify/edge-functions/manifest.json', JSON.stringify(edge_manifest));
216218
}
@@ -288,12 +290,14 @@ function generate_lambda_functions({ builder, publish, split }) {
288290

289291
writeFileSync(`.netlify/functions-internal/${name}.mjs`, fn);
290292
writeFileSync(`.netlify/functions-internal/${name}.json`, fn_config);
291-
builder.trace({
292-
entrypoint: `.netlify/functions-internal/${name}.mjs`,
293-
tracing: '.netlify/server/tracing.server.js',
294-
start: `${name}.start.mjs`,
295-
exports: ['handler']
296-
});
293+
if (builder.hasServerTracingFile()) {
294+
builder.trace({
295+
entrypoint: `.netlify/functions-internal/${name}.mjs`,
296+
tracing: '.netlify/server/tracing.server.js',
297+
start: `${name}.start.mjs`,
298+
exports: ['handler']
299+
});
300+
}
297301

298302
const redirect = `/.netlify/functions/${name} 200`;
299303
redirects.push(`${pattern} ${redirect}`);
@@ -308,11 +312,13 @@ function generate_lambda_functions({ builder, publish, split }) {
308312

309313
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.json`, fn_config);
310314
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`, fn);
311-
builder.trace({
312-
entrypoint: `.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`,
313-
tracing: '.netlify/server/tracing.server.js',
314-
exports: ['handler']
315-
});
315+
if (builder.hasServerTracingFile()) {
316+
builder.trace({
317+
entrypoint: `.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`,
318+
tracing: '.netlify/server/tracing.server.js',
319+
exports: ['handler']
320+
});
321+
}
316322

317323
redirects.push(`* /.netlify/functions/${FUNCTION_PREFIX}render 200`);
318324
}

packages/adapter-node/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ export default function (opts = {}) {
5151
/** @type {Record<string, string>} */
5252
const input = {
5353
index: `${tmp}/index.js`,
54-
manifest: `${tmp}/manifest.js`,
55-
'tracing.server': `${tmp}/tracing.server.js`
54+
manifest: `${tmp}/manifest.js`
5655
};
5756

57+
if (builder.hasServerTracingFile()) {
58+
input['tracing.server'] = `${tmp}/tracing.server.js`;
59+
}
60+
5861
// we bundle the Vite output so that deployments only need
5962
// their production dependencies. Anything in devDependencies
6063
// will get included in the bundled code
@@ -94,11 +97,13 @@ export default function (opts = {}) {
9497
}
9598
});
9699

97-
builder.trace({
98-
entrypoint: `${out}/index.js`,
99-
tracing: `${out}/server/tracing.server.js`,
100-
exports: ['path', 'host', 'port', 'server']
101-
});
100+
if (builder.hasServerTracingFile()) {
101+
builder.trace({
102+
entrypoint: `${out}/index.js`,
103+
tracing: `${out}/server/tracing.server.js`,
104+
exports: ['path', 'host', 'port', 'server']
105+
});
106+
}
102107
},
103108

104109
supports: {

packages/adapter-vercel/index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ const plugin = function (defaults = {}) {
100100
MANIFEST: './manifest.js'
101101
}
102102
});
103-
builder.trace({
104-
entrypoint: `${tmp}/index.js`,
105-
tracing: `${builder.getServerDirectory()}/tracing.server.js`
106-
});
103+
if (builder.hasServerTracingFile()) {
104+
builder.trace({
105+
entrypoint: `${tmp}/index.js`,
106+
tracing: `${builder.getServerDirectory()}/tracing.server.js`
107+
});
108+
}
107109

108110
write(
109111
`${tmp}/manifest.js`,
@@ -178,26 +180,31 @@ const plugin = function (defaults = {}) {
178180
outfile: `${outdir}/index.js`,
179181
...esbuild_config
180182
});
181-
const instrumentation_result = await esbuild.build({
182-
entryPoints: [`${builder.getServerDirectory()}/tracing.server.js`],
183-
outfile: `${outdir}/tracing.server.js`,
184-
...esbuild_config
185-
});
186183

187-
builder.trace({
188-
entrypoint: `${outdir}/index.js`,
189-
tracing: `${outdir}/tracing.server.js`,
190-
tla: false
191-
});
184+
let instrumentation_result;
185+
if (builder.hasServerTracingFile()) {
186+
instrumentation_result = await esbuild.build({
187+
entryPoints: [`${builder.getServerDirectory()}/tracing.server.js`],
188+
outfile: `${outdir}/tracing.server.js`,
189+
...esbuild_config
190+
});
191+
192+
builder.trace({
193+
entrypoint: `${outdir}/index.js`,
194+
tracing: `${outdir}/tracing.server.js`,
195+
tla: false
196+
});
197+
}
192198

193-
if (result.warnings.length > 0 || instrumentation_result.warnings.length > 0) {
194-
const formatted = await esbuild.formatMessages(
195-
[...result.warnings, ...instrumentation_result.warnings],
196-
{
197-
kind: 'warning',
198-
color: true
199-
}
200-
);
199+
const warnings = instrumentation_result
200+
? [...result.warnings, ...instrumentation_result.warnings]
201+
: result.warnings;
202+
203+
if (warnings.length > 0) {
204+
const formatted = await esbuild.formatMessages(warnings, {
205+
kind: 'warning',
206+
color: true
207+
});
201208

202209
console.error(formatted.join('\n'));
203210
}

packages/kit/src/core/adapt/builder.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ export function create_builder({
236236
return copy(`${config.kit.outDir}/output/server`, dest);
237237
},
238238

239+
hasServerTracingFile() {
240+
return existsSync(`${config.kit.outDir}/output/server/tracing.server.js`);
241+
},
242+
239243
trace({ entrypoint, tracing, start = 'start.js', tla = true, exports = ['default'] }) {
240-
if (!config.kit.experimental.tracing.server) {
241-
return;
242-
}
243244
if (!existsSync(tracing)) {
244245
throw new Error(
245246
`Tracing file ${tracing} not found. This is probably a bug in your adapter.`

packages/kit/src/exports/public.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,23 @@ export interface Builder {
194194
}
195195
) => string[];
196196

197+
/**
198+
* Check if the server tracing file exists.
199+
* @returns true if the server tracing file exists, false otherwise
200+
*/
201+
hasServerTracingFile: () => boolean;
202+
197203
/**
198204
* Trace `entrypoint` with `tracing`.
199205
*
200-
* If server tracing is enabled, renames `entrypoint` to `start` and creates a new module at
206+
* Renames `entrypoint` to `start` and creates a new module at
201207
* `entrypoint` which imports `tracing` and then dynamically imports `start`. This allows
202208
* the module hooks necessary for tracing libraries to be loaded prior to any application code.
203209
*
204210
* Caveats:
205211
* - "Live exports" will not work. If your adapter uses live exports, your users will need to manually import the server instrumentation on startup.
206212
* - If `tla` is `false`, OTEL auto-instrumentation may not work properly. Use it if your environment supports it.
213+
* - Use {@link hasServerTracingFile} to check if the user has a server tracing file; if they don't, you shouldn't do this.
207214
*
208215
* @param options an object containing the following properties:
209216
* @param options.entrypoint the path to the entrypoint to trace.

packages/kit/src/exports/vite/dev/index.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs, { existsSync } from 'node:fs';
1+
import fs from 'node:fs';
22
import path from 'node:path';
33
import process from 'node:process';
44
import { URL } from 'node:url';
@@ -502,7 +502,10 @@ export async function dev(vite, vite_config, svelte_config) {
502502
return;
503503
}
504504

505-
await load_module_if_exists(vite, tracing.server);
505+
const server_tracing = resolve_entry(tracing.server);
506+
if (server_tracing) {
507+
await vite.ssrLoadModule(server_tracing);
508+
}
506509

507510
// we have to import `Server` before calling `set_assets`
508511
const { Server } = /** @type {import('types').ServerModule} */ (
@@ -663,19 +666,3 @@ function has_correct_case(file, assets) {
663666

664667
return false;
665668
}
666-
667-
/**
668-
* @param {import('vite').ViteDevServer} vite
669-
* @param {string} path
670-
* @returns {Promise<void>}
671-
*/
672-
async function load_module_if_exists(vite, path) {
673-
let extless_path = path;
674-
if (extless_path.endsWith('.js') || extless_path.endsWith('.ts')) {
675-
extless_path = extless_path.slice(-3);
676-
}
677-
678-
if (existsSync(`${extless_path}.js`) || existsSync(`${extless_path}.ts`)) {
679-
await vite.ssrLoadModule(extless_path);
680-
}
681-
}

packages/kit/src/exports/vite/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import prerender from '../../core/postbuild/prerender.js';
2323
import analyse from '../../core/postbuild/analyse.js';
2424
import { s } from '../../utils/misc.js';
2525
import { hash } from '../../utils/hash.js';
26-
import { dedent, isSvelte5Plus, write } from '../../core/sync/utils.js';
26+
import { dedent, isSvelte5Plus } from '../../core/sync/utils.js';
2727
import {
2828
env_dynamic_private,
2929
env_dynamic_public,
@@ -740,17 +740,13 @@ Tips:
740740
});
741741

742742
// ...and the server tracing file
743-
const instrument_server = resolve_entry(kit.files.tracing.server);
744-
if (instrument_server) {
743+
const server_tracing = resolve_entry(kit.files.tracing.server);
744+
if (server_tracing) {
745745
const { adapter } = kit;
746746
if (adapter && !adapter.supports?.tracing?.()) {
747-
throw new Error(`${instrument_server} is unsupported in ${adapter.name}.`);
747+
throw new Error(`${server_tracing} is unsupported in ${adapter.name}.`);
748748
}
749-
input['tracing.server'] = instrument_server;
750-
} else {
751-
const generated_path = `${kit.outDir}/generated/server/tracing.server.js`;
752-
write(generated_path, 'export {}');
753-
input['tracing.server'] = generated_path;
749+
input['tracing.server'] = server_tracing;
754750
}
755751

756752
// ...and every .remote file

packages/kit/types/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,23 @@ declare module '@sveltejs/kit' {
171171
}
172172
) => string[];
173173

174+
/**
175+
* Check if the server tracing file exists.
176+
* @returns true if the server tracing file exists, false otherwise
177+
*/
178+
hasServerTracingFile: () => boolean;
179+
174180
/**
175181
* Trace `entrypoint` with `tracing`.
176182
*
177-
* If server tracing is enabled, renames `entrypoint` to `start` and creates a new module at
183+
* Renames `entrypoint` to `start` and creates a new module at
178184
* `entrypoint` which imports `tracing` and then dynamically imports `start`. This allows
179185
* the module hooks necessary for tracing libraries to be loaded prior to any application code.
180186
*
181187
* Caveats:
182188
* - "Live exports" will not work. If your adapter uses live exports, your users will need to manually import the server instrumentation on startup.
183189
* - If `tla` is `false`, OTEL auto-instrumentation may not work properly. Use it if your environment supports it.
190+
* - Use {@link hasServerTracingFile} to check if the user has a server tracing file; if they don't, you shouldn't do this.
184191
*
185192
* @param options an object containing the following properties:
186193
* @param options.entrypoint the path to the entrypoint to trace.

0 commit comments

Comments
 (0)