From d3328f8f55c6e3e2e7405a8e499d50555e9bec1a Mon Sep 17 00:00:00 2001 From: Martin Seidel <33285712+seidelmartin@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:42:16 +0200 Subject: [PATCH] fix: Removed deprecated properties usage in Fastify instrumentation (#1679) Co-authored-by: Marc Pichler --- .../package.json | 2 +- .../src/instrumentation.ts | 22 +++++++++++++------ .../src/utils.ts | 10 +++------ .../test/instrumentation.test.ts | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index ec08914fcc..7009ae9e10 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -54,7 +54,7 @@ "@opentelemetry/sdk-trace-node": "^1.8.0", "@types/express": "4.17.18", "@types/mocha": "7.0.2", - "@types/node": "18.6.5", + "@types/node": "18.15.3", "fastify": "4.18.0", "mocha": "7.2.0", "nyc": "15.1.0", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index ff7cc504a5..ba96a020dd 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -93,8 +93,11 @@ export class FastifyInstrumentation extends InstrumentationBase { } instrumentation._wrap(reply, 'send', instrumentation._patchSend()); + const anyRequest = request as any; + const rpcMetadata = getRPCMetadata(context.active()); - const routeName = request.routerPath; + const routeName = + anyRequest.routeOptions?.config?.url || request.routerPath; if (routeName && rpcMetadata?.type === RPCType.HTTP) { rpcMetadata.route = routeName; } @@ -176,7 +179,7 @@ export class FastifyInstrumentation extends InstrumentationBase { const handler = args[1] as HandlerOriginal; const pluginName = this.pluginName; if (applicationHookNames.includes(name)) { - return original.apply(this, [name as any, handler]); + return original.apply(this, [name, handler] as never); } const syncFunctionWithDone = @@ -184,14 +187,14 @@ export class FastifyInstrumentation extends InstrumentationBase { handler.constructor.name !== 'AsyncFunction'; return original.apply(this, [ - name as any, + name, instrumentation._wrapHandler( pluginName, name, handler, syncFunctionWithDone ), - ]); + ] as never); }; }; } @@ -259,8 +262,12 @@ export class FastifyInstrumentation extends InstrumentationBase { if (!instrumentation.isEnabled()) { return done(); } - const requestContext = (request as any).context || {}; - const handlerName = (requestContext.handler?.name || '').substr(6); + const anyRequest = request as any; + + const handler = + anyRequest.routeOptions?.handler || anyRequest.context?.handler || {}; + + const handlerName = handler?.name.substr(6); const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ handlerName || this.pluginName || ANONYMOUS_NAME }`; @@ -268,7 +275,8 @@ export class FastifyInstrumentation extends InstrumentationBase { const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER, - [SemanticAttributes.HTTP_ROUTE]: request.routerPath, + [SemanticAttributes.HTTP_ROUTE]: + anyRequest.routeOptions?.config?.url || request.routerPath, }; if (handlerName) { spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName; diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts index 0ff05492a3..84874607e2 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts @@ -14,12 +14,7 @@ * limitations under the License. */ -import { - Span, - SpanAttributes, - SpanStatusCode, - Tracer, -} from '@opentelemetry/api'; +import { Attributes, Span, SpanStatusCode, Tracer } from '@opentelemetry/api'; import { spanRequestSymbol } from './constants'; import type { PluginFastifyReply } from './internal-types'; @@ -35,13 +30,14 @@ export function startSpan( reply: PluginFastifyReply, tracer: Tracer, spanName: string, - spanAttributes: SpanAttributes = {} + spanAttributes: Attributes = {} ) { const span = tracer.startSpan(spanName, { attributes: spanAttributes }); const spans: Span[] = reply[spanRequestSymbol] || []; spans.push(span); + // eslint-disable-next-line @typescript-eslint/no-floating-promises Object.defineProperty(reply, spanRequestSymbol, { enumerable: false, configurable: true, diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index b5869f434a..90a881a321 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -190,7 +190,7 @@ describe('fastify', () => { async function subsystem(fastify: FastifyInstance) { fastify.addHook( 'onRequest', - async ( + ( req: FastifyRequest, res: FastifyReply, next: HookHandlerDoneFunction