From fca26140ac601a28b52929b27086612872e4685d Mon Sep 17 00:00:00 2001 From: Daniel Getu Date: Tue, 25 May 2021 11:02:51 -0700 Subject: [PATCH] Add rejection of combined detectors with differing Schema URLs --- .../opentelemetry/sdk/resources/__init__.py | 9 +++++ .../tests/resources/test_resources.py | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 9890862a7f5..ad6b5127227 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -296,6 +296,15 @@ def get_aggregated_resources( detector = detectors[detector_ind] try: detected_resources = future.result(timeout=timeout) + if ( + final_resource.schema_url != "" + and detected_resources.schema_url != "" + and final_resource != detected_resources + ): + logger.error( + "Failed to aggregate resources: The Schema URL of the detectors are not empty and are different" + ) + return _EMPTY_RESOURCE # pylint: disable=broad-except except Exception as ex: if detector.raise_on_error: diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 6184493fc43..42883fec6f3 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -296,6 +296,41 @@ def test_aggregated_resources_multiple_detectors(self): ), ) + def test_aggregated_resources_different_schema_urls(self): + resource_detector1 = mock.Mock(spec=resources.ResourceDetector) + resource_detector1.detect.return_value = resources.Resource( + {"key1": "value1"}, "" + ) + resource_detector2 = mock.Mock(spec=resources.ResourceDetector) + resource_detector2.detect.return_value = resources.Resource( + {"key2": "value2", "key3": "value3"}, "url1" + ) + resource_detector3 = mock.Mock(spec=resources.ResourceDetector) + resource_detector3.detect.return_value = resources.Resource( + { + "key2": "try_to_overwrite_existing_value", + "key3": "try_to_overwrite_existing_value", + "key4": "value4", + }, + "url2", + ) + self.assertEqual( + resources.get_aggregated_resources( + [resource_detector1, resource_detector2] + ), + resources.Resource( + {"key1": "value1", "key2": "value2", "key3": "value3"}, + "url1", + ), + ) + with self.assertLogs(level=ERROR): + self.assertEqual( + resources.get_aggregated_resources( + [resource_detector2, resource_detector3] + ), + resources._EMPTY_RESOURCE, + ) + def test_resource_detector_ignore_error(self): resource_detector = mock.Mock(spec=resources.ResourceDetector) resource_detector.detect.side_effect = Exception()