From fdbef7302dd3e38dea9367b8e217d2350dd3c680 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 7 Jul 2023 17:54:48 +0200 Subject: [PATCH 01/10] Proof of concept. Fixes #2033 --- api/include/opentelemetry/metrics/meter_provider.h | 14 ++++++++++++++ api/include/opentelemetry/metrics/noop.h | 11 +++++++++++ api/include/opentelemetry/version.h | 3 ++- .../test/otlp_grpc_log_record_exporter_test.cc | 6 +++--- sdk/include/opentelemetry/sdk/metrics/meter.h | 3 ++- .../opentelemetry/sdk/metrics/meter_provider.h | 8 ++++++++ sdk/src/metrics/meter.cc | 10 ++++++---- sdk/src/metrics/meter_provider.cc | 13 +++++++++++-- sdk/test/logs/log_record_test.cc | 4 ++-- 9 files changed, 59 insertions(+), 13 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 654c4022ea..33b50a4b0a 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -8,6 +8,11 @@ #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE +namespace common +{ +class KeyValueIterable; +}; + namespace metrics { @@ -26,9 +31,18 @@ class MeterProvider * Optionally a version can be passed to create a named and versioned Meter * instance. */ + +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + virtual nostd::shared_ptr GetMeter( + nostd::string_view library_name, + nostd::string_view library_version = "", + nostd::string_view schema_url = "", + const common::KeyValueIterable *attributes = nullptr) noexcept = 0; +#else virtual nostd::shared_ptr GetMeter(nostd::string_view library_name, nostd::string_view library_version = "", nostd::string_view schema_url = "") noexcept = 0; +#endif }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 3a754a6e00..f15c15ca62 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -196,12 +196,23 @@ class NoopMeterProvider final : public MeterProvider public: NoopMeterProvider() : meter_{nostd::shared_ptr(new NoopMeter)} {} +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + nostd::shared_ptr GetMeter( + nostd::string_view /* library_name */, + nostd::string_view /* library_version */, + nostd::string_view /* schema_url */, + const common::KeyValueIterable * /* attributes */) noexcept override + { + return meter_; + } +#else nostd::shared_ptr GetMeter(nostd::string_view /* library_name */, nostd::string_view /* library_version */, nostd::string_view /* schema_url */) noexcept override { return meter_; } +#endif private: nostd::shared_ptr meter_; diff --git a/api/include/opentelemetry/version.h b/api/include/opentelemetry/version.h index 2178591728..74188e58fc 100644 --- a/api/include/opentelemetry/version.h +++ b/api/include/opentelemetry/version.h @@ -6,7 +6,8 @@ #include "opentelemetry/common/macros.h" #include "opentelemetry/detail/preprocessor.h" -#define OPENTELEMETRY_ABI_VERSION_NO 1 +// For proof of concept only, testing version 2 build. +#define OPENTELEMETRY_ABI_VERSION_NO 2 #define OPENTELEMETRY_VERSION "1.9.1" #define OPENTELEMETRY_VERSION_MAJOR 1 #define OPENTELEMETRY_VERSION_MINOR 9 diff --git a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc index f2ed416327..518caaba5e 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -154,7 +154,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest) std::unique_ptr trace_stub_interface( trace_mock_stub); - auto trace_provider = opentelemetry::nostd::shared_ptr( + auto trace_provider = opentelemetry::nostd::shared_ptr( opentelemetry::sdk::trace::TracerProviderFactory::Create( opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create( GetExporter(trace_stub_interface)))); @@ -174,7 +174,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest) auto logger = provider->GetLogger("test", "opentelelemtry_library", "", schema_url, {{"scope_key1", "scope_value"}, {"scope_key2", 2}}); - std::unordered_map attributes; + std::unordered_map attributes; attributes["service.name"] = "unit_test_service"; attributes["tenant.id"] = "test_user"; attributes["bool_value"] = true; @@ -197,7 +197,7 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest) opentelemetry::trace::Provider::SetTracerProvider( opentelemetry::nostd::shared_ptr( new opentelemetry::trace::NoopTracerProvider())); - trace_provider = opentelemetry::nostd::shared_ptr(); + trace_provider = opentelemetry::nostd::shared_ptr(); } } // namespace otlp diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 41a10f69e9..36dea2b183 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -42,7 +42,8 @@ class Meter final : public opentelemetry::metrics::Meter explicit Meter( std::weak_ptr meter_context, std::unique_ptr scope = - opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("")) noexcept; + opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(""), + const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept; nostd::unique_ptr> CreateUInt64Counter( nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index b27fcfb969..24eb15e612 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -48,10 +48,18 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider */ explicit MeterProvider(std::unique_ptr context) noexcept; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + nostd::shared_ptr GetMeter( + nostd::string_view name, + nostd::string_view version = "", + nostd::string_view schema_url = "", + const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept override; +#else nostd::shared_ptr GetMeter( nostd::string_view name, nostd::string_view version = "", nostd::string_view schema_url = "") noexcept override; +#endif /** * Obtain the resource associated with this meter provider. diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 6b8b6c8925..517964cdc5 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -26,13 +26,15 @@ namespace metrics namespace metrics = opentelemetry::metrics; namespace nostd = opentelemetry::nostd; -Meter::Meter( - std::weak_ptr meter_context, - std::unique_ptr instrumentation_scope) noexcept +Meter::Meter(std::weak_ptr meter_context, + std::unique_ptr instrumentation_scope, + const opentelemetry::common::KeyValueIterable *attributes) noexcept : scope_{std::move(instrumentation_scope)}, meter_context_{meter_context}, observable_registry_(new ObservableRegistry()) -{} +{ + // FIXME: use attributes +} nostd::unique_ptr> Meter::CreateUInt64Counter( nostd::string_view name, diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index a36260e201..2bdb853281 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -35,8 +35,17 @@ MeterProvider::MeterProvider(std::unique_ptr views, nostd::shared_ptr MeterProvider::GetMeter( nostd::string_view name, nostd::string_view version, - nostd::string_view schema_url) noexcept + nostd::string_view schema_url +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + , + const opentelemetry::common::KeyValueIterable *attributes +#endif + ) noexcept { +#if OPENTELEMETRY_ABI_VERSION_NO < 2 + const opentelemetry::common::KeyValueIterable *attributes = nullptr; +#endif + if (name.data() == nullptr || name == "") { OTEL_INTERNAL_LOG_WARN("[MeterProvider::GetMeter] Library name is empty."); @@ -54,7 +63,7 @@ nostd::shared_ptr MeterProvider::GetMeter( } } auto lib = instrumentationscope::InstrumentationScope::Create(name, version, schema_url); - auto meter = std::shared_ptr(new Meter(context_, std::move(lib))); + auto meter = std::shared_ptr(new Meter(context_, std::move(lib), attributes)); context_->AddMeter(meter); return nostd::shared_ptr{meter}; } diff --git a/sdk/test/logs/log_record_test.cc b/sdk/test/logs/log_record_test.cc index 0e5de03050..4f7deb06d0 100644 --- a/sdk/test/logs/log_record_test.cc +++ b/sdk/test/logs/log_record_test.cc @@ -99,13 +99,13 @@ class TestBodyLogger : public opentelemetry::logs::Logger } } - const opentelemetry::v1::common::AttributeValue &GetLastLogRecord() const noexcept + const opentelemetry::common::AttributeValue &GetLastLogRecord() const noexcept { return last_body_; } private: - opentelemetry::v1::common::AttributeValue last_body_; + opentelemetry::common::AttributeValue last_body_; }; // Define a basic LoggerProvider class that returns an instance of the logger class defined above From 63fde43a373ce30ec038f0b51e5b2553608865ac Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 7 Jul 2023 22:13:56 +0200 Subject: [PATCH 02/10] Fixed instrumentation scope --- sdk/include/opentelemetry/sdk/metrics/meter.h | 3 +-- sdk/src/metrics/meter.cc | 10 ++++---- sdk/src/metrics/meter_provider.cc | 23 ++++++++++++++----- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 36dea2b183..41a10f69e9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -42,8 +42,7 @@ class Meter final : public opentelemetry::metrics::Meter explicit Meter( std::weak_ptr meter_context, std::unique_ptr scope = - opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(""), - const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept; + opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("")) noexcept; nostd::unique_ptr> CreateUInt64Counter( nostd::string_view name, diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 517964cdc5..6b8b6c8925 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -26,15 +26,13 @@ namespace metrics namespace metrics = opentelemetry::metrics; namespace nostd = opentelemetry::nostd; -Meter::Meter(std::weak_ptr meter_context, - std::unique_ptr instrumentation_scope, - const opentelemetry::common::KeyValueIterable *attributes) noexcept +Meter::Meter( + std::weak_ptr meter_context, + std::unique_ptr instrumentation_scope) noexcept : scope_{std::move(instrumentation_scope)}, meter_context_{meter_context}, observable_registry_(new ObservableRegistry()) -{ - // FIXME: use attributes -} +{} nostd::unique_ptr> Meter::CreateUInt64Counter( nostd::string_view name, diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 2bdb853281..62bea2b0f5 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -42,10 +42,6 @@ nostd::shared_ptr MeterProvider::GetMeter( #endif ) noexcept { -#if OPENTELEMETRY_ABI_VERSION_NO < 2 - const opentelemetry::common::KeyValueIterable *attributes = nullptr; -#endif - if (name.data() == nullptr || name == "") { OTEL_INTERNAL_LOG_WARN("[MeterProvider::GetMeter] Library name is empty."); @@ -62,8 +58,23 @@ nostd::shared_ptr MeterProvider::GetMeter( return nostd::shared_ptr{meter}; } } - auto lib = instrumentationscope::InstrumentationScope::Create(name, version, schema_url); - auto meter = std::shared_ptr(new Meter(context_, std::move(lib), attributes)); + + std::unique_ptr scope; + +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + if (attributes != nullptr) + { + instrumentationscope::InstrumentationScopeAttributes attrs_map(*attributes); + scope = + instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map); + } + else +#endif + { + scope = instrumentationscope::InstrumentationScope::Create(name, version, schema_url); + } + + auto meter = std::shared_ptr(new Meter(context_, std::move(scope))); context_->AddMeter(meter); return nostd::shared_ptr{meter}; } From b1b735fc6cb74553c82e69184390ff922bd75e96 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 7 Jul 2023 22:51:33 +0200 Subject: [PATCH 03/10] Cleanup --- api/include/opentelemetry/metrics/meter_provider.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 33b50a4b0a..dee6dee3cc 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -11,7 +11,7 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace common { class KeyValueIterable; -}; +} namespace metrics { From d2a107a40064b631eac0bb457ebe6519bf0fb28a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 7 Jul 2023 23:13:38 +0200 Subject: [PATCH 04/10] Code simplification. --- .../sdk/common/attribute_utils.h | 17 +++++++++++++-- sdk/src/metrics/meter_provider.cc | 21 +++++++------------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/common/attribute_utils.h b/sdk/include/opentelemetry/sdk/common/attribute_utils.h index c8afd657e2..e21649b695 100644 --- a/sdk/include/opentelemetry/sdk/common/attribute_utils.h +++ b/sdk/include/opentelemetry/sdk/common/attribute_utils.h @@ -105,10 +105,10 @@ struct AttributeConverter class AttributeMap : public std::unordered_map { public: - // Contruct empty attribute map + // Construct empty attribute map AttributeMap() : std::unordered_map() {} - // Contruct attribute map and populate with attributes + // Construct attribute map and populate with attributes AttributeMap(const opentelemetry::common::KeyValueIterable &attributes) : AttributeMap() { attributes.ForEachKeyValue( @@ -118,6 +118,19 @@ class AttributeMap : public std::unordered_map }); } + // Construct attribute map and populate with optional attributes + AttributeMap(const opentelemetry::common::KeyValueIterable *attributes) : AttributeMap() + { + if (attributes != nullptr) + { + attributes->ForEachKeyValue( + [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { + SetAttribute(key, value); + return true; + }); + } + } + // Construct map from initializer list by applying `SetAttribute` transform for every attribute AttributeMap( std::initializer_list> diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 62bea2b0f5..b4ee865df7 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -42,6 +42,10 @@ nostd::shared_ptr MeterProvider::GetMeter( #endif ) noexcept { +#if OPENTELEMETRY_ABI_VERSION_NO < 2 + const opentelemetry::common::KeyValueIterable *attributes = nullptr; +#endif + if (name.data() == nullptr || name == "") { OTEL_INTERNAL_LOG_WARN("[MeterProvider::GetMeter] Library name is empty."); @@ -59,20 +63,9 @@ nostd::shared_ptr MeterProvider::GetMeter( } } - std::unique_ptr scope; - -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 - if (attributes != nullptr) - { - instrumentationscope::InstrumentationScopeAttributes attrs_map(*attributes); - scope = - instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map); - } - else -#endif - { - scope = instrumentationscope::InstrumentationScope::Create(name, version, schema_url); - } + instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes); + auto scope = + instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map); auto meter = std::shared_ptr(new Meter(context_, std::move(scope))); context_->AddMeter(meter); From b404d40af7d79190027e1ed4a2df15533039ae6b Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 20 Sep 2023 22:19:54 +0200 Subject: [PATCH 05/10] Added CI to test ABI VERSION 2 Cleanup --- .github/workflows/ci.yml | 32 +++++++++++++ .../opentelemetry/metrics/meter_provider.h | 47 +++++++++++++++---- ci/do_ci.sh | 25 ++++++++++ 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d8db91364..5d2e0c4837 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -193,6 +193,38 @@ jobs: run: | (cd ./functional/otlp; ./run_test.sh) + cmake_clang_maintainer_abiv2_test: + name: CMake clang 14 (maintainer mode, abiv2) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: 'recursive' + - name: setup + env: + CC: /usr/bin/clang-14 + CXX: /usr/bin/clang++-14 + PROTOBUF_VERSION: 21.12 + run: | + sudo -E ./ci/setup_cmake.sh + sudo -E ./ci/setup_ci_environment.sh + sudo -E ./ci/install_protobuf.sh + - name: run cmake clang (maintainer mode, abiv2) + env: + CC: /usr/bin/clang-14 + CXX: /usr/bin/clang++-14 + run: | + ./ci/do_ci.sh cmake.maintainer.abiv2.test + - name: generate test cert + env: + CFSSL_VERSION: 1.6.3 + run: | + sudo -E ./tools/setup-cfssl.sh + (cd ./functional/cert; ./generate_cert.sh) + - name: run func test + run: | + (cd ./functional/otlp; ./run_test.sh) + cmake_msvc_maintainer_test: name: CMake msvc (maintainer mode) runs-on: windows-latest diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 6d465fd434..115b6590a7 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -3,16 +3,14 @@ #pragma once +#include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/common/key_value_iterable_view.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/type_traits.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace common -{ -class KeyValueIterable; -} - namespace metrics { @@ -25,25 +23,56 @@ class MeterProvider { public: virtual ~MeterProvider() = default; + +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 /** * Gets or creates a named Meter instance. * - * Optionally a version can be passed to create a named and versioned Meter - * instance. + * @since ABI_VERSION 2 + * + * @param[in] library_name Meter instrumentation scope + * @param[in] library_version Version, optional + * @param[in] schema_url Schema URL, optional + * @param[in] attributes Instrumentation scope attributes, optional */ - -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 virtual nostd::shared_ptr GetMeter( nostd::string_view library_name, nostd::string_view library_version = "", nostd::string_view schema_url = "", const common::KeyValueIterable *attributes = nullptr) noexcept = 0; #else + /** + * Gets or creates a named Meter instance. + * + * @since ABI_VERSION 1 + * + * @param[in] library_name Meter instrumentation scope + * @param[in] library_version Version, optional + * @param[in] schema_url Schema URL, optional + */ virtual nostd::shared_ptr GetMeter(nostd::string_view library_name, nostd::string_view library_version = "", nostd::string_view schema_url = "") noexcept = 0; #endif +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + nostd::shared_ptr GetMeter( + nostd::string_view library_name, + nostd::string_view library_version, + nostd::string_view schema_url, + std::initializer_list> attributes) + { + nostd::span> atributes_span{ + attributes.begin(), attributes.end()}; + + common::KeyValueIterableView< + nostd::span>> + iterable_attributes{atributes_span}; + + return GetMeter(library_name, library_version, schema_url, &iterable_attributes); + } +#endif + #ifdef ENABLE_REMOVE_METER_PREVIEW virtual void RemoveMeter(nostd::string_view library_name, nostd::string_view library_version = "", diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 6452faa451..b5afe89c71 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -174,6 +174,31 @@ elif [[ "$1" == "cmake.maintainer.cpp11.async.test" ]]; then make -k -j $(nproc) make test exit 0 +elif [[ "$1" == "cmake.maintainer.abiv2.test" ]]; then + cd "${BUILD_DIR}" + rm -rf * + cmake ${CMAKE_OPTIONS[@]} \ + -DWITH_OTLP_HTTP=ON \ + -DWITH_OTLP_HTTP_SSL_PREVIEW=ON \ + -DWITH_OTLP_HTTP_SSL_TLS_PREVIEW=ON \ + -DWITH_REMOVE_METER_PREVIEW=ON \ + -DWITH_PROMETHEUS=ON \ + -DWITH_EXAMPLES=ON \ + -DWITH_EXAMPLES_HTTP=ON \ + -DWITH_ZIPKIN=ON \ + -DBUILD_W3CTRACECONTEXT_TEST=ON \ + -DWITH_ELASTICSEARCH=ON \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ + -DWITH_ASYNC_EXPORT_PREVIEW=OFF \ + -DOTELCPP_MAINTAINER_MODE=ON \ + -DWITH_NO_DEPRECATED_CODE=ON \ + -DWITH_ABI_VERSION_1=OFF \ + -DWITH_ABI_VERSION_2=ON \ + ${IWYU} \ + "${SRC_DIR}" + eval "$MAKE_COMMAND" + make test + exit 0 elif [[ "$1" == "cmake.with_async_export.test" ]]; then cd "${BUILD_DIR}" rm -rf * From 7610dc9ef5fe1f74292079dc39ec6d3e325d0316 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 20 Sep 2023 22:51:36 +0200 Subject: [PATCH 06/10] cleanup --- .../opentelemetry/metrics/meter_provider.h | 32 +++++++++---------- api/include/opentelemetry/metrics/noop.h | 8 ++--- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 115b6590a7..1e1402688a 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -30,14 +30,14 @@ class MeterProvider * * @since ABI_VERSION 2 * - * @param[in] library_name Meter instrumentation scope - * @param[in] library_version Version, optional - * @param[in] schema_url Schema URL, optional + * @param[in] name Meter instrumentation scope + * @param[in] version Instrumentation scope version, optional + * @param[in] schema_url Instrumentation scope schema URL, optional * @param[in] attributes Instrumentation scope attributes, optional */ virtual nostd::shared_ptr GetMeter( - nostd::string_view library_name, - nostd::string_view library_version = "", + nostd::string_view name, + nostd::string_view version = "", nostd::string_view schema_url = "", const common::KeyValueIterable *attributes = nullptr) noexcept = 0; #else @@ -46,30 +46,30 @@ class MeterProvider * * @since ABI_VERSION 1 * - * @param[in] library_name Meter instrumentation scope - * @param[in] library_version Version, optional - * @param[in] schema_url Schema URL, optional + * @param[in] name Meter instrumentation scope + * @param[in] version Instrumentation scope version, optional + * @param[in] schema_url Instrumentation scope schema URL, optional */ - virtual nostd::shared_ptr GetMeter(nostd::string_view library_name, - nostd::string_view library_version = "", - nostd::string_view schema_url = "") noexcept = 0; + virtual nostd::shared_ptr GetMeter(nostd::string_view name, + nostd::string_view version = "", + nostd::string_view schema_url = "") noexcept = 0; #endif #if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::shared_ptr GetMeter( - nostd::string_view library_name, - nostd::string_view library_version, + nostd::string_view name, + nostd::string_view version, nostd::string_view schema_url, std::initializer_list> attributes) { - nostd::span> atributes_span{ + nostd::span> attributes_span{ attributes.begin(), attributes.end()}; common::KeyValueIterableView< nostd::span>> - iterable_attributes{atributes_span}; + iterable_attributes{attributes_span}; - return GetMeter(library_name, library_version, schema_url, &iterable_attributes); + return GetMeter(name, version, schema_url, &iterable_attributes); } #endif diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 46999db0cd..edbbe2c100 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -198,16 +198,16 @@ class NoopMeterProvider final : public MeterProvider #if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::shared_ptr GetMeter( - nostd::string_view /* library_name */, - nostd::string_view /* library_version */, + nostd::string_view /* name */, + nostd::string_view /* version */, nostd::string_view /* schema_url */, const common::KeyValueIterable * /* attributes */) noexcept override { return meter_; } #else - nostd::shared_ptr GetMeter(nostd::string_view /* library_name */, - nostd::string_view /* library_version */, + nostd::shared_ptr GetMeter(nostd::string_view /* name */, + nostd::string_view /* version */, nostd::string_view /* schema_url */) noexcept override { return meter_; From d4ec4afdb7560ab0a538f1ad2950d6707f4c729b Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 25 Sep 2023 23:24:45 +0200 Subject: [PATCH 07/10] Implemented support for key value iterable. Added unit tests. --- .../opentelemetry/metrics/meter_provider.h | 87 +++++++++++++++---- api/include/opentelemetry/metrics/noop.h | 2 +- .../sdk/metrics/meter_provider.h | 8 +- sdk/src/metrics/meter_provider.cc | 13 +-- sdk/test/metrics/meter_provider_sdk_test.cc | 64 ++++++++++++++ 5 files changed, 148 insertions(+), 26 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 1e1402688a..9eef69af5f 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -25,8 +25,9 @@ class MeterProvider virtual ~MeterProvider() = default; #if OPENTELEMETRY_ABI_VERSION_NO >= 2 + /** - * Gets or creates a named Meter instance. + * Gets or creates a named Meter instance (ABI). * * @since ABI_VERSION 2 * @@ -35,42 +36,96 @@ class MeterProvider * @param[in] schema_url Instrumentation scope schema URL, optional * @param[in] attributes Instrumentation scope attributes, optional */ - virtual nostd::shared_ptr GetMeter( + virtual nostd::shared_ptr DoGetMeter( nostd::string_view name, - nostd::string_view version = "", - nostd::string_view schema_url = "", - const common::KeyValueIterable *attributes = nullptr) noexcept = 0; -#else + nostd::string_view version, + nostd::string_view schema_url, + const common::KeyValueIterable *attributes) noexcept = 0; + /** - * Gets or creates a named Meter instance. + * Gets or creates a named Meter instance (API helper). * - * @since ABI_VERSION 1 + * @since ABI_VERSION 2 * * @param[in] name Meter instrumentation scope - * @param[in] version Instrumentation scope version, optional - * @param[in] schema_url Instrumentation scope schema URL, optional + * @param[in] version Instrumentation scope version + * @param[in] schema_url Instrumentation scope schema URL + * @param[in] attributes Instrumentation scope attributes */ - virtual nostd::shared_ptr GetMeter(nostd::string_view name, - nostd::string_view version = "", - nostd::string_view schema_url = "") noexcept = 0; -#endif + nostd::shared_ptr GetMeter(nostd::string_view name, + nostd::string_view version = "", + nostd::string_view schema_url = "", + const common::KeyValueIterable *attributes = nullptr) + { + return DoGetMeter(name, version, schema_url, attributes); + } -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + /** + * Gets or creates a named Meter instance (API helper). + * + * @since ABI_VERSION 2 + * + * @param[in] name Meter instrumentation scope + * @param[in] version Instrumentation scope version + * @param[in] schema_url Instrumentation scope schema URL + * @param[in] attributes Instrumentation scope attributes + */ nostd::shared_ptr GetMeter( nostd::string_view name, nostd::string_view version, nostd::string_view schema_url, std::initializer_list> attributes) { + /* Build a container from std::initializer_list. */ nostd::span> attributes_span{ attributes.begin(), attributes.end()}; + /* Build a view on the container. */ common::KeyValueIterableView< nostd::span>> iterable_attributes{attributes_span}; - return GetMeter(name, version, schema_url, &iterable_attributes); + /* Add attributes using the view. */ + return DoGetMeter(name, version, schema_url, &iterable_attributes); } + + /** + * Gets or creates a named Meter instance (API helper). + * + * @since ABI_VERSION 2 + * + * @param[in] name Meter instrumentation scope + * @param[in] version Instrumentation scope version + * @param[in] schema_url Instrumentation scope schema URL + * @param[in] attributes Instrumentation scope attributes + */ + template ::value> * = nullptr> + nostd::shared_ptr GetMeter(nostd::string_view name, + nostd::string_view version, + nostd::string_view schema_url, + const T &attributes) + { + /* Build a view on the container. */ + common::KeyValueIterableView iterable_attributes(attributes); + + /* Add attributes using the view. */ + return DoGetMeter(name, version, schema_url, &iterable_attributes); + } + +#else + /** + * Gets or creates a named Meter instance (ABI) + * + * @since ABI_VERSION 1 + * + * @param[in] name Meter instrumentation scope + * @param[in] version Instrumentation scope version, optional + * @param[in] schema_url Instrumentation scope schema URL, optional + */ + virtual nostd::shared_ptr GetMeter(nostd::string_view name, + nostd::string_view version = "", + nostd::string_view schema_url = "") noexcept = 0; #endif #ifdef ENABLE_REMOVE_METER_PREVIEW diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index edbbe2c100..6c91793080 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -197,7 +197,7 @@ class NoopMeterProvider final : public MeterProvider NoopMeterProvider() : meter_{nostd::shared_ptr(new NoopMeter)} {} #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - nostd::shared_ptr GetMeter( + nostd::shared_ptr DoGetMeter( nostd::string_view /* name */, nostd::string_view /* version */, nostd::string_view /* schema_url */, diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index 4fc251f434..f24c949b55 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -49,11 +49,11 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider explicit MeterProvider(std::unique_ptr context) noexcept; #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - nostd::shared_ptr GetMeter( + nostd::shared_ptr DoGetMeter( nostd::string_view name, - nostd::string_view version = "", - nostd::string_view schema_url = "", - const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept override; + nostd::string_view version, + nostd::string_view schema_url, + const opentelemetry::common::KeyValueIterable *attributes) noexcept override; #else nostd::shared_ptr GetMeter( nostd::string_view name, diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 297fe4582c..37367afcc4 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -32,15 +32,18 @@ MeterProvider::MeterProvider(std::unique_ptr views, OTEL_INTERNAL_LOG_DEBUG("[MeterProvider] MeterProvider created."); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +nostd::shared_ptr MeterProvider::DoGetMeter( + nostd::string_view name, + nostd::string_view version, + nostd::string_view schema_url, + const opentelemetry::common::KeyValueIterable *attributes) noexcept +#else nostd::shared_ptr MeterProvider::GetMeter( nostd::string_view name, nostd::string_view version, - nostd::string_view schema_url -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 - , - const opentelemetry::common::KeyValueIterable *attributes + nostd::string_view schema_url) noexcept #endif - ) noexcept { #if OPENTELEMETRY_ABI_VERSION_NO < 2 const opentelemetry::common::KeyValueIterable *attributes = nullptr; diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index a1ca8de330..08816964db 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -59,6 +59,70 @@ TEST(MeterProvider, GetMeter) mp1.Shutdown(); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +TEST(MeterProvider, GetMeterAbiv2) +{ + MeterProvider mp; + + auto m1 = mp.GetMeter("name1", "version1", "url1"); + + auto m2 = mp.GetMeter("name2", "version2", "url2", nullptr); + + auto m3 = mp.GetMeter("name3", "version3", "url3", {{"accept_single_attr", true}}); + + std::pair attr4 = { + "accept_single_attr", true}; + + auto m4 = mp.GetMeter("name4", "version4", "url4", {attr4}); + + auto m5 = mp.GetMeter("name5", "version5", "url5", {{"foo", "1"}, {"bar", "2"}}); + + std::initializer_list< + std::pair> + attrs6 = {{"foo", "1"}, {"bar", "2"}}; + + auto m6 = mp.GetMeter("name6", "version6", "url6", attrs6); + + typedef std::pair KV; + + std::initializer_list attrs7 = {{"foo", "1"}, {"bar", "2"}}; + auto m7 = mp.GetMeter("name7", "version7", "url7", attrs6); + + auto m8 = mp.GetMeter("name8", "version8", "url8", + {{"a", "string"}, + {"b", false}, + {"c", 314159}, + {"d", (unsigned int)314159}, + {"e", (int32_t)-20}, + {"f", (uint32_t)20}, + {"g", (int64_t)-20}, + {"h", (uint64_t)20}, + {"i", 3.1}, + {"j", "string"}}); + + std::map attr9{ + {"a", "string"}, {"b", false}, {"c", 314159}, {"d", (unsigned int)314159}, + {"e", (int32_t)-20}, {"f", (uint32_t)20}, {"g", (int64_t)-20}, {"h", (uint64_t)20}, + {"i", 3.1}, {"j", "string"}}; + + auto m9 = mp.GetMeter("name9", "version9", "url9", attr9); + + ASSERT_NE(nullptr, m1); + ASSERT_NE(nullptr, m2); + ASSERT_NE(nullptr, m3); + ASSERT_NE(nullptr, m4); + ASSERT_NE(nullptr, m5); + ASSERT_NE(nullptr, m6); + ASSERT_NE(nullptr, m7); + ASSERT_NE(nullptr, m8); + ASSERT_NE(nullptr, m9); + + // cleanup properly without crash + mp.ForceFlush(); + mp.Shutdown(); +} +#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */ + #ifdef ENABLE_REMOVE_METER_PREVIEW TEST(MeterProvider, RemoveMeter) { From 2125acb075505911a104d4260883473b10ad6dc8 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 25 Sep 2023 23:50:19 +0200 Subject: [PATCH 08/10] Fixed build break. --- sdk/test/metrics/meter_provider_sdk_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 08816964db..fc62aec4db 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -86,7 +86,7 @@ TEST(MeterProvider, GetMeterAbiv2) typedef std::pair KV; std::initializer_list attrs7 = {{"foo", "1"}, {"bar", "2"}}; - auto m7 = mp.GetMeter("name7", "version7", "url7", attrs6); + auto m7 = mp.GetMeter("name7", "version7", "url7", attrs7); auto m8 = mp.GetMeter("name8", "version8", "url8", {{"a", "string"}, From 8f9f1ec3e06dfb1ab084240b8f36e5ae8d728d0c Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 26 Sep 2023 12:09:35 +0200 Subject: [PATCH 09/10] Resolved overload issue. Expanded unit tests --- .../opentelemetry/metrics/meter_provider.h | 19 ++-- api/include/opentelemetry/metrics/noop.h | 2 +- .../sdk/metrics/meter_provider.h | 7 +- sdk/src/metrics/meter_provider.cc | 2 +- sdk/test/metrics/meter_provider_sdk_test.cc | 92 ++++++++++++++++--- 5 files changed, 97 insertions(+), 25 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 9eef69af5f..9612604ad1 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -36,7 +36,7 @@ class MeterProvider * @param[in] schema_url Instrumentation scope schema URL, optional * @param[in] attributes Instrumentation scope attributes, optional */ - virtual nostd::shared_ptr DoGetMeter( + virtual nostd::shared_ptr GetMeter( nostd::string_view name, nostd::string_view version, nostd::string_view schema_url, @@ -53,11 +53,10 @@ class MeterProvider * @param[in] attributes Instrumentation scope attributes */ nostd::shared_ptr GetMeter(nostd::string_view name, - nostd::string_view version = "", - nostd::string_view schema_url = "", - const common::KeyValueIterable *attributes = nullptr) + nostd::string_view version = "", + nostd::string_view schema_url = "") { - return DoGetMeter(name, version, schema_url, attributes); + return GetMeter(name, version, schema_url, nullptr); } /** @@ -86,7 +85,7 @@ class MeterProvider iterable_attributes{attributes_span}; /* Add attributes using the view. */ - return DoGetMeter(name, version, schema_url, &iterable_attributes); + return GetMeter(name, version, schema_url, &iterable_attributes); } /** @@ -110,7 +109,7 @@ class MeterProvider common::KeyValueIterableView iterable_attributes(attributes); /* Add attributes using the view. */ - return DoGetMeter(name, version, schema_url, &iterable_attributes); + return GetMeter(name, version, schema_url, &iterable_attributes); } #else @@ -129,9 +128,9 @@ class MeterProvider #endif #ifdef ENABLE_REMOVE_METER_PREVIEW - virtual void RemoveMeter(nostd::string_view library_name, - nostd::string_view library_version = "", - nostd::string_view schema_url = "") noexcept = 0; + virtual void RemoveMeter(nostd::string_view name, + nostd::string_view version = "", + nostd::string_view schema_url = "") noexcept = 0; #endif }; } // namespace metrics diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 6c91793080..edbbe2c100 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -197,7 +197,7 @@ class NoopMeterProvider final : public MeterProvider NoopMeterProvider() : meter_{nostd::shared_ptr(new NoopMeter)} {} #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - nostd::shared_ptr DoGetMeter( + nostd::shared_ptr GetMeter( nostd::string_view /* name */, nostd::string_view /* version */, nostd::string_view /* schema_url */, diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index f24c949b55..00f9a35ac2 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -48,8 +48,13 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider */ explicit MeterProvider(std::unique_ptr context) noexcept; + /* + Make sure GetMeter() helpers from the API are seen in overload resolution. + */ + using opentelemetry::metrics::MeterProvider::GetMeter; + #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - nostd::shared_ptr DoGetMeter( + nostd::shared_ptr GetMeter( nostd::string_view name, nostd::string_view version, nostd::string_view schema_url, diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 37367afcc4..23ddbc75cf 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -33,7 +33,7 @@ MeterProvider::MeterProvider(std::unique_ptr views, } #if OPENTELEMETRY_ABI_VERSION_NO >= 2 -nostd::shared_ptr MeterProvider::DoGetMeter( +nostd::shared_ptr MeterProvider::GetMeter( nostd::string_view name, nostd::string_view version, nostd::string_view schema_url, diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index fc62aec4db..1844f113d2 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -65,28 +65,84 @@ TEST(MeterProvider, GetMeterAbiv2) MeterProvider mp; auto m1 = mp.GetMeter("name1", "version1", "url1"); + ASSERT_NE(nullptr, m1); auto m2 = mp.GetMeter("name2", "version2", "url2", nullptr); + ASSERT_NE(nullptr, m2); auto m3 = mp.GetMeter("name3", "version3", "url3", {{"accept_single_attr", true}}); + ASSERT_NE(nullptr, m3); + { + auto meter = static_cast(m3.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 1); + auto attr = attrs.find("accept_single_attr"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), true); + } std::pair attr4 = { "accept_single_attr", true}; - auto m4 = mp.GetMeter("name4", "version4", "url4", {attr4}); + ASSERT_NE(nullptr, m4); + { + auto meter = static_cast(m4.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 1); + auto attr = attrs.find("accept_single_attr"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), true); + } auto m5 = mp.GetMeter("name5", "version5", "url5", {{"foo", "1"}, {"bar", "2"}}); + ASSERT_NE(nullptr, m5); + { + auto meter = static_cast(m5.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 2); + auto attr = attrs.find("bar"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), "2"); + } std::initializer_list< std::pair> - attrs6 = {{"foo", "1"}, {"bar", "2"}}; + attrs6 = {{"foo", "1"}, {"bar", 42}}; auto m6 = mp.GetMeter("name6", "version6", "url6", attrs6); + ASSERT_NE(nullptr, m6); + { + auto meter = static_cast(m6.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 2); + auto attr = attrs.find("bar"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), 42); + } typedef std::pair KV; - std::initializer_list attrs7 = {{"foo", "1"}, {"bar", "2"}}; + std::initializer_list attrs7 = {{"foo", 3.14}, {"bar", "2"}}; auto m7 = mp.GetMeter("name7", "version7", "url7", attrs7); + ASSERT_NE(nullptr, m7); + { + auto meter = static_cast(m7.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 2); + auto attr = attrs.find("foo"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), 3.14); + } auto m8 = mp.GetMeter("name8", "version8", "url8", {{"a", "string"}, @@ -99,6 +155,17 @@ TEST(MeterProvider, GetMeterAbiv2) {"h", (uint64_t)20}, {"i", 3.1}, {"j", "string"}}); + ASSERT_NE(nullptr, m8); + { + auto meter = static_cast(m8.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 10); + auto attr = attrs.find("e"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), -20); + } std::map attr9{ {"a", "string"}, {"b", false}, {"c", 314159}, {"d", (unsigned int)314159}, @@ -106,16 +173,17 @@ TEST(MeterProvider, GetMeterAbiv2) {"i", 3.1}, {"j", "string"}}; auto m9 = mp.GetMeter("name9", "version9", "url9", attr9); - - ASSERT_NE(nullptr, m1); - ASSERT_NE(nullptr, m2); - ASSERT_NE(nullptr, m3); - ASSERT_NE(nullptr, m4); - ASSERT_NE(nullptr, m5); - ASSERT_NE(nullptr, m6); - ASSERT_NE(nullptr, m7); - ASSERT_NE(nullptr, m8); ASSERT_NE(nullptr, m9); + { + auto meter = static_cast(m9.get()); + auto scope = meter->GetInstrumentationScope(); + auto attrs = scope->GetAttributes(); + ASSERT_EQ(attrs.size(), 10); + auto attr = attrs.find("h"); + ASSERT_FALSE(attr == attrs.end()); + ASSERT_TRUE(opentelemetry::nostd::holds_alternative(attr->second)); + EXPECT_EQ(opentelemetry::nostd::get(attr->second), 20); + } // cleanup properly without crash mp.ForceFlush(); From 1ac42b6d7ca48802212181c207dc2109570d7b5c Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 26 Sep 2023 14:47:32 +0200 Subject: [PATCH 10/10] Cleanup, changelog. --- CHANGELOG.md | 12 ++++++++++++ api/include/opentelemetry/metrics/meter_provider.h | 13 ++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38a077e7d8..713e99ec5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,18 @@ Increment the: [#2324](https://github.com/open-telemetry/opentelemetry-cpp/pull/2324) * [EXPORTER] Handle attribute key collisions caused by sanitation [#2324](https://github.com/open-telemetry/opentelemetry-cpp/pull/2326) +* [API] Add InstrumentationScope attributes in MeterProvider::GetMeter() + [#2224](https://github.com/open-telemetry/opentelemetry-cpp/pull/2224) + +Important changes: + +* [API] Add InstrumentationScope attributes in MeterProvider::GetMeter() + [#2224](https://github.com/open-telemetry/opentelemetry-cpp/pull/2224) + * MeterProvider::GetMeter() now accepts InstrumentationScope attributes. + * Because this is an `ABI` breaking change, the fix is only available + with the `CMake` option `WITH_ABI_VERSION_2=ON`. + * When building with `CMake` option `WITH_ABI_VERSION_1=ON` (by default) + the `ABI` is unchanged, and the fix is not available. ## [1.11.0] 2023-08-21 diff --git a/api/include/opentelemetry/metrics/meter_provider.h b/api/include/opentelemetry/metrics/meter_provider.h index 9612604ad1..152e543d36 100644 --- a/api/include/opentelemetry/metrics/meter_provider.h +++ b/api/include/opentelemetry/metrics/meter_provider.h @@ -32,9 +32,9 @@ class MeterProvider * @since ABI_VERSION 2 * * @param[in] name Meter instrumentation scope - * @param[in] version Instrumentation scope version, optional - * @param[in] schema_url Instrumentation scope schema URL, optional - * @param[in] attributes Instrumentation scope attributes, optional + * @param[in] version Instrumentation scope version + * @param[in] schema_url Instrumentation scope schema URL + * @param[in] attributes Instrumentation scope attributes (optional, may be nullptr) */ virtual nostd::shared_ptr GetMeter( nostd::string_view name, @@ -48,9 +48,8 @@ class MeterProvider * @since ABI_VERSION 2 * * @param[in] name Meter instrumentation scope - * @param[in] version Instrumentation scope version - * @param[in] schema_url Instrumentation scope schema URL - * @param[in] attributes Instrumentation scope attributes + * @param[in] version Instrumentation scope version, optional + * @param[in] schema_url Instrumentation scope schema URL, optional */ nostd::shared_ptr GetMeter(nostd::string_view name, nostd::string_view version = "", @@ -96,7 +95,7 @@ class MeterProvider * @param[in] name Meter instrumentation scope * @param[in] version Instrumentation scope version * @param[in] schema_url Instrumentation scope schema URL - * @param[in] attributes Instrumentation scope attributes + * @param[in] attributes Instrumentation scope attributes container */ template ::value> * = nullptr>