Skip to content

[OpenTelemetry] Tracing limitations in middleware #80445

@r34son

Description

@r34son

Link to the code that reproduces this issue

https://github.com/r34son/nextjs-handle-request-span-repro

To Reproduce

  1. Start Jaeger: pnpm jaeger
  2. Run app in second terminal: pnpm dev
  3. Open http://localhost:3000 in browser
  4. View traces at http://localhost:16686

Current vs. Expected behavior

Current Behavior

  1. Trace Fragmentation:

    • Middleware and page executions appear as completely separate traces in Jaeger:
Image - Example: Request to `/` shows:
 - One trace for middleware with `next.span_type` "BaseServer.handleRequest":
Image
 - Separate trace for page render with `next.span_type` "BaseServer.handleRequest":
Image
  1. Edge Runtime Middleware Limitations:
    • Edge middleware shows no OpenTelemetry spans (no custom, and fetch) - see screen above
    • Only Node middleware produces traces (you can repro on node-middleware branch):
Image

Expected Behavior

  1. Unified Tracing:

    • Single end-to-end trace for each request
  2. Edge Runtime Support:

    • Consistent tracing capabilities across Edge and Node runtimes
    • Basic span creation and instrumentation in Edge middleware

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:39 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 22.15.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 10.9.0
Relevant Packages:
  next: 15.3.2 // Latest available version is detected (15.3.2).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Trying to add custom logging for requests. See https://github.com/r34son/nextjs-handle-request-span-repro/blob/main/access-log-span-processor.ts. But middleware and page rendering generate separate traces in OpenTelemetry, rather than a single unified trace for the complete request lifecycle.

Related:
#67737
#47660
#78926
#51231
#51233
#46543

Metadata

Metadata

Assignees

No one assigned

    Labels

    InstrumentationRelated to Next.js Instrumentation.MiddlewareRelated to Next.js Middleware.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions