Skip to content
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

[SDK] Fix schema URL precedence bug in Resource::Merge #2036

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Increment the:

## [Unreleased]

* [RESOURCE SDK] Fix schema URL precedence bug in `Resource::Merge`.
[#2036](https://github.com/open-telemetry/opentelemetry-cpp/pull/2036)

## [1.8.3] 2023-03-06

* Provide version major/minor/patch macros
Expand Down
4 changes: 4 additions & 0 deletions sdk/include/opentelemetry/sdk/resource/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class Resource
* with the other Resource. In case of a collision, the other Resource takes
* precedence.
*
* The specification notes that if schema urls collide, the resulting
* schema url is implementation-defined. In the C++ implementation, the
* schema url of @param other is picked.
*
* @param other the Resource that will be merged with this.
* @returns the newly merged Resource.
*/
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/resource/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Resource Resource::Merge(const Resource &other) const noexcept
{
ResourceAttributes merged_resource_attributes(other.attributes_);
merged_resource_attributes.insert(attributes_.begin(), attributes_.end());
return Resource(merged_resource_attributes, other.schema_url_);
return Resource(merged_resource_attributes,
other.schema_url_.empty() ? schema_url_ : other.schema_url_);
}

Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url)
Expand Down
30 changes: 29 additions & 1 deletion sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ namespace nostd = opentelemetry::nostd;
class TestResource : public Resource
{
public:
TestResource(ResourceAttributes attributes = ResourceAttributes()) : Resource(attributes) {}
TestResource(ResourceAttributes attributes = ResourceAttributes(), std::string schema_url = {})
: Resource(attributes, schema_url)
{}
};

TEST(ResourceTest, create_without_servicename)
Expand Down Expand Up @@ -170,6 +172,32 @@ TEST(ResourceTest, MergeEmptyString)
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
}

TEST(ResourceTest, MergeSchemaUrl)
{
const std::string url = "https://opentelemetry.io/schemas/v3.1.4";

TestResource resource_empty_url({}, "");
TestResource resource_some_url({}, url);
TestResource resource_different_url({}, "different");

// Specified behavior:
auto merged_both_empty = resource_empty_url.Merge(resource_empty_url);
EXPECT_TRUE(merged_both_empty.GetSchemaURL().empty());

auto merged_old_empty = resource_empty_url.Merge(resource_some_url);
EXPECT_EQ(merged_old_empty.GetSchemaURL(), url);

auto merged_updating_empty = resource_some_url.Merge(resource_empty_url);
EXPECT_EQ(merged_updating_empty.GetSchemaURL(), url);

auto merged_same_url = resource_some_url.Merge(resource_some_url);
EXPECT_EQ(merged_same_url.GetSchemaURL(), url);

// Implementation-defined behavior:
auto merged_different_url = resource_different_url.Merge(resource_some_url);
EXPECT_EQ(merged_different_url.GetSchemaURL(), url);
}

#ifndef NO_GETENV
TEST(ResourceTest, OtelResourceDetector)
{
Expand Down