@athenna/logger 5.17.0 → 5.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/formatters/Formatter.d.ts +5 -0
- package/src/formatters/Formatter.js +28 -5
- package/src/formatters/JsonFormatter.js +1 -0
- package/src/types/ContextBinding.d.ts +13 -0
- package/src/types/ContextBinding.js +9 -0
- package/src/types/index.d.ts +1 -0
- package/src/types/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/logger",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.18.0",
|
|
4
4
|
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "João Lenon <lenon@athenna.io>",
|
|
@@ -63,7 +63,6 @@
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@aws-lambda-powertools/logger": "^1.18.1",
|
|
66
|
-
"@opentelemetry/api": "^1.9.0",
|
|
67
66
|
"@opentelemetry/api-logs": "^0.213.0",
|
|
68
67
|
"cls-rtracer": "^2.6.3",
|
|
69
68
|
"telegraf": "^4.16.3"
|
|
@@ -74,7 +73,8 @@
|
|
|
74
73
|
"@athenna/ioc": "^5.2.0",
|
|
75
74
|
"@athenna/test": "^5.5.0",
|
|
76
75
|
"@athenna/tsconfig": "^5.0.0",
|
|
77
|
-
"@opentelemetry/
|
|
76
|
+
"@opentelemetry/api": "^1.9.1",
|
|
77
|
+
"@opentelemetry/context-async-hooks": "^2.7.0",
|
|
78
78
|
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
79
79
|
"@typescript-eslint/parser": "^8.38.0",
|
|
80
80
|
"commitizen": "^4.3.1",
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
+
import type { Context as OtelContext } from '@opentelemetry/api';
|
|
9
10
|
export declare abstract class Formatter {
|
|
10
11
|
/**
|
|
11
12
|
* Holds the configuration object of formatter.
|
|
@@ -39,6 +40,10 @@ export declare abstract class Formatter {
|
|
|
39
40
|
* Get the span id for formatter.
|
|
40
41
|
*/
|
|
41
42
|
spanId(): string | null;
|
|
43
|
+
/**
|
|
44
|
+
* Resolve configured context bindings using the active OpenTelemetry context.
|
|
45
|
+
*/
|
|
46
|
+
contextBindings(activeContext?: OtelContext): Record<string, any>;
|
|
42
47
|
/**
|
|
43
48
|
* Create the timestamp for formatter.
|
|
44
49
|
*/
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import rTracer from 'cls-rtracer';
|
|
10
10
|
import { hostname } from 'node:os';
|
|
11
|
-
import {
|
|
12
|
-
|
|
11
|
+
import { Is, Color, Module } from '@athenna/common';
|
|
12
|
+
const otelApi = await Module.safeImport('@opentelemetry/api');
|
|
13
13
|
export class Formatter {
|
|
14
14
|
constructor() {
|
|
15
15
|
/**
|
|
@@ -46,14 +46,37 @@ export class Formatter {
|
|
|
46
46
|
* Get the trace id for formatter.
|
|
47
47
|
*/
|
|
48
48
|
traceId() {
|
|
49
|
-
|
|
50
|
-
(
|
|
49
|
+
if (otelApi?.trace?.getActiveSpan()?.spanContext().traceId) {
|
|
50
|
+
return otelApi?.trace?.getActiveSpan()?.spanContext().traceId;
|
|
51
|
+
}
|
|
52
|
+
return (rTracer.id() || null);
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
55
|
* Get the span id for formatter.
|
|
54
56
|
*/
|
|
55
57
|
spanId() {
|
|
56
|
-
return trace
|
|
58
|
+
return otelApi?.trace?.getActiveSpan()?.spanContext().spanId || null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve configured context bindings using the active OpenTelemetry context.
|
|
62
|
+
*/
|
|
63
|
+
contextBindings(activeContext) {
|
|
64
|
+
if (!otelApi) {
|
|
65
|
+
throw new Error('The package @opentelemetry/api is not installed');
|
|
66
|
+
}
|
|
67
|
+
if (!activeContext) {
|
|
68
|
+
activeContext = otelApi.context.active();
|
|
69
|
+
}
|
|
70
|
+
const contextBindings = this.configs.contextBindings || [];
|
|
71
|
+
const resolved = {};
|
|
72
|
+
for (const binding of contextBindings) {
|
|
73
|
+
const value = binding.resolver(activeContext);
|
|
74
|
+
if (Is.Undefined(value)) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
resolved[binding.field] = value;
|
|
78
|
+
}
|
|
79
|
+
return resolved;
|
|
57
80
|
}
|
|
58
81
|
/**
|
|
59
82
|
* Create the timestamp for formatter.
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
import type { Context } from '@opentelemetry/api';
|
|
10
|
+
export type ContextBinding = {
|
|
11
|
+
field: string;
|
|
12
|
+
resolver: (activeContext: Context) => any;
|
|
13
|
+
};
|
package/src/types/index.d.ts
CHANGED
package/src/types/index.js
CHANGED