Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Mock httpbin for requests tests



Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py
@@ -25,7 +25,7 @@
      import httpx
      from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
 
-     url = "https://httpbin.org/get"
+     url = "https://some.url/get"
      HTTPXClientInstrumentor().instrument()
 
      with httpx.Client() as client:
@@ -46,7 +46,7 @@
     import httpx
     from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
 
-    url = "https://httpbin.org/get"
+    url = "https://some.url/get"
 
     with httpx.Client(transport=telemetry_transport) as client:
         HTTPXClientInstrumentor.instrument_client(client)
@@ -91,7 +91,7 @@
         SyncOpenTelemetryTransport,
     )
 
-    url = "https://httpbin.org/get"
+    url = "https://some.url/get"
     transport = httpx.HTTPTransport()
     telemetry_transport = SyncOpenTelemetryTransport(transport)
 


Test patch:
diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
--- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
+++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py
@@ -705,11 +705,11 @@ def test_response_attributes_invalid_status_code(self):
         self.assertEqual(self.span.set_status.call_count, 1)
 
     def test_credential_removal(self):
-        self.scope["server"] = ("username:password@httpbin.org", 80)
+        self.scope["server"] = ("username:password@mock", 80)
         self.scope["path"] = "/status/200"
         attrs = otel_asgi.collect_request_attributes(self.scope)
         self.assertEqual(
-            attrs[SpanAttributes.HTTP_URL], "http://httpbin.org/status/200"
+            attrs[SpanAttributes.HTTP_URL], "http://mock/status/200"
         )
 
     def test_collect_target_attribute_missing(self):
diff --git a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py
--- a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py
+++ b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py
@@ -97,7 +97,7 @@ class BaseTestCases:
     class BaseTest(TestBase, metaclass=abc.ABCMeta):
         # pylint: disable=no-member
 
-        URL = "http://httpbin.org/status/200"
+        URL = "http://mock/status/200"
         response_hook = staticmethod(_response_hook)
         request_hook = staticmethod(_request_hook)
         no_update_request_hook = staticmethod(_no_update_request_hook)
@@ -165,7 +165,7 @@ def test_basic_multiple(self):
             self.assert_span(num_spans=2)
 
         def test_not_foundbasic(self):
-            url_404 = "http://httpbin.org/status/404"
+            url_404 = "http://mock/status/404"
 
             with respx.mock:
                 respx.get(url_404).mock(httpx.Response(404))
diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py
--- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py
+++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py
@@ -63,7 +63,7 @@ class RequestsIntegrationTestBase(abc.ABC):
     # pylint: disable=no-member
     # pylint: disable=too-many-public-methods
 
-    URL = "http://httpbin.org/status/200"
+    URL = "http://mock/status/200"
 
     # pylint: disable=invalid-name
     def setUp(self):
@@ -152,7 +152,7 @@ def response_hook(span, request_obj, response):
         self.assertEqual(span.attributes["response_hook_attr"], "value")
 
     def test_excluded_urls_explicit(self):
-        url_404 = "http://httpbin.org/status/404"
+        url_404 = "http://mock/status/404"
         httpretty.register_uri(
             httpretty.GET,
             url_404,
@@ -194,7 +194,7 @@ def name_callback(method, url):
         self.assertEqual(span.name, "HTTP GET")
 
     def test_not_foundbasic(self):
-        url_404 = "http://httpbin.org/status/404"
+        url_404 = "http://mock/status/404"
         httpretty.register_uri(
             httpretty.GET,
             url_404,
@@ -460,7 +460,7 @@ def perform_request(url: str, session: requests.Session = None):
         return session.get(url)
 
     def test_credential_removal(self):
-        new_url = "http://username:password@httpbin.org/status/200"
+        new_url = "http://username:password@mock/status/200"
         self.perform_request(new_url)
         span = self.assert_span()
 
diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py
--- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py
+++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_metrics_instrumentation.py
@@ -27,8 +27,8 @@
 
 
 class TestUrllibMetricsInstrumentation(TestBase):
-    URL = "http://httpbin.org/status/200"
-    URL_POST = "http://httpbin.org/post"
+    URL = "http://mock/status/200"
+    URL_POST = "http://mock/post"
 
     def setUp(self):
         super().setUp()
diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py
--- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py
+++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py
@@ -46,9 +46,9 @@
 class RequestsIntegrationTestBase(abc.ABC):
     # pylint: disable=no-member
 
-    URL = "http://httpbin.org/status/200"
-    URL_TIMEOUT = "http://httpbin.org/timeout/0"
-    URL_EXCEPTION = "http://httpbin.org/exception/0"
+    URL = "http://mock/status/200"
+    URL_TIMEOUT = "http://mock/timeout/0"
+    URL_EXCEPTION = "http://mock/exception/0"
 
     # pylint: disable=invalid-name
     def setUp(self):
@@ -83,7 +83,7 @@ def setUp(self):
         )
         httpretty.register_uri(
             httpretty.GET,
-            "http://httpbin.org/status/500",
+            "http://mock/status/500",
             status=500,
         )
 
@@ -142,7 +142,7 @@ def test_basic(self):
         )
 
     def test_excluded_urls_explicit(self):
-        url_201 = "http://httpbin.org/status/201"
+        url_201 = "http://mock/status/201"
         httpretty.register_uri(
             httpretty.GET,
             url_201,
@@ -172,7 +172,7 @@ def test_excluded_urls_from_env(self):
         self.assert_span(num_spans=1)
 
     def test_not_foundbasic(self):
-        url_404 = "http://httpbin.org/status/404/"
+        url_404 = "http://mock/status/404/"
         httpretty.register_uri(
             httpretty.GET,
             url_404,
@@ -336,14 +336,14 @@ def test_custom_tracer_provider(self):
 
     def test_requests_exception_with_response(self, *_, **__):
         with self.assertRaises(HTTPError):
-            self.perform_request("http://httpbin.org/status/500")
+            self.perform_request("http://mock/status/500")
 
         span = self.assert_span()
         self.assertEqual(
             dict(span.attributes),
             {
                 SpanAttributes.HTTP_METHOD: "GET",
-                SpanAttributes.HTTP_URL: "http://httpbin.org/status/500",
+                SpanAttributes.HTTP_URL: "http://mock/status/500",
                 SpanAttributes.HTTP_STATUS_CODE: 500,
             },
         )
@@ -365,7 +365,7 @@ def test_requests_timeout_exception(self, *_, **__):
         self.assertEqual(span.status.status_code, StatusCode.ERROR)
 
     def test_credential_removal(self):
-        url = "http://username:password@httpbin.org/status/200"
+        url = "http://username:password@mock/status/200"
 
         with self.assertRaises(Exception):
             self.perform_request(url)
diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py
--- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py
+++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py
@@ -35,8 +35,8 @@
 
 
 class TestURLLib3Instrumentor(TestBase):
-    HTTP_URL = "http://httpbin.org/status/200"
-    HTTPS_URL = "https://httpbin.org/status/200"
+    HTTP_URL = "http://mock/status/200"
+    HTTPS_URL = "https://mock/status/200"
 
     def setUp(self):
         super().setUp()
@@ -123,7 +123,7 @@ def test_basic_http_success(self):
         self.assert_success_span(response, self.HTTP_URL)
 
     def test_basic_http_success_using_connection_pool(self):
-        pool = urllib3.HTTPConnectionPool("httpbin.org")
+        pool = urllib3.HTTPConnectionPool("mock")
         response = pool.request("GET", "/status/200")
 
         self.assert_success_span(response, self.HTTP_URL)
@@ -133,13 +133,13 @@ def test_basic_https_success(self):
         self.assert_success_span(response, self.HTTPS_URL)
 
     def test_basic_https_success_using_connection_pool(self):
-        pool = urllib3.HTTPSConnectionPool("httpbin.org")
+        pool = urllib3.HTTPSConnectionPool("mock")
         response = pool.request("GET", "/status/200")
 
         self.assert_success_span(response, self.HTTPS_URL)
 
     def test_basic_not_found(self):
-        url_404 = "http://httpbin.org/status/404"
+        url_404 = "http://mock/status/404"
         httpretty.register_uri(httpretty.GET, url_404, status=404)
 
         response = self.perform_request(url_404)
@@ -152,30 +152,30 @@ def test_basic_not_found(self):
         self.assertIs(trace.status.StatusCode.ERROR, span.status.status_code)
 
     def test_basic_http_non_default_port(self):
-        url = "http://httpbin.org:666/status/200"
+        url = "http://mock:666/status/200"
         httpretty.register_uri(httpretty.GET, url, body="Hello!")
 
         response = self.perform_request(url)
         self.assert_success_span(response, url)
 
     def test_basic_http_absolute_url(self):
-        url = "http://httpbin.org:666/status/200"
+        url = "http://mock:666/status/200"
         httpretty.register_uri(httpretty.GET, url, body="Hello!")
-        pool = urllib3.HTTPConnectionPool("httpbin.org", port=666)
+        pool = urllib3.HTTPConnectionPool("mock", port=666)
         response = pool.request("GET", url)
 
         self.assert_success_span(response, url)
 
     def test_url_open_explicit_arg_parameters(self):
-        url = "http://httpbin.org:666/status/200"
+        url = "http://mock:666/status/200"
         httpretty.register_uri(httpretty.GET, url, body="Hello!")
-        pool = urllib3.HTTPConnectionPool("httpbin.org", port=666)
+        pool = urllib3.HTTPConnectionPool("mock", port=666)
         response = pool.urlopen(method="GET", url="/status/200")
 
         self.assert_success_span(response, url)
 
     def test_excluded_urls_explicit(self):
-        url_201 = "http://httpbin.org/status/201"
+        url_201 = "http://mock/status/201"
         httpretty.register_uri(
             httpretty.GET,
             url_201,
@@ -301,7 +301,7 @@ def url_filter(url):
         self.assert_success_span(response, self.HTTP_URL)
 
     def test_credential_removal(self):
-        url = "http://username:password@httpbin.org/status/200"
+        url = "http://username:password@mock/status/200"
 
         response = self.perform_request(url)
         self.assert_success_span(response, self.HTTP_URL)
@@ -339,7 +339,7 @@ def request_hook(span, request, headers, body):
         headers = {"header1": "value1", "header2": "value2"}
         body = "param1=1&param2=2"
 
-        pool = urllib3.HTTPConnectionPool("httpbin.org")
+        pool = urllib3.HTTPConnectionPool("mock")
         response = pool.request(
             "POST", "/status/200", body=body, headers=headers
         )
@@ -366,7 +366,7 @@ def request_hook(span, request, headers, body):
 
         body = "param1=1&param2=2"
 
-        pool = urllib3.HTTPConnectionPool("httpbin.org")
+        pool = urllib3.HTTPConnectionPool("mock")
         response = pool.urlopen("POST", "/status/200", body)
 
         self.assertEqual(b"Hello!", response.data)
diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_metrics.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_metrics.py
--- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_metrics.py
+++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_metrics.py
@@ -26,7 +26,7 @@
 
 
 class TestURLLib3InstrumentorMetric(HttpTestBase, TestBase):
-    HTTP_URL = "http://httpbin.org/status/200"
+    HTTP_URL = "http://mock/status/200"
 
     def setUp(self):
         super().setUp()
@@ -68,11 +68,11 @@ def test_basic_metrics(self):
                     min_data_point=client_duration_estimated,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "GET",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -91,11 +91,11 @@ def test_basic_metrics(self):
                     min_data_point=0,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "GET",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -116,11 +116,11 @@ def test_basic_metrics(self):
                     min_data_point=expected_size,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "GET",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -144,11 +144,11 @@ def test_str_request_body_size_metrics(self):
                     min_data_point=6,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "POST",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -172,11 +172,11 @@ def test_bytes_request_body_size_metrics(self):
                     min_data_point=6,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "POST",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -201,11 +201,11 @@ def test_fields_request_body_size_metrics(self):
                     min_data_point=expected_value,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "POST",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
@@ -229,11 +229,11 @@ def test_bytesio_request_body_size_metrics(self):
                     min_data_point=6,
                     attributes={
                         "http.flavor": "1.1",
-                        "http.host": "httpbin.org",
+                        "http.host": "mock",
                         "http.method": "POST",
                         "http.scheme": "http",
                         "http.status_code": 200,
-                        "net.peer.name": "httpbin.org",
+                        "net.peer.name": "mock",
                         "net.peer.port": 80,
                     },
                 )
diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py
--- a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py
+++ b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py
@@ -437,10 +437,10 @@ def test_response_attributes(self):
         self.span.set_attribute.assert_has_calls(expected, any_order=True)
 
     def test_credential_removal(self):
-        self.environ["HTTP_HOST"] = "username:password@httpbin.com"
+        self.environ["HTTP_HOST"] = "username:password@mock"
         self.environ["PATH_INFO"] = "/status/200"
         expected = {
-            SpanAttributes.HTTP_URL: "http://httpbin.com/status/200",
+            SpanAttributes.HTTP_URL: "http://mock/status/200",
             SpanAttributes.NET_HOST_PORT: 80,
         }
         self.assertGreaterEqual(