Skip to content

Commit 3849da1

Browse files
committed
better handling for query_string
1 parent bc6f0a9 commit 3849da1

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ def collect_request_attributes(scope):
8383
server_host = server[0] + (":" + str(port) if port != 80 else "")
8484
full_path = scope.get("root_path", "") + scope.get("path", "")
8585
http_url = scope.get("scheme", "http") + "://" + server_host + full_path
86-
if scope.get("query_string") and http_url:
87-
if isinstance(scope["query_string"], bytes):
88-
http_url = http_url + ("?" + scope.get("query_string").decode("utf8"))
89-
else:
90-
http_url = http_url + ("?" + urllib.parse.unquote(scope.get("query_string")))
86+
query_string = scope.get("query_string")
87+
if query_string and http_url:
88+
if isinstance(query_string, bytes):
89+
query_string = query_string.decode("utf8")
90+
http_url = http_url + ("?" + urllib.parse.unquote(query_string))
9191

9292
result = {
9393
"component": scope["type"],

ext/opentelemetry-ext-asgi/tests/test_asgi_middleware.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def setUp(self):
295295
setup_testing_defaults(self.scope)
296296
self.span = mock.create_autospec(trace_api.Span, spec_set=True)
297297

298-
def test_query_string_utf8(self):
298+
def test_request_attributes(self):
299299
self.scope["query_string"] = b"foo=bar"
300300

301301
attrs = otel_asgi.collect_request_attributes(self.scope)
@@ -315,25 +315,20 @@ def test_query_string_utf8(self):
315315
},
316316
)
317317

318-
def test_query_string_percent_encoding(self):
319-
self.scope["query_string"] = urllib.parse.quote(b"foo=bar")
318+
def test_query_string(self):
319+
self.scope["query_string"] = b"foo=bar"
320+
attrs = otel_asgi.collect_request_attributes(self.scope)
321+
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")
320322

323+
def test_query_string_percent_bytes(self):
324+
self.scope["query_string"] = b"foo%3Dbar"
321325
attrs = otel_asgi.collect_request_attributes(self.scope)
322-
self.assertDictEqual(
323-
attrs,
324-
{
325-
"component": "http",
326-
"http.method": "GET",
327-
"http.host": "127.0.0.1",
328-
"http.target": "/",
329-
"http.url": "http://127.0.0.1/?foo=bar",
330-
"host.port": 80,
331-
"http.scheme": "http",
332-
"http.flavor": "1.0",
333-
"net.peer.ip": "127.0.0.1",
334-
"net.peer.port": 32767,
335-
},
336-
)
326+
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")
327+
328+
def test_query_string_percent_str(self):
329+
self.scope["query_string"] = "foo%3Dbar"
330+
attrs = otel_asgi.collect_request_attributes(self.scope)
331+
self.assertEqual(attrs["http.url"], "http://127.0.0.1/?foo=bar")
337332

338333
def test_response_attributes(self):
339334
otel_asgi.set_status_code(self.span, 404)

0 commit comments

Comments
 (0)