Skip to content

Commit 7c4be24

Browse files
committed
Fix: Fix Unit tests
1 parent 255e646 commit 7c4be24

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

google/cloud/storage/_opentelemetry_tracing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def _set_api_request_attr(request, client):
115115
if request.get("path"):
116116
full_url = client._connection.build_api_url(request.get("path"))
117117
attr.update(_get_opentelemetry_attributes_from_url(full_url, strip_query=True))
118-
if request.get("timeout"):
119-
attr["connect_timeout,read_timeout"] = request.get("timeout")
118+
if "timeout" in request:
119+
attr["connect_timeout,read_timeout"] = str(request.get("timeout"))
120120
return attr
121121

122122

@@ -131,8 +131,16 @@ def _get_opentelemetry_attributes_from_url(
131131
):
132132
"""Helper to assemble OpenTelemetry span attributes from a URL."""
133133
u = urlparse(url)
134+
netloc = u.netloc
135+
# u.hostname is always lowercase. We parse netloc to preserve casing.
136+
# netloc format: [userinfo@]host[:port]
137+
if "@" in netloc:
138+
netloc = netloc.split("@", 1)[1]
139+
if ":" in netloc and not netloc.endswith("]"): # Handle IPv6 literal
140+
netloc = netloc.split(":", 1)[0]
141+
134142
attributes = {
135-
"server.address": u.hostname,
143+
"server.address": netloc,
136144
"server.port": u.port,
137145
"url.scheme": u.scheme,
138146
"url.path": u.path,

tests/unit/test__opentelemetry_tracing.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ def test_get_final_attributes(setup, setup_optin):
148148
"user_agent.original": f"gcloud-python/{__version__}",
149149
"http.request.method": "GET",
150150
"server.address": "testOtel.org",
151-
"server.port": None,
152151
"url.path": "/foo/bar/baz",
153152
"url.scheme": "https",
154-
"http.request.timeout": str((100, 100)),
153+
"connect_timeout,read_timeout": str((100, 100)),
155154
"retry": f"multiplier{retry_obj._multiplier}/deadline{retry_obj._deadline}/max{retry_obj._maximum}/initial{retry_obj._initial}/predicate{retry_obj._predicate}",
156155
}
157156
expected_attributes.update(_opentelemetry_tracing._cloud_trace_adoption_attrs)
@@ -219,6 +218,20 @@ def test__get_opentelemetry_attributes_from_url():
219218
assert attrs == expected
220219

221220

221+
def test__get_opentelemetry_attributes_from_url_with_query():
222+
url = "https://example.com/path?query=true&another=false"
223+
expected = {
224+
"server.address": "example.com",
225+
"server.port": None,
226+
"url.scheme": "https",
227+
"url.path": "/path",
228+
"url.query": "query=true&another=false",
229+
}
230+
# Test not stripping query
231+
attrs = _opentelemetry_tracing._get_opentelemetry_attributes_from_url(url, strip_query=False)
232+
assert attrs == expected
233+
234+
222235
def test_set_api_request_attr_with_pii_in_query():
223236
client = mock.Mock()
224237
client._connection.build_api_url.return_value = "https://example.com/path?sensitive=true&token=secret"
@@ -230,7 +243,7 @@ def test_set_api_request_attr_with_pii_in_query():
230243
"server.port": None,
231244
"url.scheme": "https",
232245
"url.path": "/path",
233-
"http.request.timeout": "60",
246+
"connect_timeout,read_timeout": "60",
234247
}
235248
attr = _opentelemetry_tracing._set_api_request_attr(request, client)
236249
assert attr == expected_attributes
@@ -243,4 +256,4 @@ def test_set_api_request_attr_no_timeout():
243256

244257
request = {"method": "GET", "path": "/path"}
245258
attr = _opentelemetry_tracing._set_api_request_attr(request, client)
246-
assert "http.request.timeout" not in attr
259+
assert "connect_timeout,read_timeout" not in attr

tests/unit/test_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ def test__list_resource_w_defaults(self):
684684
credentials = _make_credentials()
685685
client = self._make_one(project=project, credentials=credentials)
686686
connection = client._base_connection = _make_connection()
687+
connection.build_api_url = mock.Mock(return_value="http://example.com" + path)
687688

688689
iterator = client._list_resource(
689690
path=path,
@@ -719,6 +720,7 @@ def test__list_resource_w_explicit(self):
719720
credentials = _make_credentials()
720721
client = self._make_one(project=project, credentials=credentials)
721722
connection = client._base_connection = _make_connection()
723+
connection.build_api_url = mock.Mock(return_value="http://example.com" + path)
722724

723725
iterator = client._list_resource(
724726
path=path,

0 commit comments

Comments
 (0)