Skip to content

Commit 405472d

Browse files
Samuronblumamir
andauthored
perf(instrumentation-nestjs-core): extract reusable span attributes to outer scope (#2087)
Co-authored-by: Amir Blum <[email protected]>
1 parent 3ad9fdf commit 405472d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

plugins/node/opentelemetry-instrumentation-nestjs-core/src/instrumentation.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,21 @@ function createWrapCreateHandler(tracer: api.Tracer, moduleVersion?: string) {
148148
) {
149149
arguments[1] = createWrapHandler(tracer, moduleVersion, callback);
150150
const handler = original.apply(this, arguments as any);
151+
const callbackName = callback.name;
152+
const instanceName =
153+
instance.constructor && instance.constructor.name
154+
? instance.constructor.name
155+
: 'UnnamedInstance';
156+
const spanName = callbackName
157+
? `${instanceName}.${callbackName}`
158+
: instanceName;
159+
151160
return function (
152161
this: any,
153162
req: any,
154163
res: any,
155164
next: (...args: any[]) => unknown
156165
) {
157-
const callbackName = callback.name;
158-
const instanceName =
159-
instance.constructor && instance.constructor.name
160-
? instance.constructor.name
161-
: 'UnnamedInstance';
162-
const spanName = callbackName
163-
? `${instanceName}.${callbackName}`
164-
: instanceName;
165-
166166
const span = tracer.startSpan(spanName, {
167167
attributes: {
168168
...Instrumentation.COMMON_ATTRIBUTES,
@@ -197,15 +197,17 @@ function createWrapHandler(
197197
moduleVersion: string | undefined,
198198
handler: Function
199199
) {
200+
const spanName = handler.name || 'anonymous nest handler';
201+
const options = {
202+
attributes: {
203+
...Instrumentation.COMMON_ATTRIBUTES,
204+
[AttributeNames.VERSION]: moduleVersion,
205+
[AttributeNames.TYPE]: NestType.REQUEST_HANDLER,
206+
[AttributeNames.CALLBACK]: handler.name,
207+
},
208+
};
200209
const wrappedHandler = function (this: RouterExecutionContext) {
201-
const span = tracer.startSpan(handler.name || 'anonymous nest handler', {
202-
attributes: {
203-
...Instrumentation.COMMON_ATTRIBUTES,
204-
[AttributeNames.VERSION]: moduleVersion,
205-
[AttributeNames.TYPE]: NestType.REQUEST_HANDLER,
206-
[AttributeNames.CALLBACK]: handler.name,
207-
},
208-
});
210+
const span = tracer.startSpan(spanName, options);
209211
const spanContext = api.trace.setSpan(api.context.active(), span);
210212

211213
return api.context.with(spanContext, async () => {

0 commit comments

Comments
 (0)