newrelic / newrelic-browser-agent

New Relic Browser Agent
Apache License 2.0
69 stars 38 forks source link

Cannot read properties of undefined (reading 'setCustomAttribute') #1057

Open DrewML opened 1 month ago

DrewML commented 1 month ago

(Moved from https://github.com/newrelic/node-newrelic/issues/2238)

Description

I'm using the newrelic@11.12.0 package in a node.js @ 20.11.0 web app. This project uses newrelic.getBrowserTimingHeader() to load the browser SDK for clients. According to the inlined code, I think the version of the browser agent loaded is 1.260.1

Although it's not recommended, this app uses another 3rd party client-side error monitoring tool in addition to New Relic. Through this 3rd party tool, I've noticed the New Relic browser SDK occasionally throwing the exception Cannot read properties of undefined (reading 'setCustomAttribute').

The stack trace reported by the 3rd party tool starts at my app's helper function, setNewRelicAttribute. The body of that function is a tiny wrapper around window.newrelic.setCustomAttribute(key, val) with some safety checks to make sure newrelic is present on window.

image

To try and narrow down a culprit for the exception, I set a breakpoint in my setNewRelicAttribute function in the browser, and walked through the calls into the browser agent, trying to match stack frames from the browser and from the 3rd party tool to their source on Github. Here's the trail I found into the browser agent:

  1. First call into the browser agent hits the inline function in src/loaders/api/api.js used to expose public API methods from the agent
  2. Second call hits the caller function
  3. 3rd call is inside of the forEach loop iterating over each initialized agent

Based on this trail, I suspect that the check for val.exposed && val.api[fnName] is throwing an exception due to val.api being undefined at that point in execution for some reason, for some clients.

Expected Behavior

Calls to window.newrelic.setCustomAttribute should not throw an exception

Troubleshooting or NR Diag results

Steps to Reproduce

I've not yet been able to replicate the exception myself. Based on events in the 3rd party tool, this seems to predominantly impact clients using Chrome on Windows.

Additional Info

This isn't a critical issue since I can just wrap the body of the setNewRelicAttribute wrapper function in a try/catch for the time being.

We'll also try updating to the latest version of the agent in the near future to rule out a fix already being main.

workato-integration[bot] commented 1 month ago

https://new-relic.atlassian.net/browse/NR-276448

patrickhousley commented 1 week ago

Hi @DrewML could you verify which version of the browser agent you are seeing this issue? Did you confirm there is a trend showing this is mainly Chrome on Windows? Is it a specific version of Chrome? The code in question is synchronous and executes immediately when the agent is injected to the browser HTML. We could make a small change to verify val.api but I don't know that we could test the issue since the code is synchronous.

patrickhousley commented 5 days ago

Hi @DrewML We have not received a reply to the last message asking to confirm the version of the browser agent. We also recently pushed out an update and you should be getting that. Let me know if you are still having this issue. If you are, can you provide some info around timing of your call to newrelic.setCustomAttribute? Is that happening after the browser agent script? If your site is public, a link to it would also be helpful if you are comfortable providing that here. Thanks.

cwli24 commented 4 days ago

Planning to add safeguard to this code; would still like further information on how this is reproduced if possible.