Skip to content

Commit 108da26

Browse files
author
toonew
committed
fix(): decorator spand error
当async context run方法未执行时,会没有上下文
1 parent aa54b1c commit 108da26

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

lib/decorator/spand.decorator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ export function SpanD(name: string): MethodDecorator {
1515
const original = descriptor.value;
1616

1717
descriptor.value = function (...args: any[]) {
18-
const tracer = TracingModule.tracer;
18+
let context: any;
19+
try {
20+
context = AsyncContext.getInstance().get(TRACER_CARRIER_INFO);
21+
} catch (err) {
22+
return original.apply(this, args);
23+
}
1924

20-
let context = AsyncContext.getInstance().get(TRACER_CARRIER_INFO);
2125
if (!context) {
2226
context = {};
2327
AsyncContext.getInstance().set(TRACER_CARRIER_INFO, context);
2428
}
2529

30+
const tracer = TracingModule.tracer;
2631
const ctx = tracer.extract(FORMAT_TEXT_MAP, context);
2732

2833
let span: Span;

test/test2.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Created by Rain on 2020/7/22
3+
*/
4+
import * as assert from 'assert';
5+
import { SpanD } from '../lib';
6+
7+
class Test2Spec {
8+
@SpanD('Test2Spc')
9+
say() {
10+
return 's';
11+
}
12+
}
13+
14+
describe('test2', () => {
15+
it(' async context not run before spanD run ', () => {
16+
const test = new Test2Spec();
17+
18+
assert.strictEqual(test.say(), 's');
19+
});
20+
});

0 commit comments

Comments
 (0)