diff --git a/elasticapm/contrib/opentelemetry/span.py b/elasticapm/contrib/opentelemetry/span.py index 558ea3914a..a2e229a2c3 100644 --- a/elasticapm/contrib/opentelemetry/span.py +++ b/elasticapm/contrib/opentelemetry/span.py @@ -238,11 +238,11 @@ def parse_net_name(url: str): port = http_port_from_scheme(u.scheme) return u.netloc if not port else "{}:{}".format(u.netloc, port) - peer_port = attributes.get("net.peer.port", -1) - net_name = attributes.get("net.peer.name", attributes.get("net.peer.ip", "")) + net_port = attributes.get("net.peer.port", -1) + net_name = net_peer = attributes.get("net.peer.name", attributes.get("net.peer.ip", "")) - if net_name and (peer_port > 0): - net_name = "{}:{}".format(net_name, peer_port) + if net_name and (net_port > 0): + net_name = f"{net_name}:{net_port}" if attributes.get("db.system"): span_type = "db" @@ -267,8 +267,11 @@ def parse_net_name(url: str): elif attributes.get("http.url") or attributes.get("http.scheme"): span_type = "external" span_subtype = "http" - if attributes.get("http.host") and attributes.get("http.scheme"): - resource = "{}:{}".format(attributes["http.host"], attributes["http.scheme"]) + http_host = attributes.get("http.host", net_peer) + if http_host: + if net_port < 0: + net_port = http_port_from_scheme(attributes.get("http.scheme")) + resource = http_host if net_port < 0 else f"{http_host}:{net_port}" elif attributes.get("http.url"): resource = parse_net_name(attributes["http.url"])