From 405472df3b1d218daab79e9caa7c8a51c3d7a7bf Mon Sep 17 00:00:00 2001 From: Ievgen Makukh Date: Mon, 29 Apr 2024 11:36:06 +0300 Subject: [PATCH] perf(instrumentation-nestjs-core): extract reusable span attributes to outer scope (#2087) Co-authored-by: Amir Blum --- .../src/instrumentation.ts | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-nestjs-core/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-nestjs-core/src/instrumentation.ts index 619c68760f..c480f3a546 100644 --- a/plugins/node/opentelemetry-instrumentation-nestjs-core/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-nestjs-core/src/instrumentation.ts @@ -148,21 +148,21 @@ function createWrapCreateHandler(tracer: api.Tracer, moduleVersion?: string) { ) { arguments[1] = createWrapHandler(tracer, moduleVersion, callback); const handler = original.apply(this, arguments as any); + const callbackName = callback.name; + const instanceName = + instance.constructor && instance.constructor.name + ? instance.constructor.name + : 'UnnamedInstance'; + const spanName = callbackName + ? `${instanceName}.${callbackName}` + : instanceName; + return function ( this: any, req: any, res: any, next: (...args: any[]) => unknown ) { - const callbackName = callback.name; - const instanceName = - instance.constructor && instance.constructor.name - ? instance.constructor.name - : 'UnnamedInstance'; - const spanName = callbackName - ? `${instanceName}.${callbackName}` - : instanceName; - const span = tracer.startSpan(spanName, { attributes: { ...Instrumentation.COMMON_ATTRIBUTES, @@ -197,15 +197,17 @@ function createWrapHandler( moduleVersion: string | undefined, handler: Function ) { + const spanName = handler.name || 'anonymous nest handler'; + const options = { + attributes: { + ...Instrumentation.COMMON_ATTRIBUTES, + [AttributeNames.VERSION]: moduleVersion, + [AttributeNames.TYPE]: NestType.REQUEST_HANDLER, + [AttributeNames.CALLBACK]: handler.name, + }, + }; const wrappedHandler = function (this: RouterExecutionContext) { - const span = tracer.startSpan(handler.name || 'anonymous nest handler', { - attributes: { - ...Instrumentation.COMMON_ATTRIBUTES, - [AttributeNames.VERSION]: moduleVersion, - [AttributeNames.TYPE]: NestType.REQUEST_HANDLER, - [AttributeNames.CALLBACK]: handler.name, - }, - }); + const span = tracer.startSpan(spanName, options); const spanContext = api.trace.setSpan(api.context.active(), span); return api.context.with(spanContext, async () => {