Skip to content

Commit

Permalink
Update Resource.merge to return an empty resource when an error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
dgetu committed May 25, 2021
1 parent 5af86c5 commit 00dac38
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/tests/resources/test_resources.py
Expand Up @@ -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.
Expand Down

0 comments on commit 00dac38

Please sign in to comment.