Skip to content

Commit 3ac4620

Browse files
committed
handle empty attributes
1 parent 0b9fbcd commit 3ac4620

File tree

1 file changed

+9
-2
lines changed
  • ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi

1 file changed

+9
-2
lines changed

Diff for: ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ def collect_request_attributes(scope):
8080
server = scope.get("server") or ["0.0.0.0", 80]
8181
port = server[1]
8282
server_host = server[0] + (":" + str(port) if port != 80 else "")
83-
http_url = scope.get("scheme") + "://" + server_host + scope.get("path")
84-
if scope.get("query_string"):
83+
http_url = (
84+
scope.get("scheme") + "://" + server_host + scope.get("path", "")
85+
if scope.get("scheme") and server_host and scope.get("path")
86+
else None
87+
)
88+
if scope.get("query_string") and http_url:
8589
http_url = http_url + ("?" + scope.get("query_string").decode("utf8"))
8690

8791
result = {
@@ -107,6 +111,9 @@ def collect_request_attributes(scope):
107111
result["net.peer.ip"] = scope.get("client")[0]
108112
result["net.peer.port"] = scope.get("client")[1]
109113

114+
# remove None values
115+
result = {k: v for k, v in result.items() if v is not None}
116+
110117
return result
111118

112119

0 commit comments

Comments
 (0)