-
Notifications
You must be signed in to change notification settings - Fork 643
feat: restify instrumentation #416
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 1 commit
0149bcf
96f73e6
4f920f5
92af233
3c37d7f
32b8931
ee64e4d
b6c8318
a46adb7
5d67867
c4657df
cbbdea0
c760527
4ca8036
e9ab22e
0dc22aa
6c380a2
3b8072b
fa55dab
ac8d41b
520d54e
b5be54c
14772ba
c8914d1
88f6052
df87efd
4b58434
c4dee5c
f8bcb5c
fd906b1
f4f4b08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,103 +39,141 @@ export class RestifyInstrumentation extends InstrumentationBase< | |
| typeof restify | ||
| > { | ||
| constructor() { | ||
| super( | ||
| `@opentelemetry/instrumentation-${MODULE_NAME}`, | ||
| VERSION | ||
| ); | ||
| super(`@opentelemetry/instrumentation-${MODULE_NAME}`, VERSION); | ||
| } | ||
|
|
||
| private _moduleVersion: string | undefined; | ||
| private _isDisabled = false; | ||
|
|
||
| init() { | ||
| const module = new InstrumentationNodeModuleDefinition<typeof restify>( | ||
| MODULE_NAME, | ||
| SUPPORTED_VERSIONS, | ||
| (moduleExports, moduleVersion) => { | ||
| this._moduleVersion = moduleVersion; | ||
| return moduleExports; | ||
| } | ||
| ); | ||
|
|
||
| module.files.push(new InstrumentationNodeModuleFile<typeof restify>( | ||
| 'restify/lib/server.js', | ||
| MODULE_NAME, | ||
| SUPPORTED_VERSIONS, | ||
| (moduleExports, moduleVersion) => { | ||
| diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`); | ||
| this._isDisabled = false; | ||
| const Server: any = moduleExports; | ||
| for (const name of RESTIFY_METHODS) { | ||
| if (isWrapped(Server.prototype[name])) { | ||
| this._unwrap(Server.prototype, name); | ||
| } | ||
| this._wrap(Server.prototype, name as keyof Server, this._methodPatcher.bind(this)); | ||
| } | ||
| for (const name of RESTIFY_MW_METHODS) { | ||
| if (isWrapped(Server.prototype[name])) { | ||
| this._unwrap(Server.prototype, name); | ||
| } | ||
| this._wrap(Server.prototype, name as keyof Server, this._middlewarePatcher.bind(this)); | ||
| } | ||
| this._moduleVersion = moduleVersion; | ||
| return moduleExports; | ||
| }, | ||
| (moduleExports, moduleVersion) => { | ||
| diag.debug(`Removing patch for ${MODULE_NAME}@${moduleVersion}`); | ||
| this._isDisabled = true; | ||
| if (moduleExports) { | ||
| } | ||
| ); | ||
|
|
||
| module.files.push( | ||
| new InstrumentationNodeModuleFile<typeof restify>( | ||
| 'restify/lib/server.js', | ||
| SUPPORTED_VERSIONS, | ||
| (moduleExports, moduleVersion) => { | ||
| diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`); | ||
| this._isDisabled = false; | ||
| const Server: any = moduleExports; | ||
| for (const name of RESTIFY_METHODS) { | ||
| this._unwrap(Server.prototype, name as keyof Server); | ||
| if (isWrapped(Server.prototype[name])) { | ||
| this._unwrap(Server.prototype, name); | ||
| } | ||
| this._wrap( | ||
| Server.prototype, | ||
| name as keyof Server, | ||
| this._methodPatcher.bind(this) | ||
| ); | ||
| } | ||
| for (const name of RESTIFY_MW_METHODS) { | ||
| this._unwrap(Server.prototype, name as keyof Server); | ||
| if (isWrapped(Server.prototype[name])) { | ||
| this._unwrap(Server.prototype, name); | ||
| } | ||
| this._wrap( | ||
| Server.prototype, | ||
| name as keyof Server, | ||
| this._middlewarePatcher.bind(this) | ||
| ); | ||
| } | ||
| return moduleExports; | ||
| }, | ||
| (moduleExports, moduleVersion) => { | ||
| diag.debug(`Removing patch for ${MODULE_NAME}@${moduleVersion}`); | ||
| this._isDisabled = true; | ||
| if (moduleExports) { | ||
| const Server: any = moduleExports; | ||
| for (const name of RESTIFY_METHODS) { | ||
| this._unwrap(Server.prototype, name as keyof Server); | ||
| } | ||
| for (const name of RESTIFY_MW_METHODS) { | ||
| this._unwrap(Server.prototype, name as keyof Server); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| )); | ||
| ) | ||
| ); | ||
|
|
||
| return module; | ||
| } | ||
|
|
||
| private _middlewarePatcher (original: Function, methodName?: string) { | ||
| private _middlewarePatcher(original: Function, methodName?: string) { | ||
| const instrumentation = this; | ||
| return function (this: Server, ...handler: restify.RequestHandler[][] | restify.RequestHandler[]) { | ||
| return original.call(this, instrumentation._handlerPatcher({ type: types.LayerType.MIDDLEWARE, methodName }, handler)); | ||
| return function (this: Server, ...handler: types.NestedRequestHandlers) { | ||
| return original.call( | ||
| this, | ||
| instrumentation._handlerPatcher( | ||
| { type: types.LayerType.MIDDLEWARE, methodName }, | ||
| handler | ||
| ) | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| private _methodPatcher (original: Function, methodName?: string) { | ||
| private _methodPatcher(original: Function, methodName?: string) { | ||
| const instrumentation = this; | ||
| return function (this: Server, path: any, ...handler: restify.RequestHandler[][] | restify.RequestHandler[]) { | ||
| return original.call(this, path, ...instrumentation._handlerPatcher({ type: types.LayerType.REQUEST_HANDLER, path, methodName }, handler)); | ||
| return function ( | ||
| this: Server, | ||
| path: any, | ||
| ...handler: types.NestedRequestHandlers | ||
| ) { | ||
| return original.call( | ||
| this, | ||
| path, | ||
| ...instrumentation._handlerPatcher( | ||
|
Member
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 like how cleanly you've kept the reuse between methodPatcher and middlewarePatcher - not too complex but still keeping the essential bits parameterized instead of copied+pasted.
Member
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. Thanks! :) |
||
| { type: types.LayerType.REQUEST_HANDLER, path, methodName }, | ||
| handler | ||
| ) | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| // will return the same type as `handler`, but all functions recusively patched | ||
| private _handlerPatcher (metadata: types.Metadata, handler: restify.RequestHandler | restify.RequestHandler[] | restify.RequestHandler[][]): any { | ||
| private _handlerPatcher( | ||
| metadata: types.Metadata, | ||
| handler: restify.RequestHandler | types.NestedRequestHandlers | ||
| ): any { | ||
| if (Array.isArray(handler)) { | ||
| return (handler as restify.RequestHandler[]).map((handler: any) => this._handlerPatcher(metadata, handler)); | ||
| return handler.map(handler => this._handlerPatcher(metadata, handler)); | ||
| } | ||
| if (typeof handler === 'function') { | ||
| return (req: types.Request, res: restify.Response, next: restify.Next) => { | ||
| return ( | ||
| req: types.Request, | ||
| res: restify.Response, | ||
| next: restify.Next | ||
| ) => { | ||
| if (this._isDisabled) { | ||
| return handler(req, res, next); | ||
| } | ||
| // const parentSpan = api.getSpan(api?.context?.active()); | ||
|
||
| const route = (typeof req.getRoute === 'function' ? req.getRoute()?.path : req.route?.path); | ||
| const route = | ||
| typeof req.getRoute === 'function' | ||
| ? req.getRoute()?.path | ||
| : req.route?.path; | ||
| const spanName = route || metadata.methodName || metadata.type; | ||
| const attributes = { | ||
| [types.CustomAttributeNames.VERSION]: this._moduleVersion || 'n/a', | ||
| [types.CustomAttributeNames.TYPE]: metadata.type, | ||
| [types.CustomAttributeNames.METHOD]: metadata.methodName, | ||
| [HttpAttribute.HTTP_ROUTE]: route, | ||
| } | ||
| const span = this.tracer.startSpan(spanName, { | ||
| attributes, | ||
| }, api.context.active()); // TODO: <- with this I intend to find and attach all consecutive handlers to HTTP span | ||
| }; | ||
| const span = this.tracer.startSpan( | ||
| spanName, | ||
| { | ||
| attributes, | ||
| }, | ||
| api.context.active() | ||
| ); // TODO: <- with this I intend to find and attach all consecutive handlers to HTTP span | ||
| // .. but instead all spans are attached to the previous handler's span. | ||
| const endSpan = once(span.end.bind(span)); | ||
vmarchaud marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const patchedNext = (err: any) => { | ||
| const patchedNext = (err?: any) => { | ||
| endSpan(); | ||
| next(err); | ||
| }; | ||
|
|
@@ -157,7 +195,7 @@ export class RestifyInstrumentation extends InstrumentationBase< | |
| req, | ||
| res, | ||
| patchedNext | ||
| ); | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
|
|
||
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.
I think middleware spans should have middleware name as the span name (not sure if possibly with restify), e.g. the operation tree looks like this currently, where each middleware operation name is the route name:

However the express instrumentation contains middleware names:

Also I think middleware spans should have the incoming request span as its parent.
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.
Yep, it's possible. Express takes the name of the function it seems, which can be undefined at times, but still better than nothing. Thanks!
I generally agree but don't want to change that because there might not be a request span if HTTP instrumentation is not enabled, so the structure would be different between HTTP instrumentation enabled and not.