From a27a5e4f333887386354fd965ddf7b56e644330d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 17:15:10 +0530 Subject: [PATCH 01/30] resource api --- .../opentelemetry/sdk/resource/resource.h | 71 +++++++++++++++++ .../sdk/resource/resource_detector.h | 28 +++++++ sdk/src/CMakeLists.txt | 1 + sdk/src/resource/BUILD | 26 +++++++ sdk/src/resource/CMakeLists.txt | 16 ++++ sdk/src/resource/resource.cc | 76 +++++++++++++++++++ sdk/src/resource/resource_detector.cc | 22 ++++++ sdk/test/CMakeLists.txt | 1 + sdk/test/resource/BUILD | 55 ++++++++++++++ sdk/test/resource/CMakeLists.txt | 9 +++ sdk/test/resource/resource_test.cc | 38 ++++++++++ 11 files changed, 343 insertions(+) create mode 100644 sdk/include/opentelemetry/sdk/resource/resource.h create mode 100644 sdk/include/opentelemetry/sdk/resource/resource_detector.h create mode 100644 sdk/src/resource/BUILD create mode 100644 sdk/src/resource/CMakeLists.txt create mode 100644 sdk/src/resource/resource.cc create mode 100644 sdk/src/resource/resource_detector.cc create mode 100644 sdk/test/resource/BUILD create mode 100644 sdk/test/resource/CMakeLists.txt create mode 100644 sdk/test/resource/resource_test.cc diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h new file mode 100644 index 0000000000..3d6f4916b3 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -0,0 +1,71 @@ +#pragma once + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/trace/attribute_utils.h" + + +#include +#include +#include +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + +class Resource +{ +public: + +Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept ; + +Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept ; + +Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept; + +template ::value> * = nullptr> +Resource( const T &attributes ) noexcept : Resource (common::KeyValueIterableView(attributes)) { } + +Resource( const std::initializer_list> attributes ) noexcept +: Resource (nostd::span>{attributes.begin(), + attributes.end()}) { } + + +const opentelemetry::sdk::trace::AttributeMap& GetAttributes() const noexcept ; + +std::unique_ptr Merge(const Resource &other) ; + +static std::unique_ptr Create(const opentelemetry::common::KeyValueIterable &attributes) ; + +static std::unique_ptr Create( + std::initializer_list> attributes) noexcept +{ + return Create(nostd::span>{attributes.begin(), + attributes.end()}); +} + +template ::value> * = nullptr> +static std::unique_ptr Create( const T &attributes ) noexcept +{ + return Create(common::KeyValueIterableView(attributes)); +} + +static std::unique_ptr CreateEmpty() ; + +static std::unique_ptr Create(const std::string &attributes) ; + +private: +const opentelemetry::sdk::trace::AttributeMap attribute_map_; + +}; + +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h new file mode 100644 index 0000000000..4b52b27048 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -0,0 +1,28 @@ +#pragma once + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/sdk/resource/resource.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + +class ResourceDetector +{ +public: + virtual std::unique_ptr Detect() = 0; +}; + +class OTELResourceDetector : public ResourceDetector +{ +public: + std::unique_ptr Detect() noexcept override ; + +}; + +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/CMakeLists.txt b/sdk/src/CMakeLists.txt index 8e0a99edc0..56adec57d5 100644 --- a/sdk/src/CMakeLists.txt +++ b/sdk/src/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory(trace) add_subdirectory(metrics) add_subdirectory(logs) add_subdirectory(version) +add_subdirectory(resource) diff --git a/sdk/src/resource/BUILD b/sdk/src/resource/BUILD new file mode 100644 index 0000000000..ec48f92aed --- /dev/null +++ b/sdk/src/resource/BUILD @@ -0,0 +1,26 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "resources", + srcs = glob(["**/*.cc"]), + hdrs = glob(["**/*.h"]), + include_prefix = "src/resource", + deps = [ + "//api", + "//sdk:headers", + ], +) diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt new file mode 100644 index 0000000000..0b75bf4be1 --- /dev/null +++ b/sdk/src/resource/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library(opentelemetry_resources resource.cc resource_detector.cc) + +set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) + +target_link_libraries(opentelemetry_resources opentelemetry_common) + +target_include_directories( + opentelemetry_resources + PUBLIC "$") + +install( + TARGETS opentelemetry_resources + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc new file mode 100644 index 0000000000..5d54d00f7e --- /dev/null +++ b/sdk/src/resource/resource.cc @@ -0,0 +1,76 @@ +#include "opentelemetry/version.h" +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/nostd/span.h" + + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + +const std::string TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language" ; +const std::string TELEMETRY_SDK_NAME = "telemetry.sdk.name" ; +const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version" ; + +Resource::Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept : attribute_map_(attributes){ } + +Resource::Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept : attribute_map_(attributes){ } + +Resource::Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept : attribute_map_(std::move(attributes)){ } + + + + +const opentelemetry::sdk::trace::AttributeMap& Resource::GetAttributes() const noexcept +{ + return attribute_map_; +} + +std::unique_ptr Resource::Merge(const Resource &other) +{ + opentelemetry::sdk::trace::AttributeMap merged_resource_attributes(other.GetAttributes()); + merged_resource_attributes.AddAttributes(attribute_map_); + return std::unique_ptr(new Resource(merged_resource_attributes)); +} + +std::unique_ptr Resource::Create(const opentelemetry::common::KeyValueIterable &attributes) +{ + Resource default_resource ( {{ + { TELEMETRY_SDK_LANGUAGE, "cpp"}, + { TELEMETRY_SDK_NAME, "opentelemetry"}, + { TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION} + } }); + + if (attributes.size() > 0) { + Resource input_resource(attributes); + auto merged_resource = default_resource.Merge(input_resource); + return merged_resource->Merge(*(OTELResourceDetector().Detect())); + } + return default_resource.Merge(*(OTELResourceDetector().Detect())); +} + +std::unique_ptr Resource::CreateEmpty() +{ + return Create({}); +} + +std::unique_ptr Resource::Create(const std::string &attributes) +{ + opentelemetry::sdk::trace::AttributeMap resource_attributes; + std::istringstream iss(attributes); + std::string token; + while (std::getline(iss, token, ',' )) + { + size_t pos = token.find('='); + std::string key = token.substr(0, pos); + std::string value = token.substr(pos + 1); + resource_attributes.SetAttribute(key, value); + } + return std::unique_ptr(new Resource(resource_attributes)); +} + +} +} +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc new file mode 100644 index 0000000000..7ac491e042 --- /dev/null +++ b/sdk/src/resource/resource_detector.cc @@ -0,0 +1,22 @@ +#include "opentelemetry/sdk/resource/resource_detector.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace resource +{ + + const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; + + + std::unique_ptr OTELResourceDetector::Detect() noexcept + { + char *resource_attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); + if (resource_attributes_str == nullptr) + return std::unique_ptr(new Resource({})); + return Resource::Create(std::string(resource_attributes_str)); + } + +} +} +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/CMakeLists.txt b/sdk/test/CMakeLists.txt index 633f9c3465..eae1209f46 100644 --- a/sdk/test/CMakeLists.txt +++ b/sdk/test/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory(common) add_subdirectory(trace) add_subdirectory(metrics) add_subdirectory(logs) +add_subdirectory(resource) diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD new file mode 100644 index 0000000000..e70527243f --- /dev/null +++ b/sdk/test/resource/BUILD @@ -0,0 +1,55 @@ +cc_test( + name = "logger_provider_sdk_test", + srcs = [ + "logger_provider_sdk_test.cc", + ], + deps = [ + "//api", + "//sdk/src/logs", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "logger_sdk_test", + srcs = [ + "logger_sdk_test.cc", + ], + deps = [ + "//sdk/src/logs", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "simple_log_processor_test", + srcs = [ + "simple_log_processor_test.cc", + ], + deps = [ + "//sdk/src/logs", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "log_record_test", + srcs = [ + "log_record_test.cc", + ], + deps = [ + "//sdk/src/logs", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "batch_log_processor_test", + srcs = [ + "batch_log_processor_test.cc", + ], + deps = [ + "//sdk/src/logs", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/sdk/test/resource/CMakeLists.txt b/sdk/test/resource/CMakeLists.txt new file mode 100644 index 0000000000..514b98680b --- /dev/null +++ b/sdk/test/resource/CMakeLists.txt @@ -0,0 +1,9 @@ +foreach(testname resource_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_resources) + gtest_add_tests( + TARGET ${testname} + TEST_PREFIX resources. + TEST_LIST ${testname}) +endforeach() diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc new file mode 100644 index 0000000000..87ae6b8426 --- /dev/null +++ b/sdk/test/resource/resource_test.cc @@ -0,0 +1,38 @@ +#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/sdk/trace/attribute_utils.h" +#include "opentelemetry/common/key_value_iterable_view.h" +#include "opentelemetry/nostd/string_view.h" + +#include +#include + +#include + +TEST(ResourceTest, create) +{ + + std::map expected_attributes = {{"service","backend"},{"version", "1"},{"cost", "234.23"},{"telemetry.sdk.language","cpp"},{"telemetry.sdk.name", "opentelemetry"},{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; + auto resource = opentelemetry::sdk::resource::Resource::Create({{"service","backend"}, {"version", "1"}, {"cost", "234.23"}}); + auto received_attributes = resource->GetAttributes().GetAttributes(); + + for (auto &e: received_attributes) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + //std::cout << e.first << ":" << opentelemetry::nostd::get(e.second) << "\n"; + + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + + std::map attributes = {{"service","backend"}, {"version", "1"}, {"cost", "234.23"}}; + auto resource2 = opentelemetry::sdk::resource::Resource::Create> (attributes); + auto received_attributes2 = resource2->GetAttributes().GetAttributes(); + for (auto &e: received_attributes2) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + //std::cout << e.first << ":" << opentelemetry::nostd::get(e.second) << "\n"; + + } + + //opentelemetry::sdk::resource::Resource resource5(attributes); +} \ No newline at end of file From abe82879937ceea8cec2d69800b036a7ee4feeec Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 19:29:12 +0530 Subject: [PATCH 02/30] format and add tests --- .../opentelemetry/sdk/resource/resource.h | 73 ++++++------ .../sdk/resource/resource_detector.h | 9 +- .../opentelemetry/sdk/trace/attribute_utils.h | 13 ++ sdk/src/resource/CMakeLists.txt | 2 +- sdk/src/resource/resource.cc | 90 +++++++------- sdk/src/resource/resource_detector.cc | 22 ++-- sdk/test/resource/BUILD | 50 +------- sdk/test/resource/resource_test.cc | 111 ++++++++++++++---- 8 files changed, 205 insertions(+), 165 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 3d6f4916b3..42cb39e0a4 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -1,16 +1,15 @@ #pragma once -#include "opentelemetry/version.h" -#include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/sdk/version/version.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/sdk/trace/attribute_utils.h" +#include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/version.h" - -#include +#include #include #include -#include +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -21,51 +20,53 @@ namespace resource class Resource { public: + Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept; -Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept ; - -Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept ; + Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept; -Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept; + Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept; -template ::value> * = nullptr> -Resource( const T &attributes ) noexcept : Resource (common::KeyValueIterableView(attributes)) { } + template ::value> * = nullptr> + Resource(const T &attributes) noexcept : Resource(common::KeyValueIterableView(attributes)) + {} -Resource( const std::initializer_list> attributes ) noexcept -: Resource (nostd::span>{attributes.begin(), - attributes.end()}) { } + Resource(const std::initializer_list> + attributes) noexcept + : Resource(nostd::span>{ + attributes.begin(), attributes.end()}) + {} + const opentelemetry::sdk::trace::AttributeMap &GetAttributes() const noexcept; -const opentelemetry::sdk::trace::AttributeMap& GetAttributes() const noexcept ; + std::unique_ptr Merge(const Resource &other); -std::unique_ptr Merge(const Resource &other) ; + static std::unique_ptr Create( + const opentelemetry::common::KeyValueIterable &attributes); -static std::unique_ptr Create(const opentelemetry::common::KeyValueIterable &attributes) ; + static std::unique_ptr Create( + std::initializer_list> + attributes) noexcept + { + return Create(nostd::span>{ + attributes.begin(), attributes.end()}); + } -static std::unique_ptr Create( - std::initializer_list> attributes) noexcept -{ - return Create(nostd::span>{attributes.begin(), - attributes.end()}); -} - -template ::value> * = nullptr> -static std::unique_ptr Create( const T &attributes ) noexcept -{ + template ::value> * = nullptr> + static std::unique_ptr Create(const T &attributes) noexcept + { return Create(common::KeyValueIterableView(attributes)); -} + } -static std::unique_ptr CreateEmpty() ; + static std::unique_ptr CreateEmpty(); -static std::unique_ptr Create(const std::string &attributes) ; + static std::unique_ptr Create(const std::string &attributes); private: -const opentelemetry::sdk::trace::AttributeMap attribute_map_; - + const opentelemetry::sdk::trace::AttributeMap attribute_map_; }; -} // namespace trace +} // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index 4b52b27048..f1fbeabeab 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -1,8 +1,8 @@ #pragma once -#include "opentelemetry/version.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -13,16 +13,15 @@ namespace resource class ResourceDetector { public: - virtual std::unique_ptr Detect() = 0; + virtual std::unique_ptr Detect() = 0; }; class OTELResourceDetector : public ResourceDetector { public: - std::unique_ptr Detect() noexcept override ; - + std::unique_ptr Detect() noexcept override; }; -} // namespace trace +} // namespace resource } // namespace sdk OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h index 821ad953fa..3aa9f3481a 100644 --- a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h @@ -116,6 +116,19 @@ class AttributeMap return attributes_; } + void AddAttributes(const AttributeMap &other) + { + for (auto &attr : other.attributes_) + { + if ((attributes_.find(attr.first) == attributes_.end()) || + (nostd::holds_alternative(attributes_[attr.first]) && + nostd::get(attributes_[attr.first]).size() == 0)) + { + attributes_[attr.first] = attr.second; + } + } + } + void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { diff --git a/sdk/src/resource/CMakeLists.txt b/sdk/src/resource/CMakeLists.txt index 0b75bf4be1..f8e8267dc2 100644 --- a/sdk/src/resource/CMakeLists.txt +++ b/sdk/src/resource/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(opentelemetry_resources resource.cc resource_detector.cc) +add_library(opentelemetry_resources resource.cc resource_detector.cc) set_target_properties(opentelemetry_resources PROPERTIES EXPORT_NAME resources) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 5d54d00f7e..df9bc3bc39 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -1,8 +1,7 @@ -#include "opentelemetry/version.h" #include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/nostd/span.h" - +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -10,67 +9,70 @@ namespace sdk namespace resource { -const std::string TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language" ; -const std::string TELEMETRY_SDK_NAME = "telemetry.sdk.name" ; -const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version" ; - -Resource::Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept : attribute_map_(attributes){ } +const std::string TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language"; +const std::string TELEMETRY_SDK_NAME = "telemetry.sdk.name"; +const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version"; -Resource::Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept : attribute_map_(attributes){ } +Resource::Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept + : attribute_map_(attributes) +{} -Resource::Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept : attribute_map_(std::move(attributes)){ } +Resource::Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept + : attribute_map_(attributes) +{} +Resource::Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept + : attribute_map_(std::move(attributes)) +{} - - -const opentelemetry::sdk::trace::AttributeMap& Resource::GetAttributes() const noexcept +const opentelemetry::sdk::trace::AttributeMap &Resource::GetAttributes() const noexcept { - return attribute_map_; + return attribute_map_; } std::unique_ptr Resource::Merge(const Resource &other) { - opentelemetry::sdk::trace::AttributeMap merged_resource_attributes(other.GetAttributes()); - merged_resource_attributes.AddAttributes(attribute_map_); - return std::unique_ptr(new Resource(merged_resource_attributes)); + opentelemetry::sdk::trace::AttributeMap merged_resource_attributes(other.GetAttributes()); + merged_resource_attributes.AddAttributes(attribute_map_); + return std::unique_ptr(new Resource(merged_resource_attributes)); } -std::unique_ptr Resource::Create(const opentelemetry::common::KeyValueIterable &attributes) +std::unique_ptr Resource::Create( + const opentelemetry::common::KeyValueIterable &attributes) { - Resource default_resource ( {{ - { TELEMETRY_SDK_LANGUAGE, "cpp"}, - { TELEMETRY_SDK_NAME, "opentelemetry"}, - { TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION} - } }); - - if (attributes.size() > 0) { - Resource input_resource(attributes); - auto merged_resource = default_resource.Merge(input_resource); - return merged_resource->Merge(*(OTELResourceDetector().Detect())); - } - return default_resource.Merge(*(OTELResourceDetector().Detect())); + Resource default_resource({{{TELEMETRY_SDK_LANGUAGE, "cpp"}, + {TELEMETRY_SDK_NAME, "opentelemetry"}, + {TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION}}}); + + if (attributes.size() > 0) + { + Resource input_resource(attributes); + auto merged_resource = default_resource.Merge(input_resource); + return merged_resource->Merge(*(OTELResourceDetector().Detect())); + } + return default_resource.Merge(*(OTELResourceDetector().Detect())); } std::unique_ptr Resource::CreateEmpty() { - return Create({}); + return Create({}); } std::unique_ptr Resource::Create(const std::string &attributes) { - opentelemetry::sdk::trace::AttributeMap resource_attributes; - std::istringstream iss(attributes); - std::string token; - while (std::getline(iss, token, ',' )) - { - size_t pos = token.find('='); - std::string key = token.substr(0, pos); - std::string value = token.substr(pos + 1); - resource_attributes.SetAttribute(key, value); - } - return std::unique_ptr(new Resource(resource_attributes)); + opentelemetry::sdk::trace::AttributeMap resource_attributes; + std::istringstream iss(attributes); + std::string token; + while (std::getline(iss, token, ',')) + { + size_t pos = token.find('='); + std::string key = token.substr(0, pos); + std::string value = token.substr(pos + 1); + resource_attributes.SetAttribute(key, value); + } + return std::unique_ptr(new Resource(resource_attributes)); } -} -} +} // namespace resource +} // namespace sdk OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 7ac491e042..43448926da 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -1,4 +1,5 @@ #include "opentelemetry/sdk/resource/resource_detector.h" +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -6,17 +7,16 @@ namespace sdk namespace resource { - const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; +const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; - - std::unique_ptr OTELResourceDetector::Detect() noexcept - { - char *resource_attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); - if (resource_attributes_str == nullptr) - return std::unique_ptr(new Resource({})); - return Resource::Create(std::string(resource_attributes_str)); - } - -} +std::unique_ptr OTELResourceDetector::Detect() noexcept +{ + char *resource_attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); + if (resource_attributes_str == nullptr) + return std::unique_ptr(new Resource({})); + return Resource::Create(std::string(resource_attributes_str)); } + +} // namespace resource +} // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index e70527243f..765f16100a 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -1,55 +1,11 @@ cc_test( - name = "logger_provider_sdk_test", + name = "resouce_test", srcs = [ - "logger_provider_sdk_test.cc", + "resource_test.cc", ], deps = [ "//api", - "//sdk/src/logs", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "logger_sdk_test", - srcs = [ - "logger_sdk_test.cc", - ], - deps = [ - "//sdk/src/logs", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "simple_log_processor_test", - srcs = [ - "simple_log_processor_test.cc", - ], - deps = [ - "//sdk/src/logs", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "log_record_test", - srcs = [ - "log_record_test.cc", - ], - deps = [ - "//sdk/src/logs", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "batch_log_processor_test", - srcs = [ - "batch_log_processor_test.cc", - ], - deps = [ - "//sdk/src/logs", + "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], ) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 87ae6b8426..bbb85bfbf0 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -1,9 +1,10 @@ #include "opentelemetry/sdk/resource/resource.h" -#include "opentelemetry/sdk/resource/resource_detector.h" -#include "opentelemetry/sdk/trace/attribute_utils.h" #include "opentelemetry/common/key_value_iterable_view.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/resource/resource_detector.h" +#include "opentelemetry/sdk/trace/attribute_utils.h" +#include #include #include @@ -12,27 +13,95 @@ TEST(ResourceTest, create) { - std::map expected_attributes = {{"service","backend"},{"version", "1"},{"cost", "234.23"},{"telemetry.sdk.language","cpp"},{"telemetry.sdk.name", "opentelemetry"},{"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; - auto resource = opentelemetry::sdk::resource::Resource::Create({{"service","backend"}, {"version", "1"}, {"cost", "234.23"}}); - auto received_attributes = resource->GetAttributes().GetAttributes(); + std::map expected_attributes = { + {"service", "backend"}, + {"version", "1"}, + {"cost", "234.23"}, + {"telemetry.sdk.language", "cpp"}, + {"telemetry.sdk.name", "opentelemetry"}, + {"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; + auto resource = opentelemetry::sdk::resource::Resource::Create( + {{"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}); + auto received_attributes = resource->GetAttributes().GetAttributes(); + + for (auto &e : received_attributes) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + + std::map attributes = { + {"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}; + auto resource2 = opentelemetry::sdk::resource::Resource::Create< + std::map>(attributes); + auto received_attributes2 = resource2->GetAttributes().GetAttributes(); + for (auto &e : received_attributes2) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes2.size(), expected_attributes.size()); +} + +TEST(ResourceTest, Merge) +{ + std::map expected_attributes = {{"service", "backend"}, + {"host", "service-host"}}; + opentelemetry::sdk::resource::Resource resource1({{"service", "backend"}}); + opentelemetry::sdk::resource::Resource resource2({{"host", "service-host"}}); + + auto merged_resource = resource1.Merge(resource2); + auto received_attributes = merged_resource->GetAttributes().GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); +} - for (auto &e: received_attributes) - { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); - //std::cout << e.first << ":" << opentelemetry::nostd::get(e.second) << "\n"; +TEST(ResourceTest, MergeEmptyString) +{ + std::map expected_attributes = {{"service", "backend"}, + {"host", "service-host"}}; + opentelemetry::sdk::resource::Resource resource1({{"service", ""}, {"host", "service-host"}}); + opentelemetry::sdk::resource::Resource resource2( + {{"service", "backend"}, {"host", "another-service-host"}}); - } - EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + auto merged_resource = resource1.Merge(resource2); + auto received_attributes = merged_resource->GetAttributes().GetAttributes(); +} - std::map attributes = {{"service","backend"}, {"version", "1"}, {"cost", "234.23"}}; - auto resource2 = opentelemetry::sdk::resource::Resource::Create> (attributes); - auto received_attributes2 = resource2->GetAttributes().GetAttributes(); - for (auto &e: received_attributes2) - { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); - //std::cout << e.first << ":" << opentelemetry::nostd::get(e.second) << "\n"; +// this test uses putenv to set the env variable - this is not available on windows +#ifdef __linux__ +TEST(ResourceTest, OtelResourceDetector) +{ + std::map expected_attributes = {{"k", "v"}}; + char env[] = "OTEL_RESOURCE_ATTRIBUTES=k=v"; + putenv(env); + opentelemetry::sdk::resource::OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource->GetAttributes().GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); + char env2[] = "OTEL_RESOURCE_ATTRIBUTES="; + putenv(env2); +} - } +TEST(ResourceTest, OtelResourceDetectorEmptyEnv) +{ + std::map expected_attributes = {}; + char env[] = "OTEL_RESOURCE_ATTRIBUTES="; + putenv(env); + opentelemetry::sdk::resource::OTELResourceDetector detector; + auto resource = detector.Detect(); + auto received_attributes = resource->GetAttributes().GetAttributes(); + for (auto &e : received_attributes) + { + EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); +} - //opentelemetry::sdk::resource::Resource resource5(attributes); -} \ No newline at end of file +#endif \ No newline at end of file From 083b1699897c1d36030c81b00a16cb7eb6015a44 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 20:07:25 +0530 Subject: [PATCH 03/30] fix bazel --- sdk/test/resource/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index 765f16100a..a3b7b55b9b 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -1,5 +1,5 @@ cc_test( - name = "resouce_test", + name = "resource_test", srcs = [ "resource_test.cc", ], From b61acddb401c03ec7000e48a94c6184421659745 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 20:15:24 +0530 Subject: [PATCH 04/30] fix bazel another try --- sdk/test/resource/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index a3b7b55b9b..17e53c0830 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -5,7 +5,7 @@ cc_test( ], deps = [ "//api", - "//sdk/src/resource", + "//sdk/src/resources", "@com_google_googletest//:gtest_main", ], ) From 4540bc5e4ae5299a775f081c57eabcaf1436d8be Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 20:26:12 +0530 Subject: [PATCH 05/30] more fixes --- sdk/src/resource/BUILD | 2 +- sdk/test/resource/BUILD | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/resource/BUILD b/sdk/src/resource/BUILD index ec48f92aed..e42948b7eb 100644 --- a/sdk/src/resource/BUILD +++ b/sdk/src/resource/BUILD @@ -15,7 +15,7 @@ package(default_visibility = ["//visibility:public"]) cc_library( - name = "resources", + name = "resource", srcs = glob(["**/*.cc"]), hdrs = glob(["**/*.h"]), include_prefix = "src/resource", diff --git a/sdk/test/resource/BUILD b/sdk/test/resource/BUILD index 17e53c0830..a3b7b55b9b 100644 --- a/sdk/test/resource/BUILD +++ b/sdk/test/resource/BUILD @@ -5,7 +5,7 @@ cc_test( ], deps = [ "//api", - "//sdk/src/resources", + "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], ) From dfe857c59de301051ec9624770885f9490a59dbf Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Jan 2021 20:37:59 +0530 Subject: [PATCH 06/30] fix resouce interface constructor --- sdk/include/opentelemetry/sdk/resource/resource.h | 2 -- sdk/src/resource/resource.cc | 4 ---- 2 files changed, 6 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 42cb39e0a4..f26786a9ad 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -24,8 +24,6 @@ class Resource Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept; - Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept; - template ::value> * = nullptr> Resource(const T &attributes) noexcept : Resource(common::KeyValueIterableView(attributes)) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index df9bc3bc39..ef4883ec85 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -21,10 +21,6 @@ Resource::Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) no : attribute_map_(attributes) {} -Resource::Resource(opentelemetry::sdk::trace::AttributeMap &&attributes) noexcept - : attribute_map_(std::move(attributes)) -{} - const opentelemetry::sdk::trace::AttributeMap &Resource::GetAttributes() const noexcept { return attribute_map_; From 767300702161c8344d39127b27f39ea8884487a7 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Sat, 9 Jan 2021 23:46:16 +0530 Subject: [PATCH 07/30] fix review comments --- .../opentelemetry/sdk/resource/resource.h | 46 +++---------- .../sdk/resource/resource_detector.h | 4 +- sdk/src/resource/resource.cc | 67 +++++++++---------- sdk/src/resource/resource_detector.cc | 22 ++++-- sdk/test/resource/resource_test.cc | 49 +++++++++----- 5 files changed, 91 insertions(+), 97 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index f26786a9ad..0438e41353 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -17,52 +17,26 @@ namespace sdk namespace resource { +using ResourceAttributes = + std::unordered_map; + class Resource { public: - Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept; - - Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept; - - template ::value> * = nullptr> - Resource(const T &attributes) noexcept : Resource(common::KeyValueIterableView(attributes)) - {} - - Resource(const std::initializer_list> - attributes) noexcept - : Resource(nostd::span>{ - attributes.begin(), attributes.end()}) - {} - - const opentelemetry::sdk::trace::AttributeMap &GetAttributes() const noexcept; - - std::unique_ptr Merge(const Resource &other); + Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; - static std::unique_ptr Create( - const opentelemetry::common::KeyValueIterable &attributes); + const ResourceAttributes &GetAttributes() const noexcept; - static std::unique_ptr Create( - std::initializer_list> - attributes) noexcept - { - return Create(nostd::span>{ - attributes.begin(), attributes.end()}); - } + std::shared_ptr Merge(const Resource &other); - template ::value> * = nullptr> - static std::unique_ptr Create(const T &attributes) noexcept - { - return Create(common::KeyValueIterableView(attributes)); - } + static std::shared_ptr Create(const ResourceAttributes &attributes); - static std::unique_ptr CreateEmpty(); + static Resource &GetEmpty(); - static std::unique_ptr Create(const std::string &attributes); + static Resource &GetDefault(); private: - const opentelemetry::sdk::trace::AttributeMap attribute_map_; + ResourceAttributes attributes_; }; } // namespace resource diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index f1fbeabeab..a111fa43b2 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -13,13 +13,13 @@ namespace resource class ResourceDetector { public: - virtual std::unique_ptr Detect() = 0; + virtual std::shared_ptr Detect() = 0; }; class OTELResourceDetector : public ResourceDetector { public: - std::unique_ptr Detect() noexcept override; + std::shared_ptr Detect() noexcept override; }; } // namespace resource diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index ef4883ec85..ecd9f54f19 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -13,60 +13,53 @@ const std::string TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language"; const std::string TELEMETRY_SDK_NAME = "telemetry.sdk.name"; const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version"; -Resource::Resource(const opentelemetry::common::KeyValueIterable &attributes) noexcept - : attribute_map_(attributes) -{} +Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {} -Resource::Resource(const opentelemetry::sdk::trace::AttributeMap &attributes) noexcept - : attribute_map_(attributes) -{} - -const opentelemetry::sdk::trace::AttributeMap &Resource::GetAttributes() const noexcept -{ - return attribute_map_; -} - -std::unique_ptr Resource::Merge(const Resource &other) +std::shared_ptr Resource::Merge(const Resource &other) { - opentelemetry::sdk::trace::AttributeMap merged_resource_attributes(other.GetAttributes()); - merged_resource_attributes.AddAttributes(attribute_map_); - return std::unique_ptr(new Resource(merged_resource_attributes)); + ResourceAttributes merged_resource_attributes(attributes_); + for (auto &elem : other.attributes_) + { + if ((merged_resource_attributes.find(elem.first) == merged_resource_attributes.end()) || + (nostd::holds_alternative(attributes_[elem.first]) && + nostd::get(attributes_[elem.first]).size() == 0)) + { + merged_resource_attributes[elem.first] = elem.second; + } + } + return std::make_shared(Resource(merged_resource_attributes)); } -std::unique_ptr Resource::Create( - const opentelemetry::common::KeyValueIterable &attributes) +std::shared_ptr Resource::Create(const ResourceAttributes &attributes) { - Resource default_resource({{{TELEMETRY_SDK_LANGUAGE, "cpp"}, - {TELEMETRY_SDK_NAME, "opentelemetry"}, - {TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION}}}); + auto default_resource = Resource::GetDefault(); if (attributes.size() > 0) { - Resource input_resource(attributes); - auto merged_resource = default_resource.Merge(input_resource); + Resource tmp_resource(attributes); + auto merged_resource = tmp_resource.Merge(default_resource); return merged_resource->Merge(*(OTELResourceDetector().Detect())); } return default_resource.Merge(*(OTELResourceDetector().Detect())); } -std::unique_ptr Resource::CreateEmpty() +Resource &Resource::GetEmpty() { - return Create({}); + static Resource empty; + return empty; } -std::unique_ptr Resource::Create(const std::string &attributes) +Resource &Resource::GetDefault() { - opentelemetry::sdk::trace::AttributeMap resource_attributes; - std::istringstream iss(attributes); - std::string token; - while (std::getline(iss, token, ',')) - { - size_t pos = token.find('='); - std::string key = token.substr(0, pos); - std::string value = token.substr(pos + 1); - resource_attributes.SetAttribute(key, value); - } - return std::unique_ptr(new Resource(resource_attributes)); + static Resource default_resource({{TELEMETRY_SDK_LANGUAGE, "cpp"}, + {TELEMETRY_SDK_NAME, "opentelemetry"}, + {TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION}}); + return default_resource; +} + +const ResourceAttributes &Resource::GetAttributes() const noexcept +{ + return attributes_; } } // namespace resource diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 43448926da..cc93d7cccf 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -9,12 +9,24 @@ namespace resource const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; -std::unique_ptr OTELResourceDetector::Detect() noexcept +std::shared_ptr OTELResourceDetector::Detect() noexcept { - char *resource_attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); - if (resource_attributes_str == nullptr) - return std::unique_ptr(new Resource({})); - return Resource::Create(std::string(resource_attributes_str)); + char *attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); + if (attributes_str == nullptr) + return std::make_shared(Resource::GetEmpty()); + + ResourceAttributes attributes; + std::istringstream iss(attributes_str); + std::string token; + while (std::getline(iss, token, ',')) + { + size_t pos = token.find('='); + std::string key = token.substr(0, pos); + std::string value = token.substr(pos + 1); + attributes[key] = value; + } + + return std::make_shared(Resource(attributes)); } } // namespace resource diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index bbb85bfbf0..7f27a547b4 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -5,8 +5,8 @@ #include "opentelemetry/sdk/trace/attribute_utils.h" #include -#include #include +#include #include @@ -22,7 +22,7 @@ TEST(ResourceTest, create) {"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; auto resource = opentelemetry::sdk::resource::Resource::Create( {{"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}); - auto received_attributes = resource->GetAttributes().GetAttributes(); + auto received_attributes = resource->GetAttributes(); for (auto &e : received_attributes) { @@ -30,14 +30,16 @@ TEST(ResourceTest, create) } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); - std::map attributes = { + opentelemetry::sdk::resource::ResourceAttributes attributes = { {"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}; - auto resource2 = opentelemetry::sdk::resource::Resource::Create< - std::map>(attributes); - auto received_attributes2 = resource2->GetAttributes().GetAttributes(); + auto resource2 = opentelemetry::sdk::resource::Resource::Create(attributes); + auto received_attributes2 = resource2->GetAttributes(); for (auto &e : received_attributes2) { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + EXPECT_EQ(expected_attributes.find(e.first)->second, + opentelemetry::nostd::get(e.second)); } EXPECT_EQ(received_attributes2.size(), expected_attributes.size()); } @@ -46,14 +48,19 @@ TEST(ResourceTest, Merge) { std::map expected_attributes = {{"service", "backend"}, {"host", "service-host"}}; - opentelemetry::sdk::resource::Resource resource1({{"service", "backend"}}); - opentelemetry::sdk::resource::Resource resource2({{"host", "service-host"}}); + opentelemetry::sdk::resource::Resource resource1( + opentelemetry::sdk::resource::ResourceAttributes({{"service", "backend"}})); + opentelemetry::sdk::resource::Resource resource2( + opentelemetry::sdk::resource::ResourceAttributes({{"host", "service-host"}})); auto merged_resource = resource1.Merge(resource2); - auto received_attributes = merged_resource->GetAttributes().GetAttributes(); + auto received_attributes = merged_resource->GetAttributes(); for (auto &e : received_attributes) { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + EXPECT_EQ(expected_attributes.find(e.first)->second, + opentelemetry::nostd::get(e.second)); } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } @@ -67,7 +74,7 @@ TEST(ResourceTest, MergeEmptyString) {{"service", "backend"}, {"host", "another-service-host"}}); auto merged_resource = resource1.Merge(resource2); - auto received_attributes = merged_resource->GetAttributes().GetAttributes(); + auto received_attributes = merged_resource->GetAttributes(); } // this test uses putenv to set the env variable - this is not available on windows @@ -75,14 +82,19 @@ TEST(ResourceTest, MergeEmptyString) TEST(ResourceTest, OtelResourceDetector) { std::map expected_attributes = {{"k", "v"}}; - char env[] = "OTEL_RESOURCE_ATTRIBUTES=k=v"; + + char env[] = "OTEL_RESOURCE_ATTRIBUTES=k=v"; putenv(env); + opentelemetry::sdk::resource::OTELResourceDetector detector; auto resource = detector.Detect(); - auto received_attributes = resource->GetAttributes().GetAttributes(); + auto received_attributes = resource->GetAttributes(); for (auto &e : received_attributes) { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + EXPECT_EQ(expected_attributes.find(e.first)->second, + opentelemetry::nostd::get(e.second)); } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); char env2[] = "OTEL_RESOURCE_ATTRIBUTES="; @@ -96,10 +108,13 @@ TEST(ResourceTest, OtelResourceDetectorEmptyEnv) putenv(env); opentelemetry::sdk::resource::OTELResourceDetector detector; auto resource = detector.Detect(); - auto received_attributes = resource->GetAttributes().GetAttributes(); + auto received_attributes = resource->GetAttributes(); for (auto &e : received_attributes) { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); + if (expected_attributes.find(e.first) != expected_attributes.end()) + EXPECT_EQ(expected_attributes.find(e.first)->second, + opentelemetry::nostd::get(e.second)); } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } From d5b389f8bb4d7895f8aafe1edabb629092230f26 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 00:14:53 +0530 Subject: [PATCH 08/30] revert change --- .../opentelemetry/sdk/trace/attribute_utils.h | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h index 3aa9f3481a..1a0f57f048 100644 --- a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h @@ -115,20 +115,7 @@ class AttributeMap { return attributes_; } - - void AddAttributes(const AttributeMap &other) - { - for (auto &attr : other.attributes_) - { - if ((attributes_.find(attr.first) == attributes_.end()) || - (nostd::holds_alternative(attributes_[attr.first]) && - nostd::get(attributes_[attr.first]).size() == 0)) - { - attributes_[attr.first] = attr.second; - } - } - } - + void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { From 9fbe8bec835a6258f93c024485a03e319e9cc20f Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 00:16:43 +0530 Subject: [PATCH 09/30] revert change --- sdk/include/opentelemetry/sdk/trace/attribute_utils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h index 1a0f57f048..72102e1bce 100644 --- a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h @@ -115,7 +115,6 @@ class AttributeMap { return attributes_; } - void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { From 208f23103149dfac81d0e3c2415b8b7acc49be2d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 00:20:00 +0530 Subject: [PATCH 10/30] revert change --- sdk/include/opentelemetry/sdk/trace/attribute_utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h index 72102e1bce..1a0f57f048 100644 --- a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h @@ -115,6 +115,7 @@ class AttributeMap { return attributes_; } + void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { From 60034d9c32e7be2e8a11e31c8ae5e4102c797ee8 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 10:52:06 +0530 Subject: [PATCH 11/30] fix merge conflict --- sdk/include/opentelemetry/sdk/trace/attribute_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h index 1a0f57f048..821ad953fa 100644 --- a/sdk/include/opentelemetry/sdk/trace/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/trace/attribute_utils.h @@ -115,7 +115,7 @@ class AttributeMap { return attributes_; } - + void SetAttribute(nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { From bfdd7e140cf682ba5023723db8a51438ef2e74ee Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 13:20:45 +0530 Subject: [PATCH 12/30] fix attributes_utils.h --- sdk/include/opentelemetry/sdk/resource/resource.h | 6 +++--- sdk/src/resource/resource.cc | 2 +- sdk/test/resource/resource_test.cc | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 0438e41353..3d2260ad25 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -2,7 +2,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/sdk/trace/attribute_utils.h" +#include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/version/version.h" #include "opentelemetry/version.h" @@ -18,7 +18,7 @@ namespace resource { using ResourceAttributes = - std::unordered_map; + std::unordered_map; class Resource { @@ -27,7 +27,7 @@ class Resource const ResourceAttributes &GetAttributes() const noexcept; - std::shared_ptr Merge(const Resource &other); + std::shared_ptr Merge(const Resource &other) noexcept; static std::shared_ptr Create(const ResourceAttributes &attributes); diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index ecd9f54f19..a6e0fe323f 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -15,7 +15,7 @@ const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version"; Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {} -std::shared_ptr Resource::Merge(const Resource &other) +std::shared_ptr Resource::Merge(const Resource &other) noexcept { ResourceAttributes merged_resource_attributes(attributes_); for (auto &elem : other.attributes_) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 7f27a547b4..1fe6ce1b72 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -1,8 +1,8 @@ #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/common/key_value_iterable_view.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/resource/resource_detector.h" -#include "opentelemetry/sdk/trace/attribute_utils.h" #include #include From 2abd049b4624b7e120d330e1ebca0f6114b4ae99 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 14:45:23 +0530 Subject: [PATCH 13/30] fix review comments --- sdk/include/opentelemetry/sdk/resource/resource_detector.h | 4 ++-- sdk/src/resource/resource.cc | 7 ++++--- sdk/src/resource/resource_detector.cc | 1 - 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index a111fa43b2..62b132b701 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -13,13 +13,13 @@ namespace resource class ResourceDetector { public: - virtual std::shared_ptr Detect() = 0; + virtual std::shared_ptr Detect() = 0; }; class OTELResourceDetector : public ResourceDetector { public: - std::shared_ptr Detect() noexcept override; + std::shared_ptr Detect() noexcept override; }; } // namespace resource diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index a6e0fe323f..badf4c6187 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -32,15 +32,16 @@ std::shared_ptr Resource::Merge(const Resource &other) noexcept std::shared_ptr Resource::Create(const ResourceAttributes &attributes) { - auto default_resource = Resource::GetDefault(); + static std::shared_ptr otel_resource = OTELResourceDetector().Detect(); + auto default_resource = Resource::GetDefault(); if (attributes.size() > 0) { Resource tmp_resource(attributes); auto merged_resource = tmp_resource.Merge(default_resource); - return merged_resource->Merge(*(OTELResourceDetector().Detect())); + return merged_resource->Merge(*otel_resource); } - return default_resource.Merge(*(OTELResourceDetector().Detect())); + return default_resource.Merge(*otel_resource); } Resource &Resource::GetEmpty() diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index cc93d7cccf..4cbda9d5fd 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -25,7 +25,6 @@ std::shared_ptr OTELResourceDetector::Detect() noexcept std::string value = token.substr(pos + 1); attributes[key] = value; } - return std::make_shared(Resource(attributes)); } From f71a47c1e396a1d23c6173cf00af518aa596f214 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 20:14:50 +0530 Subject: [PATCH 14/30] fix attributes --- .../opentelemetry/sdk/resource/resource.h | 40 +++++++++++++++++-- .../sdk/resource/resource_detector.h | 2 +- sdk/src/resource/resource.cc | 10 +++-- sdk/src/resource/resource_detector.cc | 5 ++- sdk/test/resource/resource_test.cc | 26 +++++++++--- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 3d2260ad25..a0c7eaa106 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -1,8 +1,7 @@ #pragma once -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/sdk/common/attribute_utils.h" +#include "opentelemetry/sdk/resource/resource_detector.h" #include "opentelemetry/sdk/version/version.h" #include "opentelemetry/version.h" @@ -23,20 +22,53 @@ using ResourceAttributes = class Resource { public: - Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; - const ResourceAttributes &GetAttributes() const noexcept; + /** + * Returns a new, merged {@link Resource} by merging the current Resource + * with the other Resource. In case of a collision, current Resource takes + * precedence. + * + * @param other the Resource that will be merged with this. + * @returns the newly merged Resource. + */ + std::shared_ptr Merge(const Resource &other) noexcept; + /** + * Returns a newly created Resource with the specified attributes. + * It adds (merge) SDK attributes and OTEL attributes before returning. + * @param attributes for this resource + * @returns the newly created Resource. + */ + static std::shared_ptr Create(const ResourceAttributes &attributes); + /** + * Returns an Empty resource. + */ + static Resource &GetEmpty(); + /** + * Returns a Resource that indentifies the SDK in use. + */ + static Resource &GetDefault(); +protected: + /** + * The constructor is private and only for use internally by the class and + * inside ResourceDetector class. + * Users should use the Create factory method to obtain a Resource + * instance. + */ + Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; + private: ResourceAttributes attributes_; + + friend class OTELResourceDetector; }; } // namespace resource diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index 62b132b701..13ad0c1922 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -1,7 +1,6 @@ #pragma once #include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -10,6 +9,7 @@ namespace sdk namespace resource { +class Resource; class ResourceDetector { public: diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index badf4c6187..7b359ab14c 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -32,8 +32,10 @@ std::shared_ptr Resource::Merge(const Resource &other) noexcept std::shared_ptr Resource::Create(const ResourceAttributes &attributes) { - static std::shared_ptr otel_resource = OTELResourceDetector().Detect(); - auto default_resource = Resource::GetDefault(); + + // static std::shared_ptr otel_resource = OTELResourceDetector().Detect(); + auto default_resource = Resource::GetDefault(); + auto otel_resource = OTELResourceDetector().Detect(); if (attributes.size() > 0) { @@ -46,8 +48,8 @@ std::shared_ptr Resource::Create(const ResourceAttributes &attributes) Resource &Resource::GetEmpty() { - static Resource empty; - return empty; + static Resource empty_resource; + return empty_resource; } Resource &Resource::GetDefault() diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 4cbda9d5fd..1c8158048f 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -1,5 +1,6 @@ #include "opentelemetry/sdk/resource/resource_detector.h" #include +#include "opentelemetry/sdk/resource/resource.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -13,8 +14,8 @@ std::shared_ptr OTELResourceDetector::Detect() noexcept { char *attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); if (attributes_str == nullptr) - return std::make_shared(Resource::GetEmpty()); - + return std::make_shared(Resource()); + // return Resource::GetEmpty(); ResourceAttributes attributes; std::istringstream iss(attributes_str); std::string token; diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 1fe6ce1b72..85df3eb15b 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -10,6 +10,23 @@ #include +class TestResource : public opentelemetry::sdk::resource::Resource::Resource +{ +public: + TestResource(opentelemetry::sdk::resource::ResourceAttributes attributes = + opentelemetry::sdk::resource::ResourceAttributes()) + : Resource(attributes) + {} +}; + +/* +class ResourceTest : public testing::Test{ +protected: + TestResource resource; + +}; +*/ + TEST(ResourceTest, create) { @@ -48,9 +65,9 @@ TEST(ResourceTest, Merge) { std::map expected_attributes = {{"service", "backend"}, {"host", "service-host"}}; - opentelemetry::sdk::resource::Resource resource1( + TestResource resource1( opentelemetry::sdk::resource::ResourceAttributes({{"service", "backend"}})); - opentelemetry::sdk::resource::Resource resource2( + TestResource resource2( opentelemetry::sdk::resource::ResourceAttributes({{"host", "service-host"}})); auto merged_resource = resource1.Merge(resource2); @@ -69,9 +86,8 @@ TEST(ResourceTest, MergeEmptyString) { std::map expected_attributes = {{"service", "backend"}, {"host", "service-host"}}; - opentelemetry::sdk::resource::Resource resource1({{"service", ""}, {"host", "service-host"}}); - opentelemetry::sdk::resource::Resource resource2( - {{"service", "backend"}, {"host", "another-service-host"}}); + TestResource resource1({{"service", ""}, {"host", "service-host"}}); + TestResource resource2({{"service", "backend"}, {"host", "another-service-host"}}); auto merged_resource = resource1.Merge(resource2); auto received_attributes = merged_resource->GetAttributes(); From f7e49097f0687c9941326ff4c265c14c63f2db4c Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 20:28:51 +0530 Subject: [PATCH 15/30] add doc --- .../opentelemetry/sdk/resource/resource_detector.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index 13ad0c1922..3ccf8edf50 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -10,12 +10,20 @@ namespace resource { class Resource; + +/** + * Interface for a Resource Detector + */ class ResourceDetector { public: virtual std::shared_ptr Detect() = 0; }; +/** + * OTelResourceDetector to detect the presence of and create a Resource + * from the OTEL_RESOURCE_ATTRIBUTES environment variable. + */ class OTELResourceDetector : public ResourceDetector { public: From bc3bebc4b582eb132a77ce3066ed7151e179acb3 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 20:40:02 +0530 Subject: [PATCH 16/30] fix static --- sdk/src/resource/resource.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 7b359ab14c..c38ed53b29 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -32,10 +32,9 @@ std::shared_ptr Resource::Merge(const Resource &other) noexcept std::shared_ptr Resource::Create(const ResourceAttributes &attributes) { - - // static std::shared_ptr otel_resource = OTELResourceDetector().Detect(); - auto default_resource = Resource::GetDefault(); - auto otel_resource = OTELResourceDetector().Detect(); + static auto otel_resource = OTELResourceDetector().Detect(); + auto default_resource = Resource::GetDefault(); + // auto otel_resource = OTELResourceDetector().Detect(); if (attributes.size() > 0) { From 48a30d4cd8264a5818ca9513c2151e9c0d3bb603 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 12 Jan 2021 21:08:19 +0530 Subject: [PATCH 17/30] fix test --- sdk/test/resource/resource_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 85df3eb15b..d73004cab8 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -10,7 +10,7 @@ #include -class TestResource : public opentelemetry::sdk::resource::Resource::Resource +class TestResource : public opentelemetry::sdk::resource::Resource { public: TestResource(opentelemetry::sdk::resource::ResourceAttributes attributes = From d8d015a5e0dc3632e841701aa78b663267630e67 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Sun, 17 Jan 2021 22:11:29 +0530 Subject: [PATCH 18/30] review comments --- examples/batch/CMakeLists.txt | 3 ++- examples/multithreaded/CMakeLists.txt | 5 +++-- examples/simple/CMakeLists.txt | 2 +- ext/src/zpages/CMakeLists.txt | 2 +- .../opentelemetry/sdk/resource/resource.h | 4 ++-- .../sdk/resource/resource_detector.h | 4 ++-- sdk/include/opentelemetry/sdk/trace/tracer.h | 6 +++++- .../opentelemetry/sdk/trace/tracer_provider.h | 17 +++++++++++++---- sdk/src/resource/resource.cc | 10 +++++----- sdk/src/resource/resource_detector.cc | 6 +++--- sdk/src/trace/span.cc | 4 +++- sdk/src/trace/span.h | 6 ++++-- sdk/src/trace/tracer.cc | 11 +++++++---- sdk/src/trace/tracer_provider.cc | 13 +++++++++++-- sdk/test/resource/resource_test.cc | 12 ++++++------ sdk/test/trace/CMakeLists.txt | 10 ++++++++-- 16 files changed, 76 insertions(+), 39 deletions(-) diff --git a/examples/batch/CMakeLists.txt b/examples/batch/CMakeLists.txt index 41654aeb93..604e11d116 100644 --- a/examples/batch/CMakeLists.txt +++ b/examples/batch/CMakeLists.txt @@ -4,4 +4,5 @@ add_executable(batch_span_processor_example main.cc) target_link_libraries( batch_span_processor_example ${CMAKE_THREAD_LIBS_INIT} ${CORE_RUNTIME_LIBS} - opentelemetry_exporter_ostream_span opentelemetry_trace) + opentelemetry_exporter_ostream_span opentelemetry_trace + opentelemetry_resources) diff --git a/examples/multithreaded/CMakeLists.txt b/examples/multithreaded/CMakeLists.txt index 0721f2e210..51a47539f0 100644 --- a/examples/multithreaded/CMakeLists.txt +++ b/examples/multithreaded/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include) add_executable(example_multithreaded main.cc) -target_link_libraries(example_multithreaded ${CMAKE_THREAD_LIBS_INIT} - opentelemetry_trace opentelemetry_exporter_ostream_span) +target_link_libraries( + example_multithreaded ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + opentelemetry_resources opentelemetry_exporter_ostream_span) diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index b86593ee37..87cc5946b8 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -6,4 +6,4 @@ target_link_libraries(foo_library opentelemetry_exporter_ostream_span add_executable(example_simple main.cc) target_link_libraries(example_simple ${CMAKE_THREAD_LIBS_INIT} foo_library - opentelemetry_trace) + opentelemetry_trace opentelemetry_resources) diff --git a/ext/src/zpages/CMakeLists.txt b/ext/src/zpages/CMakeLists.txt index 5440ea3fa6..433ecfe62c 100644 --- a/ext/src/zpages/CMakeLists.txt +++ b/ext/src/zpages/CMakeLists.txt @@ -10,7 +10,7 @@ set_target_properties(opentelemetry_zpages PROPERTIES EXPORT_NAME zpages) target_link_libraries( opentelemetry_zpages PUBLIC opentelemetry_ext opentelemetry_api - opentelemetry_trace) + opentelemetry_trace opentelemetry_resources) install( TARGETS opentelemetry_zpages diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index a0c7eaa106..d0a71b5801 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -33,7 +33,7 @@ class Resource * @returns the newly merged Resource. */ - std::shared_ptr Merge(const Resource &other) noexcept; + Resource Merge(const Resource &other) noexcept; /** * Returns a newly created Resource with the specified attributes. @@ -42,7 +42,7 @@ class Resource * @returns the newly created Resource. */ - static std::shared_ptr Create(const ResourceAttributes &attributes); + static Resource Create(const ResourceAttributes &attributes); /** * Returns an Empty resource. diff --git a/sdk/include/opentelemetry/sdk/resource/resource_detector.h b/sdk/include/opentelemetry/sdk/resource/resource_detector.h index 3ccf8edf50..76a38d60b7 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource_detector.h +++ b/sdk/include/opentelemetry/sdk/resource/resource_detector.h @@ -17,7 +17,7 @@ class Resource; class ResourceDetector { public: - virtual std::shared_ptr Detect() = 0; + virtual Resource Detect() = 0; }; /** @@ -27,7 +27,7 @@ class ResourceDetector class OTELResourceDetector : public ResourceDetector { public: - std::shared_ptr Detect() noexcept override; + Resource Detect() noexcept override; }; } // namespace resource diff --git a/sdk/include/opentelemetry/sdk/trace/tracer.h b/sdk/include/opentelemetry/sdk/trace/tracer.h index f633c8ed67..e721a1e4d2 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer.h @@ -1,6 +1,7 @@ #pragma once #include "opentelemetry/sdk/common/atomic_shared_ptr.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/trace/noop.h" @@ -23,7 +24,9 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th * nullptr. */ explicit Tracer(std::shared_ptr processor, - std::shared_ptr sampler = std::make_shared()) noexcept; + std::shared_ptr sampler = std::make_shared(), + const opentelemetry::sdk::resource::Resource &resource = + opentelemetry::sdk::resource::Resource::Create({})) noexcept; /** * Set the span processor associated with this tracer. @@ -57,6 +60,7 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th private: opentelemetry::sdk::AtomicSharedPtr processor_; const std::shared_ptr sampler_; + const opentelemetry::sdk::resource::Resource &resource_; }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h index 6e4c7b81ab..85dec7837d 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h @@ -5,6 +5,7 @@ #include #include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/sdk/trace/tracer.h" @@ -25,9 +26,10 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider * @param sampler The sampler for this tracer provider. This must * not be a nullptr. */ - explicit TracerProvider( - std::shared_ptr processor, - std::shared_ptr sampler = std::make_shared()) noexcept; + explicit TracerProvider(std::shared_ptr processor, + std::shared_ptr sampler = std::make_shared(), + opentelemetry::sdk::resource::Resource &&resource = + opentelemetry::sdk::resource::Resource::Create({})) noexcept; opentelemetry::nostd::shared_ptr GetTracer( nostd::string_view library_name, @@ -48,10 +50,16 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider /** * Obtain the sampler associated with this tracer provider. - * @return The span processor for this tracer provider. + * @return The sampler for this tracer provider. */ std::shared_ptr GetSampler() const noexcept; + /** + * Obtain the resource associated with this tracer provider. + * @return The resource for this tracer provider. + */ + const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; + /** * Shutdown the span processor associated with this tracer provider. */ @@ -61,6 +69,7 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider opentelemetry::sdk::AtomicSharedPtr processor_; std::shared_ptr tracer_; const std::shared_ptr sampler_; + const opentelemetry::sdk::resource::Resource resource_; }; } // namespace trace } // namespace sdk diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index c38ed53b29..add0e17cff 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -15,7 +15,7 @@ const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version"; Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {} -std::shared_ptr Resource::Merge(const Resource &other) noexcept +Resource Resource::Merge(const Resource &other) noexcept { ResourceAttributes merged_resource_attributes(attributes_); for (auto &elem : other.attributes_) @@ -27,10 +27,10 @@ std::shared_ptr Resource::Merge(const Resource &other) noexcept merged_resource_attributes[elem.first] = elem.second; } } - return std::make_shared(Resource(merged_resource_attributes)); + return Resource(merged_resource_attributes); } -std::shared_ptr Resource::Create(const ResourceAttributes &attributes) +Resource Resource::Create(const ResourceAttributes &attributes) { static auto otel_resource = OTELResourceDetector().Detect(); auto default_resource = Resource::GetDefault(); @@ -40,9 +40,9 @@ std::shared_ptr Resource::Create(const ResourceAttributes &attributes) { Resource tmp_resource(attributes); auto merged_resource = tmp_resource.Merge(default_resource); - return merged_resource->Merge(*otel_resource); + return merged_resource.Merge(otel_resource); } - return default_resource.Merge(*otel_resource); + return default_resource.Merge(otel_resource); } Resource &Resource::GetEmpty() diff --git a/sdk/src/resource/resource_detector.cc b/sdk/src/resource/resource_detector.cc index 1c8158048f..1f52680b14 100644 --- a/sdk/src/resource/resource_detector.cc +++ b/sdk/src/resource/resource_detector.cc @@ -10,11 +10,11 @@ namespace resource const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; -std::shared_ptr OTELResourceDetector::Detect() noexcept +Resource OTELResourceDetector::Detect() noexcept { char *attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES); if (attributes_str == nullptr) - return std::make_shared(Resource()); + return Resource(); // return Resource::GetEmpty(); ResourceAttributes attributes; std::istringstream iss(attributes_str); @@ -26,7 +26,7 @@ std::shared_ptr OTELResourceDetector::Detect() noexcept std::string value = token.substr(pos + 1); attributes[key] = value; } - return std::make_shared(Resource(attributes)); + return Resource(attributes); } } // namespace resource diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index a9b1851c5c..e37f3bc40a 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -63,7 +63,8 @@ Span::Span(std::shared_ptr &&tracer, const opentelemetry::common::KeyValueIterable &attributes, const trace_api::SpanContextKeyValueIterable &links, const trace_api::StartSpanOptions &options, - const trace_api::SpanContext &parent_span_context) noexcept + const trace_api::SpanContext &parent_span_context, + const opentelemetry::sdk::resource::Resource &resource) noexcept : tracer_{std::move(tracer)}, processor_{processor}, recordable_{processor_->MakeRecordable()}, @@ -108,6 +109,7 @@ Span::Span(std::shared_ptr &&tracer, recordable_->SetSpanKind(options.kind); recordable_->SetStartTime(NowOr(options.start_system_time)); start_steady_time = NowOr(options.start_steady_time); + // recordable_->SetResource(resource_); TODO processor_->OnStart(*recordable_); } diff --git a/sdk/src/trace/span.h b/sdk/src/trace/span.h index d4499985fb..d42e9f61ff 100644 --- a/sdk/src/trace/span.h +++ b/sdk/src/trace/span.h @@ -21,12 +21,14 @@ class Span final : public trace_api::Span const opentelemetry::common::KeyValueIterable &attributes, const trace_api::SpanContextKeyValueIterable &links, const trace_api::StartSpanOptions &options, - const trace_api::SpanContext &parent_span_context) noexcept; + const trace_api::SpanContext &parent_span_context, + const opentelemetry::sdk::resource::Resource &resource) noexcept; ~Span() override; // trace_api::Span - void SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept override; + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept override; void AddEvent(nostd::string_view name) noexcept override; diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 5bfac6bc55..7b8734959a 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -10,8 +10,10 @@ namespace sdk { namespace trace { -Tracer::Tracer(std::shared_ptr processor, std::shared_ptr sampler) noexcept - : processor_{processor}, sampler_{sampler} +Tracer::Tracer(std::shared_ptr processor, + std::shared_ptr sampler, + const opentelemetry::sdk::resource::Resource &resource) noexcept + : processor_{processor}, sampler_{sampler}, resource_{resource} {} void Tracer::SetProcessor(std::shared_ptr processor) noexcept @@ -71,8 +73,9 @@ nostd::shared_ptr Tracer::StartSpan( } else { - auto span = nostd::shared_ptr{new (std::nothrow) Span{ - this->shared_from_this(), processor_.load(), name, attributes, links, options, parent}}; + auto span = nostd::shared_ptr{ + new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, attributes, + links, options, parent, resource_}}; // if the attributes is not nullptr, add attributes to the span. if (sampling_result.attributes) diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 4040ea0226..76d914f26d 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -6,8 +6,12 @@ namespace sdk namespace trace { TracerProvider::TracerProvider(std::shared_ptr processor, - std::shared_ptr sampler) noexcept - : processor_{processor}, tracer_(new Tracer(std::move(processor), sampler)), sampler_(sampler) + std::shared_ptr sampler, + opentelemetry::sdk::resource::Resource &&resource) noexcept + : processor_{processor}, + tracer_(new Tracer(std::move(processor), sampler, resource)), + sampler_(sampler), + resource_(resource) {} opentelemetry::nostd::shared_ptr TracerProvider::GetTracer( @@ -35,6 +39,11 @@ std::shared_ptr TracerProvider::GetSampler() const noexcept return sampler_; } +const opentelemetry::sdk::resource::Resource &TracerProvider::GetResource() const noexcept +{ + return resource_; +} + bool TracerProvider::Shutdown() noexcept { return GetProcessor()->Shutdown(); diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index d73004cab8..a603817ee2 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -39,7 +39,7 @@ TEST(ResourceTest, create) {"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; auto resource = opentelemetry::sdk::resource::Resource::Create( {{"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}); - auto received_attributes = resource->GetAttributes(); + auto received_attributes = resource.GetAttributes(); for (auto &e : received_attributes) { @@ -50,7 +50,7 @@ TEST(ResourceTest, create) opentelemetry::sdk::resource::ResourceAttributes attributes = { {"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}; auto resource2 = opentelemetry::sdk::resource::Resource::Create(attributes); - auto received_attributes2 = resource2->GetAttributes(); + auto received_attributes2 = resource2.GetAttributes(); for (auto &e : received_attributes2) { EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); @@ -71,7 +71,7 @@ TEST(ResourceTest, Merge) opentelemetry::sdk::resource::ResourceAttributes({{"host", "service-host"}})); auto merged_resource = resource1.Merge(resource2); - auto received_attributes = merged_resource->GetAttributes(); + auto received_attributes = merged_resource.GetAttributes(); for (auto &e : received_attributes) { EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); @@ -90,7 +90,7 @@ TEST(ResourceTest, MergeEmptyString) TestResource resource2({{"service", "backend"}, {"host", "another-service-host"}}); auto merged_resource = resource1.Merge(resource2); - auto received_attributes = merged_resource->GetAttributes(); + auto received_attributes = merged_resource.GetAttributes(); } // this test uses putenv to set the env variable - this is not available on windows @@ -104,7 +104,7 @@ TEST(ResourceTest, OtelResourceDetector) opentelemetry::sdk::resource::OTELResourceDetector detector; auto resource = detector.Detect(); - auto received_attributes = resource->GetAttributes(); + auto received_attributes = resource.GetAttributes(); for (auto &e : received_attributes) { EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); @@ -124,7 +124,7 @@ TEST(ResourceTest, OtelResourceDetectorEmptyEnv) putenv(env); opentelemetry::sdk::resource::OTELResourceDetector detector; auto resource = detector.Detect(); - auto received_attributes = resource->GetAttributes(); + auto received_attributes = resource.GetAttributes(); for (auto &e : received_attributes) { EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index 0ba567a102..8a39c10a29 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -17,6 +17,7 @@ foreach( ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common opentelemetry_trace + opentelemetry_resources opentelemetry_exporter_in_memory) gtest_add_tests( TARGET ${testname} @@ -26,5 +27,10 @@ endforeach() add_executable(sampler_benchmark sampler_benchmark.cc) target_link_libraries( - sampler_benchmark benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT} - ${CORE_RUNTIME_LIBS} opentelemetry_trace opentelemetry_exporter_in_memory) + sampler_benchmark + benchmark::benchmark + ${CMAKE_THREAD_LIBS_INIT} + ${CORE_RUNTIME_LIBS} + opentelemetry_trace + opentelemetry_resources + opentelemetry_exporter_in_memory) From 48ab9eacdecd76fb67585ed76ec7d86867243cf7 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Sun, 17 Jan 2021 23:22:33 +0530 Subject: [PATCH 19/30] fix bazel build --- examples/batch/BUILD | 1 + examples/multithreaded/BUILD | 1 + examples/otlp/BUILD | 1 + examples/simple/BUILD | 1 + examples/zpages/BUILD | 1 + exporters/otlp/BUILD | 1 + ext/test/zpages/BUILD | 2 ++ sdk/test/trace/BUILD | 3 +++ 8 files changed, 11 insertions(+) diff --git a/examples/batch/BUILD b/examples/batch/BUILD index 2c9e6ef9e1..a5a92e2d29 100644 --- a/examples/batch/BUILD +++ b/examples/batch/BUILD @@ -7,6 +7,7 @@ cc_binary( deps = [ "//api", "//exporters/ostream:ostream_span_exporter", + "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/multithreaded/BUILD b/examples/multithreaded/BUILD index 640e1c78fc..4d428601ac 100644 --- a/examples/multithreaded/BUILD +++ b/examples/multithreaded/BUILD @@ -6,6 +6,7 @@ cc_binary( deps = [ "//api", "//exporters/ostream:ostream_span_exporter", + "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 29a4035697..9566dbf878 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -20,6 +20,7 @@ cc_binary( ":foo_library", "//api", "//exporters/otlp:otlp_exporter", + "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/simple/BUILD b/examples/simple/BUILD index a8e843e29d..5feb80241d 100644 --- a/examples/simple/BUILD +++ b/examples/simple/BUILD @@ -20,6 +20,7 @@ cc_binary( ":foo_library", "//api", "//exporters/ostream:ostream_span_exporter", + "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/zpages/BUILD b/examples/zpages/BUILD index a277c44810..08f09e4ae5 100644 --- a/examples/zpages/BUILD +++ b/examples/zpages/BUILD @@ -26,6 +26,7 @@ cc_binary( deps = [ "//ext:headers", "//ext/src/zpages", + "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index a10357a460..12acb58634 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -65,6 +65,7 @@ cc_test( deps = [ ":otlp_exporter", "//api", + "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], ) diff --git a/ext/test/zpages/BUILD b/ext/test/zpages/BUILD index 07c287d2fa..076e1fc6ce 100644 --- a/ext/test/zpages/BUILD +++ b/ext/test/zpages/BUILD @@ -17,6 +17,7 @@ cc_test( ], deps = [ "//ext/src/zpages", + "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], @@ -29,6 +30,7 @@ cc_test( ], deps = [ "//ext/src/zpages", + "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index 7322697e39..8f71385885 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -6,6 +6,7 @@ cc_test( "tracer_provider_test.cc", ], deps = [ + "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], @@ -52,6 +53,7 @@ cc_test( ], deps = [ "//exporters/memory:in_memory_span_exporter", + "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], @@ -107,6 +109,7 @@ otel_cc_benchmark( srcs = ["sampler_benchmark.cc"], deps = [ "//exporters/memory:in_memory_span_exporter", + "//sdk/src/resource", "//sdk/src/trace", ], ) From f51516546aee63f61a0d0261bb72cdc89116d74a Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Sun, 17 Jan 2021 23:24:24 +0530 Subject: [PATCH 20/30] remove un-necesaary include --- sdk/include/opentelemetry/sdk/resource/resource.h | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index d0a71b5801..2b17ac191e 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -5,7 +5,6 @@ #include "opentelemetry/sdk/version/version.h" #include "opentelemetry/version.h" -#include #include #include #include From 5b01258e16ba5122d77b0201e4368547b3b63ca9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 18 Jan 2021 10:54:46 +0530 Subject: [PATCH 21/30] add test for different resoure types --- sdk/test/resource/resource_test.cc | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index a603817ee2..cdefb9eeae 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -30,33 +30,32 @@ class ResourceTest : public testing::Test{ TEST(ResourceTest, create) { - std::map expected_attributes = { + opentelemetry::sdk::resource::ResourceAttributes expected_attributes = { {"service", "backend"}, - {"version", "1"}, - {"cost", "234.23"}, + {"version", (uint32_t)1}, + {"cost", 234.23}, {"telemetry.sdk.language", "cpp"}, {"telemetry.sdk.name", "opentelemetry"}, {"telemetry.sdk.version", OPENTELEMETRY_SDK_VERSION}}; - auto resource = opentelemetry::sdk::resource::Resource::Create( - {{"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}); - auto received_attributes = resource.GetAttributes(); - - for (auto &e : received_attributes) - { - EXPECT_EQ(expected_attributes[e.first], opentelemetry::nostd::get(e.second)); - } - EXPECT_EQ(received_attributes.size(), expected_attributes.size()); opentelemetry::sdk::resource::ResourceAttributes attributes = { - {"service", "backend"}, {"version", "1"}, {"cost", "234.23"}}; + {"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) { + EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end()); if (expected_attributes.find(e.first) != expected_attributes.end()) - EXPECT_EQ(expected_attributes.find(e.first)->second, - opentelemetry::nostd::get(e.second)); + 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_attributes2.size(), expected_attributes.size()); } From e9745f120092faeeb345227f471e27e81a30fb5d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 18 Jan 2021 11:28:03 +0530 Subject: [PATCH 22/30] fix otlp --- examples/otlp/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index 8b77baad38..d5173ef7ba 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -12,6 +12,7 @@ target_link_libraries( ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library opentelemetry_trace + opentelemetry_resources ${CORE_RUNTIME_LIBS} opentelemetry_exporter_otprotocol gRPC::grpc++) From 01fd5105e2df89613b5bbfed1d1826c1c5ceb8ae Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 18 Jan 2021 12:41:14 +0530 Subject: [PATCH 23/30] fix valgrind error --- sdk/test/resource/resource_test.cc | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index cdefb9eeae..5174e2e825 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -19,14 +19,6 @@ class TestResource : public opentelemetry::sdk::resource::Resource {} }; -/* -class ResourceTest : public testing::Test{ -protected: - TestResource resource; - -}; -*/ - TEST(ResourceTest, create) { @@ -80,7 +72,6 @@ TEST(ResourceTest, Merge) } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } - TEST(ResourceTest, MergeEmptyString) { std::map expected_attributes = {{"service", "backend"}, @@ -94,6 +85,7 @@ TEST(ResourceTest, MergeEmptyString) // this test uses putenv to set the env variable - this is not available on windows #ifdef __linux__ + TEST(ResourceTest, OtelResourceDetector) { std::map expected_attributes = {{"k", "v"}}; @@ -112,15 +104,13 @@ TEST(ResourceTest, OtelResourceDetector) opentelemetry::nostd::get(e.second)); } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); - char env2[] = "OTEL_RESOURCE_ATTRIBUTES="; - putenv(env2); + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); } TEST(ResourceTest, OtelResourceDetectorEmptyEnv) { std::map expected_attributes = {}; - char env[] = "OTEL_RESOURCE_ATTRIBUTES="; - putenv(env); + unsetenv("OTEL_RESOURCE_ATTRIBUTES"); opentelemetry::sdk::resource::OTELResourceDetector detector; auto resource = detector.Detect(); auto received_attributes = resource.GetAttributes(); @@ -133,5 +123,4 @@ TEST(ResourceTest, OtelResourceDetectorEmptyEnv) } EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } - -#endif \ No newline at end of file +#endif // __linux__ \ No newline at end of file From b023c2af6ca8037974ee7fcc762908109285bca6 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 18 Jan 2021 13:17:48 +0530 Subject: [PATCH 24/30] modify simple trace example to use resource --- examples/simple/main.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/simple/main.cc b/examples/simple/main.cc index 485d31c2ec..cdf8371c77 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -14,8 +14,11 @@ void initTracer() new opentelemetry::exporter::trace::OStreamSpanExporter); auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); - auto provider = nostd::shared_ptr( - new sdktrace::TracerProvider(processor)); + opentelemetry::sdk::resource::OTELResourceDetector detector; + auto provider = + nostd::shared_ptr(new sdktrace::TracerProvider( + processor, std::make_shared(), + detector.Detect())); // Set the global trace provider opentelemetry::trace::Provider::SetTracerProvider(provider); } From 1586018b8b2e5ade598e4642a41267dbc3ed3356 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 18 Jan 2021 14:00:50 +0530 Subject: [PATCH 25/30] fix simple example --- examples/simple/main.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/simple/main.cc b/examples/simple/main.cc index cdf8371c77..43df020b63 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -14,11 +14,10 @@ void initTracer() new opentelemetry::exporter::trace::OStreamSpanExporter); auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); - opentelemetry::sdk::resource::OTELResourceDetector detector; auto provider = nostd::shared_ptr(new sdktrace::TracerProvider( processor, std::make_shared(), - detector.Detect())); + opentelemetry::sdk::resource::Resource::Create({}))); // Set the global trace provider opentelemetry::trace::Provider::SetTracerProvider(provider); } From 9158d71a09d8ebdbf741645a110d654d6336d350 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 21 Jan 2021 23:25:32 +0530 Subject: [PATCH 26/30] make resource ctor private --- examples/batch/BUILD | 1 - examples/batch/CMakeLists.txt | 3 +-- examples/multithreaded/BUILD | 1 - examples/multithreaded/CMakeLists.txt | 5 ++--- examples/multithreaded/main.cc | 3 ++- examples/otlp/BUILD | 1 - examples/otlp/CMakeLists.txt | 1 - examples/simple/BUILD | 1 - examples/simple/CMakeLists.txt | 2 +- examples/simple/main.cc | 8 ++++---- ext/src/zpages/CMakeLists.txt | 2 +- ext/test/zpages/BUILD | 2 -- ext/test/zpages/tracez_data_aggregator_test.cc | 4 +++- ext/test/zpages/tracez_processor_test.cc | 7 +++++-- sdk/include/opentelemetry/sdk/resource/resource.h | 5 +++-- sdk/include/opentelemetry/sdk/trace/tracer.h | 5 ++--- .../opentelemetry/sdk/trace/tracer_provider.h | 9 +++++---- sdk/src/resource/resource.cc | 1 - sdk/src/trace/BUILD | 1 + sdk/src/trace/CMakeLists.txt | 3 ++- sdk/src/trace/tracer.cc | 4 ++-- sdk/src/trace/tracer_provider.cc | 6 +++--- sdk/test/trace/sampler_benchmark.cc | 5 ++++- sdk/test/trace/tracer_provider_test.cc | 8 +++++--- sdk/test/trace/tracer_test.cc | 14 ++++++++++---- 25 files changed, 56 insertions(+), 46 deletions(-) diff --git a/examples/batch/BUILD b/examples/batch/BUILD index a5a92e2d29..2c9e6ef9e1 100644 --- a/examples/batch/BUILD +++ b/examples/batch/BUILD @@ -7,7 +7,6 @@ cc_binary( deps = [ "//api", "//exporters/ostream:ostream_span_exporter", - "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/batch/CMakeLists.txt b/examples/batch/CMakeLists.txt index 604e11d116..41654aeb93 100644 --- a/examples/batch/CMakeLists.txt +++ b/examples/batch/CMakeLists.txt @@ -4,5 +4,4 @@ add_executable(batch_span_processor_example main.cc) target_link_libraries( batch_span_processor_example ${CMAKE_THREAD_LIBS_INIT} ${CORE_RUNTIME_LIBS} - opentelemetry_exporter_ostream_span opentelemetry_trace - opentelemetry_resources) + opentelemetry_exporter_ostream_span opentelemetry_trace) diff --git a/examples/multithreaded/BUILD b/examples/multithreaded/BUILD index 4d428601ac..640e1c78fc 100644 --- a/examples/multithreaded/BUILD +++ b/examples/multithreaded/BUILD @@ -6,7 +6,6 @@ cc_binary( deps = [ "//api", "//exporters/ostream:ostream_span_exporter", - "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/multithreaded/CMakeLists.txt b/examples/multithreaded/CMakeLists.txt index 51a47539f0..0721f2e210 100644 --- a/examples/multithreaded/CMakeLists.txt +++ b/examples/multithreaded/CMakeLists.txt @@ -1,6 +1,5 @@ include_directories(${CMAKE_SOURCE_DIR}/exporters/ostream/include) add_executable(example_multithreaded main.cc) -target_link_libraries( - example_multithreaded ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace - opentelemetry_resources opentelemetry_exporter_ostream_span) +target_link_libraries(example_multithreaded ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_trace opentelemetry_exporter_ostream_span) diff --git a/examples/multithreaded/main.cc b/examples/multithreaded/main.cc index f4d7726ccc..1a756c973b 100644 --- a/examples/multithreaded/main.cc +++ b/examples/multithreaded/main.cc @@ -1,3 +1,4 @@ +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/simple_processor.h" #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" @@ -18,7 +19,7 @@ void initTracer() auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); auto provider = nostd::shared_ptr( - new sdktrace::TracerProvider(processor)); + new sdktrace::TracerProvider(processor, opentelemetry::sdk::resource::Resource::Create({}))); // Set the global trace provider opentelemetry::trace::Provider::SetTracerProvider(provider); } diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index 9566dbf878..29a4035697 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -20,7 +20,6 @@ cc_binary( ":foo_library", "//api", "//exporters/otlp:otlp_exporter", - "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index d5173ef7ba..8b77baad38 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -12,7 +12,6 @@ target_link_libraries( ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library opentelemetry_trace - opentelemetry_resources ${CORE_RUNTIME_LIBS} opentelemetry_exporter_otprotocol gRPC::grpc++) diff --git a/examples/simple/BUILD b/examples/simple/BUILD index 5feb80241d..a8e843e29d 100644 --- a/examples/simple/BUILD +++ b/examples/simple/BUILD @@ -20,7 +20,6 @@ cc_binary( ":foo_library", "//api", "//exporters/ostream:ostream_span_exporter", - "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index 87cc5946b8..b86593ee37 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -6,4 +6,4 @@ target_link_libraries(foo_library opentelemetry_exporter_ostream_span add_executable(example_simple main.cc) target_link_libraries(example_simple ${CMAKE_THREAD_LIBS_INIT} foo_library - opentelemetry_trace opentelemetry_resources) + opentelemetry_trace) diff --git a/examples/simple/main.cc b/examples/simple/main.cc index 43df020b63..5d22c6b155 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -14,10 +14,10 @@ void initTracer() new opentelemetry::exporter::trace::OStreamSpanExporter); auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); - auto provider = - nostd::shared_ptr(new sdktrace::TracerProvider( - processor, std::make_shared(), - opentelemetry::sdk::resource::Resource::Create({}))); + auto provider = nostd::shared_ptr( + new sdktrace::TracerProvider(processor, opentelemetry::sdk::resource::Resource::Create({}), + std::make_shared())); + // Set the global trace provider opentelemetry::trace::Provider::SetTracerProvider(provider); } diff --git a/ext/src/zpages/CMakeLists.txt b/ext/src/zpages/CMakeLists.txt index 433ecfe62c..5440ea3fa6 100644 --- a/ext/src/zpages/CMakeLists.txt +++ b/ext/src/zpages/CMakeLists.txt @@ -10,7 +10,7 @@ set_target_properties(opentelemetry_zpages PROPERTIES EXPORT_NAME zpages) target_link_libraries( opentelemetry_zpages PUBLIC opentelemetry_ext opentelemetry_api - opentelemetry_trace opentelemetry_resources) + opentelemetry_trace) install( TARGETS opentelemetry_zpages diff --git a/ext/test/zpages/BUILD b/ext/test/zpages/BUILD index 076e1fc6ce..07c287d2fa 100644 --- a/ext/test/zpages/BUILD +++ b/ext/test/zpages/BUILD @@ -17,7 +17,6 @@ cc_test( ], deps = [ "//ext/src/zpages", - "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], @@ -30,7 +29,6 @@ cc_test( ], deps = [ "//ext/src/zpages", - "//sdk/src/resource", "//sdk/src/trace", "@com_google_googletest//:gtest_main", ], diff --git a/ext/test/zpages/tracez_data_aggregator_test.cc b/ext/test/zpages/tracez_data_aggregator_test.cc index 78812e22b3..24355db266 100644 --- a/ext/test/zpages/tracez_data_aggregator_test.cc +++ b/ext/test/zpages/tracez_data_aggregator_test.cc @@ -3,6 +3,7 @@ #include #include "opentelemetry/ext/zpages/tracez_processor.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/recordable.h" #include "opentelemetry/sdk/trace/tracer.h" @@ -34,7 +35,8 @@ class TracezDataAggregatorTest : public ::testing::Test void SetUp() override { std::shared_ptr processor(new TracezSpanProcessor()); - tracer = std::shared_ptr(new Tracer(processor)); + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + tracer = std::shared_ptr(new Tracer(processor, resource)); tracez_data_aggregator = std::unique_ptr( new TracezDataAggregator(processor, milliseconds(10))); } diff --git a/ext/test/zpages/tracez_processor_test.cc b/ext/test/zpages/tracez_processor_test.cc index 65acddd807..2d9246495a 100644 --- a/ext/test/zpages/tracez_processor_test.cc +++ b/ext/test/zpages/tracez_processor_test.cc @@ -6,6 +6,7 @@ #include "opentelemetry/ext/zpages/threadsafe_span_data.h" #include "opentelemetry/nostd/span.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/tracer.h" using namespace opentelemetry::sdk::trace; @@ -174,8 +175,10 @@ class TracezProcessor : public ::testing::Test protected: void SetUp() override { - processor = std::shared_ptr(new TracezSpanProcessor()); - tracer = std::shared_ptr(new Tracer(processor)); + processor = std::shared_ptr(new TracezSpanProcessor()); + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + + tracer = std::shared_ptr(new Tracer(processor, resource)); auto spans = processor->GetSpanSnapshot(); running = spans.running; completed = std::move(spans.completed); diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 2b17ac191e..def1b31969 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -21,6 +21,8 @@ using ResourceAttributes = class Resource { public: + Resource(const Resource &) = default; + const ResourceAttributes &GetAttributes() const noexcept; /** @@ -55,7 +57,7 @@ class Resource static Resource &GetDefault(); -protected: +private: /** * The constructor is private and only for use internally by the class and * inside ResourceDetector class. @@ -64,7 +66,6 @@ class Resource */ Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; -private: ResourceAttributes attributes_; friend class OTELResourceDetector; diff --git a/sdk/include/opentelemetry/sdk/trace/tracer.h b/sdk/include/opentelemetry/sdk/trace/tracer.h index e721a1e4d2..8a9d955b67 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer.h @@ -24,9 +24,8 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th * nullptr. */ explicit Tracer(std::shared_ptr processor, - std::shared_ptr sampler = std::make_shared(), - const opentelemetry::sdk::resource::Resource &resource = - opentelemetry::sdk::resource::Resource::Create({})) noexcept; + const opentelemetry::sdk::resource::Resource &resource, + std::shared_ptr sampler = std::make_shared()) noexcept; /** * Set the span processor associated with this tracer. diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h index 85dec7837d..a6de0a998d 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h @@ -26,10 +26,11 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider * @param sampler The sampler for this tracer provider. This must * not be a nullptr. */ - explicit TracerProvider(std::shared_ptr processor, - std::shared_ptr sampler = std::make_shared(), - opentelemetry::sdk::resource::Resource &&resource = - opentelemetry::sdk::resource::Resource::Create({})) noexcept; + explicit TracerProvider( + std::shared_ptr processor, + opentelemetry::sdk::resource::Resource &&resource = + opentelemetry::sdk::resource::Resource::Create({}), + std::shared_ptr sampler = std::make_shared()) noexcept; opentelemetry::nostd::shared_ptr GetTracer( nostd::string_view library_name, diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index add0e17cff..3770eca9e5 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -34,7 +34,6 @@ Resource Resource::Create(const ResourceAttributes &attributes) { static auto otel_resource = OTELResourceDetector().Detect(); auto default_resource = Resource::GetDefault(); - // auto otel_resource = OTELResourceDetector().Detect(); if (attributes.size() > 0) { diff --git a/sdk/src/trace/BUILD b/sdk/src/trace/BUILD index 221066076b..34ec5108d2 100644 --- a/sdk/src/trace/BUILD +++ b/sdk/src/trace/BUILD @@ -23,5 +23,6 @@ cc_library( "//api", "//sdk:headers", "//sdk/src/common:random", + "//sdk/src/resource", ], ) diff --git a/sdk/src/trace/CMakeLists.txt b/sdk/src/trace/CMakeLists.txt index 2ae5ef7bb6..d4ff82f63e 100644 --- a/sdk/src/trace/CMakeLists.txt +++ b/sdk/src/trace/CMakeLists.txt @@ -5,7 +5,8 @@ add_library( set_target_properties(opentelemetry_trace PROPERTIES EXPORT_NAME trace) -target_link_libraries(opentelemetry_trace PUBLIC opentelemetry_common) +target_link_libraries(opentelemetry_trace PUBLIC opentelemetry_common + opentelemetry_resources) target_include_directories( opentelemetry_trace diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 7b8734959a..27e3aad58c 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -11,8 +11,8 @@ namespace sdk namespace trace { Tracer::Tracer(std::shared_ptr processor, - std::shared_ptr sampler, - const opentelemetry::sdk::resource::Resource &resource) noexcept + const opentelemetry::sdk::resource::Resource &resource, + std::shared_ptr sampler) noexcept : processor_{processor}, sampler_{sampler}, resource_{resource} {} diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 76d914f26d..b37ae5de56 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -6,10 +6,10 @@ namespace sdk namespace trace { TracerProvider::TracerProvider(std::shared_ptr processor, - std::shared_ptr sampler, - opentelemetry::sdk::resource::Resource &&resource) noexcept + opentelemetry::sdk::resource::Resource &&resource, + std::shared_ptr sampler) noexcept : processor_{processor}, - tracer_(new Tracer(std::move(processor), sampler, resource)), + tracer_(new Tracer(std::move(processor), resource, sampler)), sampler_(sampler), resource_(resource) {} diff --git a/sdk/test/trace/sampler_benchmark.cc b/sdk/test/trace/sampler_benchmark.cc index 4d31d0f43f..5606206ce6 100644 --- a/sdk/test/trace/sampler_benchmark.cc +++ b/sdk/test/trace/sampler_benchmark.cc @@ -1,4 +1,5 @@ #include "opentelemetry/exporters/memory/in_memory_span_exporter.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" @@ -118,7 +119,9 @@ void BenchmarkSpanCreation(std::shared_ptr sampler, benchmark::State &s { std::unique_ptr exporter(new InMemorySpanExporter()); auto processor = std::make_shared(std::move(exporter)); - auto tracer = std::shared_ptr(new Tracer(processor, sampler)); + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + auto tracer = + std::shared_ptr(new Tracer(processor, resource, sampler)); while (state.KeepRunning()) { diff --git a/sdk/test/trace/tracer_provider_test.cc b/sdk/test/trace/tracer_provider_test.cc index 6226a16d3c..47c7ccc827 100644 --- a/sdk/test/trace/tracer_provider_test.cc +++ b/sdk/test/trace/tracer_provider_test.cc @@ -1,4 +1,5 @@ #include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/sdk/trace/simple_processor.h" @@ -7,12 +8,13 @@ #include using namespace opentelemetry::sdk::trace; +using namespace opentelemetry::sdk::resource; TEST(TracerProvider, GetTracer) { std::shared_ptr processor(new SimpleSpanProcessor(nullptr)); - TracerProvider tp1(processor); + TracerProvider tp1(processor, Resource::Create({})); auto t1 = tp1.GetTracer("test"); auto t2 = tp1.GetTracer("test"); auto t3 = tp1.GetTracer("different", "1.0.0"); @@ -30,7 +32,7 @@ TEST(TracerProvider, GetTracer) ASSERT_EQ(processor, sdkTracer1->GetProcessor()); ASSERT_EQ("AlwaysOnSampler", sdkTracer1->GetSampler()->GetDescription()); - TracerProvider tp2(processor, std::make_shared()); + TracerProvider tp2(processor, Resource::Create({}), std::make_shared()); auto sdkTracer2 = dynamic_cast(tp2.GetTracer("test").get()); ASSERT_EQ("AlwaysOffSampler", sdkTracer2->GetSampler()->GetDescription()); } @@ -54,7 +56,7 @@ TEST(TracerProvider, GetSampler) // Create a TracerProvicer with a custom AlwaysOffSampler. std::shared_ptr processor2(new SimpleSpanProcessor(nullptr)); - TracerProvider tp2(processor2, std::make_shared()); + TracerProvider tp2(processor2, Resource::Create({}), std::make_shared()); auto t3 = tp2.GetSampler(); ASSERT_EQ("AlwaysOffSampler", t3->GetDescription()); diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 45a5bfc020..d244ef2528 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -1,5 +1,6 @@ #include "opentelemetry/sdk/trace/tracer.h" #include "opentelemetry/exporters/memory/in_memory_span_exporter.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/sdk/trace/samplers/always_off.h" #include "opentelemetry/sdk/trace/samplers/always_on.h" #include "opentelemetry/sdk/trace/samplers/parent.h" @@ -9,6 +10,7 @@ #include using namespace opentelemetry::sdk::trace; +using namespace opentelemetry::sdk::resource; using opentelemetry::core::SteadyTimestamp; using opentelemetry::core::SystemTimestamp; namespace nostd = opentelemetry::nostd; @@ -49,7 +51,8 @@ std::shared_ptr initTracer( std::unique_ptr &&exporter) { auto processor = std::make_shared(std::move(exporter)); - return std::shared_ptr(new Tracer(processor)); + auto resource = Resource::Create({}); + return std::shared_ptr(new Tracer(processor, resource)); } std::shared_ptr initTracer( @@ -57,7 +60,9 @@ std::shared_ptr initTracer( std::shared_ptr sampler) { auto processor = std::make_shared(std::move(exporter)); - return std::shared_ptr(new Tracer(processor, sampler)); + auto resource = Resource::Create({}); + + return std::shared_ptr(new Tracer(processor, resource, sampler)); } } // namespace @@ -275,9 +280,10 @@ TEST(Tracer, StartSpanWithAttributesCopy) TEST(Tracer, GetSampler) { + auto resource = Resource::Create({}); // Create a Tracer with a default AlwaysOnSampler std::shared_ptr processor_1(new SimpleSpanProcessor(nullptr)); - std::shared_ptr tracer_on(new Tracer(std::move(processor_1))); + std::shared_ptr tracer_on(new Tracer(std::move(processor_1), resource)); auto t1 = tracer_on->GetSampler(); ASSERT_EQ("AlwaysOnSampler", t1->GetDescription()); @@ -285,7 +291,7 @@ TEST(Tracer, GetSampler) // Create a Tracer with a AlwaysOffSampler std::shared_ptr processor_2(new SimpleSpanProcessor(nullptr)); std::shared_ptr tracer_off( - new Tracer(std::move(processor_2), std::make_shared())); + new Tracer(std::move(processor_2), resource, std::make_shared())); auto t2 = tracer_off->GetSampler(); ASSERT_EQ("AlwaysOffSampler", t2->GetDescription()); From 35dbf99be19408cfbf72b9062c7ff11220a9b152 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 22 Jan 2021 00:44:49 +0530 Subject: [PATCH 27/30] fix access --- .../opentelemetry/sdk/resource/resource.h | 3 ++- sdk/src/resource/resource.cc | 20 ++++++++---------- sdk/test/resource/resource_test.cc | 21 +++++++++++++------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index def1b31969..3e0de52aa8 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -57,7 +57,7 @@ class Resource static Resource &GetDefault(); -private: +protected: /** * The constructor is private and only for use internally by the class and * inside ResourceDetector class. @@ -66,6 +66,7 @@ class Resource */ Resource(const ResourceAttributes &attributes = ResourceAttributes()) noexcept; +private: ResourceAttributes attributes_; friend class OTELResourceDetector; diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 3770eca9e5..9a4acfff6a 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -9,20 +9,18 @@ namespace sdk namespace resource { -const std::string TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language"; -const std::string TELEMETRY_SDK_NAME = "telemetry.sdk.name"; -const std::string TELEMETRY_SDK_VERSION = "telemetry.sdk.version"; +const std::string kTelemetrySdkLanguage = "telemetry.sdk.language"; +const std::string kTelemetrySdkName = "telemetry.sdk.name"; +const std::string kTelemetrySdkVersion = "telemetry.sdk.version"; Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_(attributes) {} Resource Resource::Merge(const Resource &other) noexcept { - ResourceAttributes merged_resource_attributes(attributes_); - for (auto &elem : other.attributes_) + ResourceAttributes merged_resource_attributes(other.attributes_); + for (auto &elem : attributes_) { - if ((merged_resource_attributes.find(elem.first) == merged_resource_attributes.end()) || - (nostd::holds_alternative(attributes_[elem.first]) && - nostd::get(attributes_[elem.first]).size() == 0)) + if (merged_resource_attributes.find(elem.first) == merged_resource_attributes.end()) { merged_resource_attributes[elem.first] = elem.second; } @@ -52,9 +50,9 @@ Resource &Resource::GetEmpty() Resource &Resource::GetDefault() { - static Resource default_resource({{TELEMETRY_SDK_LANGUAGE, "cpp"}, - {TELEMETRY_SDK_NAME, "opentelemetry"}, - {TELEMETRY_SDK_VERSION, OPENTELEMETRY_SDK_VERSION}}); + static Resource default_resource({{kTelemetrySdkLanguage, "cpp"}, + {kTelemetrySdkName, "opentelemetry"}, + {kTelemetrySdkVersion, OPENTELEMETRY_SDK_VERSION}}); return default_resource; } diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index 5174e2e825..1a565128e0 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -54,12 +54,12 @@ TEST(ResourceTest, create) TEST(ResourceTest, Merge) { - std::map expected_attributes = {{"service", "backend"}, - {"host", "service-host"}}; TestResource resource1( opentelemetry::sdk::resource::ResourceAttributes({{"service", "backend"}})); TestResource resource2( opentelemetry::sdk::resource::ResourceAttributes({{"host", "service-host"}})); + std::map expected_attributes = {{"service", "backend"}, + {"host", "service-host"}}; auto merged_resource = resource1.Merge(resource2); auto received_attributes = merged_resource.GetAttributes(); @@ -74,13 +74,22 @@ TEST(ResourceTest, Merge) } TEST(ResourceTest, MergeEmptyString) { - std::map expected_attributes = {{"service", "backend"}, - {"host", "service-host"}}; - TestResource resource1({{"service", ""}, {"host", "service-host"}}); - TestResource resource2({{"service", "backend"}, {"host", "another-service-host"}}); + TestResource resource1({{"service", "backend"}, {"host", "service-host"}}); + TestResource resource2({{"service", ""}, {"host", "another-service-host"}}); + std::map expected_attributes = {{"service", ""}, + {"host", "another-service-host"}}; auto merged_resource = resource1.Merge(resource2); auto received_attributes = merged_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(expected_attributes.find(e.first)->second, + opentelemetry::nostd::get(e.second)); + } + EXPECT_EQ(received_attributes.size(), expected_attributes.size()); } // this test uses putenv to set the env variable - this is not available on windows From adbd86d7405291f74aac00b8bf3c7a26b1b8c45f Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 22 Jan 2021 00:54:32 +0530 Subject: [PATCH 28/30] fixes --- examples/zpages/BUILD | 1 - exporters/otlp/BUILD | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/zpages/BUILD b/examples/zpages/BUILD index 08f09e4ae5..a277c44810 100644 --- a/examples/zpages/BUILD +++ b/examples/zpages/BUILD @@ -26,7 +26,6 @@ cc_binary( deps = [ "//ext:headers", "//ext/src/zpages", - "//sdk/src/resource", "//sdk/src/trace", ], ) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 12acb58634..a10357a460 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -65,7 +65,6 @@ cc_test( deps = [ ":otlp_exporter", "//api", - "//sdk/src/resource", "@com_google_googletest//:gtest_main", ], ) From ef459941d4163a82dac4c6a85f010ef3591d3c0d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 22 Jan 2021 00:58:29 +0530 Subject: [PATCH 29/30] fix comment --- sdk/include/opentelemetry/sdk/resource/resource.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/resource/resource.h b/sdk/include/opentelemetry/sdk/resource/resource.h index 3e0de52aa8..3efecff255 100644 --- a/sdk/include/opentelemetry/sdk/resource/resource.h +++ b/sdk/include/opentelemetry/sdk/resource/resource.h @@ -59,7 +59,7 @@ class Resource protected: /** - * The constructor is private and only for use internally by the class and + * The constructor is protected and only for use internally by the class and * inside ResourceDetector class. * Users should use the Create factory method to obtain a Resource * instance. From a272b9299c730a98210bd47f27a7dd03330a2cb4 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 22 Jan 2021 09:53:15 +0530 Subject: [PATCH 30/30] Update sdk/src/resource/resource.cc Co-authored-by: Johannes Tax --- sdk/src/resource/resource.cc | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index 9a4acfff6a..7df4b6a668 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -18,13 +18,7 @@ Resource::Resource(const ResourceAttributes &attributes) noexcept : attributes_( Resource Resource::Merge(const Resource &other) noexcept { ResourceAttributes merged_resource_attributes(other.attributes_); - for (auto &elem : attributes_) - { - if (merged_resource_attributes.find(elem.first) == merged_resource_attributes.end()) - { - merged_resource_attributes[elem.first] = elem.second; - } - } + merged_resource_attributes.insert(attributes_.begin(), attributes_.end()); return Resource(merged_resource_attributes); } @@ -63,4 +57,4 @@ const ResourceAttributes &Resource::GetAttributes() const noexcept } // namespace resource } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE