DataDog / browser-sdk

Datadog Browser SDK
Apache License 2.0
302 stars 131 forks source link

🐛 Some requests being processed as both XHR and Fetch, produces duplicate headers #2940

Open ValerieNayak opened 2 months ago

ValerieNayak commented 2 months ago

Describe the bug A clear and concise description of what the bug is and on which product (rum, logs).

This issue occurs on rum. Some requests generate headers in both traceXhr and traceFetch. These requests have duplicate headers.

x-datadog-origin: rum, rum
x-datadog-parent-id: 332227412463181825, 1741862826606084281
x-datadog-sampling-priority: 0, 0
x-datadog-trace-id: 916912013437384357, 7469972749533929927

To Reproduce Steps to reproduce the behavior:

We know the steps are being processed through both traceXhr and traceFetch, but we don't have a reproduction at this point.

Expected behavior A clear and concise description of what you expected to happen.

We expect that a single request outputs a single trace ID in request headers.

cy-moi commented 2 months ago

Hi @ValerieNayak , Would you mind to give us your init script or process (NPM, CDN async, etc.)? It looks like multiple sdks have been initiated. If you need help on setting up, do not hesitate to reach out to Datadog Support.

ValerieNayak commented 2 months ago

Hi @cy-moi, thanks for your reply. We are initializing using NPM. We have already ruled out multiple initiations because we set a breakpoint at the init step in the code and confirmed that it is only being hit once. With additional breakpoints, we've identified that a single request is being traced through both traceFetch and traceXhr.

ValerieNayak commented 2 months ago

Upon further investigation, we've seen that we're using isomorphic-fetch and this turns a fetch request into an xhr request. According to MDN dev docs on XMLHttpRequest: setRequestHeader() method,

If this method is called several times with the same header, the values are merged into one single request header.

Could we have it so Datadog is not double tracing in this case?

cy-moi commented 1 month ago

Hi @ValerieNayak ,

Thank you for reporting this. This is indeed a limitation of ours and we value your feedback very much. You can report a feature request with Datadog Support to help us prioritizing it.