Skip to content

Commit a9dcc2a

Browse files
committed
fix: pino trace linking issue
1 parent 7831007 commit a9dcc2a

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

packages/node-logger/src/logger.ts

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import stringifySafe from 'json-stringify-safe';
2-
import { Attributes, diag, DiagConsoleLogger } from '@opentelemetry/api';
2+
import {
3+
Attributes,
4+
context as apiContext,
5+
diag,
6+
trace,
7+
DiagConsoleLogger,
8+
} from '@opentelemetry/api';
39
import { getEnvWithoutDefaults } from '@opentelemetry/core';
410
import {
511
BatchLogRecordProcessor,
612
BufferConfig,
713
LoggerProvider,
814
} from '@opentelemetry/sdk-logs';
915
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
10-
import { Logger as OtelLogger, SeverityNumber } from '@opentelemetry/api-logs';
16+
import { Logger as OtelLogger, LogRecord } from '@opentelemetry/api-logs';
1117
import {
1218
Resource,
1319
defaultServiceName,
@@ -47,6 +53,47 @@ const DEFAULT_SERVICE_NAME = otelEnv.OTEL_SERVICE_NAME ?? defaultServiceName();
4753

4854
const LOG_PREFIX = `⚠️ ${_LOG_PREFIX}`;
4955

56+
/**
57+
* Load context from attributes and set it to logRecord.context
58+
*
59+
* @param {LogRecord} logRecord
60+
* @returns {LogRecord}
61+
*/
62+
function loadContext(logRecord: LogRecord): LogRecord {
63+
let context = apiContext.active();
64+
let attributes = logRecord.attributes;
65+
66+
if (typeof attributes !== 'undefined') {
67+
const { trace_id, span_id, trace_flags, ...otherAttributes } =
68+
logRecord.attributes as Attributes & {
69+
trace_id?: string;
70+
span_id?: string;
71+
trace_flags?: number;
72+
};
73+
74+
if (
75+
typeof trace_id !== 'undefined' &&
76+
typeof span_id !== 'undefined' &&
77+
typeof trace_flags !== 'undefined'
78+
) {
79+
context = trace.setSpanContext(context, {
80+
traceId: trace_id,
81+
spanId: span_id,
82+
traceFlags: trace_flags,
83+
isRemote: true,
84+
});
85+
}
86+
87+
attributes = otherAttributes;
88+
}
89+
90+
return {
91+
...logRecord,
92+
attributes,
93+
context,
94+
};
95+
}
96+
5097
export const jsonToString = (json) => {
5198
try {
5299
return JSON.stringify(json);
@@ -159,14 +206,16 @@ export class Logger {
159206

160207
postMessage(level: string, body: string, attributes: Attributes = {}): void {
161208
hdx('Emitting log from HyperDX node logger...');
162-
this.logger.emit({
163-
// TODO: should map to otel severity number
164-
severityNumber: 0,
165-
// TODO: set up the mapping between different downstream log levels
166-
severityText: level,
167-
body,
168-
attributes,
169-
timestamp: this.parseTimestamp(attributes),
170-
});
209+
this.logger.emit(
210+
loadContext({
211+
// TODO: should map to otel severity number
212+
severityNumber: 0,
213+
// TODO: set up the mapping between different downstream log levels
214+
severityText: level,
215+
body,
216+
attributes,
217+
timestamp: this.parseTimestamp(attributes),
218+
}),
219+
);
171220
}
172221
}

0 commit comments

Comments
 (0)