From 6ecd201f95b4e813c71c8552b31c3ebcb29702f8 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 6 Jul 2023 22:46:53 +0200 Subject: [PATCH] [SDK] MeterProvider should own MeterContext, not share it (#2218) --- CHANGELOG.md | 9 +++++++++ examples/otlp/grpc_metric_main.cc | 4 +--- examples/otlp/http_metric_main.cc | 4 +--- sdk/include/opentelemetry/sdk/metrics/meter_provider.h | 4 ++-- .../opentelemetry/sdk/metrics/meter_provider_factory.h | 2 +- sdk/src/metrics/meter_provider.cc | 4 +++- sdk/src/metrics/meter_provider_factory.cc | 2 +- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52a9e19f26..83814e8c59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ Increment the: * [API] Remove include_trace_context [#2194](https://github.com/open-telemetry/opentelemetry-cpp/pull/2194) +* [SDK] MeterProvider should own MeterContext, not share it + [#2218](https://github.com/open-telemetry/opentelemetry-cpp/pull/2218) + Important changes: * [REMOVAL] Remove the jaeger exporter @@ -28,6 +31,12 @@ Important changes: * The CMake `WITH_JAEGER` option has been removed * Please remove usage of `WITH_JAEGER` from user scripts and makefiles. +* [SDK] MeterProvider should own MeterContext, not share it + [#2218](https://github.com/open-telemetry/opentelemetry-cpp/pull/2218) + * The `MeterProvider` constructor now takes a `unique_ptr` on + `MeterContext`, instead of a `shared_ptr`. + * Please adjust SDK configuration code accordingly. + ## [1.9.1] 2023-05-26 * [DEPRECATION] Drop C++11 support diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 40d9777776..60d59d1e28 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -48,9 +48,7 @@ void InitMetrics() auto context = metric_sdk::MeterContextFactory::Create(); context->AddMetricReader(std::move(reader)); - std::shared_ptr s_context(std::move(context)); - - auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); + auto u_provider = metric_sdk::MeterProviderFactory::Create(std::move(context)); std::shared_ptr provider(std::move(u_provider)); metrics_api::Provider::SetMeterProvider(provider); diff --git a/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc index a361285fed..f33d57772f 100644 --- a/examples/otlp/http_metric_main.cc +++ b/examples/otlp/http_metric_main.cc @@ -52,9 +52,7 @@ void InitMetrics() auto context = metric_sdk::MeterContextFactory::Create(); context->AddMetricReader(std::move(reader)); - std::shared_ptr s_context(std::move(context)); - - auto u_provider = metric_sdk::MeterProviderFactory::Create(s_context); + auto u_provider = metric_sdk::MeterProviderFactory::Create(std::move(context)); std::shared_ptr provider(std::move(u_provider)); metrics_api::Provider::SetMeterProvider(provider); diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index fedf6aac61..b27fcfb969 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -44,9 +44,9 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider /** * Initialize a new meter provider with a specified context - * @param context The shared meter configuration/pipeline for this provider. + * @param context The owned meter configuration/pipeline for this provider. */ - explicit MeterProvider(std::shared_ptr context) noexcept; + explicit MeterProvider(std::unique_ptr context) noexcept; nostd::shared_ptr GetMeter( nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 7a1343980c..6c0375ad9d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -69,7 +69,7 @@ class MeterProviderFactory const opentelemetry::sdk::resource::Resource &resource); static std::unique_ptr Create( - std::shared_ptr context); + std::unique_ptr context); }; } // namespace metrics diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 1e9dc5f434..a36260e201 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -21,7 +21,9 @@ namespace metrics namespace resource = opentelemetry::sdk::resource; namespace metrics_api = opentelemetry::metrics; -MeterProvider::MeterProvider(std::shared_ptr context) noexcept : context_{context} {} +MeterProvider::MeterProvider(std::unique_ptr context) noexcept + : context_(std::move(context)) +{} MeterProvider::MeterProvider(std::unique_ptr views, sdk::resource::Resource resource) noexcept diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc index 38527c90d8..bd6bbe932c 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -46,7 +46,7 @@ std::unique_ptr MeterProviderFactory::Cre } std::unique_ptr MeterProviderFactory::Create( - std::shared_ptr context) + std::unique_ptr context) { std::unique_ptr provider( new metrics_sdk::MeterProvider(std::move(context)));