Skip to content

Commit 00dac38

Browse files
committed
Update Resource.merge to return an empty resource when an error occurs
1 parent 5af86c5 commit 00dac38

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ def merge(self, other: "Resource") -> "Resource":
197197
If a key exists on both the old and updating resource, the value of the
198198
updating resource will override the old resource value.
199199
200-
The new `schema_url` will take an updated value only if the original
200+
The updating resource's `schema_url` will be used only if the old
201201
`schema_url` is empty. Attempting to merge two resources with
202-
different, non-empty values for `schema_url` will result in an error.
202+
different, non-empty values for `schema_url` will result in an error
203+
and return an empty resource.
203204
204205
Args:
205206
other: The other resource to be merged.
@@ -217,10 +218,10 @@ def merge(self, other: "Resource") -> "Resource":
217218
elif self.schema_url == other.schema_url:
218219
schema_url = other.schema_url
219220
else:
220-
schema_url = ""
221221
logger.error(
222222
"Failed to merge resources: The Schema URL of the old and updating resources are not empty and are different"
223223
)
224+
return _EMPTY_RESOURCE
224225

225226
return Resource(merged_attributes, schema_url)
226227

opentelemetry-sdk/tests/resources/test_resources.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_resource_merge(self):
147147
left = resources.Resource.create({}, schema_urls[0])
148148
right = resources.Resource.create({}, schema_urls[1])
149149
with self.assertLogs(level=ERROR):
150-
self.assertEqual(left.merge(right).schema_url, "")
150+
self.assertEqual(left.merge(right), resources._EMPTY_RESOURCE)
151151

152152
def test_resource_merge_empty_string(self):
153153
"""Verify Resource.merge behavior with the empty string.

0 commit comments

Comments
 (0)