diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index bcc0e5b4c27..30d6267a8fa 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -26,6 +26,10 @@ them to supported data types (long for int/short, double for float). For invalid attributes we now throw an exception instead of logging an error. ([#1720](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1720)) +* Merging "this" resource with an "other" resource now prioritizes the "other" + resource's attributes in a conflict. We've rectified to follow a recent + change to the spec. We previously prioritized "this" resource's tags. + ([#1728](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1728)) ## 1.0.0-rc1.1 diff --git a/src/OpenTelemetry/Resources/Resource.cs b/src/OpenTelemetry/Resources/Resource.cs index fcb73f8fc82..6d8f68e308c 100644 --- a/src/OpenTelemetry/Resources/Resource.cs +++ b/src/OpenTelemetry/Resources/Resource.cs @@ -57,34 +57,34 @@ internal Resource(IEnumerable> attributes) public IEnumerable> Attributes { get; } /// - /// Returns a new, merged by merging the current with the. - /// other . In case of a collision the current takes precedence. + /// Returns a new, merged by merging the old with the + /// other . In case of a collision the other takes precedence. /// - /// The that will be merged with. this. + /// The that will be merged with this. /// . public Resource Merge(Resource other) { var newAttributes = new Dictionary(); - foreach (var attribute in this.Attributes) - { - if (!newAttributes.TryGetValue(attribute.Key, out var value) || (value is string strValue && string.IsNullOrEmpty(strValue))) - { - newAttributes[attribute.Key] = attribute.Value; - } - } - if (other != null) { foreach (var attribute in other.Attributes) { - if (!newAttributes.TryGetValue(attribute.Key, out var value) || (value is string strValue && string.IsNullOrEmpty(strValue))) + if (!newAttributes.TryGetValue(attribute.Key, out var value)) { newAttributes[attribute.Key] = attribute.Value; } } } + foreach (var attribute in this.Attributes) + { + if (!newAttributes.TryGetValue(attribute.Key, out var value)) + { + newAttributes[attribute.Key] = attribute.Value; + } + } + return new Resource(newAttributes); } diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index a9d4d61dff5..3abd7845d3f 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -315,19 +315,19 @@ public void MergeResource_MultiAttributeSource_DuplicatedKeysInPrimary() } [Fact] - public void MergeResource_SecondaryCanOverridePrimaryEmptyAttributeValue() + public void MergeResource_UpdatingResourceOverridesCurrentResource() { // Arrange - var primaryAttributes = new Dictionary { { "value", string.Empty } }; - var secondaryAttributes = new Dictionary { { "value", "not empty" } }; - var primaryResource = new Resource(primaryAttributes); - var secondaryResource = new Resource(secondaryAttributes); + var currentAttributes = new Dictionary { { "value", "currentValue" } }; + var updatingAttributes = new Dictionary { { "value", "updatedValue" } }; + var currentResource = new Resource(currentAttributes); + var updatingResource = new Resource(updatingAttributes); - var newResource = primaryResource.Merge(secondaryResource); + var newResource = currentResource.Merge(updatingResource); // Assert Assert.Single(newResource.Attributes); - Assert.Contains(new KeyValuePair("value", "not empty"), newResource.Attributes); + Assert.Contains(new KeyValuePair("value", "updatedValue"), newResource.Attributes); } [Fact]