|
1 | 1 | 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'; |
3 | 9 | import { getEnvWithoutDefaults } from '@opentelemetry/core'; |
4 | 10 | import { |
5 | 11 | BatchLogRecordProcessor, |
6 | 12 | BufferConfig, |
7 | 13 | LoggerProvider, |
8 | 14 | } from '@opentelemetry/sdk-logs'; |
9 | 15 | 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'; |
11 | 17 | import { |
12 | 18 | Resource, |
13 | 19 | defaultServiceName, |
@@ -47,6 +53,47 @@ const DEFAULT_SERVICE_NAME = otelEnv.OTEL_SERVICE_NAME ?? defaultServiceName(); |
47 | 53 |
|
48 | 54 | const LOG_PREFIX = `⚠️ ${_LOG_PREFIX}`; |
49 | 55 |
|
| 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 | + |
50 | 97 | export const jsonToString = (json) => { |
51 | 98 | try { |
52 | 99 | return JSON.stringify(json); |
@@ -159,14 +206,16 @@ export class Logger { |
159 | 206 |
|
160 | 207 | postMessage(level: string, body: string, attributes: Attributes = {}): void { |
161 | 208 | 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 | + ); |
171 | 220 | } |
172 | 221 | } |
0 commit comments