@@ -31,11 +31,11 @@ import {
3131 isPreConnectionFailure ,
3232 resolveErrorInfoDomain ,
3333 resolveErrorInfoMetadataAttributes ,
34+ resolveErrorResponsePayload ,
3435 resolveErrorType ,
3536 resolveExceptionMessage ,
3637 resolveExceptionStacktrace ,
3738 resolveExceptionType ,
38- resolveResponsePayload ,
3939 resolveResponseStatusCode ,
4040 resolveRpcStatusName ,
4141} from './errorUtil' ;
@@ -63,19 +63,15 @@ export interface LogPayload {
6363}
6464
6565/**
66- * Resolves a `LogSeverity` value across `google-logging-utils` versions where
67- * `LogSeverity` may be exported as a type only.
66+ * Resolves a `LogSeverity` enum value. Until a `google-logging-utils` release
67+ * containing #9718 is published to npm (this repo uses `link-workspace-packages=false`),
68+ * the published package exports `LogSeverity` as a type only while each enum member's
69+ * runtime value equals its uppercase level string.
6870 */
6971function resolveLogSeverity (
7072 level : 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR' ,
7173) : loggingUtils . LogSeverity {
72- return (
73- (
74- loggingUtils as {
75- LogSeverity ?: Record < string , loggingUtils . LogSeverity > ;
76- }
77- ) . LogSeverity ?. [ level ] ?? ( level as loggingUtils . LogSeverity )
78- ) ;
74+ return level as loggingUtils . LogSeverity ;
7975}
8076
8177/**
@@ -172,22 +168,32 @@ export function buildLogAttributes(
172168
173169/**
174170 * Executes `fn` and attaches stream, promise, callback, or synchronous error
175- * handlers to invoke `onError` when the operation fails.
171+ * handlers to invoke `onError` at most once when the operation fails.
176172 */
177173function executeLoggedOperation < T > (
178174 fn : ( loggedCallback ?: APICallback ) => T ,
179175 onError : ( err : unknown ) => void ,
180176 isStreamCall : boolean ,
181177 userCallback ?: APICallback ,
182178) : T {
179+ // Latch to emit at most one log per invocation across execution boundaries.
180+ let hasLogged = false ;
181+ const logOnce = ( err : unknown ) => {
182+ if ( hasLogged ) {
183+ return ;
184+ }
185+ hasLogged = true ;
186+ onError ( err ) ;
187+ } ;
188+
183189 // Wrap user callback to log RPC errors before forwarding.
184190 let callbackInvoked = false ;
185191 const loggedCallback : APICallback | undefined = userCallback
186192 ? function ( this : unknown , ...args : Parameters < APICallback > ) {
187193 callbackInvoked = true ;
188194 const err = args [ 0 ] ;
189195 if ( err ) {
190- onError ( err ) ;
196+ logOnce ( err ) ;
191197 }
192198 userCallback . apply ( this , args ) ;
193199 }
@@ -198,15 +204,15 @@ function executeLoggedOperation<T>(
198204 const result = fn ( loggedCallback ) ;
199205 const promiseTarget = ! isStreamCall ? getPromiseTarget ( result ) : null ;
200206 if ( isStreamCall && result instanceof EventEmitter ) {
201- handleStream ( result , onError , ( ) => { } , Boolean ( loggedCallback ) ) ;
207+ handleStream ( result , logOnce , ( ) => { } , Boolean ( loggedCallback ) ) ;
202208 } else if ( promiseTarget ) {
203- handlePromise ( promiseTarget , onError , ( ) => { } ) ;
209+ handlePromise ( promiseTarget , logOnce , ( ) => { } ) ;
204210 }
205211 return result ;
206212 } catch ( syncErr ) {
207213 // Avoid logging errors thrown inside userCallback after RPC completion.
208214 if ( ! callbackInvoked ) {
209- onError ( syncErr ) ;
215+ logOnce ( syncErr ) ;
210216 }
211217 throw syncErr ;
212218 }
@@ -232,18 +238,10 @@ export function logCall<T = GaxCallResult>(
232238 isStreamCall = false ,
233239 userCallback ?: APICallback ,
234240) : T {
235- // Latch to emit at most one warning log per logical request.
236- let hasLoggedL3 = false ;
237241 // Snapshot initial context in case async rejection loses active context.
238242 const initialContext = context . active ( ) ;
239243
240244 const emitL3WarnLog = ( err : unknown ) => {
241- // Suppress duplicate emissions for the same call.
242- if ( hasLoggedL3 ) {
243- return ;
244- }
245- hasLoggedL3 = true ;
246-
247245 // Resolve logger and active T3 client request span (or outer OpenTelemetry span when GAX tracing is disabled).
248246 const logger = getGaxLogger ( staticArgs ?. gcpClientService ) ;
249247 const activeTrace = getActiveTraceContext ( ) ;
@@ -300,20 +298,22 @@ export function logNetworkAttempt<T = GaxCallResult>(
300298 const initialContext = context . active ( ) ;
301299
302300 const emitL4DebugLog = ( err : unknown ) => {
303- // Resolve logger and active T4 attempt span context for this attempt .
301+ // Resolve logger and active T4 attempt span (or outer OpenTelemetry span when GAX tracing is disabled) .
304302 const logger = getGaxLogger ( staticArgs ?. gcpClientService ) ;
303+ const activeTrace = getActiveTraceContext ( ) ;
304+ const initialTrace = getActiveTraceContext ( initialContext ) ;
305305 const attemptSpan =
306- getActiveTraceContext ( ) ?. attemptSpan ??
307- getActiveTraceContext ( initialContext ) ?. attemptSpan ;
306+ activeTrace ?. attemptSpan ??
307+ initialTrace ?. attemptSpan ??
308+ activeTrace ?. span ??
309+ initialTrace ?. span ;
308310 const spanContext = attemptSpan ?. spanContext ( ) ;
309311 const traceId = spanContext ?. traceId ;
310312 const spanId = spanContext ?. spanId ;
311313
312- // Build structured log attributes and attach attempt metadata payload.
314+ // Build structured log attributes and attach raw attempt response payload when present .
313315 const attributes = buildLogAttributes ( err , attemptDynamicArgs , staticArgs ) ;
314- const responsePayload = resolveResponsePayload (
315- ( err as { metadata ?: unknown } | null | undefined ) ?. metadata ,
316- ) ;
316+ const responsePayload = resolveErrorResponsePayload ( err ) ;
317317 if ( responsePayload !== undefined ) {
318318 attributes [ 'response.payload' ] = responsePayload ;
319319 }
0 commit comments