getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.77k stars 1.52k forks source link

Problems with Next.js SDK + OTEL #11046

Open lforst opened 4 months ago

lforst commented 4 months ago

Just a collection of things I noticed when trying out the Next.js SDK with our newest OTEL implementation. This list is WIP:

Findings:

Problems:

s1gr1d commented 3 months ago

I created a Next.js test application based on this PR where I excluded our HTTP integration. I tried different scenarios (httpIntegration added/excluded in SDK, Next.js version in test app, manually adding startSpan to client function or server route handler).

I will differentiate between up until Next.js v14.0.1-canary.0 and from v14.0.1-canary.1 ongoing, because that made the biggest difference in the outcome.

As it is about checking the spans created by route handlers, most of the logic and console.logs where happening in packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts. In the test application, I fetched GET /api/delayed-res/3 (route handler) with a button click in a client component.

Until Next.js 14.0.1-canary.0 (also v13)

When fetching, the rootSpan is defined and looks something like this:

 {
  "span_id": "073d3f27a455bf13",
  "trace_id": "85d3b8afd4e840cf9d2f79e496508240",
  "data": {
    "next.span_name": "GET /api/delayed-res/3",
    "next.span_type": "BaseServer.handleRequest",
    "http.method": "GET",
    "http.target": "/api/delayed-res/3",
    "sentry.sample_rate": 1,
    "sentry.parentIsRemote": true
  },
  "description": "GET /api/delayed-res/3",
  "parent_span_id": "a763e5c6d28d2bc7",
  "start_timestamp": 1712306141.038
}

The traces show up correctly (except for the parameter) in Spotlight.

With a startSpan wrapper on the route handler: image

Without a startSpan wrapper: image

When including the httpIntegration, the spans are not sampled (just like how it is after v14.0.1-canary.1) and the root span also looks like how it looks there.

After Next.js 14.0.1-canary.1

When fetching, rootSpan is defined and looks like this:

{
 "span_id": "e450a61e70c6d723",
  "trace_id": "aa4b89228bce4a5481bb73f672ea3342"
}

Spotlight does not show any traces coming in. Wrapping the route handler code in a startSpan doesn't show traces either.

Only when adding startSpan around the client function which fetches the server route, the spans pop up in Spotlight. BUT they are missing the parent and the three requests seem to have the same trace ID.

image_1712306421891_0