Skip to content

Commit 3a80cb1

Browse files
committed
feat: support @athenna/otel
1 parent 345c0f4 commit 3a80cb1

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/event",
3-
"version": "5.5.0",
3+
"version": "5.7.0",
44
"description": "The Athenna events handler with queue store. Based on Emittery syntax.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/events/EventImpl.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
*/
99

1010
import { Config } from '@athenna/config'
11-
import { Is, Macroable } from '@athenna/common'
1211
import { Listener } from '#src/events/Listener'
13-
import { QueueImpl, type ConnectionOptions } from '@athenna/queue'
12+
import { Is, Macroable, Module } from '@athenna/common'
1413
import type { EventClosure, Context } from '#src/types'
14+
import { QueueImpl, type ConnectionOptions } from '@athenna/queue'
15+
16+
const otelModule = await Module.safeImport('@athenna/otel')
1517

1618
export class EventImpl extends Macroable {
1719
/**
@@ -460,12 +462,23 @@ export class EventImpl extends Macroable {
460462
emittedAt: data.emittedAt
461463
}
462464

463-
await record.closure(ctx)
465+
await this.runWithOtelContext(ctx, () => record.closure(ctx))
464466

465467
await queue.ack(job.id)
466468
})
467469
}
468470

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+
469482
private parseConnectionName(con: string) {
470483
if (con === 'default') {
471484
return Config.get('event.store')

tests/unit/events/EventImplTest.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,36 @@ export class EventImplTest {
291291
assert.equal(specific, 1)
292292
}
293293

294+
@Test()
295+
public async shouldRunQueuedListenersThroughOtelContextWrapper({ assert }: Context) {
296+
const event = Event.store('memoryA') as any
297+
const originalRunWithOtelContext = event.runWithOtelContext?.bind(event)
298+
let wrappedCtx: any = null
299+
let closureCtx: any = null
300+
301+
event.runWithOtelContext = (ctx, callback) => {
302+
wrappedCtx = ctx
303+
304+
return callback()
305+
}
306+
307+
try {
308+
event.on('otel:event', ctx => {
309+
closureCtx = ctx
310+
})
311+
312+
await event.emit('otel:event', { ok: true })
313+
await Sleep.for(40).milliseconds().wait()
314+
} finally {
315+
event.runWithOtelContext = originalRunWithOtelContext
316+
}
317+
318+
assert.isDefined(wrappedCtx)
319+
assert.deepEqual(wrappedCtx, closureCtx)
320+
assert.equal(wrappedCtx.event, 'otel:event')
321+
assert.deepEqual(wrappedCtx.data, { ok: true })
322+
}
323+
294324
@Test()
295325
public async shouldBeAbleToEmitSequentially({ assert }: Context) {
296326
const event = Event.store('memoryA')

0 commit comments

Comments
 (0)