From baad9ad0160806be931bd0cca2e54d020c224b47 Mon Sep 17 00:00:00 2001 From: Jackson Weber Date: Thu, 18 Apr 2024 16:45:18 -0700 Subject: [PATCH 1/2] Serialize nested objects properly. --- packages/winston-transport/src/utils.ts | 6 ++---- .../test/openTelemetryTransport.test.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/winston-transport/src/utils.ts b/packages/winston-transport/src/utils.ts index 976d0f55da..7f9eaae73e 100644 --- a/packages/winston-transport/src/utils.ts +++ b/packages/winston-transport/src/utils.ts @@ -65,10 +65,8 @@ export function emitLogRecord( ): void { const { message, level, ...splat } = record; const attributes: LogAttributes = {}; - for (const key in splat) { - if (Object.prototype.hasOwnProperty.call(splat, key)) { - attributes[key] = splat[key]; - } + for (const [key, value] of Object.entries(splat)) { + attributes[key] = typeof value === 'object' ? JSON.stringify(value) : value; } const logRecord: LogRecord = { severityNumber: getSeverityNumber(level), diff --git a/packages/winston-transport/test/openTelemetryTransport.test.ts b/packages/winston-transport/test/openTelemetryTransport.test.ts index 17516e5e51..d67e5cbf0e 100644 --- a/packages/winston-transport/test/openTelemetryTransport.test.ts +++ b/packages/winston-transport/test/openTelemetryTransport.test.ts @@ -71,6 +71,25 @@ describe('OpenTelemetryTransportV3', () => { ); }); + it('emit LogRecord with nested objects', () => { + const errorMsg = 'test error'; + const transport = new OpenTelemetryTransportV3(); + const errorEvent = { + message: errorMsg, + meta: { error: 'test error' }, + level: 'error', + }; + const parameters = Object.assign({ message: kMessage }, errorEvent); + transport.log(parameters, () => {}); + const logRecords = memoryLogExporter.getFinishedLogRecords(); + assert.strictEqual(logRecords.length, 1); + assert.strictEqual(logRecords[0].body, errorMsg); + assert.strictEqual( + logRecords[0].attributes['meta'], + JSON.stringify(errorEvent.meta) + ); + }); + describe('emit logRecord severity', () => { it('npm levels', () => { const callback = () => {}; From c087a2e9edf373eb36913e7755bd39ac37b78a73 Mon Sep 17 00:00:00 2001 From: Jackson Weber Date: Thu, 18 Apr 2024 17:03:51 -0700 Subject: [PATCH 2/2] Fix lint by including valid instances of any. --- packages/winston-transport/src/OpenTelemetryTransportV3.ts | 1 + packages/winston-transport/src/utils.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/winston-transport/src/OpenTelemetryTransportV3.ts b/packages/winston-transport/src/OpenTelemetryTransportV3.ts index fe759676e9..58ff7c5ba3 100644 --- a/packages/winston-transport/src/OpenTelemetryTransportV3.ts +++ b/packages/winston-transport/src/OpenTelemetryTransportV3.ts @@ -27,6 +27,7 @@ export class OpenTelemetryTransportV3 extends TransportStream { this._logger = logs.getLogger('@opentelemetry/winston-transport', VERSION); } + // eslint-disable-next-line @typescript-eslint/no-explicit-any public override log(info: any, next: () => void) { try { emitLogRecord(info, this._logger); diff --git a/packages/winston-transport/src/utils.ts b/packages/winston-transport/src/utils.ts index 7f9eaae73e..f863ae7f70 100644 --- a/packages/winston-transport/src/utils.ts +++ b/packages/winston-transport/src/utils.ts @@ -60,6 +60,7 @@ function getSeverityNumber(level: string): SeverityNumber | undefined { } export function emitLogRecord( + // eslint-disable-next-line @typescript-eslint/no-explicit-any record: Record, logger: Logger ): void {