refactor(instrumentation): add browser-native InstrumentationBase and migrate instrumentations - #278
Draft
overbalance wants to merge 1 commit into
Conversation
martinkuba
reviewed
May 14, 2026
martinkuba
reviewed
May 14, 2026
| */ | ||
| export abstract class InstrumentationBase< | ||
| ConfigType extends InstrumentationConfig = InstrumentationConfig, | ||
| > extends CoreInstrumentationBase<ConfigType> { |
Contributor
There was a problem hiding this comment.
I wonder if we could get rid of this core dependency altogether. (Maybe a separate PR)
Contributor
Author
There was a problem hiding this comment.
I refactored entirely to remove it. See the description for more details re: Node.
martinkuba
reviewed
May 14, 2026
overbalance
force-pushed
the
overbalance/unify-instrumentation-enable-disable
branch
2 times, most recently
from
June 5, 2026 00:15
cb0c9fd to
4782d17
Compare
overbalance
force-pushed
the
overbalance/unify-instrumentation-enable-disable
branch
from
June 5, 2026 16:29
4782d17 to
32bf3f8
Compare
overbalance
force-pushed
the
overbalance/unify-instrumentation-enable-disable
branch
from
August 25, 2026 06:40
32bf3f8 to
f799326
Compare
… migrate instrumentations Signed-off-by: overbalance <overbalance@users.noreply.github.com>
overbalance
force-pushed
the
overbalance/unify-instrumentation-enable-disable
branch
from
September 4, 2026 05:29
f799326 to
33f8251
Compare
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which problem is this PR solving?
Every instrumentation in this package extends
InstrumentationBasefrom@opentelemetry/instrumentation. That base class is built for Node.js. It patches modules throughrequire-in-the-middle, and it asks each instrumentation for module definitions in aninit()method.A browser has no module system to patch. So all of that code does nothing here. It only adds weight.
There are two more problems:
@opentelemetry/instrumentationNode-only. The browser shim is a temporary workaround. See thenoop-normalizenote that points to [Instrumentation] Cannot build web project using webpack due to reference to nodejs core package (path) opentelemetry-js#4373.This PR gives the package its own base class for browsers.
Short description of the changes
Add
src/InstrumentationBase/. The new class implements the publicInstrumentationinterface, soregisterInstrumentationsstill accepts these objects. At runtime it needs only@opentelemetry/apiand@opentelemetry/api-logs.It keeps what the instrumentations use:
_wrapIt drops all Node module patching:
init(),getModuleDefinitionsandrequire-in-the-middle.enable()anddisable()stay abstract.Wrap only, never unwrap
The class has
_wrapbut no_unwrapand no_massUnwrap.Restoring a patched browser API is not safe. Other code may have wrapped the same method after us, and there is no reliable way to tear that down. So this package does not allow it. Leaving the method out enforces the rule by construction.
This changes what
disable()means. It stops emission. It does not restore the global. An instrumentation can still remove its own listeners, its ownPerformanceObserverand its ownAbortController. That is unrelated to unwrapping.Other changes
init()override.safeExecuteInTheMiddleintosrc/utils/. It was the only runtime helper the instrumentations still took from@opentelemetry/instrumentation.@opentelemetry/instrumentation,InstrumentationandInstrumentationConfig. No runtime use of itsInstrumentationBaseis left. One test still importsregisterInstrumentationsfrom it.#instrumentation-baseto theimportsmap inpackage.json.xhr: make a retry safe
XhrInstrumentation.enable()wrapsopenfirst, thensend. Ifsendfails because another script locked it,openstays wrapped. The old code called_unwraphere, and that method is now gone.Leaving the patch in place is safe.
enable()returns before it sets_isEnabled, so the wrapper passes every call straight through.But a retry was not safe.
_isXhrPatchedis set only after both methods are wrapped, so a secondenable()call wrappedopenagain. Each retry added another layer, and no layer could be removed. A new_isOpenPatchedflag now guards theopenwrap, so a retry does not add a layer.Public API
No change. Constructors, config types and behavior stay the same.
Type of change
How Has This Been Tested?
npm run check(tsc and eslint) is clean across the workspace. This includes theexamples-*andsandboxconsumers, which confirms the public API did not change.biome checkis clean.The full vitest suite passes: 324 tests in
browser-instrumentationand 64 inbrowser-sdk. Every instrumentation now runs against the new base.fetch,xhrandweb-vitalsrun in real Chromium.New
InstrumentationBase.test.tscovers:enabledisfalsesetConfigapplies the default_wrapinstalls in place and keeps the originalsetTracerProviderswaps the tracer_unwrapNew xhr tests cover the case where
openis wrapped andsendthen fails. They check that:openpatch stays installedTo make
sendfail for real, the test defines it aswritable: falseand keeps it configurable. The test can then undo it afterward.Fixed a test isolation leak in the console test. Patches are install only, so one shared mock
consolecollected a wrapper from every test and leaked records between them. Each test now starts from a freshconsole.Checklist: