From 2e8e14fd4639ae0ea8b70823d87ffba15d53fe3a Mon Sep 17 00:00:00 2001 From: Ryo Kather Date: Wed, 2 Jun 2021 14:33:19 -0700 Subject: [PATCH] Implemented username and pw check on urllib --- .../src/opentelemetry/instrumentation/urllib/__init__.py | 5 +++-- .../tests/test_urllib_integration.py | 9 +++++++++ tox.ini | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py index fcac480e02..4bd9e83d45 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py @@ -39,6 +39,7 @@ import functools import types from typing import Collection +import yarl from urllib.request import ( # pylint: disable=no-name-in-module,import-error OpenerDirector, Request, @@ -144,7 +145,7 @@ def _instrumented_open_call( labels = { SpanAttributes.HTTP_METHOD: method, - SpanAttributes.HTTP_URL: url, + SpanAttributes.HTTP_URL: str(yarl.URL(url).with_user(None)), } with tracer.start_as_current_span( @@ -153,7 +154,7 @@ def _instrumented_open_call( exception = None if span.is_recording(): span.set_attribute(SpanAttributes.HTTP_METHOD, method) - span.set_attribute(SpanAttributes.HTTP_URL, url) + span.set_attribute(SpanAttributes.HTTP_URL, str(yarl.URL(url).with_user(None))) headers = get_or_create_headers() inject(headers) diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py index b05fe00012..d0b21b5861 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py @@ -318,6 +318,15 @@ def test_requests_timeout_exception(self, *_, **__): span = self.assert_span() self.assertEqual(span.status.status_code, StatusCode.ERROR) + def test_credentials_url(self): + url = "http://username:password@httpbin.org/status/200" + + with self.assertRaises(Exception): + self.perform_request(url) + + span = self.assert_span() + print(span.attributes) + self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL) class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase): @staticmethod diff --git a/tox.ini b/tox.ini index bf3f289f91..905a7b5d5d 100644 --- a/tox.ini +++ b/tox.ini @@ -245,7 +245,7 @@ commands_pre = flask: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-flask[test] - urllib: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test] + urllib: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test] urllib3: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib3[test]