From 326852059fc994cf2881deed3f668f0868d93aad Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 19 Mar 2021 17:45:39 +0530 Subject: [PATCH 1/3] add service.name if missing --- sdk/src/resource/resource.cc | 24 ++++++++++------ sdk/test/resource/resource_test.cc | 45 ++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 7df4b6a668..40354b5b5d 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -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) {} @@ -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)); - 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(it_process_executable_name->second); + } + resource.attributes_[kServiceName] = default_service_name; } - return default_resource.Merge(otel_resource); + return resource; } Resource &Resource::GetEmpty() diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 1a565128e0..a30df18c04 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -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 = { @@ -28,15 +28,48 @@ 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(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get(e.second)); + else if (e.first == "cost") + EXPECT_EQ(opentelemetry::nostd::get(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get(e.second)); + else + EXPECT_EQ(opentelemetry::nostd::get(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get(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") @@ -49,7 +82,7 @@ TEST(ResourceTest, create) EXPECT_EQ(opentelemetry::nostd::get(expected_attributes.find(e.first)->second), opentelemetry::nostd::get(e.second)); } - EXPECT_EQ(received_attributes2.size(), expected_attributes.size()); + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name } TEST(ResourceTest, Merge) From acdcc9ca2217a20d8cf3a54106db7cfdca4162b2 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 19 Mar 2021 18:04:09 +0530 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1286cf5de..f8f15d75b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 9f52cff3973bd9c144072aadf4af2f8ea20bbfdb Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 22 Mar 2021 09:57:52 +0530 Subject: [PATCH 3/3] add test for empty resource --- sdk/test/resource/resource_test.cc | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index a30df18c04..4e2a9a4dc7 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -54,7 +54,6 @@ TEST(ResourceTest, create_without_servicename) TEST(ResourceTest, create_with_servicename) { - opentelemetry::sdk::resource::ResourceAttributes expected_attributes = { {"version", (uint32_t)1}, {"cost", 234.23}, @@ -63,7 +62,6 @@ TEST(ResourceTest, create_with_servicename) {"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); @@ -85,6 +83,26 @@ TEST(ResourceTest, create_with_servicename) 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(expected_attributes.find(e.first)->second), + opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name +} TEST(ResourceTest, Merge) { TestResource resource1( @@ -105,6 +123,7 @@ TEST(ResourceTest, Merge) } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } + TEST(ResourceTest, MergeEmptyString) { TestResource resource1({{"service", "backend"}, {"host", "service-host"}});