Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "5.20.0",
"version": "5.21.0",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -64,7 +64,6 @@
"dependencies": {
"@aws-lambda-powertools/logger": "^1.18.1",
"@opentelemetry/api-logs": "^0.213.0",
"cls-rtracer": "^2.6.3",
"telegraf": "^4.16.3"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions src/formatters/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* file that was distributed with this source code.
*/

import rTracer from 'cls-rtracer'

import { hostname } from 'node:os'
import { Is, Color, Module } from '@athenna/common'
import type { ContextBinding } from '#src/types/ContextBinding'
Expand Down Expand Up @@ -65,7 +63,7 @@ export abstract class Formatter {
return otelApi?.trace?.getActiveSpan()?.spanContext().traceId
}

return (rTracer.id() || null) as string | null
return null
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/JsonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class JsonFormatter extends Formatter {
hostname: this.hostname(),
traceId: this.traceId(),
spanId: this.spanId(),
...this.contextBindings(),
...this.contextBindings()
}

if (Is.String(message)) {
Expand Down
47 changes: 31 additions & 16 deletions tests/unit/formatters/JsonFormatterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* file that was distributed with this source code.
*/

import { Is } from '@athenna/common'
import { runWithId } from 'cls-rtracer'
import { JsonFormatter } from '#src/formatters/JsonFormatter'
import { Test, BeforeEach, AfterEach, type Context } from '@athenna/test'
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
Expand Down Expand Up @@ -41,18 +39,15 @@ export default class JsonFormatterTest {
}

@Test()
public async shouldBeAbleToFormatLogsToJsonFormatWithTheTraceId({ assert }: Context) {
public async shouldKeepTraceCorrelationNullWhenThereIsNoActiveOtelSpan({ assert }: Context) {
const formatter = new JsonFormatter().config({ level: 'info' })
const message = JSON.parse(formatter.format('hello'))

runWithId(() => {
const message = JSON.parse(formatter.format('hello'))

assert.equal(message.msg, 'hello')
assert.equal(message.level, 'info')
assert.equal(message.pid, process.pid)
assert.isTrue(Is.Uuid(message.traceId))
assert.equal(message.spanId, null)
})
assert.equal(message.msg, 'hello')
assert.equal(message.level, 'info')
assert.equal(message.pid, process.pid)
assert.equal(message.traceId, null)
assert.equal(message.spanId, null)
}

@Test()
Expand Down Expand Up @@ -96,7 +91,7 @@ export default class JsonFormatterTest {
contextBindings: [
{
field: 'tenantId',
resolver: activeContext => activeContext.getValue(tenantIdKey)
resolve: activeContext => activeContext.getValue(tenantIdKey)
}
]
})
Expand All @@ -120,7 +115,7 @@ export default class JsonFormatterTest {
contextBindings: [
{
field: 'namespace',
resolver: activeContext => activeContext.getValue(namespaceKey)
resolve: activeContext => activeContext.getValue(namespaceKey)
}
]
})
Expand All @@ -143,7 +138,7 @@ export default class JsonFormatterTest {
contextBindings: [
{
field: 'tenantId',
resolver: () => undefined
resolve: () => undefined
}
]
})
Expand All @@ -153,6 +148,26 @@ export default class JsonFormatterTest {
assert.isFalse(Object.hasOwn(message, 'tenantId'))
}

@Test()
public async shouldSkipContextBindingsWhenRequestContextIsNotInitialized({ assert }: Context) {
const { OtelImpl } = await import(new URL('../../../../Otel/src/otel/OtelImpl.js', import.meta.url).href)
const otel = new OtelImpl()
const formatter = new JsonFormatter().config({
level: 'info',
contextBindings: [
{
field: 'exampleId',
resolve: activeContext => otel.getCurrentContextValue('exampleId', activeContext)
}
]
})

const message = JSON.parse(formatter.format('hello'))

assert.equal(message.msg, 'hello')
assert.isFalse(Object.hasOwn(message, 'exampleId'))
}

@Test()
public async shouldAllowResolversToUseGetCurrentContextValueWithoutWithContextValue({ assert }: Context) {
const { OtelImpl } = await import(new URL('../../../../Otel/src/otel/OtelImpl.js', import.meta.url).href)
Expand All @@ -162,7 +177,7 @@ export default class JsonFormatterTest {
contextBindings: [
{
field: 'exampleId',
resolver: activeContext => otel.getCurrentContextValue('exampleId', activeContext)
resolve: activeContext => otel.getCurrentContextValue('exampleId', activeContext)
}
]
})
Expand Down
10 changes: 3 additions & 7 deletions tests/unit/formatters/RequestFormatterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export default class RequestFormatterTest {
assert.equal(message.metadata.statusCode, 200)
assert.equal(message.metadata.url, ctx.request.hostUrl)
assert.equal(message.metadata.path, ctx.request.baseUrl)
assert.isDefined(message.metadata.createdAt)
assert.equal(message.metadata.spanId, null)
}

Expand Down Expand Up @@ -167,9 +166,7 @@ export default class RequestFormatterTest {
}

@Test()
public async shouldIncludeResolvedContextBindingsAtRootWhenFormattingAsJson({
assert
}: Context) {
public async shouldIncludeResolvedContextBindingsAtRootWhenFormattingAsJson({ assert }: Context) {
const exampleIdKey = Symbol('exampleId')
const formatter = new RequestFormatter().config({
level: 'info',
Expand Down Expand Up @@ -207,9 +204,8 @@ export default class RequestFormatterTest {
}
}

const message = context.with(
context.active().setValue(exampleIdKey as any, 'example-id-from-context'),
() => JSON.parse(formatter.format(ctx))
const message = context.with(context.active().setValue(exampleIdKey as any, 'example-id-from-context'), () =>
JSON.parse(formatter.format(ctx))
)

assert.equal(message.exampleId, 'example-id-from-context')
Expand Down
Loading