From 0b37f8886f8a57b34525631ebd7bb2cf52c51084 Mon Sep 17 00:00:00 2001 From: Tasty213 <30747514+Tasty213@users.noreply.github.com> Date: Thu, 2 Nov 2023 20:18:54 +0000 Subject: [PATCH] Don't throw and exception when raise on error is set to false (#293) * don't throw and exception when raise on error is set to false. * fix no new line at end of gcp resource detector test file * make the expected resources value for the same as the default resources * remove uneeded else --- .../gcp_resource_detector/__init__.py | 2 +- .../tests/test_gcp_resource_detector.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/__init__.py b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/__init__.py index b465efd6..8645925e 100644 --- a/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/__init__.py +++ b/opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/__init__.py @@ -198,6 +198,6 @@ def detect(self) -> "Resource": if found_resources: self.gcp_resources = found_resources break - if not self.gcp_resources: + if self.raise_on_error and not self.gcp_resources: raise NoGoogleResourcesFound() return Resource(self.gcp_resources) diff --git a/opentelemetry-resourcedetector-gcp/tests/test_gcp_resource_detector.py b/opentelemetry-resourcedetector-gcp/tests/test_gcp_resource_detector.py index b17a5ae4..402828fa 100644 --- a/opentelemetry-resourcedetector-gcp/tests/test_gcp_resource_detector.py +++ b/opentelemetry-resourcedetector-gcp/tests/test_gcp_resource_detector.py @@ -526,5 +526,17 @@ def test_resource_finding_fallback(self, getter): def test_no_resources_found(self, getter): # If no Google resources were found, we throw an exception getter.return_value.json.side_effect = Exception - resource_finder = GoogleCloudResourceDetector() + + resource_finder = GoogleCloudResourceDetector(raise_on_error=True) + self.assertRaises(NoGoogleResourcesFound, resource_finder.detect) + + def test_detector_dont_raise_on_error(self, getter): + # If no Google resources were found, we throw an exception + getter.return_value.json.side_effect = Exception + detector = GoogleCloudResourceDetector(raise_on_error=False) + expected_resources = Resource({}) + + resources = detector.detect() + + self.assertEqual(resources, expected_resources)