Skip to content

Commit 1004fff

Browse files
committed
fix: double call to span.end() if the decorated method returns a promise
1 parent 2246769 commit 1004fff

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

packages/utils/src/lib/server/telemetry.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const traceDecoratorFactory = (name: string, version?: string) => {
1818
const className = ctx.static ? this.name : this.constructor.name;
1919

2020
return tracer.startActiveSpan([className, ctx.name].filter(Boolean).join('.'), options, (span: Span): R => {
21+
// NOTE: do not use `finally` to call `span.end()` because it causes a double-call if `result` is a promise, and we can't await here otherwise the decorator becomes async
2122
try {
2223
const result = target.call(this, ...args) as R;
2324
if (result && typeof result === 'object' && 'then' in result && typeof result.then === 'function') {
@@ -39,16 +40,18 @@ export const traceDecoratorFactory = (name: string, version?: string) => {
3940
);
4041
}
4142

43+
span.end();
44+
4245
return result;
4346
} catch (err: unknown) {
4447
span.setStatus({ code: SpanStatusCode.ERROR });
4548
if (err instanceof Error) {
4649
span.recordException(err);
4750
}
4851

49-
throw err;
50-
} finally {
5152
span.end();
53+
54+
throw err;
5255
}
5356
});
5457
} as Value;

0 commit comments

Comments
 (0)