@@ -56,6 +56,7 @@ type spanEnrichmentContext struct {
5656 urlFull * url.URL
5757
5858 peerService string
59+ serverAddress string
5960 urlScheme string
6061 urlDomain string
6162 urlPath string
@@ -68,6 +69,7 @@ type spanEnrichmentContext struct {
6869 messagingSystem string
6970 messagingDestinationName string
7071
72+ serverPort int64
7173 urlPort int64
7274 httpStatusCode int64
7375
@@ -91,6 +93,24 @@ func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
9193 switch k {
9294 case semconv .AttributePeerService :
9395 s .peerService = v .Str ()
96+ case semconv .AttributeServerAddress :
97+ s .serverAddress = v .Str ()
98+ case semconv .AttributeServerPort :
99+ s .serverPort = v .Int ()
100+ case semconv .AttributeNetPeerName :
101+ if s .serverAddress == "" {
102+ // net.peer.name is deprecated, so has lower priority
103+ // only set when not already set with server.address
104+ // and allowed to be overridden by server.address.
105+ s .serverAddress = v .Str ()
106+ }
107+ case semconv .AttributeNetPeerPort :
108+ if s .serverPort == 0 {
109+ // net.peer.port is deprecated, so has lower priority
110+ // only set when not already set with server.port and
111+ // allowed to be overridden by server.port.
112+ s .serverPort = v .Int ()
113+ }
94114 case semconv .AttributeMessagingDestinationName :
95115 s .isMessaging = true
96116 s .messagingDestinationName = v .Str ()
@@ -385,7 +405,10 @@ func (s *spanEnrichmentContext) setServiceTarget(span ptrace.Span) {
385405 }
386406 case s .isHTTP :
387407 targetType = "http"
388- if resource := getHostPort (s .urlFull , s .urlDomain , s .urlPort ); resource != "" {
408+ if resource := getHostPort (
409+ s .urlFull , s .urlDomain , s .urlPort ,
410+ s .serverAddress , s .serverPort , // fallback
411+ ); resource != "" {
389412 targetName = resource
390413 }
391414 }
@@ -419,7 +442,10 @@ func (s *spanEnrichmentContext) setDestinationService(span ptrace.Span) {
419442 }
420443 case s .isRPC , s .isHTTP :
421444 if destnResource == "" {
422- if res := getHostPort (s .urlFull , s .urlDomain , s .urlPort ); res != "" {
445+ if res := getHostPort (
446+ s .urlFull , s .urlDomain , s .urlPort ,
447+ s .serverAddress , s .serverPort , // fallback
448+ ); res != "" {
423449 destnResource = res
424450 }
425451 }
@@ -585,18 +611,23 @@ func getValueForKeyInString(str string, key string, separator rune, assignChar r
585611 return ""
586612}
587613
588- // getHostPort derives the host:port value from url.* attributes. Unlike
589- // apm-data, the current code does NOT fallback to net.* or http.*
590- // attributes as most of these are now deprecated.
591- func getHostPort (urlFull * url.URL , urlDomain string , urlPort int64 ) string {
592- if urlFull != nil {
614+ func getHostPort (
615+ urlFull * url.URL , urlDomain string , urlPort int64 ,
616+ fallbackServerAddress string , fallbackServerPort int64 ,
617+ ) string {
618+ switch {
619+ case urlFull != nil :
593620 return urlFull .Host
594- }
595- if urlDomain != "" {
621+ case urlDomain != "" :
596622 if urlPort == 0 {
597623 return urlDomain
598624 }
599625 return net .JoinHostPort (urlDomain , strconv .FormatInt (urlPort , 10 ))
626+ case fallbackServerAddress != "" :
627+ if fallbackServerPort == 0 {
628+ return fallbackServerAddress
629+ }
630+ return net .JoinHostPort (fallbackServerAddress , strconv .FormatInt (fallbackServerPort , 10 ))
600631 }
601632 return ""
602633}
0 commit comments