Repository navigation
fix: keep an instance without a constructor working - #10
Open
johnnyhuirilef wants to merge 1 commit into
Open
johnnyhuirilef wants to merge 1 commit into
johnnyhuirilef wants to merge 1 commit into
Conversation
An instance with no `constructor` function threw a TypeError on its first method access once instrumentation was on. A null-prototype object and an ES module namespace are two such instances. The class name for the span came from `constructor.name` in two places: the `get` trap and `generateSpanId`. Both now read it through one helper. The helper falls back to `Object` when `constructor` is not a function.
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.
PR Checklist
PR Type
What is the current behavior?
Issue Number: N/A (issues are disabled on this repo)
Hi! 馃憢 With instrumentation on, a method call on a provider that has no
constructorfunction throwsTypeError: Cannot read properties of undefined (reading 'name'). In a route, this is a 500.The span class name comes from
constructor.name. The code reads it in two places insrc/instrument/create-instance-decorator.instrument.ts: thegettrap (line 271) andgenerateSpanId(line 570). A null-prototype object has noconstructor. An ES module namespace has none either, so{ provide: 'UTILS', useValue: utils }withimport * as utils from './utils.js'triggers it. The call fails in a trace and outside a trace. The same provider works without instrumentation.I ran the repro on 0.3.7:
Until a release has the fix,
skipInstrumentation: (instance) => instance === greetingskeeps the provider working. The repro README runs it.What is the new behavior?
classNameOf, gives the class name of an instance. Both places use it.constructoris a function with a name, the span keeps that name, as before.constructoris missing or is not a function, the span class name isObject.constructorthat is not a function used to name the spanundefined.fnfor{ constructor: 'x' }, orFake.fnfor{ constructor: { name: 'Fake' } }. Both now readObject.fn.Does this PR introduce a breaking change?
Providers with a named constructor keep their span names. Only the shapes that threw or had no usable constructor function change. A
constructorthat is not a function now givesObject, so{ constructor: { name: 'Fake' } }readsObject.fninstead ofFake.fn.Other information
The new unit specs call the decorator on these instances, with the same helpers as the other specs:
data:module, because a file in the repo goes through the Vitest transform and is no longer a native namespace.constructorthat is not a function, with anameproperty. This case isolates thetypeofcheck.They check the returned value, the started step and the ended span id, so both places are covered.
The new int spec boots a real Nest app with
instrument: ObserveInstrument. It registers a namespace as auseValueprovider and checks thatGET /greetreturns 200.All the new specs fail on
masterand pass with the fix. I also reverted each changed line one at a time, and a spec failed every time.Separate from this fix: a frozen object with methods also fails under instrumentation, plain or null-prototype. The
gettrap returns a wrapper for a read-only, non-configurable property, and the proxy invariant check rejects it. I ran it and left it out of this PR. I am happy to open a follow-up for it. 馃檪