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

Set default service.name if missing #616

Merged
merged 3 commits into from
Mar 22, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Increment the:
* [EXPORTER] Added Zipkin Exporter. ([#471](https://github.com/open-telemetry/opentelemetry-cpp/pull/471))
* [API] Added Jaeger propagator. ([#599](https://github.com/open-telemetry/opentelemetry-cpp/pull/599))
* [PROPAGATOR] Added Composite Propagator ([#597](https://github.com/open-telemetry/opentelemetry-cpp/pull/597))
* [SDK] Add service.name if missing in Resource ([#616](https://github.com/open-telemetry/opentelemetry-cpp/pull/616))

## [0.2.0] 2021-03-02

Expand Down
24 changes: 15 additions & 9 deletions sdk/src/resource/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace sdk
namespace resource
{

const std::string kTelemetrySdkLanguage = "telemetry.sdk.language";
const std::string kTelemetrySdkName = "telemetry.sdk.name";
const std::string kTelemetrySdkVersion = "telemetry.sdk.version";
const std::string kTelemetrySdkLanguage = "telemetry.sdk.language";
const std::string kTelemetrySdkName = "telemetry.sdk.name";
const std::string kTelemetrySdkVersion = "telemetry.sdk.version";
const std::string kServiceName = "service.name";
const std::string kProcessExecutableName = "process.executable.name";

Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {}

Expand All @@ -25,15 +27,19 @@ Resource Resource::Merge(const Resource &other) noexcept
Resource Resource::Create(const ResourceAttributes &attributes)
{
static auto otel_resource = OTELResourceDetector().Detect();
auto default_resource = Resource::GetDefault();
auto resource = Resource::GetDefault().Merge(otel_resource).Merge(Resource(attributes));
ThomsonTan marked this conversation as resolved.
Show resolved Hide resolved

if (attributes.size() > 0)
if (resource.attributes_.find(kServiceName) == resource.attributes_.end())
{
Resource tmp_resource(attributes);
auto merged_resource = tmp_resource.Merge(default_resource);
return merged_resource.Merge(otel_resource);
std::string default_service_name = "unknown_service";
auto it_process_executable_name = resource.attributes_.find(kProcessExecutableName);
if (it_process_executable_name != resource.attributes_.end())
{
default_service_name += ":" + nostd::get<std::string>(it_process_executable_name->second);
}
resource.attributes_[kServiceName] = default_service_name;
}
return default_resource.Merge(otel_resource);
return resource;
}

Resource &Resource::GetEmpty()
Expand Down
64 changes: 58 additions & 6 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestResource : public opentelemetry::sdk::resource::Resource
{}
};

TEST(ResourceTest, create)
TEST(ResourceTest, create_without_servicename)
{

opentelemetry::sdk::resource::ResourceAttributes expected_attributes = {
Expand All @@ -28,15 +28,46 @@ TEST(ResourceTest, create)
{"cost", 234.23},
{"telemetry.sdk.language", "cpp"},
{"telemetry.sdk.name", "opentelemetry"},
{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}};
{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION},
{"service.name", "unknown_service"}};

opentelemetry::sdk::resource::ResourceAttributes attributes = {
{"service", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
auto resource2 = opentelemetry::sdk::resource::Resource::Create(attributes);
auto received_attributes2 = resource2.GetAttributes();
for (auto &e : received_attributes2)
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
if (e.first == "version")
EXPECT_EQ(opentelemetry::nostd::get<uint32_t>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<uint32_t>(e.second));
else if (e.first == "cost")
EXPECT_EQ(opentelemetry::nostd::get<double>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<double>(e.second));
else
EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<std::string>(e.second));
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
}

TEST(ResourceTest, create_with_servicename)
{
opentelemetry::sdk::resource::ResourceAttributes expected_attributes = {
{"version", (uint32_t)1},
{"cost", 234.23},
{"telemetry.sdk.language", "cpp"},
{"telemetry.sdk.name", "opentelemetry"},
{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION},
{"service.name", "backend"},
};
opentelemetry::sdk::resource::ResourceAttributes attributes = {
{"service.name", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
if (e.first == "version")
Expand All @@ -49,9 +80,29 @@ TEST(ResourceTest, create)
EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<std::string>(e.second));
}
EXPECT_EQ(received_attributes2.size(), expected_attributes.size());
EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
}

TEST(ResourceTest, create_with_emptyatrributes)
{
opentelemetry::sdk::resource::ResourceAttributes expected_attributes = {
{"telemetry.sdk.language", "cpp"},
{"telemetry.sdk.name", "opentelemetry"},
{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION},
{"service.name", "unknown_service"},
};
opentelemetry::sdk::resource::ResourceAttributes attributes = {};
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
auto received_attributes = resource.GetAttributes();
for (auto &e : received_attributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<std::string>(e.second));
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
}
TEST(ResourceTest, Merge)
{
TestResource resource1(
Expand All @@ -72,6 +123,7 @@ TEST(ResourceTest, Merge)
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
}

TEST(ResourceTest, MergeEmptyString)
{
TestResource resource1({{"service", "backend"}, {"host", "service-host"}});
Expand Down