Skip to content

Update Resource merge key conflict precedence #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1536](https://github.com/open-telemetry/opentelemetry-python/pull/1536))
- Fix TraceState to adhere to specs
([#1502](https://github.com/open-telemetry/opentelemetry-python/pull/1502))
- Update Resource `merge` key conflict precedence
([#1544](https://github.com/open-telemetry/opentelemetry-python/pull/1544))

### Removed
- `opentelemetry-api` Remove ThreadLocalRuntimeContext since python3.4 is not supported.
Expand Down
35 changes: 27 additions & 8 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,27 @@


class Resource:
"""A Resource is an immutable representation of the entity producing telemetry as Attributes.
"""

def __init__(self, attributes: Attributes):
self._attributes = attributes.copy()

@staticmethod
def create(attributes: typing.Optional[Attributes] = None) -> "Resource":
"""Creates a new `Resource` from attributes.

Args:
attributes: Optional zero or more key-value pairs.

Returns:
The newly-created Resource.
"""
if not attributes:
resource = _DEFAULT_RESOURCE
else:
resource = _DEFAULT_RESOURCE.merge(Resource(attributes))
return resource.merge(OTELResourceDetector().detect())
attributes = {}
return _DEFAULT_RESOURCE.merge(OTELResourceDetector().detect()).merge(
Resource(attributes)
)

@staticmethod
def create_empty() -> "Resource":
Expand All @@ -122,11 +133,19 @@ def attributes(self) -> Attributes:
return self._attributes.copy()

def merge(self, other: "Resource") -> "Resource":
"""Merges this resource and an updating resource into a new `Resource`.

If a key exists on both the old and updating resource, the value of the
updating resource will override the old resource value.

Args:
other: The other resource to be merged.

Returns:
The newly-created Resource.
"""
merged_attributes = self.attributes
# pylint: disable=protected-access
for key, value in other._attributes.items():
if key not in merged_attributes or merged_attributes[key] == "":
merged_attributes[key] = value
merged_attributes.update(other.attributes)
return Resource(merged_attributes)

def __eq__(self, other: object) -> bool:
Expand Down
30 changes: 24 additions & 6 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_resource_merge_empty_string(self):
)
self.assertEqual(
left.merge(right),
resources.Resource({"service": "ui", "host": "service-host"}),
resources.Resource({"service": "not-ui", "host": "service-host"}),
)

def test_immutability(self):
Expand Down Expand Up @@ -133,7 +133,6 @@ def test_aggregated_resources_with_static_resource(self):
static_resource,
)

# Static resource values should never be overwritten
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.return_value = resources.Resource(
{"static_key": "try_to_overwrite_existing_value", "key": "value"}
Expand All @@ -142,7 +141,12 @@ def test_aggregated_resources_with_static_resource(self):
resources.get_aggregated_resources(
[resource_detector], initial_resource=static_resource
),
resources.Resource({"static_key": "static_value", "key": "value"}),
resources.Resource(
{
"static_key": "try_to_overwrite_existing_value",
"key": "value",
}
),
)

def test_aggregated_resources_multiple_detectors(self):
Expand All @@ -163,16 +167,15 @@ def test_aggregated_resources_multiple_detectors(self):
}
)

# New values should not overwrite existing values
self.assertEqual(
resources.get_aggregated_resources(
[resource_detector1, resource_detector2, resource_detector3]
),
resources.Resource(
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key2": "try_to_overwrite_existing_value",
"key3": "try_to_overwrite_existing_value",
"key4": "value4",
}
),
Expand All @@ -195,6 +198,21 @@ def test_resource_detector_raise_error(self):
Exception, resources.get_aggregated_resources, [resource_detector],
)

@mock.patch.dict(
os.environ,
{"OTEL_RESOURCE_ATTRIBUTES": "key1=env_value1,key2=env_value2"},
)
def test_env_priority(self):
resource_env = resources.Resource.create()
self.assertEqual(resource_env.attributes["key1"], "env_value1")
self.assertEqual(resource_env.attributes["key2"], "env_value2")

resource_env_override = resources.Resource.create(
{"key1": "value1", "key2": "value2"}
)
self.assertEqual(resource_env_override.attributes["key1"], "value1")
self.assertEqual(resource_env_override.attributes["key2"], "value2")


class TestOTELResourceDetector(unittest.TestCase):
def setUp(self) -> None:
Expand Down