@open-infra/telemetry-nextjs
v0.2.0
Published
Next.js instrumentation hooks and client navigation integration for OpenInfra telemetry.
Maintainers
Readme
@open-infra/telemetry-nextjs
Next.js Node-runtime instrumentation and route-handler enrichment, plus an isolated client entrypoint. Uses the existing OpenInfra server/browser runtimes and enriches active HTTP spans without starting a competing provider.
// src/instrumentation.ts: keep Node SDK imports behind the runtime check.
let telemetry: Awaited<ReturnType<typeof load>> | undefined;
async function load() {
const { createNextTelemetry } = await import('@open-infra/telemetry-nextjs');
return createNextTelemetry({ config: { serviceName: 'next-app', instrumentations: { auto: 'http' } } });
}
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
telemetry ??= await load();
await telemetry.register();
}
}
export async function onRequestError(...args: Parameters<NonNullable<typeof telemetry>['onRequestError']>) {
await telemetry?.onRequestError(...args);
}Wrap App Router handlers with withNextTelemetry(handler, { route: '/orders/[id]' }); retain the same request, response, context, and thrown errors. Use route templates rather than raw URLs or user identifiers. Handler wrapping enriches existing spans; initialize server telemetry first. Reuse an existing runtime via the runtime option.
Import NextTelemetryProvider and NextNavigationTelemetry from @open-infra/telemetry-nextjs/client in a client component. Provide the existing browser runtime/config and a stable routeName(pathname) mapper such as /orders/[id]. Collection remains gated by browser consent. The server entrypoint is Node-only; Edge execution is not supported by this adapter. Tested against Next.js 16.3.8; peer range also permits Next.js 15.
Official hooks: https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation
