open-telemetry / opentelemetry-js

OpenTelemetry JavaScript Client
https://opentelemetry.io
Apache License 2.0
2.56k stars 742 forks source link

@opentelemetry/context-zone Maximum call stack size exceeded occurs when the Redux Tool Chrome Extension is enabled for Next v13.5.4 #4213

Open WeirShi opened 8 months ago

WeirShi commented 8 months ago

What happened?

image

OpenTelemetry Setup Code

import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'
import { Resource } from '@opentelemetry/resources'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { ZoneContextManager } from '@opentelemetry/context-zone'
import { registerInstrumentations } from '@opentelemetry/instrumentation

const provider = new WebTracerProvider()
provider.register({
  contextManager: new ZoneContextManager(),
})

package.json

"dependencies": {
    "@opentelemetry/api": "^1.4.0",
    "@opentelemetry/context-zone": "^1.9.1",
    "@opentelemetry/core": "^1.13.0",
    "@opentelemetry/exporter-trace-otlp-http": "^0.35.1",
    "@opentelemetry/instrumentation": "^0.39.1",
    "@opentelemetry/instrumentation-fetch": "^0.39.1",
    "@opentelemetry/instrumentation-xml-http-request": "^0.39.1",
    "@opentelemetry/otlp-transformer": "^0.39.1",
    "@opentelemetry/resources": "^1.13.0",
    "@opentelemetry/sdk-trace-web": "^1.13.0",
    "@opentelemetry/semantic-conventions": "^1.13.0"
  }

Relevant log output

No response

dyladan commented 8 months ago

The provided screenshot is too low resolution to read and appears to be minified code. Can you please copy and paste a text version of the stacktrace?

Can you also try using the stack context manager and see if the bug still persists? This context manager is not recommended for production but should help us to eliminate variables for debugging.

WeirShi commented 8 months ago

This is the stack error message

RangeError: Maximum call stack size exceeded at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:73933) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483)

and May I ask if there is any other package to replace @opentelemetry/context-zone?
Thanks
dyladan commented 8 months ago

normalizeRouterState is not opentelemetry code. This might be caused by the extension. Can you please provide a reproduction so we can look into this further?

dyladan commented 8 months ago

What is making you think this is caused by the context manager? I don't see reference to it in the stacktrace or error message?

WeirShi commented 8 months ago

Demo

This is a Next.js demo project that includes a secondary development of sdk.js based on OpenTelemetry and imports the "context" package. In Next.js, if you load this project using the "Script beforeInteractive" or "Script afterInteractive" modes during production builds and with Redux DevTools enabled, it will result in an error stating "Maximum call stack size exceeded.". However, if you load the project using the "lazyOnload" mode, this issue should not occur.

MSNev commented 8 months ago

with Redux DevTools enabled, it will result in an error stating "Maximum call stack size exceeded.". However, if you load the project using the "lazyOnload" mode, this issue should not occur.

This REALLY does sound like the DevTools is adding something to the base prototype (of some class) that has a recursive loop (back to a parent object) so that when the recursive code attempt to traverse the object tree it goes forever.

And based on the normalizeRouterState in the recursive call stack this seems to be what that function is doing.

So it seems like the DevTools is seeing something that the Context Manager is adding that triggeres this scenario -- exactly what! Well thats the million dollar question. Can you (@WeirShir) do some debugging and identify what that normalizeRouterState is repeating on as this will help identify "who" is the culprit. I suspect a fix will be required in the normalizeRouterState and/or the DevTools and not (specifically) in the Context Manager.

WeirShi commented 8 months ago

This sounds like an issue I need to raise with Redux Tool officials.

markerikson commented 8 months ago

FWIW normalizeRouterState is part of Next's use of the Redux DevTools:

As far as I know, the Redux DevTools don't do anything to "mess with prototypes".

icyJoseph commented 7 months ago

Really interesting... in this stackblitz, https://stackblitz.com/edit/github-wywdtb?file=src%2Fapp%2Flayout.tsx

You can npm i, and then npm run build && npx serve out - and you'll see in the console the issues.

What seems to happen is that the object prototype is polluted, or somehow there's a self-referential object that just stacks up calls to the normalizeRouterState function.

I am more on the first option:

Screenshot 2023-11-29 at 16 32 45

Why would the zone object end up in the Next.js redux dev tools code 🤔 it has to be pollution right? Note that this reproduction repository uses Next.js' export mode as well.

icyJoseph commented 7 months ago

Once you get to this point of the debugging, past the __PAGE__ stuff from RSC, there's no way back, it'll just stack up calls to normalizeRouterState 🤔

Screenshot 2023-11-29 at 16 36 35 Screenshot 2023-11-29 at 16 38 39
dyladan commented 3 months ago

This sounds like an issue I need to raise with Redux Tool officials

@WeirShi did you ever raise this with redux tools?

markerikson commented 3 months ago

@dyladan per above this appears to be an issue with Next's code, not the Redux DevTools

dyladan commented 3 months ago

Sorry. I guess the more pertinent question is "can this be closed?" It doesn't seem to be caused by anything we're doing