Skip to content

Commit 2674121

Browse files
committed
🔧 add logs to trace
1 parent be6ddc2 commit 2674121

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/util/logger.util.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import { anonymizeKey } from './anonymize-key';
2+
import { context, trace } from '@opentelemetry/api';
3+
4+
function addMessageToTraceSpan(
5+
method: 'log' | 'error' | 'warn',
6+
message: string,
7+
args?: unknown[],
8+
) {
9+
const span = trace.getSpan(context.active());
10+
if (span) {
11+
span.addEvent(method, { message, args: args ? args.join(',') : '' });
12+
}
13+
}
214

315
/**
416
* Logging function equivalent to console.log
@@ -13,6 +25,8 @@ export const infoLogger = (
1325
apiKey?: string,
1426
...args: unknown[]
1527
): void => {
28+
addMessageToTraceSpan('log', message, args);
29+
1630
logger(console.info, source, message, apiKey, ...args);
1731
};
1832

@@ -29,6 +43,8 @@ export const errorLogger = (
2943
apiKey?: string,
3044
...args: unknown[]
3145
): void => {
46+
addMessageToTraceSpan('error', message, args);
47+
3248
logger(console.error, source, message, apiKey, ...args);
3349
};
3450

@@ -45,6 +61,8 @@ export const warnLogger = (
4561
apiKey?: string,
4662
...args: unknown[]
4763
): void => {
64+
addMessageToTraceSpan('warn', message, args);
65+
4866
logger(console.warn, source, message, apiKey, ...args);
4967
};
5068

0 commit comments

Comments
 (0)