Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(tracing): record exception and set correct span status
  • Loading branch information
mentos1386 committed Mar 25, 2022
1 parent 3f20adc commit 616c12b
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/tracing/decorators/span.ts
@@ -1,4 +1,9 @@
import { trace } from '@opentelemetry/api';
import { Span as ApiSpan, SpanStatusCode, trace } from '@opentelemetry/api';

const recordException = (span: ApiSpan, error: any) => {
span.recordException(error);
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
};

export function Span(name?: string) {
return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => {
Expand All @@ -10,13 +15,27 @@ export function Span(name?: string) {

return tracer.startActiveSpan(spanName, span => {
if (method.constructor.name === 'AsyncFunction') {
return method.apply(this, args).finally(() => {
span.end();
});
return method
.apply(this, args)
.catch(error => {
recordException(span, error);
// Throw error to propagate it further
throw error;
})
.finally(() => {
span.end();
});
}

try {
return method.apply(this, args);
} catch (error) {
recordException(span, error);
// Throw error to propagate it further
throw error;
} finally {
span.end();
}
const result = method.apply(this, args);
span.end();
return result;
});
};
};
Expand Down

0 comments on commit 616c12b

Please sign in to comment.