@@ -22,8 +22,30 @@ import {
2222 SpanKind ,
2323} from '@opentelemetry/api' ;
2424import {
25- NetTransportValues ,
26- SemanticAttributes ,
25+ NETTRANSPORTVALUES_IP_TCP ,
26+ NETTRANSPORTVALUES_IP_UDP ,
27+ SEMATTRS_HTTP_CLIENT_IP ,
28+ SEMATTRS_HTTP_FLAVOR ,
29+ SEMATTRS_HTTP_HOST ,
30+ SEMATTRS_HTTP_METHOD ,
31+ SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH ,
32+ SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED ,
33+ SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH ,
34+ SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED ,
35+ SEMATTRS_HTTP_ROUTE ,
36+ SEMATTRS_HTTP_SCHEME ,
37+ SEMATTRS_HTTP_SERVER_NAME ,
38+ SEMATTRS_HTTP_STATUS_CODE ,
39+ SEMATTRS_HTTP_TARGET ,
40+ SEMATTRS_HTTP_URL ,
41+ SEMATTRS_HTTP_USER_AGENT ,
42+ SEMATTRS_NET_HOST_IP ,
43+ SEMATTRS_NET_HOST_NAME ,
44+ SEMATTRS_NET_HOST_PORT ,
45+ SEMATTRS_NET_PEER_IP ,
46+ SEMATTRS_NET_PEER_NAME ,
47+ SEMATTRS_NET_PEER_PORT ,
48+ SEMATTRS_NET_TRANSPORT ,
2749} from '@opentelemetry/semantic-conventions' ;
2850import {
2951 IncomingHttpHeaders ,
@@ -167,10 +189,9 @@ export const setRequestContentLengthAttribute = (
167189 if ( length === null ) return ;
168190
169191 if ( isCompressed ( request . headers ) ) {
170- attributes [ SemanticAttributes . HTTP_REQUEST_CONTENT_LENGTH ] = length ;
192+ attributes [ SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH ] = length ;
171193 } else {
172- attributes [ SemanticAttributes . HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED ] =
173- length ;
194+ attributes [ SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED ] = length ;
174195 }
175196} ;
176197
@@ -187,10 +208,9 @@ export const setResponseContentLengthAttribute = (
187208 if ( length === null ) return ;
188209
189210 if ( isCompressed ( response . headers ) ) {
190- attributes [ SemanticAttributes . HTTP_RESPONSE_CONTENT_LENGTH ] = length ;
211+ attributes [ SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH ] = length ;
191212 } else {
192- attributes [ SemanticAttributes . HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED ] =
193- length ;
213+ attributes [ SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED ] = length ;
194214 }
195215} ;
196216
@@ -343,20 +363,19 @@ export const getOutgoingRequestAttributes = (
343363 const headers = requestOptions . headers || { } ;
344364 const userAgent = headers [ 'user-agent' ] ;
345365 const attributes : SpanAttributes = {
346- [ SemanticAttributes . HTTP_URL ] : getAbsoluteUrl (
366+ [ SEMATTRS_HTTP_URL ] : getAbsoluteUrl (
347367 requestOptions ,
348368 headers ,
349369 `${ options . component } :`
350370 ) ,
351- [ SemanticAttributes . HTTP_METHOD ] : method ,
352- [ SemanticAttributes . HTTP_TARGET ] : requestOptions . path || '/' ,
353- [ SemanticAttributes . NET_PEER_NAME ] : hostname ,
354- [ SemanticAttributes . HTTP_HOST ] :
355- requestOptions . headers ?. host ?? `${ hostname } :${ port } ` ,
371+ [ SEMATTRS_HTTP_METHOD ] : method ,
372+ [ SEMATTRS_HTTP_TARGET ] : requestOptions . path || '/' ,
373+ [ SEMATTRS_NET_PEER_NAME ] : hostname ,
374+ [ SEMATTRS_HTTP_HOST ] : requestOptions . headers ?. host ?? `${ hostname } :${ port } ` ,
356375 } ;
357376
358377 if ( userAgent !== undefined ) {
359- attributes [ SemanticAttributes . HTTP_USER_AGENT ] = userAgent ;
378+ attributes [ SEMATTRS_HTTP_USER_AGENT ] = userAgent ;
360379 }
361380 return Object . assign ( attributes , options . hookAttributes ) ;
362381} ;
@@ -369,10 +388,9 @@ export const getOutgoingRequestMetricAttributes = (
369388 spanAttributes : SpanAttributes
370389) : MetricAttributes => {
371390 const metricAttributes : MetricAttributes = { } ;
372- metricAttributes [ SemanticAttributes . HTTP_METHOD ] =
373- spanAttributes [ SemanticAttributes . HTTP_METHOD ] ;
374- metricAttributes [ SemanticAttributes . NET_PEER_NAME ] =
375- spanAttributes [ SemanticAttributes . NET_PEER_NAME ] ;
391+ metricAttributes [ SEMATTRS_HTTP_METHOD ] = spanAttributes [ SEMATTRS_HTTP_METHOD ] ;
392+ metricAttributes [ SEMATTRS_NET_PEER_NAME ] =
393+ spanAttributes [ SEMATTRS_NET_PEER_NAME ] ;
376394 //TODO: http.url attribute, it should substitute any parameters to avoid high cardinality.
377395 return metricAttributes ;
378396} ;
@@ -384,11 +402,11 @@ export const getOutgoingRequestMetricAttributes = (
384402export const getAttributesFromHttpKind = ( kind ?: string ) : SpanAttributes => {
385403 const attributes : SpanAttributes = { } ;
386404 if ( kind ) {
387- attributes [ SemanticAttributes . HTTP_FLAVOR ] = kind ;
405+ attributes [ SEMATTRS_HTTP_FLAVOR ] = kind ;
388406 if ( kind . toUpperCase ( ) !== 'QUIC' ) {
389- attributes [ SemanticAttributes . NET_TRANSPORT ] = NetTransportValues . IP_TCP ;
407+ attributes [ SEMATTRS_NET_TRANSPORT ] = NETTRANSPORTVALUES_IP_TCP ;
390408 } else {
391- attributes [ SemanticAttributes . NET_TRANSPORT ] = NetTransportValues . IP_UDP ;
409+ attributes [ SEMATTRS_NET_TRANSPORT ] = NETTRANSPORTVALUES_IP_UDP ;
392410 }
393411 }
394412 return attributes ;
@@ -406,13 +424,13 @@ export const getOutgoingRequestAttributesOnResponse = (
406424 const attributes : SpanAttributes = { } ;
407425 if ( socket ) {
408426 const { remoteAddress, remotePort } = socket ;
409- attributes [ SemanticAttributes . NET_PEER_IP ] = remoteAddress ;
410- attributes [ SemanticAttributes . NET_PEER_PORT ] = remotePort ;
427+ attributes [ SEMATTRS_NET_PEER_IP ] = remoteAddress ;
428+ attributes [ SEMATTRS_NET_PEER_PORT ] = remotePort ;
411429 }
412430 setResponseContentLengthAttribute ( response , attributes ) ;
413431
414432 if ( statusCode ) {
415- attributes [ SemanticAttributes . HTTP_STATUS_CODE ] = statusCode ;
433+ attributes [ SEMATTRS_HTTP_STATUS_CODE ] = statusCode ;
416434 attributes [ AttributeNames . HTTP_STATUS_TEXT ] = (
417435 statusMessage || ''
418436 ) . toUpperCase ( ) ;
@@ -430,12 +448,11 @@ export const getOutgoingRequestMetricAttributesOnResponse = (
430448 spanAttributes : SpanAttributes
431449) : MetricAttributes => {
432450 const metricAttributes : MetricAttributes = { } ;
433- metricAttributes [ SemanticAttributes . NET_PEER_PORT ] =
434- spanAttributes [ SemanticAttributes . NET_PEER_PORT ] ;
435- metricAttributes [ SemanticAttributes . HTTP_STATUS_CODE ] =
436- spanAttributes [ SemanticAttributes . HTTP_STATUS_CODE ] ;
437- metricAttributes [ SemanticAttributes . HTTP_FLAVOR ] =
438- spanAttributes [ SemanticAttributes . HTTP_FLAVOR ] ;
451+ metricAttributes [ SEMATTRS_NET_PEER_PORT ] =
452+ spanAttributes [ SEMATTRS_NET_PEER_PORT ] ;
453+ metricAttributes [ SEMATTRS_HTTP_STATUS_CODE ] =
454+ spanAttributes [ SEMATTRS_HTTP_STATUS_CODE ] ;
455+ metricAttributes [ SEMATTRS_HTTP_FLAVOR ] = spanAttributes [ SEMATTRS_HTTP_FLAVOR ] ;
439456 return metricAttributes ;
440457} ;
441458
@@ -465,31 +482,31 @@ export const getIncomingRequestAttributes = (
465482 'localhost' ;
466483 const serverName = options . serverName ;
467484 const attributes : SpanAttributes = {
468- [ SemanticAttributes . HTTP_URL ] : getAbsoluteUrl (
485+ [ SEMATTRS_HTTP_URL ] : getAbsoluteUrl (
469486 requestUrl ,
470487 headers ,
471488 `${ options . component } :`
472489 ) ,
473- [ SemanticAttributes . HTTP_HOST ] : host ,
474- [ SemanticAttributes . NET_HOST_NAME ] : hostname ,
475- [ SemanticAttributes . HTTP_METHOD ] : method ,
476- [ SemanticAttributes . HTTP_SCHEME ] : options . component ,
490+ [ SEMATTRS_HTTP_HOST ] : host ,
491+ [ SEMATTRS_NET_HOST_NAME ] : hostname ,
492+ [ SEMATTRS_HTTP_METHOD ] : method ,
493+ [ SEMATTRS_HTTP_SCHEME ] : options . component ,
477494 } ;
478495
479496 if ( typeof ips === 'string' ) {
480- attributes [ SemanticAttributes . HTTP_CLIENT_IP ] = ips . split ( ',' ) [ 0 ] ;
497+ attributes [ SEMATTRS_HTTP_CLIENT_IP ] = ips . split ( ',' ) [ 0 ] ;
481498 }
482499
483500 if ( typeof serverName === 'string' ) {
484- attributes [ SemanticAttributes . HTTP_SERVER_NAME ] = serverName ;
501+ attributes [ SEMATTRS_HTTP_SERVER_NAME ] = serverName ;
485502 }
486503
487504 if ( requestUrl ) {
488- attributes [ SemanticAttributes . HTTP_TARGET ] = requestUrl . path || '/' ;
505+ attributes [ SEMATTRS_HTTP_TARGET ] = requestUrl . path || '/' ;
489506 }
490507
491508 if ( userAgent !== undefined ) {
492- attributes [ SemanticAttributes . HTTP_USER_AGENT ] = userAgent ;
509+ attributes [ SEMATTRS_HTTP_USER_AGENT ] = userAgent ;
493510 }
494511 setRequestContentLengthAttribute ( request , attributes ) ;
495512
@@ -506,14 +523,11 @@ export const getIncomingRequestMetricAttributes = (
506523 spanAttributes : SpanAttributes
507524) : MetricAttributes => {
508525 const metricAttributes : MetricAttributes = { } ;
509- metricAttributes [ SemanticAttributes . HTTP_SCHEME ] =
510- spanAttributes [ SemanticAttributes . HTTP_SCHEME ] ;
511- metricAttributes [ SemanticAttributes . HTTP_METHOD ] =
512- spanAttributes [ SemanticAttributes . HTTP_METHOD ] ;
513- metricAttributes [ SemanticAttributes . NET_HOST_NAME ] =
514- spanAttributes [ SemanticAttributes . NET_HOST_NAME ] ;
515- metricAttributes [ SemanticAttributes . HTTP_FLAVOR ] =
516- spanAttributes [ SemanticAttributes . HTTP_FLAVOR ] ;
526+ metricAttributes [ SEMATTRS_HTTP_SCHEME ] = spanAttributes [ SEMATTRS_HTTP_SCHEME ] ;
527+ metricAttributes [ SEMATTRS_HTTP_METHOD ] = spanAttributes [ SEMATTRS_HTTP_METHOD ] ;
528+ metricAttributes [ SEMATTRS_NET_HOST_NAME ] =
529+ spanAttributes [ SEMATTRS_NET_HOST_NAME ] ;
530+ metricAttributes [ SEMATTRS_HTTP_FLAVOR ] = spanAttributes [ SEMATTRS_HTTP_FLAVOR ] ;
517531 //TODO: http.target attribute, it should substitute any parameters to avoid high cardinality.
518532 return metricAttributes ;
519533} ;
@@ -535,18 +549,18 @@ export const getIncomingRequestAttributesOnResponse = (
535549 const attributes : SpanAttributes = { } ;
536550 if ( socket ) {
537551 const { localAddress, localPort, remoteAddress, remotePort } = socket ;
538- attributes [ SemanticAttributes . NET_HOST_IP ] = localAddress ;
539- attributes [ SemanticAttributes . NET_HOST_PORT ] = localPort ;
540- attributes [ SemanticAttributes . NET_PEER_IP ] = remoteAddress ;
541- attributes [ SemanticAttributes . NET_PEER_PORT ] = remotePort ;
552+ attributes [ SEMATTRS_NET_HOST_IP ] = localAddress ;
553+ attributes [ SEMATTRS_NET_HOST_PORT ] = localPort ;
554+ attributes [ SEMATTRS_NET_PEER_IP ] = remoteAddress ;
555+ attributes [ SEMATTRS_NET_PEER_PORT ] = remotePort ;
542556 }
543- attributes [ SemanticAttributes . HTTP_STATUS_CODE ] = statusCode ;
557+ attributes [ SEMATTRS_HTTP_STATUS_CODE ] = statusCode ;
544558 attributes [ AttributeNames . HTTP_STATUS_TEXT ] = (
545559 statusMessage || ''
546560 ) . toUpperCase ( ) ;
547561
548562 if ( rpcMetadata ?. type === RPCType . HTTP && rpcMetadata . route !== undefined ) {
549- attributes [ SemanticAttributes . HTTP_ROUTE ] = rpcMetadata . route ;
563+ attributes [ SEMATTRS_HTTP_ROUTE ] = rpcMetadata . route ;
550564 }
551565 return attributes ;
552566} ;
@@ -559,13 +573,12 @@ export const getIncomingRequestMetricAttributesOnResponse = (
559573 spanAttributes : SpanAttributes
560574) : MetricAttributes => {
561575 const metricAttributes : MetricAttributes = { } ;
562- metricAttributes [ SemanticAttributes . HTTP_STATUS_CODE ] =
563- spanAttributes [ SemanticAttributes . HTTP_STATUS_CODE ] ;
564- metricAttributes [ SemanticAttributes . NET_HOST_PORT ] =
565- spanAttributes [ SemanticAttributes . NET_HOST_PORT ] ;
566- if ( spanAttributes [ SemanticAttributes . HTTP_ROUTE ] !== undefined ) {
567- metricAttributes [ SemanticAttributes . HTTP_ROUTE ] =
568- spanAttributes [ SemanticAttributes . HTTP_ROUTE ] ;
576+ metricAttributes [ SEMATTRS_HTTP_STATUS_CODE ] =
577+ spanAttributes [ SEMATTRS_HTTP_STATUS_CODE ] ;
578+ metricAttributes [ SEMATTRS_NET_HOST_PORT ] =
579+ spanAttributes [ SEMATTRS_NET_HOST_PORT ] ;
580+ if ( spanAttributes [ SEMATTRS_HTTP_ROUTE ] !== undefined ) {
581+ metricAttributes [ SEMATTRS_HTTP_ROUTE ] = spanAttributes [ SEMATTRS_HTTP_ROUTE ] ;
569582 }
570583 return metricAttributes ;
571584} ;
0 commit comments