From 00dac383b5dcf2679f6f47535a8d54c663fabcb8 Mon Sep 17 00:00:00 2001 From: Daniel Getu Date: Tue, 25 May 2021 10:29:39 -0700 Subject: [PATCH] Update Resource.merge to return an empty resource when an error occurs --- .../src/opentelemetry/sdk/resources/__init__.py | 7 ++++--- opentelemetry-sdk/tests/resources/test_resources.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 86be16643a..9890862a7f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -197,9 +197,10 @@ def merge(self, other: "Resource") -> "Resource": If a key exists on both the old and updating resource, the value of the updating resource will override the old resource value. - The new `schema_url` will take an updated value only if the original + The updating resource's `schema_url` will be used only if the old `schema_url` is empty. Attempting to merge two resources with - different, non-empty values for `schema_url` will result in an error. + different, non-empty values for `schema_url` will result in an error + and return an empty resource. Args: other: The other resource to be merged. @@ -217,10 +218,10 @@ def merge(self, other: "Resource") -> "Resource": elif self.schema_url == other.schema_url: schema_url = other.schema_url else: - schema_url = "" logger.error( "Failed to merge resources: The Schema URL of the old and updating resources are not empty and are different" ) + return _EMPTY_RESOURCE return Resource(merged_attributes, schema_url) diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 76af6cb269..6184493fc4 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -147,7 +147,7 @@ def test_resource_merge(self): left = resources.Resource.create({}, schema_urls[0]) right = resources.Resource.create({}, schema_urls[1]) with self.assertLogs(level=ERROR): - self.assertEqual(left.merge(right).schema_url, "") + self.assertEqual(left.merge(right), resources._EMPTY_RESOURCE) def test_resource_merge_empty_string(self): """Verify Resource.merge behavior with the empty string.