|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import { Config } from '@athenna/config' |
11 | | -import { Is, Macroable } from '@athenna/common' |
12 | 11 | import { Listener } from '#src/events/Listener' |
13 | | -import { QueueImpl, type ConnectionOptions } from '@athenna/queue' |
| 12 | +import { Is, Macroable, Module } from '@athenna/common' |
14 | 13 | import type { EventClosure, Context } from '#src/types' |
| 14 | +import { QueueImpl, type ConnectionOptions } from '@athenna/queue' |
| 15 | + |
| 16 | +const otelModule = await Module.safeImport('@athenna/otel') |
15 | 17 |
|
16 | 18 | export class EventImpl extends Macroable { |
17 | 19 | /** |
@@ -460,12 +462,23 @@ export class EventImpl extends Macroable { |
460 | 462 | emittedAt: data.emittedAt |
461 | 463 | } |
462 | 464 |
|
463 | | - await record.closure(ctx) |
| 465 | + await this.runWithOtelContext(ctx, () => record.closure(ctx)) |
464 | 466 |
|
465 | 467 | await queue.ack(job.id) |
466 | 468 | }) |
467 | 469 | } |
468 | 470 |
|
| 471 | + private runWithOtelContext<T>(ctx: Context, callback: () => T): T { |
| 472 | + if (!Config.is('event.otel.contextEnabled', true) || !otelModule) { |
| 473 | + return callback() |
| 474 | + } |
| 475 | + |
| 476 | + return otelModule.Otel.withContext(callback, { |
| 477 | + bindings: Config.get('event.otel.contextBindings', []), |
| 478 | + resolveBinding: binding => binding.resolve(ctx) |
| 479 | + }) |
| 480 | + } |
| 481 | + |
469 | 482 | private parseConnectionName(con: string) { |
470 | 483 | if (con === 'default') { |
471 | 484 | return Config.get('event.store') |
|
0 commit comments