Skip to content

feat: tracing.server.ts #14117

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ export default function (options = {}) {
ASSETS: assets_binding
}
});
if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: worker_dest,
tracing: `${builder.getServerDirectory()}/tracing.server.js`,
tla: false
});
}

// _headers
if (existsSync('_headers')) {
Expand Down Expand Up @@ -184,7 +191,8 @@ export default function (options = {}) {
}

return true;
}
},
tracing: () => true
}
};
}
Expand Down
50 changes: 45 additions & 5 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @import { BuildOptions } from 'esbuild' */
import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join, resolve, posix } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -106,7 +107,8 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
}

return true;
}
},
tracing: () => true
}
};
}
Expand Down Expand Up @@ -174,9 +176,8 @@ async function generate_edge_functions({ builder }) {
version: 1
};

await esbuild.build({
entryPoints: [`${tmp}/entry.js`],
outfile: '.netlify/edge-functions/render.js',
/** @type {BuildOptions} */
const esbuild_config = {
bundle: true,
format: 'esm',
platform: 'browser',
Expand All @@ -194,7 +195,29 @@ async function generate_edge_functions({ builder }) {
// https://docs.netlify.com/edge-functions/api/#runtime-environment
external: builtinModules.map((id) => `node:${id}`),
alias: Object.fromEntries(builtinModules.map((id) => [id, `node:${id}`]))
});
};
await Promise.all([
esbuild.build({
entryPoints: [`${tmp}/entry.js`],
outfile: '.netlify/edge-functions/render.js',
...esbuild_config
}),
builder.hasServerTracingFile() &&
esbuild.build({
entryPoints: [`${builder.getServerDirectory()}/tracing.server.js`],
outfile: '.netlify/edge/tracing.server.js',
...esbuild_config
})
]);

if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: '.netlify/edge-functions/render.js',
tracing: '.netlify/edge/tracing.server.js',
tla: false,
start: '.netlify/edge/start.js'
});
}

writeFileSync('.netlify/edge-functions/manifest.json', JSON.stringify(edge_manifest));
}
Expand Down Expand Up @@ -272,6 +295,14 @@ function generate_lambda_functions({ builder, publish, split }) {

writeFileSync(`.netlify/functions-internal/${name}.mjs`, fn);
writeFileSync(`.netlify/functions-internal/${name}.json`, fn_config);
if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: `.netlify/functions-internal/${name}.mjs`,
tracing: '.netlify/server/tracing.server.js',
start: `.netlify/functions-start/${name}.start.mjs`,
exports: ['handler']
});
}

const redirect = `/.netlify/functions/${name} 200`;
redirects.push(`${pattern} ${redirect}`);
Expand All @@ -286,6 +317,15 @@ function generate_lambda_functions({ builder, publish, split }) {

writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.json`, fn_config);
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`, fn);
if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: `.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`,
tracing: '.netlify/server/tracing.server.js',
start: `.netlify/functions-start/${FUNCTION_PREFIX}render.start.mjs`,
exports: ['handler']
});
}

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

Expand Down
26 changes: 21 additions & 5 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ export default function (opts = {}) {

const pkg = JSON.parse(readFileSync('package.json', 'utf8'));

/** @type {Record<string, string>} */
const input = {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
};

if (builder.hasServerTracingFile()) {
input['tracing.server'] = `${tmp}/tracing.server.js`;
}

// we bundle the Vite output so that deployments only need
// their production dependencies. Anything in devDependencies
// will get included in the bundled code
const bundle = await rollup({
input: {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
},
input,
external: [
// dependencies could have deep exports, so we need a regex
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
Expand Down Expand Up @@ -89,10 +96,19 @@ export default function (opts = {}) {
ENV_PREFIX: JSON.stringify(envPrefix)
}
});

if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: `${out}/index.js`,
tracing: `${out}/server/tracing.server.js`,
exports: ['path', 'host', 'port', 'server']
});
}
},

supports: {
read: () => true
read: () => true,
tracing: () => true
}
};
}
45 changes: 38 additions & 7 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @import { BuildOptions } from 'esbuild' */
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
Expand Down Expand Up @@ -93,13 +94,18 @@ const plugin = function (defaults = {}) {
const dir = `${dirs.functions}/${name}.func`;

const relativePath = path.posix.relative(tmp, builder.getServerDirectory());

builder.copy(`${files}/serverless.js`, `${tmp}/index.js`, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
if (builder.hasServerTracingFile()) {
builder.trace({
entrypoint: `${tmp}/index.js`,
tracing: `${builder.getServerDirectory()}/tracing.server.js`
});
}

write(
`${tmp}/manifest.js`,
Expand Down Expand Up @@ -136,9 +142,9 @@ const plugin = function (defaults = {}) {
);

try {
const result = await esbuild.build({
entryPoints: [`${tmp}/edge.js`],
outfile: `${dirs.functions}/${name}.func/index.js`,
const outdir = `${dirs.functions}/${name}.func`;
/** @type {BuildOptions} */
const esbuild_config = {
// minimum Node.js version supported is v14.6.0 that is mapped to ES2019
// https://edge-runtime.vercel.app/features/polyfills
// TODO verify the latest ES version the edge runtime supports
Expand Down Expand Up @@ -168,10 +174,34 @@ const plugin = function (defaults = {}) {
'.eot': 'copy',
'.otf': 'copy'
}
};
const result = await esbuild.build({
entryPoints: [`${tmp}/edge.js`],
outfile: `${outdir}/index.js`,
...esbuild_config
});

if (result.warnings.length > 0) {
const formatted = await esbuild.formatMessages(result.warnings, {
let instrumentation_result;
if (builder.hasServerTracingFile()) {
instrumentation_result = await esbuild.build({
entryPoints: [`${builder.getServerDirectory()}/tracing.server.js`],
outfile: `${outdir}/tracing.server.js`,
...esbuild_config
});

builder.trace({
entrypoint: `${outdir}/index.js`,
tracing: `${outdir}/tracing.server.js`,
tla: false
});
}

const warnings = instrumentation_result
? [...result.warnings, ...instrumentation_result.warnings]
: result.warnings;

if (warnings.length > 0) {
const formatted = await esbuild.formatMessages(warnings, {
kind: 'warning',
color: true
});
Expand Down Expand Up @@ -477,7 +507,8 @@ const plugin = function (defaults = {}) {
}

return true;
}
},
tracing: () => true
}
};
};
Expand Down
Loading
Loading