From 7e41524e667615afe5a0ee00c72d4562bf5975d8 Mon Sep 17 00:00:00 2001 From: Sergey Lyapustin Date: Thu, 5 Dec 2024 16:45:40 +0100 Subject: [PATCH] refactor(zipkin-exporter): simplify success status check and add test for 204 response --- .../src/opentelemetry/exporter/zipkin/json/__init__.py | 3 +-- .../tests/test_zipkin_exporter.py | 8 ++++++++ .../opentelemetry/exporter/zipkin/proto/http/__init__.py | 3 +-- .../tests/test_zipkin_exporter.py | 8 ++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/exporter/opentelemetry-exporter-zipkin-json/src/opentelemetry/exporter/zipkin/json/__init__.py b/exporter/opentelemetry-exporter-zipkin-json/src/opentelemetry/exporter/zipkin/json/__init__.py index ba313db942a..5bd1c0d9544 100644 --- a/exporter/opentelemetry-exporter-zipkin-json/src/opentelemetry/exporter/zipkin/json/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin-json/src/opentelemetry/exporter/zipkin/json/__init__.py @@ -91,7 +91,6 @@ from opentelemetry.trace import Span DEFAULT_ENDPOINT = "http://localhost:9411/api/v2/spans" -REQUESTS_SUCCESS_STATUS_CODES = (200, 202) logger = logging.getLogger(__name__) @@ -171,7 +170,7 @@ def export(self, spans: Sequence[Span]) -> SpanExportResult: timeout=self.timeout, ) - if result.status_code not in REQUESTS_SUCCESS_STATUS_CODES: + if not result.ok: logger.error( "Traces cannot be uploaded; status code: %s, message %s", result.status_code, diff --git a/exporter/opentelemetry-exporter-zipkin-json/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin-json/tests/test_zipkin_exporter.py index 77e3ef53755..55324a258d3 100644 --- a/exporter/opentelemetry-exporter-zipkin-json/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin-json/tests/test_zipkin_exporter.py @@ -144,6 +144,14 @@ def test_export_success(self, mock_post): status = exporter.export(spans) self.assertEqual(SpanExportResult.SUCCESS, status) + @patch("requests.Session.post") + def test_export_success_no_content(self, mock_post): + mock_post.return_value = MockResponse(204) + spans = [] + exporter = ZipkinExporter() + status = exporter.export(spans) + self.assertEqual(SpanExportResult.SUCCESS, status) + @patch("requests.Session.post") def test_export_invalid_response(self, mock_post): mock_post.return_value = MockResponse(404) diff --git a/exporter/opentelemetry-exporter-zipkin-proto-http/src/opentelemetry/exporter/zipkin/proto/http/__init__.py b/exporter/opentelemetry-exporter-zipkin-proto-http/src/opentelemetry/exporter/zipkin/proto/http/__init__.py index dcb092c9cec..77648d04a92 100644 --- a/exporter/opentelemetry-exporter-zipkin-proto-http/src/opentelemetry/exporter/zipkin/proto/http/__init__.py +++ b/exporter/opentelemetry-exporter-zipkin-proto-http/src/opentelemetry/exporter/zipkin/proto/http/__init__.py @@ -88,7 +88,6 @@ from opentelemetry.trace import Span DEFAULT_ENDPOINT = "http://localhost:9411/api/v2/spans" -REQUESTS_SUCCESS_STATUS_CODES = (200, 202) logger = logging.getLogger(__name__) @@ -163,7 +162,7 @@ def export(self, spans: Sequence[Span]) -> SpanExportResult: timeout=self.timeout, ) - if result.status_code not in REQUESTS_SUCCESS_STATUS_CODES: + if not result.ok: logger.error( "Traces cannot be uploaded; status code: %s, message %s", result.status_code, diff --git a/exporter/opentelemetry-exporter-zipkin-proto-http/tests/test_zipkin_exporter.py b/exporter/opentelemetry-exporter-zipkin-proto-http/tests/test_zipkin_exporter.py index 8a3c055437a..303a02d0683 100644 --- a/exporter/opentelemetry-exporter-zipkin-proto-http/tests/test_zipkin_exporter.py +++ b/exporter/opentelemetry-exporter-zipkin-proto-http/tests/test_zipkin_exporter.py @@ -144,6 +144,14 @@ def test_export_success(self, mock_post): status = exporter.export(spans) self.assertEqual(SpanExportResult.SUCCESS, status) + @patch("requests.Session.post") + def test_export_success_no_content(self, mock_post): + mock_post.return_value = MockResponse(204) + spans = [] + exporter = ZipkinExporter() + status = exporter.export(spans) + self.assertEqual(SpanExportResult.SUCCESS, status) + @patch("requests.Session.post") def test_export_invalid_response(self, mock_post): mock_post.return_value = MockResponse(404)