Skip to content

[feature] Next.js: instrumentation.ts support #187

@mnismt

Description

@mnismt

Description

Hey! I recently added evlog in a side project - it was great, the wide events approach is great

One thing I ran into: there's no way to integrate with Next.js's instrumentation.ts. This file is how Next exposes two server-level hooks:

  • register(): runs once at server startup, before any requests
  • onRequestError(): called on every unhandled error across all routes, including SSR pages, RSC and middleware (now is proxy)

Right now withEvlog() only covers route handlers you explicitly wrap. Errors that happen outside that scope (SSR rendering failures, RSC errors, middleware crashes) are invisible to evlog -> never hit the drain

I ended up wiring this up manually in my project:

// instrumentation.ts
import { log } from '@/lib/evlog'

export function onRequestError(
  error: { digest: string } & Error,
  request: { path: string; method: string; headers: Record<string, string> },
  context: { routerKind: string; routePath: string; routeType: string; renderSource: string },
) {
  log.error({
    source: 'nextjs',
    error: { message: error.message, digest: error.digest, stack: error.stack },
    request: { path: request.path, method: request.method },
    route: { kind: context.routerKind, path: context.routePath, type: context.routeType },
    renderSource: context.renderSource,
  })
}

export async function register() {
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./instrumentation-node')
  }
}

It works, but it's all manual. No drain integration, no coexistence with createEvlog(), and the stdout/stderr capture needed its own re-entrancy guard. Would be nice to have first-class support for this.

P.S. I'll dig into the codebase and open a PR if that's welcome

Proposed Solution

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions