|
28 | 28 | from opentelemetry.trace.status import StatusCode
|
29 | 29 |
|
30 | 30 |
|
| 31 | +class InvalidResponseObjectException(Exception): |
| 32 | + def __init__(self): |
| 33 | + super().__init__() |
| 34 | + self.response = {} |
| 35 | + |
| 36 | + |
31 | 37 | class RequestsIntegrationTestBase(abc.ABC):
|
32 | 38 | # pylint: disable=no-member
|
33 | 39 |
|
@@ -307,6 +313,42 @@ def test_requests_exception_without_response(self, *_, **__):
|
307 | 313 | mocked_response.status_code = 500
|
308 | 314 | mocked_response.reason = "Internal Server Error"
|
309 | 315 |
|
| 316 | + @mock.patch( |
| 317 | + "requests.adapters.HTTPAdapter.send", |
| 318 | + side_effect=InvalidResponseObjectException, |
| 319 | + ) |
| 320 | + def test_requests_exception_without_proper_response_type(self, *_, **__): |
| 321 | + with self.assertRaises(InvalidResponseObjectException): |
| 322 | + self.perform_request(self.URL) |
| 323 | + |
| 324 | + span = self.assert_span() |
| 325 | + self.assertEqual( |
| 326 | + span.attributes, |
| 327 | + {"component": "http", "http.method": "GET", "http.url": self.URL}, |
| 328 | + ) |
| 329 | + self.assertEqual(span.status.status_code, StatusCode.ERROR) |
| 330 | + |
| 331 | + self.assertIsNotNone(RequestsInstrumentor().meter) |
| 332 | + self.assertEqual(len(RequestsInstrumentor().meter.instruments), 1) |
| 333 | + recorder = list(RequestsInstrumentor().meter.instruments.values())[0] |
| 334 | + match_key = get_dict_as_key( |
| 335 | + { |
| 336 | + "http.method": "GET", |
| 337 | + "http.url": "http://httpbin.org/status/200", |
| 338 | + } |
| 339 | + ) |
| 340 | + for key in recorder.bound_instruments.keys(): |
| 341 | + self.assertEqual(key, match_key) |
| 342 | + # pylint: disable=protected-access |
| 343 | + bound = recorder.bound_instruments.get(key) |
| 344 | + for view_data in bound.view_datas: |
| 345 | + self.assertEqual(view_data.labels, key) |
| 346 | + self.assertEqual(view_data.aggregator.current.count, 1) |
| 347 | + |
| 348 | + mocked_response = requests.Response() |
| 349 | + mocked_response.status_code = 500 |
| 350 | + mocked_response.reason = "Internal Server Error" |
| 351 | + |
310 | 352 | @mock.patch(
|
311 | 353 | "requests.adapters.HTTPAdapter.send",
|
312 | 354 | side_effect=requests.RequestException(response=mocked_response),
|
|
0 commit comments