@@ -21,6 +21,7 @@ import {
2121 NoopLogRecordProcessor ,
2222} from '@opentelemetry/sdk-logs' ;
2323import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions' ;
24+ import isPlainObject from 'lodash.isplainobject' ;
2425
2526import { version as PKG_VERSION } from '../../package.json' ;
2627import {
@@ -31,6 +32,7 @@ import {
3132 DEFAULT_SEND_INTERVAL_MS ,
3233 DEFAULT_SERVICE_NAME ,
3334} from '../constants' ;
35+ import { jsonToString } from '../utils' ;
3436
3537const LOG_PREFIX = `⚠️ [LOGGER]` ;
3638
@@ -85,6 +87,32 @@ export function getSeverityNumber(level: string): SeverityNumber | undefined {
8587 return npmLevels [ level ] ?? sysLoglevels [ level ] ?? cliLevels [ level ] ;
8688}
8789
90+ export const parseLogAttributes = (
91+ meta : Record < string , any > ,
92+ ) : LogAttributes => {
93+ try {
94+ const attributes : LogAttributes = { } ;
95+ for ( const key in meta ) {
96+ if ( Object . prototype . hasOwnProperty . call ( meta , key ) ) {
97+ const value = meta [ key ] ;
98+ // stringify array of objects
99+ if ( Array . isArray ( value ) ) {
100+ const firstItem = value [ 0 ] ;
101+ if ( isPlainObject ( firstItem ) ) {
102+ attributes [ key ] = jsonToString ( value ) ;
103+ }
104+ } else {
105+ attributes [ key ] = value ;
106+ }
107+ }
108+ }
109+ return attributes ;
110+ } catch ( error ) {
111+ diag . error ( `${ LOG_PREFIX } Failed to parse log attributes. e = ${ error } ` ) ;
112+ return meta ;
113+ }
114+ } ;
115+
88116export class Logger {
89117 private readonly _url : string ;
90118
@@ -204,19 +232,13 @@ export class Logger {
204232 body : string ,
205233 meta : Record < string , any > = { } ,
206234 ) : void {
207- const attributes : LogAttributes = { } ;
208- for ( const key in meta ) {
209- if ( Object . prototype . hasOwnProperty . call ( meta , key ) ) {
210- attributes [ key ] = meta [ key ] ;
211- }
212- }
213235 this . logger . emit ( {
214236 // TODO: should map to otel severity number
215237 severityNumber : getSeverityNumber ( level ) ,
216238 // TODO: set up the mapping between different downstream log levels
217239 severityText : level ,
218240 body,
219- attributes,
241+ attributes : parseLogAttributes ( meta ) ,
220242 timestamp : this . parseTimestamp ( meta ) ,
221243 } ) ;
222244 }
0 commit comments