@@ -26,23 +26,23 @@ import {
2626 Tracer ,
2727} from '@opentelemetry/api' ;
2828import { APICallback , GaxCallResult } from '../apitypes' ;
29- import { Status } from '../status' ;
3029import {
3130 ATTEMPT_SPAN_KEY ,
3231 AttemptTraceContext ,
3332 buildInitialAttributes ,
3433 CLIENT_REQUEST_SPAN_KEY ,
3534 DynamicTraceContext ,
3635 getActiveTraceContext ,
36+ resolveEndpointAndDomain ,
37+ resolveRpcMethod ,
3738 StaticTraceContext ,
3839} from './contextUtil' ;
3940import {
4041 isPreConnectionFailure ,
4142 recordExceptionEvent ,
4243 resolveErrorMessage ,
4344 resolveErrorType ,
44- resolveHttpStatusCode ,
45- resolveRpcStatusName ,
45+ resolveResponseStatusCode ,
4646} from './errorUtil' ;
4747
4848const attemptUrlTemplates = new WeakMap < Span , string > ( ) ;
@@ -284,59 +284,6 @@ export function handleStream(
284284 }
285285}
286286
287- /**
288- * Resolves and parses the server address, port, and target service domain (`url.domain`)
289- * from dynamic and static trace contexts, splitting `"host:port"` or `"[ipv6]:port"`
290- * strings. When `defaultPort` is provided (for low level network attempt spans), falls back to `url.domain`
291- * for the server address and defaults the port to `defaultPort` if not otherwise specified.
292- *
293- * @param {DynamicTraceContext } dynamicArgs - Dynamic trace context for the call or attempt.
294- * @param {StaticTraceContext } staticArgs - Static trace context for the client library.
295- * @param {number } [defaultPort] - Optional default port (e.g. 443 for low level network attempt spans).
296- * @returns {{rawAddress?: string; rawPort?: number; urlDomain?: string} } Resolved address, port, and domain.
297- */
298- function resolveEndpointAndDomain (
299- dynamicArgs : DynamicTraceContext ,
300- staticArgs : StaticTraceContext ,
301- defaultPort ?: number ,
302- ) : { rawAddress ?: string ; rawPort ?: number ; urlDomain ?: string } {
303- let rawAddress = dynamicArgs . serverAddress ?? staticArgs . serverAddress ;
304- let rawPort = dynamicArgs . serverPort ?? staticArgs . serverPort ;
305- const splitHostAndPort = ( addr : string ) => {
306- const match = addr . match ( / ^ ( \[ [ ^ \] ] + \] | [ ^ : ] + ) : ( \d + ) $ / ) ;
307- if ( match ) {
308- rawAddress = match [ 1 ] ;
309- rawPort = rawPort ?? Number ( match [ 2 ] ) ;
310- } else {
311- rawAddress = addr ;
312- }
313- } ;
314-
315- if ( rawAddress ) {
316- // Split embedded port from "host:port" or "[ipv6]:port" if present.
317- splitHostAndPort ( rawAddress ) ;
318- }
319-
320- let urlDomain = dynamicArgs . urlDomain ?? staticArgs . urlDomain ?? rawAddress ;
321- if ( ! urlDomain && staticArgs . gcpClientService ) {
322- // Append ".googleapis.com" when gcpClientService is a short service name.
323- urlDomain = staticArgs . gcpClientService . includes ( '.' )
324- ? staticArgs . gcpClientService
325- : `${ staticArgs . gcpClientService } .googleapis.com` ;
326- }
327-
328- if ( defaultPort !== undefined ) {
329- if ( ! rawAddress && urlDomain ) {
330- splitHostAndPort ( urlDomain ) ;
331- }
332- if ( rawAddress ) {
333- rawPort = rawPort ?? defaultPort ;
334- }
335- }
336-
337- return { rawAddress, rawPort, urlDomain} ;
338- }
339-
340287/**
341288 * Returns the transport-specific resend count attribute key.
342289 *
@@ -402,25 +349,20 @@ function setFinalStatusAttributes(
402349
403350/**
404351 * Records error attributes (`error.type`, `status.message`), `exception` span event,
405- * and `ERROR` status on a span, returning the resolved transport status codes .
352+ * and `ERROR` status on a span.
406353 *
407354 * @param {Span } span - The span on which to record the error.
408355 * @param {unknown } e - The error thrown or passed to the callback/stream/promise.
409356 * @param {'grpc' | 'http' } rpcType - The RPC transport protocol.
410357 * @param {boolean } [includeExceptionMessage=false] - Whether to include `exception.message`
411358 * on the `exception` event (true for low level network attempt spans, false for client request spans).
412- * @returns {{rpcStatusName?: string; httpStatusCode?: number} } The resolved status codes.
413359 */
414360function recordSpanError (
415361 span : Span ,
416362 e : unknown ,
417363 rpcType : 'grpc' | 'http' ,
418364 includeExceptionMessage = false ,
419- ) : { rpcStatusName ?: string ; httpStatusCode ?: number } {
420- const rpcStatusName =
421- rpcType === 'grpc' ? resolveRpcStatusName ( e ) : undefined ;
422- const httpStatusCode =
423- rpcType === 'http' ? resolveHttpStatusCode ( e ) : undefined ;
365+ ) : void {
424366 const message = e instanceof Error ? e . message : resolveErrorMessage ( e ) ;
425367 span . setAttributes ( {
426368 'error.type' : resolveErrorType ( e , rpcType ) ,
@@ -430,7 +372,6 @@ function recordSpanError(
430372 recordExceptionEvent ( span , e , rpcType , includeExceptionMessage ) ;
431373 }
432374 span . setStatus ( { code : SpanStatusCode . ERROR , message} ) ;
433- return { rpcStatusName, httpStatusCode} ;
434375}
435376
436377/**
@@ -471,28 +412,22 @@ function createSpanCompletionHandlers(
471412 let spanEnded = false ;
472413 let errorRecorded = false ;
473414 let recordedError : unknown ;
474- let rpcStatusName : string | undefined ;
475- let httpStatusCode : number | undefined ;
476415
477416 const recordError = ( e : unknown ) => {
478417 recordedError = e ;
479418 errorRecorded = true ;
480- ( { rpcStatusName, httpStatusCode} = recordSpanError (
481- span ,
482- e ,
483- rpcType ,
484- includeExceptionMessage ,
485- ) ) ;
419+ recordSpanError ( span , e , rpcType , includeExceptionMessage ) ;
486420 } ;
487421
488422 const endSpan = ( ) => {
489423 if ( ! spanEnded ) {
490424 spanEnded = true ;
491425 // Default to OK / 200 when no error was recorded.
492- if ( ! errorRecorded ) {
493- rpcStatusName = Status [ Status . OK ] ;
494- httpStatusCode = 200 ;
495- }
426+ const { rpcStatusName, httpStatusCode} = resolveResponseStatusCode (
427+ rpcType ,
428+ errorRecorded ,
429+ recordedError ,
430+ ) ;
496431 setFinalStatusAttributes (
497432 span ,
498433 rpcType ,
@@ -675,9 +610,7 @@ export function traceAttempt<T = GaxCallResult>(
675610 parentContext ?: Context ,
676611) : T {
677612 // Resolve RPC method and transport-specific span name.
678- const rpcMethod = dynamicArgs . apiName
679- ? `${ dynamicArgs . apiName } /${ dynamicArgs . methodName } `
680- : dynamicArgs . methodName ;
613+ const rpcMethod = resolveRpcMethod ( dynamicArgs ) ;
681614 const httpMethod = dynamicArgs . httpMethod ?? 'POST' ;
682615 const urlTemplate = dynamicArgs . urlTemplate ;
683616 const spanName =
0 commit comments