@@ -886,96 +886,104 @@ export const getIncomingRequestAttributes = (
886886 } ,
887887 logger : DiagLogger
888888) : Attributes => {
889- const headers = request . headers ;
890- const userAgent = headers [ 'user-agent' ] ;
891- const ips = headers [ 'x-forwarded-for' ] ;
892- const httpVersion = request . httpVersion ;
893- const host = headers . host ;
894- const hostname = host ?. replace ( / ^ ( .* ) ( : [ 0 - 9 ] { 1 , 5 } ) / , '$1' ) || 'localhost' ;
895-
896- const method = request . method ;
897- const normalizedMethod = normalizeMethod ( method ) ;
889+ const {
890+ component,
891+ enableSyntheticSourceDetection,
892+ hookAttributes,
893+ semconvStability,
894+ serverName,
895+ } = options ;
896+ const { headers, httpVersion, method } = request ;
897+ const { host, 'user-agent' : userAgent , 'x-forwarded-for' : ips } = headers ;
898+ const parsedUrl = getInfoFromIncomingMessage ( component , request , logger ) ;
899+ let newAttributes : Attributes ;
900+ let oldAttributes : Attributes ;
901+
902+ if ( semconvStability !== SemconvStability . OLD ) {
903+ // Stable attributes are used.
904+ const normalizedMethod = normalizeMethod ( method ) ;
905+ const serverAddress = getServerAddress ( request , component ) ;
906+ const remoteClientAddress = getRemoteClientAddress ( request ) ;
907+
908+ newAttributes = {
909+ [ ATTR_HTTP_REQUEST_METHOD ] : normalizedMethod ,
910+ [ ATTR_URL_SCHEME ] : component ,
911+ [ ATTR_SERVER_ADDRESS ] : serverAddress ?. host ,
912+ [ ATTR_NETWORK_PEER_ADDRESS ] : request . socket . remoteAddress ,
913+ [ ATTR_NETWORK_PEER_PORT ] : request . socket . remotePort ,
914+ [ ATTR_NETWORK_PROTOCOL_VERSION ] : request . httpVersion ,
915+ [ ATTR_USER_AGENT_ORIGINAL ] : userAgent ,
916+ } ;
898917
899- const serverAddress = getServerAddress ( request , options . component ) ;
900- const serverName = options . serverName ;
901- const remoteClientAddress = getRemoteClientAddress ( request ) ;
918+ if ( parsedUrl . pathname != null ) {
919+ newAttributes [ ATTR_URL_PATH ] = parsedUrl . pathname ;
920+ }
902921
903- const newAttributes : Attributes = {
904- [ ATTR_HTTP_REQUEST_METHOD ] : normalizedMethod ,
905- [ ATTR_URL_SCHEME ] : options . component ,
906- [ ATTR_SERVER_ADDRESS ] : serverAddress ?. host ,
907- [ ATTR_NETWORK_PEER_ADDRESS ] : request . socket . remoteAddress ,
908- [ ATTR_NETWORK_PEER_PORT ] : request . socket . remotePort ,
909- [ ATTR_NETWORK_PROTOCOL_VERSION ] : request . httpVersion ,
910- [ ATTR_USER_AGENT_ORIGINAL ] : userAgent ,
911- } ;
922+ if ( parsedUrl . search ) {
923+ // Remove leading '?' from URL search (https://developer.mozilla.org/en-US/docs/Web/API/URL/search).
924+ newAttributes [ ATTR_URL_QUERY ] = parsedUrl . search . slice ( 1 ) ;
925+ }
912926
913- const parsedUrl = getInfoFromIncomingMessage (
914- options . component ,
915- request ,
916- logger
917- ) ;
927+ if ( remoteClientAddress != null ) {
928+ newAttributes [ ATTR_CLIENT_ADDRESS ] = remoteClientAddress ;
929+ }
918930
919- if ( parsedUrl ?. pathname != null ) {
920- newAttributes [ ATTR_URL_PATH ] = parsedUrl . pathname ;
921- }
931+ if ( serverAddress ?. port != null ) {
932+ newAttributes [ ATTR_SERVER_PORT ] = Number ( serverAddress . port ) ;
933+ }
922934
923- if ( parsedUrl . search ) {
924- // Remove leading '?' from URL search (https://developer.mozilla.org/en-US/docs/Web/API/URL/search).
925- newAttributes [ ATTR_URL_QUERY ] = parsedUrl . search . slice ( 1 ) ;
926- }
935+ // Conditionally required if request method required case normalization.
936+ if ( method !== normalizedMethod ) {
937+ newAttributes [ ATTR_HTTP_REQUEST_METHOD_ORIGINAL ] = method ;
938+ }
927939
928- if ( remoteClientAddress != null ) {
929- newAttributes [ ATTR_CLIENT_ADDRESS ] = remoteClientAddress ;
940+ if ( enableSyntheticSourceDetection && userAgent ) {
941+ newAttributes [ ATTR_USER_AGENT_SYNTHETIC_TYPE ] =
942+ getSyntheticType ( userAgent ) ;
943+ }
930944 }
931945
932- if ( serverAddress ?. port != null ) {
933- newAttributes [ ATTR_SERVER_PORT ] = Number ( serverAddress . port ) ;
934- }
946+ if ( semconvStability !== SemconvStability . STABLE ) {
947+ // Old attributes are used.
948+ const hostname = host ?. replace ( / ^ ( . * ) ( : [ 0 - 9 ] { 1 , 5 } ) / , '$1' ) || 'localhost' ;
935949
936- // conditionally required if request method required case normalization
937- if ( method !== normalizedMethod ) {
938- newAttributes [ ATTR_HTTP_REQUEST_METHOD_ORIGINAL ] = method ;
939- }
950+ oldAttributes = {
951+ [ ATTR_HTTP_URL ] : parsedUrl . toString ( ) ,
952+ [ ATTR_HTTP_HOST ] : host ,
953+ [ ATTR_NET_HOST_NAME ] : hostname ,
954+ [ ATTR_HTTP_METHOD ] : method ,
955+ [ ATTR_HTTP_SCHEME ] : component ,
956+ } ;
940957
941- if ( options . enableSyntheticSourceDetection && userAgent ) {
942- newAttributes [ ATTR_USER_AGENT_SYNTHETIC_TYPE ] = getSyntheticType ( userAgent ) ;
943- }
944- const oldAttributes : Attributes = {
945- [ ATTR_HTTP_URL ] : parsedUrl . toString ( ) ,
946- [ ATTR_HTTP_HOST ] : host ,
947- [ ATTR_NET_HOST_NAME ] : hostname ,
948- [ ATTR_HTTP_METHOD ] : method ,
949- [ ATTR_HTTP_SCHEME ] : options . component ,
950- } ;
958+ if ( typeof ips === 'string' ) {
959+ oldAttributes [ ATTR_HTTP_CLIENT_IP ] = ips . split ( ',' ) [ 0 ] ;
960+ }
951961
952- if ( typeof ips === 'string' ) {
953- oldAttributes [ ATTR_HTTP_CLIENT_IP ] = ips . split ( ',' ) [ 0 ] ;
954- }
962+ if ( typeof serverName === 'string' ) {
963+ oldAttributes [ ATTR_HTTP_SERVER_NAME ] = serverName ;
964+ }
955965
956- if ( typeof serverName === 'string' ) {
957- oldAttributes [ ATTR_HTTP_SERVER_NAME ] = serverName ;
958- }
966+ if ( parsedUrl . pathname ) {
967+ oldAttributes [ ATTR_HTTP_TARGET ] =
968+ parsedUrl . pathname + parsedUrl . search || '/' ;
969+ }
959970
960- if ( parsedUrl ?. pathname ) {
961- oldAttributes [ ATTR_HTTP_TARGET ] =
962- parsedUrl ?. pathname + parsedUrl ?. search || '/' ;
963- }
971+ if ( userAgent !== undefined ) {
972+ oldAttributes [ ATTR_HTTP_USER_AGENT ] = userAgent ;
973+ }
964974
965- if ( userAgent !== undefined ) {
966- oldAttributes [ ATTR_HTTP_USER_AGENT ] = userAgent ;
975+ setRequestContentLengthAttribute ( request , oldAttributes ) ;
976+ setAttributesFromHttpKind ( httpVersion , oldAttributes ) ;
967977 }
968- setRequestContentLengthAttribute ( request , oldAttributes ) ;
969- setAttributesFromHttpKind ( httpVersion , oldAttributes ) ;
970978
971- switch ( options . semconvStability ) {
979+ switch ( semconvStability ) {
972980 case SemconvStability . STABLE :
973- return Object . assign ( newAttributes , options . hookAttributes ) ;
981+ return Object . assign ( newAttributes ! , hookAttributes ) ;
974982 case SemconvStability . OLD :
975- return Object . assign ( oldAttributes , options . hookAttributes ) ;
983+ return Object . assign ( oldAttributes ! , hookAttributes ) ;
984+ default :
985+ return Object . assign ( oldAttributes ! , newAttributes ! , hookAttributes ) ;
976986 }
977-
978- return Object . assign ( oldAttributes , newAttributes , options . hookAttributes ) ;
979987} ;
980988
981989/**
0 commit comments