vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.63k stars 26.83k forks source link

OpenTelemetry Tracing is poluted with some extra spans from middlewares #67737

Open pksorensen opened 2 months ago

pksorensen commented 2 months ago

Link to the code that reproduces this issue

https://github.com/pksorensen/nextjs-repro-otel

To Reproduce

  1. configure som otel endpoint and replace .env
  2. Start npm run dev
  3. monitor traces

Current vs. Expected behavior

It is emiting this wierd trace with next.bubble | true which seems related to old bug https://github.com/vercel/next.js/issues/55752 where i dont fully get what is going on at image

image

The expected trace is there also image which has all the childs, image

so either that first one /da/ should be a child of /[locale] or not there at all ?

I think its related to also that middleware does not run with the same traceid that the request is doing.

I added

function withTraceParent(res: NextResponse) {
    const spanContext = trace.getActiveSpan()?.spanContext();
    if (spanContext)
        res.cookies.set("traceparent", `00-${spanContext.traceId}-${spanContext.spanId}-0${spanContext.traceFlags}`);
    return res;
};

to the middleware and can inspect that the traceparent set in cookie from within the middleware is totally different from those in the above request (either of the two base requests)

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise N
  Available memory (MB): 32687
  Available CPU cores: 16
Binaries:
  Node: 20.9.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.0.0-canary.65 // Latest available version is detected (15.0.0-canary.65).
  eslint-config-next: N/A
  react: 19.0.0-rc-6f23540c7d-20240528
  react-dom: 19.0.0-rc-6f23540c7d-20240528
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

Internationalization (i18n), Middleware

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

next dev (local), next start (local), Other (Deployed)

Additional context

No response

dir commented 1 week ago

I have also noticed these next.bubble spans today in my Google Cloud Trace explorer as I've been setting up OTel. Been a pretty rough ride getting it working.

Can also add that I am not using i18n, but I am using a basic Auth.js middleware setup.