Skip to content
Draft
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
6 changes: 6 additions & 0 deletions examples/tracing/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "nitro";

export default defineConfig({
serverDir: "./server",
tracing: true,
});
11 changes: 11 additions & 0 deletions examples/tracing/package.json
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"
}
}
1 change: 1 addition & 0 deletions examples/tracing/server/middleware/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => {};
37 changes: 37 additions & 0 deletions examples/tracing/server/plugins/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { definePlugin } from "nitro";

export default definePlugin(() => {});

// --- debug tracing channels ---

debugChannel("srvx.middleware");
debugChannel("srvx.fetch");
debugChannel("h3.fetch");

function debugChannel(name: string) {
const { tracingChannel } = process.getBuiltinModule("node:diagnostics_channel");

const log = (...args: unknown[]) => console.log(`[tracing:${name}]`, ...args);
const noop = () => {};
const serializeData = (data: any) =>
Object.entries(data)
.map(([key, value]) => {
if (!value) {
value = String(value);
} else if (value instanceof Response) {
value = `Response(${value.status} ${value.statusText})`;
} else if (typeof value === "object") {
value = `{${Object.keys(value).join(",")}}`;
}
return `${key}=${value}`;
})
.join(", ");

tracingChannel(name).subscribe({
start: noop,
end: noop,
asyncStart: (data) => log("asyncStart", serializeData(data)),
asyncEnd: (data) => log("asyncEnd", serializeData(data)),
error: (data) => log("error", serializeData(data)),
});
}
1 change: 1 addition & 0 deletions examples/tracing/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => "Index Page";
3 changes: 3 additions & 0 deletions examples/tracing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "nitro/tsconfig"
}
4 changes: 4 additions & 0 deletions examples/tracing/vite.config.ts
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()] });
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/build/virtual/_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import runtimeConfig from "./runtime-config.ts";
import serverAssets from "./server-assets.ts";
import storage from "./storage.ts";
import tasks from "./tasks.ts";
import tracing from "./tracing.ts";

type VirtualTemplate = {
id: string;
Expand All @@ -34,6 +35,7 @@ export function virtualTemplates(nitro: Nitro, _polyfills: string[]): VirtualTem
serverAssets,
storage,
tasks,
tracing,
].flatMap((t) => t(nitro, _polyfills));

const customTemplates = Object.entries(nitro.options.virtual).map(([id, template]) => ({
Expand Down
19 changes: 19 additions & 0 deletions src/build/virtual/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Nitro } from "nitro/types";

export default function tracing(nitro: Nitro) {
return {
id: "#nitro/virtual/tracing",
template: () => {
return /* js */ `
import { tracingPlugin as h3Tracing } from "h3/tracing";
import { tracingPlugin as srvxTracing } from 'srvx/tracing'

export const tracingSrvxPlugins = ${nitro.options.tracing!.srvx ? "[srvxTracing()]" : "[]"};

export default function tracing(nitroApp) {
h3Tracing()(nitroApp.h3);
};
`;
},
};
}
2 changes: 2 additions & 0 deletions src/config/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { resolveURLOptions } from "./resolvers/url.ts";
import { resolveErrorOptions } from "./resolvers/error.ts";
import { resolveUnenv } from "./resolvers/unenv.ts";
import { resolveBuilder } from "./resolvers/builder.ts";
import { resolveTracingOptions } from "./resolvers/tracing.ts";

const configResolvers = [
resolveCompatibilityOptions,
Expand All @@ -41,6 +42,7 @@ const configResolvers = [
resolveErrorOptions,
resolveUnenv,
resolveBuilder,
resolveTracingOptions,
] as const;

export async function loadOptions(
Expand Down
12 changes: 12 additions & 0 deletions src/config/resolvers/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { NitroOptions } from "nitro/types";

export async function resolveTracingOptions(options: NitroOptions) {
if (!options.tracing) return;
options.tracing = {
srvx: true,
h3: true,
...(typeof options.tracing === "object" ? options.tracing : {}),
};
options.plugins = options.plugins || [];
options.plugins.push("#nitro/virtual/tracing");
}
2 changes: 2 additions & 0 deletions src/presets/node/runtime/node-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNitroApp } from "nitro/app";
import { startScheduleRunner } from "#nitro/runtime/task";
import { trapUnhandledErrors } from "#nitro/runtime/error/hooks";
import { resolveWebsocketHooks } from "#nitro/runtime/app";
import { tracingSrvxPlugins } from "#nitro/virtual/tracing";

const _parsedPort = Number.parseInt(process.env.NITRO_PORT ?? process.env.PORT ?? "");
const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;
Expand All @@ -22,6 +23,7 @@ const server = serve({
hostname: host,
tls: cert && key ? { cert, key } : undefined,
fetch: nitroApp.fetch,
plugins: [...tracingSrvxPlugins],
});

if (import.meta._websocket) {
Expand Down
1 change: 1 addition & 0 deletions src/presets/standard/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const standard = defineNitroPreset(
"srvx/deno": "srvx/deno",
"srvx/node": "srvx/node",
"srvx/generic": "srvx/generic",
"srvx/tracing": "srvx/tracing",
},
},
{
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/virtual/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "./_runtime_warn.ts";

import type { NitroAppPlugin } from "nitro/types";

declare const plugin: NitroAppPlugin;
export default plugin;

export declare const tracingSrvxPlugins: Array<() => any>;
8 changes: 8 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface NitroOptions extends PresetOptions {
ssrRoutes: string[];
serveStatic: boolean | "node" | "deno" | "inline";
noPublicDir: boolean;
tracing?: undefined | TracingOptions;
manifest?: {
deploymentId?: string;
};
Expand Down Expand Up @@ -291,6 +292,7 @@ export interface NitroConfig
| "serverEntry"
| "renderer"
| "output"
| "tracing"
>
>,
C12InputConfig<NitroConfig> {
Expand All @@ -304,6 +306,7 @@ export interface NitroConfig
serverEntry?: string | NitroOptions["serverEntry"];
renderer?: false | NitroOptions["renderer"];
output?: Partial<NitroOptions["output"]>;
tracing?: boolean | TracingOptions;
}

// ------------------------------------------------------------
Expand Down Expand Up @@ -349,6 +352,11 @@ export interface ServerAssetDir {
ignore?: string[];
}

export interface TracingOptions {
srvx?: boolean;
h3?: boolean;
}

// Storage mounts
type CustomDriverName = string & { _custom?: any };
export interface StorageMounts {
Expand Down
Loading