Skip to content

Commit b1e8c3f

Browse files
committed
fix(otel): afterReturnSync(), afterReturnAsync() of TraceLog() missing trace context
1 parent f05b507 commit b1e8c3f

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

packages/otel/src/lib/decorator.trace-log/trace-log.helper.async.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function beforeAsync(options: DecoratorExecutorParam): Promise<void
1212
const { traceService } = options
1313

1414
const type = 'before'
15-
if (! options.traceContext) {
15+
if (! options.traceContext || ! options.span) {
1616
const info = traceService.getActiveTraceInfo()
1717
options.span = info.span
1818
options.traceContext = info.traceContext
@@ -50,21 +50,28 @@ export async function afterReturnAsync(options: DecoratorExecutorParam): Promise
5050
assert(! options.error, `[@mwcp/${ConfigKey.namespace}] options.error is not undefined in afterReturnAsync().
5151
Error: ${options.error?.message}`)
5252

53-
const res: DecoratorTraceDataResp = await processDecoratorBeforeAfterAsync('after', options)
54-
if (res?.endSpanAfterTraceLog) {
55-
endTraceSpan(traceService, span, res.spanStatusOptions)
53+
if (! options.traceContext || ! options.span) {
54+
const info = traceService.getActiveTraceInfo()
55+
options.span = info.span
56+
options.traceContext = info.traceContext
5657
}
57-
58-
if (res?.endParentSpan) {
59-
if (! res.endSpanAfterTraceLog) {
58+
await context.with(options.traceContext, async () => {
59+
const res: DecoratorTraceDataResp = await processDecoratorBeforeAfterAsync('after', options)
60+
if (res?.endSpanAfterTraceLog) {
6061
endTraceSpan(traceService, span, res.spanStatusOptions)
6162
}
6263

63-
const parentSpan = traceService.retrieveParentTraceInfoBySpan(span, options.traceScope)?.span
64-
if (parentSpan) {
65-
endTraceSpan(traceService, parentSpan, res.spanStatusOptions)
64+
if (res?.endParentSpan) {
65+
if (! res.endSpanAfterTraceLog) {
66+
endTraceSpan(traceService, span, res.spanStatusOptions)
67+
}
68+
69+
const parentSpan = traceService.retrieveParentTraceInfoBySpan(span, options.traceScope)?.span
70+
if (parentSpan) {
71+
endTraceSpan(traceService, parentSpan, res.spanStatusOptions)
72+
}
6673
}
67-
}
74+
})
6875

6976
return options.methodResult
7077
}

packages/otel/src/lib/decorator.trace-log/trace-log.helper.sync.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function beforeSync(options: DecoratorExecutorParam): void {
2121
)
2222
assert(spanName, 'spanName is empty')
2323

24-
if (! options.traceContext) {
24+
if (! options.traceContext || ! options.span) {
2525
const info = traceService.getActiveTraceInfo()
2626
options.span = info.span
2727
options.traceContext = info.traceContext
@@ -61,21 +61,29 @@ export function afterReturnSync(options: DecoratorExecutorParam): unknown {
6161
assert(! options.error, `[@mwcp/${ConfigKey.namespace}] options.error is not undefined in afterReturnSync().
6262
Error: ${options.error?.message}`)
6363

64-
const res: DecoratorTraceDataResp = processDecoratorBeforeAfterSync('after', options)
65-
if (res?.endSpanAfterTraceLog) {
66-
endTraceSpan(traceService, span, res.spanStatusOptions)
64+
if (! options.traceContext || ! options.span) {
65+
const info = traceService.getActiveTraceInfo()
66+
options.span = info.span
67+
options.traceContext = info.traceContext
6768
}
68-
69-
if (res?.endParentSpan) {
70-
if (! res.endSpanAfterTraceLog) {
69+
context.with(options.traceContext, () => {
70+
const res: DecoratorTraceDataResp = processDecoratorBeforeAfterSync('after', options)
71+
if (res?.endSpanAfterTraceLog) {
7172
endTraceSpan(traceService, span, res.spanStatusOptions)
7273
}
7374

74-
const parentSpan = traceService.retrieveParentTraceInfoBySpan(span, options.traceScope)?.span
75-
if (parentSpan) {
76-
endTraceSpan(traceService, parentSpan, res.spanStatusOptions)
75+
if (res?.endParentSpan) {
76+
if (! res.endSpanAfterTraceLog) {
77+
endTraceSpan(traceService, span, res.spanStatusOptions)
78+
}
79+
80+
const parentSpan = traceService.retrieveParentTraceInfoBySpan(span, options.traceScope)?.span
81+
if (parentSpan) {
82+
endTraceSpan(traceService, parentSpan, res.spanStatusOptions)
83+
}
7784
}
78-
}
85+
})
86+
7987
return options.methodResult
8088
}
8189

packages/otel/src/lib/trace.service/trace.service.span.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export class TraceServiceSpan extends TraceServiceBase {
7979
assert(ret, 'startScopeActiveSpan() ret should not be null')
8080

8181
const scope = options.scope ?? this.getWebContext()
82-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
8382
if (scope) {
8483
this.setActiveContext(ret.traceContext, scope)
8584
}

0 commit comments

Comments
 (0)