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)