-
Notifications
You must be signed in to change notification settings - Fork 487
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
fix(instr-express): keep hidden properties in layer handlers #2137
Changes from 1 commit
7152206
82b4b83
e35bc7b
04d2918
cb556b4
e0fb92e
8f2f66c
261b756
20d765c
618e80b
686c669
49fd215
aa93499
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 |
---|---|---|
|
@@ -165,8 +165,7 @@ export class ExpressInstrumentation extends InstrumentationBase< | |
) { | ||
const route = original.apply(this, args); | ||
const layer = this._router.stack[this._router.stack.length - 1]; | ||
instrumentation._applyPatch.call( | ||
instrumentation, | ||
instrumentation._applyPatch( | ||
layer, | ||
typeof args[0] === 'string' ? args[0] : undefined | ||
); | ||
|
@@ -189,7 +188,8 @@ export class ExpressInstrumentation extends InstrumentationBase< | |
this._wrap(layer, 'handle', (original: Function) => { | ||
// TODO: instrument error handlers | ||
if (original.length === 4) return original; | ||
return function ( | ||
|
||
const patched = function ( | ||
this: ExpressLayer, | ||
req: PatchedRequest, | ||
res: express.Response | ||
|
@@ -326,6 +326,25 @@ export class ExpressInstrumentation extends InstrumentationBase< | |
} | ||
} | ||
}; | ||
|
||
// `handle` isn't just a regular function in some cases. It also contains | ||
// some properties holding metadata and state so we need to proxy them | ||
// through through patched function | ||
// ref: https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1950 | ||
Object.keys(original).forEach((key) => { | ||
Object.defineProperty(patched, key, { | ||
blumamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
get() { | ||
// @ts-expect-error -- the original function has hidden props indexed by strings | ||
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. While I understand the purpose of what we're trying to achieve, the error that comes up here feels very relevant and makes me wonder if there's a better way to do this. 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. Thanks for your review :)
For "better" you meant at the type level or you think the usage of Type error
The internal typings define the We may get rid of the error by doing some improvements in the internal types so the compiler will know the function being patched is also used as a record. Something like the type below should work. type HandlerType = Function & Record <string,any> Object.defineProperty vs copy Doing the shallow copy proposed by @blumamir fixes this issue as well but those properties represents the state of the layer. If that state is modified for the 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. Ooh, that sounds like a good idea to use that new Also thank you for the detailed explanation, that helps me understand much better! 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.
I support doing this 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. I'll update the types :) 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. Done @JamieDanielson :) |
||
return original[key]; | ||
}, | ||
set(value) { | ||
// @ts-expect-error -- the original function has hidden props indexed by strings | ||
original[key] = value; | ||
} | ||
}) | ||
}); | ||
|
||
return patched; | ||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewer: this usage of
Function.protptype.call
comes from this commit 0723d06 as a new addition.I did not dig further to get the original implementation but I think there is no need to use
.call(...)
since we have access to the container object and therefore calling_applyPatch
with the right context.