|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import urllib3 |
| 16 | +import urllib3.exceptions |
| 17 | + |
| 18 | +from opentelemetry import trace |
| 19 | +from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor |
| 20 | +from opentelemetry.test.httptest import HttpTestBase |
| 21 | +from opentelemetry.test.test_base import TestBase |
| 22 | +from opentelemetry.util.http.httplib import HttpClientInstrumentor |
| 23 | + |
| 24 | + |
| 25 | +class TestURLLib3InstrumentorWithRealSocket(HttpTestBase, TestBase): |
| 26 | + def setUp(self): |
| 27 | + super().setUp() |
| 28 | + self.assert_ip = self.server.server_address[0] |
| 29 | + self.http_host = ":".join(map(str, self.server.server_address[:2])) |
| 30 | + self.http_url_base = "http://" + self.http_host |
| 31 | + self.http_url = self.http_url_base + "/status/200" |
| 32 | + HttpClientInstrumentor().instrument() |
| 33 | + URLLib3Instrumentor().instrument() |
| 34 | + |
| 35 | + def tearDown(self): |
| 36 | + super().tearDown() |
| 37 | + HttpClientInstrumentor().uninstrument() |
| 38 | + URLLib3Instrumentor().uninstrument() |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def perform_request(url: str) -> urllib3.response.HTTPResponse: |
| 42 | + with urllib3.PoolManager() as pool: |
| 43 | + resp = pool.request("GET", url) |
| 44 | + resp.close() |
| 45 | + return resp |
| 46 | + |
| 47 | + def test_basic_http_success(self): |
| 48 | + response = self.perform_request(self.http_url) |
| 49 | + self.assert_success_span(response, self.http_url) |
| 50 | + |
| 51 | + def test_basic_http_success_using_connection_pool(self): |
| 52 | + with urllib3.HTTPConnectionPool(self.http_host, timeout=3) as pool: |
| 53 | + response = pool.request("GET", "/status/200") |
| 54 | + |
| 55 | + self.assert_success_span(response, self.http_url) |
| 56 | + |
| 57 | + # Test that when re-using an existing connection, everything still works. |
| 58 | + # Especially relevant for IP capturing. |
| 59 | + response = pool.request("GET", "/status/200") |
| 60 | + |
| 61 | + self.assert_success_span(response, self.http_url) |
| 62 | + |
| 63 | + def assert_span(self, num_spans=1): |
| 64 | + span_list = self.memory_exporter.get_finished_spans() |
| 65 | + self.assertEqual(num_spans, len(span_list)) |
| 66 | + if num_spans == 0: |
| 67 | + return None |
| 68 | + self.memory_exporter.clear() |
| 69 | + if num_spans == 1: |
| 70 | + return span_list[0] |
| 71 | + return span_list |
| 72 | + |
| 73 | + def assert_success_span( |
| 74 | + self, response: urllib3.response.HTTPResponse, url: str |
| 75 | + ): |
| 76 | + self.assertEqual(b"Hello!", response.data) |
| 77 | + |
| 78 | + span = self.assert_span() |
| 79 | + self.assertIs(trace.SpanKind.CLIENT, span.kind) |
| 80 | + self.assertEqual("HTTP GET", span.name) |
| 81 | + |
| 82 | + attributes = { |
| 83 | + "http.status_code": 200, |
| 84 | + "net.peer.ip": self.assert_ip, |
| 85 | + } |
| 86 | + self.assertGreaterEqual(span.attributes.items(), attributes.items()) |
0 commit comments