fix(instr-fetch): avoid unwrap fetch API#6575
fix(instr-fetch): avoid unwrap fetch API#6575pichlermarc merged 10 commits intoopen-telemetry:mainfrom
Conversation
Codecov Report❌ Patch coverage is
❌ Your patch status has failed because the patch coverage (58.33%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #6575 +/- ##
==========================================
- Coverage 95.76% 95.72% -0.04%
==========================================
Files 375 375
Lines 12761 12769 +8
Branches 3025 3028 +3
==========================================
+ Hits 12220 12223 +3
- Misses 541 546 +5
🚀 New features to boost your workflow:
|
| ); | ||
| return; | ||
| } | ||
| if (isWrapped(fetch)) { |
There was a problem hiding this comment.
Is this guard still needed if there are two instances of fetch instrumentation loaded? I think it will double wrap and produce double the data if that happens
There was a problem hiding this comment.
Also, do we care if another piece of code on the page manages to unwrap fetch? We could re-apply the patch here if so
There was a problem hiding this comment.
good questions :)
Is this guard still needed if there are two instances of fetch instrumentation loaded? I think it will double wrap and produce double the data if that happens
the isWrapped function only tells if something has wrapped the method 1st. I could be the same instrumentation or another one. I'm not sure if there is a way to distinguish. So unwrapping is potentially breaking another instrumentation that wraps fetch.
Also, do we care if another piece of code on the page manages to unwrap fetch? We could re-apply the patch here if so
What comes to mind is another agent or library patching the function. If it uses shimmer I think we are okay but if not we are not 100% sure that the wrap was removed.
If the following code executes after enabling the instrumentation
const nativeFetch = globalThis.fetch; // this is the wrapped function
globalThis.fetch = function (...args) {
console.log('Fetch');
return nativeFetch.apply(this, args);
}now the global fetch function does not have the shimmer props (_wrapped, __original, __unwrap) but its still calling our wrapped transitively.
I'm thinking that maybe we should patch in a way that it cannot be undone and use the instrumentation state/config to control the behavior (which this PR does except it can be unpatched) and also that we cannot protect the users for their wrong doing like creating 2 instances of the same instrumentation.
| return; | ||
| } | ||
| this._isEnabled = false; | ||
| this._usedResources = new WeakSet<PerformanceResourceTiming>(); |
There was a problem hiding this comment.
There's a small race condition here if calls are in flight because of the setTimeout above. Probably rare 🤷
There was a problem hiding this comment.
IIUC this is already happening. The 1st that comes to mind is to reuse _clearResources private method which has a check for pending tasks... or just let the resources be cleared when called by the setTimeout callback.
Also it seems clearTimingResources has no default value which becomes undefined (falsy). So if not configured otherwise the instrumentation accumulates resources and I worry that this might lead to memory issues for web apps running for a long period of time. 🤔
…fetch.ts Co-authored-by: Jared Freeze <overbalance@users.noreply.github.com>
6bc69c7
Co-authored-by: Jared Freeze <overbalance@users.noreply.github.com> Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Which problem is this PR solving?
When fetch adn XHR instrumentations are unwrapping the browser APIs they may also remove any other patch added by another instrumentation. This PR makes the
fetchinstrumentation to avoid the unwrap and check on the enabled state of the instrumentation to decide to generate telemetry or notRefs: open-telemetry/opentelemetry-browser#204
Short description of the changes
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist: