|
| 1 | +/** |
| 2 | + * @athenna/logger |
| 3 | + * |
| 4 | + * (c) João Lenon <lenon@athenna.io> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { OtelDriver } from '#src/drivers/OtelDriver' |
| 11 | +import { Test, AfterEach, BeforeEach, type Context } from '@athenna/test' |
| 12 | +import { context, ROOT_CONTEXT, trace, TraceFlags } from '@opentelemetry/api' |
| 13 | +import { logs, SeverityNumber, type LogRecord } from '@opentelemetry/api-logs' |
| 14 | +import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks' |
| 15 | + |
| 16 | +class FakeLogger { |
| 17 | + public records: LogRecord[] = [] |
| 18 | + |
| 19 | + public emit(logRecord: LogRecord): void { |
| 20 | + this.records.push(logRecord) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class FakeLoggerProvider { |
| 25 | + public logger = new FakeLogger() |
| 26 | + |
| 27 | + public getLogger() { |
| 28 | + return this.logger |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export default class OtelDriverTest { |
| 33 | + @BeforeEach() |
| 34 | + public async beforeEach() { |
| 35 | + context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable()) |
| 36 | + } |
| 37 | + |
| 38 | + @AfterEach() |
| 39 | + public async afterEach() { |
| 40 | + logs.disable() |
| 41 | + context.disable() |
| 42 | + } |
| 43 | + |
| 44 | + @Test() |
| 45 | + public async shouldEmitLogsUsingTheOpenTelemetryLoggerProvider({ assert }: Context) { |
| 46 | + const provider = new FakeLoggerProvider() |
| 47 | + |
| 48 | + logs.setGlobalLoggerProvider(provider as any) |
| 49 | + |
| 50 | + const driver = new OtelDriver({ formatter: 'json' }) |
| 51 | + |
| 52 | + driver.transport('error', 'failed to retrieve user') |
| 53 | + |
| 54 | + assert.lengthOf(provider.logger.records, 1) |
| 55 | + assert.equal(provider.logger.records[0].severityNumber, SeverityNumber.ERROR) |
| 56 | + assert.equal(provider.logger.records[0].severityText, 'ERROR') |
| 57 | + assert.equal(provider.logger.records[0].attributes['athenna.log.level'], 'error') |
| 58 | + assert.equal(provider.logger.records[0].attributes['athenna.log.stream'], 'stderr') |
| 59 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 60 | + // @ts-ignore |
| 61 | + assert.equal(provider.logger.records[0].body.msg, 'failed to retrieve user') |
| 62 | + } |
| 63 | + |
| 64 | + @Test() |
| 65 | + public async shouldEmitLogsWithTheActiveOtelContext({ assert }: Context) { |
| 66 | + const provider = new FakeLoggerProvider() |
| 67 | + const spanContext = { |
| 68 | + traceId: '11111111111111111111111111111111', |
| 69 | + spanId: '2222222222222222', |
| 70 | + traceFlags: TraceFlags.SAMPLED |
| 71 | + } |
| 72 | + |
| 73 | + logs.setGlobalLoggerProvider(provider as any) |
| 74 | + |
| 75 | + const driver = new OtelDriver({ formatter: 'json' }) |
| 76 | + |
| 77 | + context.with(trace.setSpanContext(ROOT_CONTEXT, spanContext), () => { |
| 78 | + driver.transport('info', 'hello') |
| 79 | + }) |
| 80 | + |
| 81 | + const emittedContext = provider.logger.records[0].context |
| 82 | + |
| 83 | + assert.deepEqual(trace.getSpanContext(emittedContext), spanContext) |
| 84 | + } |
| 85 | +} |
0 commit comments