From d608b84e582b41105c6871ee2e7174f7a0116d96 Mon Sep 17 00:00:00 2001 From: Tine Jozelj Date: Tue, 22 Mar 2022 15:19:49 +0100 Subject: [PATCH] fix(tracing): use startActiveSpan to ensure span in correct context --- src/tracing/decorators/span.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/tracing/decorators/span.ts b/src/tracing/decorators/span.ts index 5ef8e55..810f5a8 100644 --- a/src/tracing/decorators/span.ts +++ b/src/tracing/decorators/span.ts @@ -1,21 +1,14 @@ -import { context, trace } from '@opentelemetry/api'; +import { trace } from '@opentelemetry/api'; export function Span(name?: string) { - return ( - target: any, - propertyKey: string, - propertyDescriptor: PropertyDescriptor, - ) => { + return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => { const method = propertyDescriptor.value; // eslint-disable-next-line no-param-reassign propertyDescriptor.value = function PropertyDescriptor(...args: any[]) { - const currentSpan = trace.getSpan(context.active()); const tracer = trace.getTracer('default'); + const spanName = name || `${target.constructor.name}.${propertyKey}`; - return context.with(trace.setSpan(context.active(), currentSpan), () => { - const span = tracer.startSpan( - name || `${target.constructor.name}.${propertyKey}`, - ); + return tracer.startActiveSpan(spanName, span => { if (method.constructor.name === 'AsyncFunction') { return method.apply(this, args).finally(() => { span.end();