-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(instr-fetch): avoid unwrap fetch API #6575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
402f56e
03ac4dc
05cf192
fad4bfe
3291938
847e9c4
57855d1
44c4bf7
861a4bf
29ded9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ import type { InstrumentationConfig } from '@opentelemetry/instrumentation'; | |
| import { | ||
| SemconvStability, | ||
| semconvStabilityFromStr, | ||
| isWrapped, | ||
| InstrumentationBase, | ||
| safeExecuteInTheMiddle, | ||
| } from '@opentelemetry/instrumentation'; | ||
|
|
@@ -111,6 +110,15 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati | |
|
|
||
| private _semconvStability: SemconvStability; | ||
|
|
||
| // Note: Intentionally *not* using `_enabled` as the field name to avoid | ||
| // any possible confusion with the `_enabled` field used on the *Node.js* | ||
| // InstrumentationBase class. | ||
| // Also not initializing the fields to `false` because the base class | ||
| // constructor already call `enable` modifying their values and it will | ||
| // set the instrumentaitons in a bas state (enabled, patched but with flags set to false) | ||
| declare private _isEnabled: boolean; | ||
| declare private _isFetchPatched: boolean; | ||
|
|
||
| constructor(config: FetchInstrumentationConfig = {}) { | ||
| super('@opentelemetry/instrumentation-fetch', VERSION, config); | ||
| this._semconvStability = semconvStabilityFromStr( | ||
|
|
@@ -382,6 +390,9 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati | |
| this: typeof globalThis, | ||
| ...args: Parameters<typeof fetch> | ||
| ): Promise<Response> { | ||
| if (!plugin._isEnabled) { | ||
| return original.apply(this, args); | ||
| } | ||
| const self = this; | ||
| const url = web.parseUrl( | ||
| args[0] instanceof Request ? args[0].url : String(args[0]) | ||
|
|
@@ -611,21 +622,31 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati | |
| ); | ||
| return; | ||
| } | ||
| if (isWrapped(fetch)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this guard still needed if there are two instances of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good questions :)
the
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. |
||
| this._unwrap(globalThis, 'fetch'); | ||
| this._diag.debug('removing previous patch for constructor'); | ||
|
|
||
| if (this._isEnabled) { | ||
| return; | ||
| } | ||
| this._isEnabled = true; | ||
|
|
||
| if (this._isFetchPatched) { | ||
| this._diag.debug('fetch constructor already patched'); | ||
| return; | ||
| } | ||
| this._isFetchPatched = true; | ||
| this._wrap(globalThis, 'fetch', this._patchConstructor()); | ||
| } | ||
|
|
||
| /** | ||
| * implements unpatch function | ||
| * deactivates fetch instrumentation | ||
| */ | ||
| override disable(): void { | ||
| if (!hasBrowserPerformanceAPI) { | ||
| return; | ||
| } | ||
| this._unwrap(globalThis, 'fetch'); | ||
| if (!this._isEnabled) { | ||
| return; | ||
| } | ||
| this._isEnabled = false; | ||
| this._usedResources = new WeakSet<PerformanceResourceTiming>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a small race condition here if calls are in flight because of the setTimeout above. Probably rare 🤷
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC this is already happening. The 1st that comes to mind is to reuse Also it seems |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.