Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(tracing): use startActiveSpan to ensure span in correct context
  • Loading branch information
mentos1386 committed Mar 22, 2022
1 parent 2d50187 commit d608b84
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions 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();
Expand Down

0 comments on commit d608b84

Please sign in to comment.