@@ -4,7 +4,10 @@ import os from "node:os";
44import path from "node:path" ;
55import { Logger as TsLogger } from "tslog" ;
66import type { OpenClawConfig } from "../config/types.js" ;
7- import { emitDiagnosticEvent } from "../infra/diagnostic-events.js" ;
7+ import {
8+ emitDiagnosticEvent ,
9+ emitDiagnosticEventWithTrustedTraceContext ,
10+ } from "../infra/diagnostic-events.js" ;
811import {
912 getActiveDiagnosticTraceContext ,
1013 isValidDiagnosticSpanId ,
@@ -359,9 +362,23 @@ function findLogTraceContext(
359362 return undefined ;
360363}
361364
365+ function resolveLogTraceContext (
366+ bindings : Record < string , unknown > | undefined ,
367+ numericArgs : readonly unknown [ ] ,
368+ ) : { trace ?: DiagnosticTraceContext ; trustedTraceContext : boolean } {
369+ const explicitTrace = findLogTraceContext ( bindings , numericArgs ) ;
370+ if ( explicitTrace ) {
371+ return { trace : explicitTrace , trustedTraceContext : false } ;
372+ }
373+ const activeTrace = getActiveDiagnosticTraceContext ( ) ;
374+ return activeTrace
375+ ? { trace : activeTrace , trustedTraceContext : true }
376+ : { trustedTraceContext : false } ;
377+ }
378+
362379function buildTraceFileLogFields ( logObj : TsLogRecord ) : Record < string , string > | undefined {
363380 const { bindings, args } = extractLogBindingPrefix ( getSortedNumericLogArgs ( logObj ) ) ;
364- const trace = findLogTraceContext ( bindings , args ) ?? getActiveDiagnosticTraceContext ( ) ;
381+ const { trace } = resolveLogTraceContext ( bindings , args ) ;
365382 if ( ! trace ) {
366383 return undefined ;
367384 }
@@ -410,7 +427,7 @@ function buildDiagnosticLogRecord(logObj: TsLogRecord) {
410427 | undefined ;
411428 const { bindings, args : numericArgs } = extractLogBindingPrefix ( getSortedNumericLogArgs ( logObj ) ) ;
412429
413- const trace = findLogTraceContext ( bindings , numericArgs ) ?? getActiveDiagnosticTraceContext ( ) ;
430+ const { trace, trustedTraceContext } = resolveLogTraceContext ( bindings , numericArgs ) ;
414431 const structuredArg = numericArgs [ 0 ] ;
415432 const structuredBindings = isPlainLogRecordObject ( structuredArg ) ? structuredArg : undefined ;
416433 if ( structuredBindings ) {
@@ -456,14 +473,17 @@ function buildDiagnosticLogRecord(logObj: TsLogRecord) {
456473 . filter ( ( name ) : name is string => Boolean ( name ) ) ;
457474
458475 return {
459- type : "log.record" as const ,
460- level : meta ?. logLevelName ?? "INFO" ,
461- message,
462- ...( loggerName ? { loggerName } : { } ) ,
463- ...( loggerParents ?. length ? { loggerParents } : { } ) ,
464- ...( Object . keys ( attributes ) . length > 0 ? { attributes } : { } ) ,
465- ...( Object . keys ( code ) . length > 0 ? { code } : { } ) ,
466- ...( trace ? { trace } : { } ) ,
476+ event : {
477+ type : "log.record" as const ,
478+ level : meta ?. logLevelName ?? "INFO" ,
479+ message,
480+ ...( loggerName ? { loggerName } : { } ) ,
481+ ...( loggerParents ?. length ? { loggerParents } : { } ) ,
482+ ...( Object . keys ( attributes ) . length > 0 ? { attributes } : { } ) ,
483+ ...( Object . keys ( code ) . length > 0 ? { code } : { } ) ,
484+ ...( trace ? { trace } : { } ) ,
485+ } ,
486+ trustedTraceContext,
467487 } ;
468488}
469489
@@ -478,9 +498,11 @@ function redactLogRecordForTransport<T extends LogObj>(record: T): T {
478498function attachDiagnosticEventTransport ( logger : TsLogger < LogObj > ) : void {
479499 logger . attachTransport ( ( logObj : LogObj ) => {
480500 try {
481- emitDiagnosticEvent (
482- buildDiagnosticLogRecord ( redactLogRecordForTransport ( logObj ) as TsLogRecord ) ,
483- ) ;
501+ const record = buildDiagnosticLogRecord ( redactLogRecordForTransport ( logObj ) as TsLogRecord ) ;
502+ const emit = record . trustedTraceContext
503+ ? emitDiagnosticEventWithTrustedTraceContext
504+ : emitDiagnosticEvent ;
505+ emit ( record . event ) ;
484506 } catch {
485507 // never block on logging failures
486508 }
0 commit comments