From f4209de87a1ad9419520fda3c2d1472bd6da53ea Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Mon, 3 Aug 2026 16:11:28 -0400 Subject: [PATCH 1/2] [SDK] create instrumentation scope library --- sdk/CMakeLists.txt | 1 + .../instrumentation_scope.h | 73 +++-------- sdk/src/CMakeLists.txt | 1 + sdk/src/instrumentationscope/BUILD | 18 +++ sdk/src/instrumentationscope/CMakeLists.txt | 23 ++++ .../instrumentation_scope.cc | 113 ++++++++++++++++++ sdk/src/logs/BUILD | 1 + sdk/src/logs/CMakeLists.txt | 5 +- sdk/src/metrics/BUILD | 1 + sdk/src/metrics/CMakeLists.txt | 5 +- sdk/src/trace/BUILD | 1 + sdk/src/trace/CMakeLists.txt | 5 +- sdk/test/instrumentationscope/BUILD | 2 + sdk/test/instrumentationscope/CMakeLists.txt | 5 +- 14 files changed, 188 insertions(+), 66 deletions(-) create mode 100644 sdk/src/instrumentationscope/BUILD create mode 100644 sdk/src/instrumentationscope/CMakeLists.txt create mode 100644 sdk/src/instrumentationscope/instrumentation_scope.cc diff --git a/sdk/CMakeLists.txt b/sdk/CMakeLists.txt index 8e7d5e9def..b603c168c6 100644 --- a/sdk/CMakeLists.txt +++ b/sdk/CMakeLists.txt @@ -22,6 +22,7 @@ otel_add_component( opentelemetry_common opentelemetry_resources opentelemetry_version + opentelemetry_instrumentation_scope opentelemetry_logs opentelemetry_trace opentelemetry_metrics diff --git a/sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h b/sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h index 9ede694530..a5e9018772 100644 --- a/sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h +++ b/sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h @@ -3,14 +3,15 @@ #pragma once +#include #include -#include - +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/key_value_iterable.h" #include "opentelemetry/common/key_value_iterable_view.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/type_traits.h" #include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/nostd/utility.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/version.h" @@ -44,12 +45,7 @@ class InstrumentationScope nostd::string_view name, nostd::string_view version = "", nostd::string_view schema_url = "", - InstrumentationScopeAttributes &&attributes = {}) - { - return nostd::unique_ptr( - new InstrumentationScope{name, version, schema_url, std::move(attributes)}); - } - + InstrumentationScopeAttributes &&attributes = {}); /** * Returns a newly created InstrumentationScope with the specified library name and version. * @param name name of the instrumentation scope. @@ -62,11 +58,7 @@ class InstrumentationScope nostd::string_view name, nostd::string_view version, nostd::string_view schema_url, - const InstrumentationScopeAttributes &attributes) - { - return nostd::unique_ptr(new InstrumentationScope{ - name, version, schema_url, InstrumentationScopeAttributes(attributes)}); - } + const InstrumentationScopeAttributes &attributes); /** * Returns a newly created InstrumentationScope with the specified library name and version. @@ -98,18 +90,14 @@ class InstrumentationScope return result; } - std::size_t HashCode() const noexcept { return hash_code_; } + std::size_t HashCode() const noexcept; /** * Compare 2 instrumentation libraries. * @param other the instrumentation scope to compare to. * @returns true if the 2 instrumentation libraries are equal, false otherwise. */ - bool operator==(const InstrumentationScope &other) const noexcept - { - return this->name_ == other.name_ && this->version_ == other.version_ && - this->schema_url_ == other.schema_url_ && this->attributes_ == other.attributes_; - } + bool operator==(const InstrumentationScope &other) const noexcept; /** * Check whether the instrumentation scope has given name and version. @@ -124,52 +112,21 @@ class InstrumentationScope bool equal(const nostd::string_view name, const nostd::string_view version, const nostd::string_view schema_url = "", - const opentelemetry::common::KeyValueIterable *attributes = nullptr) const noexcept - { + const opentelemetry::common::KeyValueIterable *attributes = nullptr) const noexcept; - if (this->name_ != name || this->version_ != version || this->schema_url_ != schema_url) - { - return false; - } - - if (attributes == nullptr) - { - if (attributes_.empty()) - { - return true; - } - return false; - } - - return attributes_.EqualTo(*attributes); - } - - const std::string &GetName() const noexcept { return name_; } - const std::string &GetVersion() const noexcept { return version_; } - const std::string &GetSchemaURL() const noexcept { return schema_url_; } - const InstrumentationScopeAttributes &GetAttributes() const noexcept { return attributes_; } + const std::string &GetName() const noexcept; + const std::string &GetVersion() const noexcept; + const std::string &GetSchemaURL() const noexcept; + const InstrumentationScopeAttributes &GetAttributes() const noexcept; void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept - { - attributes_[std::string(key)] = - nostd::visit(opentelemetry::sdk::common::AttributeConverter(), value); - } + const opentelemetry::common::AttributeValue &value) noexcept; private: InstrumentationScope(nostd::string_view name, nostd::string_view version, nostd::string_view schema_url = "", - InstrumentationScopeAttributes &&attributes = {}) - : name_(name), version_(version), schema_url_(schema_url), attributes_(std::move(attributes)) - { - std::string hash_data; - hash_data.reserve(name_.size() + version_.size() + schema_url_.size()); - hash_data += name_; - hash_data += version_; - hash_data += schema_url_; - hash_code_ = std::hash{}(hash_data); - } + InstrumentationScopeAttributes &&attributes = {}); private: std::string name_; diff --git a/sdk/src/CMakeLists.txt b/sdk/src/CMakeLists.txt index e562fc0dc9..41f4881491 100644 --- a/sdk/src/CMakeLists.txt +++ b/sdk/src/CMakeLists.txt @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 add_subdirectory(common) +add_subdirectory(instrumentationscope) add_subdirectory(trace) add_subdirectory(metrics) add_subdirectory(logs) diff --git a/sdk/src/instrumentationscope/BUILD b/sdk/src/instrumentationscope/BUILD new file mode 100644 index 0000000000..3d89a28f7c --- /dev/null +++ b/sdk/src/instrumentationscope/BUILD @@ -0,0 +1,18 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +load("@rules_cc//cc:cc_library.bzl", "cc_library") + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "instrumentationscope", + srcs = [ + "instrumentation_scope.cc", + ], + include_prefix = "src/instrumentationscope", + deps = [ + "//api", + "//sdk:headers", + ], +) diff --git a/sdk/src/instrumentationscope/CMakeLists.txt b/sdk/src/instrumentationscope/CMakeLists.txt new file mode 100644 index 0000000000..5bb1ca67f8 --- /dev/null +++ b/sdk/src/instrumentationscope/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +add_library(opentelemetry_instrumentation_scope instrumentation_scope.cc) + +set_target_properties(opentelemetry_instrumentation_scope + PROPERTIES EXPORT_NAME instrumentation_scope) +set_target_version(opentelemetry_instrumentation_scope) + +target_include_directories( + opentelemetry_instrumentation_scope + PUBLIC "$" "$") + +target_link_libraries(opentelemetry_instrumentation_scope + PUBLIC opentelemetry_common) + +if(OPENTELEMETRY_INSTALL) + opentelemetry_add_pkgconfig( + instrumentation_scope + "OpenTelemetry SDK - Instrumentation Scope" + "Instrumentation Scope components for the OpenTelemetry SDK, a library for exporting telemetry." + "opentelemetry_common") +endif() diff --git a/sdk/src/instrumentationscope/instrumentation_scope.cc b/sdk/src/instrumentationscope/instrumentation_scope.cc new file mode 100644 index 0000000000..ab201eef66 --- /dev/null +++ b/sdk/src/instrumentationscope/instrumentation_scope.cc @@ -0,0 +1,113 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +#include +#include +#include +#include "opentelemetry/nostd/variant.h" + +OPENTELEMETRY_BEGIN_NAMESPACE + +namespace sdk +{ +namespace instrumentationscope +{ + +nostd::unique_ptr InstrumentationScope::Create( + nostd::string_view name, + nostd::string_view version, + nostd::string_view schema_url, + InstrumentationScopeAttributes &&attributes) +{ + return nostd::unique_ptr( + new InstrumentationScope{name, version, schema_url, std::move(attributes)}); +} + +nostd::unique_ptr InstrumentationScope::Create( + nostd::string_view name, + nostd::string_view version, + nostd::string_view schema_url, + const InstrumentationScopeAttributes &attributes) +{ + return nostd::unique_ptr(new InstrumentationScope{ + name, version, schema_url, InstrumentationScopeAttributes(attributes)}); +} + +std::size_t InstrumentationScope::HashCode() const noexcept +{ + return hash_code_; +} + +bool InstrumentationScope::operator==(const InstrumentationScope &other) const noexcept +{ + return this->name_ == other.name_ && this->version_ == other.version_ && + this->schema_url_ == other.schema_url_ && this->attributes_ == other.attributes_; +} + +bool InstrumentationScope::equal( + const nostd::string_view name, + const nostd::string_view version, + const nostd::string_view schema_url, + const opentelemetry::common::KeyValueIterable *attributes) const noexcept +{ + + if (this->name_ != name || this->version_ != version || this->schema_url_ != schema_url) + { + return false; + } + + if (attributes == nullptr) + { + if (attributes_.empty()) + { + return true; + } + return false; + } + + return attributes_.EqualTo(*attributes); +} + +const std::string &InstrumentationScope::GetName() const noexcept +{ + return name_; +} +const std::string &InstrumentationScope::GetVersion() const noexcept +{ + return version_; +} +const std::string &InstrumentationScope::GetSchemaURL() const noexcept +{ + return schema_url_; +} +const InstrumentationScopeAttributes &InstrumentationScope::GetAttributes() const noexcept +{ + return attributes_; +} + +void InstrumentationScope::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept +{ + attributes_[std::string(key)] = + nostd::visit(opentelemetry::sdk::common::AttributeConverter(), value); +} + +InstrumentationScope::InstrumentationScope(nostd::string_view name, + nostd::string_view version, + nostd::string_view schema_url, + InstrumentationScopeAttributes &&attributes) + : name_(name), version_(version), schema_url_(schema_url), attributes_(std::move(attributes)) +{ + std::string hash_data; + hash_data.reserve(name_.size() + version_.size() + schema_url_.size()); + hash_data += name_; + hash_data += version_; + hash_data += schema_url_; + hash_code_ = std::hash{}(hash_data); +} + +} // namespace instrumentationscope +} // namespace sdk + +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/logs/BUILD b/sdk/src/logs/BUILD index 2ac88900a8..e804a7a3a8 100644 --- a/sdk/src/logs/BUILD +++ b/sdk/src/logs/BUILD @@ -14,6 +14,7 @@ cc_library( "//sdk:headers", "//sdk/src/common:disabled", "//sdk/src/common:global_log_handler", + "//sdk/src/instrumentationscope", "//sdk/src/resource", ], ) diff --git a/sdk/src/logs/CMakeLists.txt b/sdk/src/logs/CMakeLists.txt index 338fa8e8f1..3be3195d56 100644 --- a/sdk/src/logs/CMakeLists.txt +++ b/sdk/src/logs/CMakeLists.txt @@ -27,8 +27,9 @@ add_library( set_target_properties(opentelemetry_logs PROPERTIES EXPORT_NAME logs) -target_link_libraries(opentelemetry_logs PUBLIC opentelemetry_resources - opentelemetry_common) +target_link_libraries( + opentelemetry_logs PUBLIC opentelemetry_resources opentelemetry_common + opentelemetry_instrumentation_scope) set_target_version(opentelemetry_logs) target_include_directories( diff --git a/sdk/src/metrics/BUILD b/sdk/src/metrics/BUILD index be1bdc73de..dcfec30949 100644 --- a/sdk/src/metrics/BUILD +++ b/sdk/src/metrics/BUILD @@ -15,6 +15,7 @@ cc_library( "//sdk/src/common:disabled", "//sdk/src/common:global_log_handler", "//sdk/src/common:random", + "//sdk/src/instrumentationscope", "//sdk/src/resource", ], ) diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 1ced671f3a..60bd5a56ee 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -40,8 +40,9 @@ add_library( set_target_properties(opentelemetry_metrics PROPERTIES EXPORT_NAME metrics) set_target_version(opentelemetry_metrics) -target_link_libraries(opentelemetry_metrics PUBLIC opentelemetry_common - opentelemetry_resources) +target_link_libraries( + opentelemetry_metrics PUBLIC opentelemetry_common opentelemetry_resources + opentelemetry_instrumentation_scope) target_include_directories( opentelemetry_metrics diff --git a/sdk/src/trace/BUILD b/sdk/src/trace/BUILD index 854ce7d261..71be1c353b 100644 --- a/sdk/src/trace/BUILD +++ b/sdk/src/trace/BUILD @@ -18,6 +18,7 @@ cc_library( "//sdk/src/common:env_variables", "//sdk/src/common:global_log_handler", "//sdk/src/common:random", + "//sdk/src/instrumentationscope", "//sdk/src/resource", ], ) diff --git a/sdk/src/trace/CMakeLists.txt b/sdk/src/trace/CMakeLists.txt index 5408f5877d..83e1a1601f 100644 --- a/sdk/src/trace/CMakeLists.txt +++ b/sdk/src/trace/CMakeLists.txt @@ -39,8 +39,9 @@ add_library( set_target_properties(opentelemetry_trace PROPERTIES EXPORT_NAME trace) set_target_version(opentelemetry_trace) -target_link_libraries(opentelemetry_trace PUBLIC opentelemetry_common - opentelemetry_resources) +target_link_libraries( + opentelemetry_trace PUBLIC opentelemetry_common opentelemetry_resources + opentelemetry_instrumentation_scope) target_include_directories( opentelemetry_trace diff --git a/sdk/test/instrumentationscope/BUILD b/sdk/test/instrumentationscope/BUILD index 34b5e0f4a0..93967b6c30 100644 --- a/sdk/test/instrumentationscope/BUILD +++ b/sdk/test/instrumentationscope/BUILD @@ -12,6 +12,8 @@ cc_test( deps = [ "//api", "//sdk:headers", + "//sdk/src/common:env_variables", + "//sdk/src/instrumentationscope", "@com_google_googletest//:gtest_main", ], ) diff --git a/sdk/test/instrumentationscope/CMakeLists.txt b/sdk/test/instrumentationscope/CMakeLists.txt index 659728300b..16382baa6a 100644 --- a/sdk/test/instrumentationscope/CMakeLists.txt +++ b/sdk/test/instrumentationscope/CMakeLists.txt @@ -5,8 +5,9 @@ include(GoogleTest) foreach(testname instrumentationscope_test) add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_sdk) + target_link_libraries( + ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_instrumentation_scope) gtest_add_tests( TARGET ${testname} TEST_PREFIX instrumentationscope. From 6b496fabfc611d4f9c0eb919727d152da0bcef02 Mon Sep 17 00:00:00 2001 From: Douglas Barker Date: Mon, 3 Aug 2026 17:14:51 -0400 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef17a78bf..13f13d0ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ Increment the: `std::mutex` blocks the waiter instead. Only this one contended lock changes; the API `SpinLockMutex` and all other SDK locks are unchanged. [#4245](https://github.com/open-telemetry/opentelemetry-cpp/pull/4245) + +* [SDK] Create instrumentation scope library + [#4351](https://github.com/open-telemetry/opentelemetry-cpp/pull/4351) + * [CONFIGURATION] Add the probability sampler to file configuration [#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334)