Skip to content

Commit 2e8e14f

Browse files
author
Ryo Kather
committed
Implemented username and pw check on urllib
1 parent b2dd4b8 commit 2e8e14f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import functools
4040
import types
4141
from typing import Collection
42+
import yarl
4243
from urllib.request import ( # pylint: disable=no-name-in-module,import-error
4344
OpenerDirector,
4445
Request,
@@ -144,7 +145,7 @@ def _instrumented_open_call(
144145

145146
labels = {
146147
SpanAttributes.HTTP_METHOD: method,
147-
SpanAttributes.HTTP_URL: url,
148+
SpanAttributes.HTTP_URL: str(yarl.URL(url).with_user(None)),
148149
}
149150

150151
with tracer.start_as_current_span(
@@ -153,7 +154,7 @@ def _instrumented_open_call(
153154
exception = None
154155
if span.is_recording():
155156
span.set_attribute(SpanAttributes.HTTP_METHOD, method)
156-
span.set_attribute(SpanAttributes.HTTP_URL, url)
157+
span.set_attribute(SpanAttributes.HTTP_URL, str(yarl.URL(url).with_user(None)))
157158

158159
headers = get_or_create_headers()
159160
inject(headers)

instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ def test_requests_timeout_exception(self, *_, **__):
318318
span = self.assert_span()
319319
self.assertEqual(span.status.status_code, StatusCode.ERROR)
320320

321+
def test_credentials_url(self):
322+
url = "http://username:[email protected]/status/200"
323+
324+
with self.assertRaises(Exception):
325+
self.perform_request(url)
326+
327+
span = self.assert_span()
328+
print(span.attributes)
329+
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL)
321330

322331
class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase):
323332
@staticmethod

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ commands_pre =
245245

246246
flask: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-flask[test]
247247

248-
urllib: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test]
248+
urllib: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test]
249249

250250
urllib3: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib3[test]
251251

0 commit comments

Comments
 (0)