@@ -40,7 +40,8 @@ def _get_default_port_db(db_system: str) -> int:
4040 return 0
4141
4242
43- def _get_default_port_http (scheme : Optional [str ]) -> int :
43+ def _get_default_port_http (attributes : Attributes ) -> int :
44+ scheme = _get_http_scheme (attributes )
4445 if scheme == "http" :
4546 return 80
4647 if scheme == "https" :
@@ -77,7 +78,8 @@ def _get_azure_sdk_target_source(attributes: Attributes) -> Optional[str]:
7778
7879def _get_http_scheme (attributes : Attributes ) -> Optional [str ]:
7980 if attributes :
80- scheme = attributes .get (SpanAttributes .HTTP_SCHEME )
81+ scheme = attributes .get (url_attributes .URL_SCHEME ) or \
82+ attributes .get (SpanAttributes .HTTP_SCHEME )
8183 if scheme :
8284 return str (scheme )
8385 return None
@@ -87,14 +89,17 @@ def _get_http_scheme(attributes: Attributes) -> Optional[str]:
8789
8890
8991@no_type_check
90- def _get_url_for_http_dependency (attributes : Attributes , scheme : Optional [ str ] = None ) -> Optional [str ]:
91- url = None
92+ def _get_url_for_http_dependency (attributes : Attributes ) -> Optional [str ]:
93+ url = ""
9294 if attributes :
93- if not scheme :
94- scheme = _get_http_scheme (attributes )
95+ # Stable sem conv only supports populating url from `url.full`
96+ if url_attributes .URL_FULL in attributes :
97+ return attributes [url_attributes .URL_FULL ]
9598 if SpanAttributes .HTTP_URL in attributes :
96- url = attributes [SpanAttributes .HTTP_URL ]
97- elif scheme and SpanAttributes .HTTP_TARGET in attributes :
99+ return attributes [SpanAttributes .HTTP_URL ]
100+ # Scheme
101+ scheme = _get_http_scheme (attributes )
102+ if scheme and SpanAttributes .HTTP_TARGET in attributes :
98103 http_target = attributes [SpanAttributes .HTTP_TARGET ]
99104 if SpanAttributes .HTTP_HOST in attributes :
100105 url = "{}://{}{}" .format (
@@ -138,52 +143,60 @@ def _get_target_for_dependency_from_peer(attributes: Attributes) -> Optional[str
138143 port = attributes [SpanAttributes .NET_PEER_PORT ]
139144 # TODO: check default port for rpc
140145 # This logic assumes default ports never conflict across dependency types
141- if port != _get_default_port_http (
142- str (attributes .get (SpanAttributes .HTTP_SCHEME ))
143- ) and port != _get_default_port_db (str (attributes .get (SpanAttributes .DB_SYSTEM ))):
146+ if port != _get_default_port_http (attributes ) and \
147+ port != _get_default_port_db (str (attributes .get (SpanAttributes .DB_SYSTEM ))):
144148 target = "{}:{}" .format (target , port )
145149 return target
146150
147151
148152@no_type_check
149153def _get_target_and_path_for_http_dependency (
150154 attributes : Attributes ,
151- target : Optional [str ],
152- url : Optional [str ],
153- scheme : Optional [str ] = None ,
155+ target : Optional [str ] = "" ,
156+ url : Optional [str ] = "" , # Usually populated by _get_url_for_http_dependency()
154157) -> Tuple [Optional [str ], str ]:
155- target_from_url = None
156- path = ""
158+ parsed_url = None
159+ path = "/"
160+ default_port = _get_default_port_http (attributes )
161+ # Find path from url
162+ try :
163+ parsed_url = urlparse (url )
164+ if parsed_url .path :
165+ path = parsed_url .path
166+ except Exception : # pylint: disable=broad-except
167+ pass
168+ # Derive target
157169 if attributes :
158- if not scheme :
159- scheme = _get_http_scheme (attributes )
160- if url :
161- try :
162- parse_url = urlparse (url )
163- path = parse_url .path
164- if not path :
165- path = "/"
166- if parse_url .port and parse_url .port == _get_default_port_http (scheme ):
167- target_from_url = parse_url .hostname
168- else :
169- target_from_url = parse_url .netloc
170- except Exception : # pylint: disable=broad-except
171- pass
172- if SpanAttributes .PEER_SERVICE not in attributes :
170+ # Target from server.*
171+ if server_attributes .SERVER_ADDRESS in attributes :
172+ target = attributes [server_attributes .SERVER_ADDRESS ]
173+ server_port = attributes .get (server_attributes .SERVER_PORT )
174+ # if not default port, include port in target
175+ if server_port != default_port :
176+ target = "{}:{}" .format (target , server_port )
177+ # We only use these values for target if not already populated by peer.service
178+ elif not SpanAttributes .PEER_SERVICE in attributes :
179+ # Target from http.host
173180 if SpanAttributes .HTTP_HOST in attributes :
174181 host = attributes [SpanAttributes .HTTP_HOST ]
175182 try :
176183 # urlparse insists on absolute URLs starting with "//"
177184 # This logic assumes host does not include a "//"
178185 host_name = urlparse ("//" + str (host ))
179- if host_name .port == _get_default_port_http (scheme ):
186+ # Ignore port from target if default port
187+ if host_name .port == default_port :
180188 target = host_name .hostname
181189 else :
190+ # Else include the whole host as the target
182191 target = str (host )
183192 except Exception : # pylint: disable=broad-except
184193 pass
185- elif target_from_url and not target :
186- target = target_from_url
194+ elif parsed_url :
195+ # Target from httpUrl
196+ if parsed_url .port and parsed_url .port == default_port :
197+ target = parsed_url .hostname
198+ else :
199+ target = parsed_url .netloc
187200 return (target , path )
188201
189202
@@ -250,7 +263,7 @@ def _get_url_for_http_request(attributes: Attributes) -> Optional[str]:
250263 if SpanAttributes .HTTP_URL in attributes :
251264 return attributes [SpanAttributes .HTTP_URL ]
252265 # Scheme
253- scheme = attributes . get ( url_attributes . URL_SCHEME ) or attributes . get ( SpanAttributes . HTTP_SCHEME )
266+ scheme = _get_http_scheme ( attributes )
254267 # Target
255268 http_target = ""
256269 if url_attributes .URL_PATH in attributes :
0 commit comments