From 88abccae3921f29ace34efa4f17073d6841f4ef0 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 8 May 2024 15:04:51 +0200 Subject: [PATCH 01/18] WIP --- api/include/opentelemetry/plugin/tracer.h | 4 +++ api/include/opentelemetry/trace/noop.h | 4 +++ api/include/opentelemetry/trace/tracer.h | 9 +++++ api/test/singleton/singleton_test.cc | 4 +++ examples/otlp/file_main.cc | 14 ++++---- examples/otlp/grpc_main.cc | 19 ++++++---- examples/plugin/plugin/tracer.h | 4 +++ exporters/otlp/CMakeLists.txt | 2 +- .../otlp/test/otlp_file_exporter_test.cc | 4 +-- .../otlp_grpc_log_record_exporter_test.cc | 4 +-- .../otlp/test/otlp_http_exporter_test.cc | 16 ++++----- functional/otlp/func_http_main.cc | 2 -- sdk/include/opentelemetry/sdk/trace/tracer.h | 29 +++++++++++++++ .../sdk/trace/tracer_provider_factory.h | 24 ++++++------- sdk/src/trace/tracer_provider_factory.cc | 35 +++++++++---------- 15 files changed, 114 insertions(+), 60 deletions(-) diff --git a/api/include/opentelemetry/plugin/tracer.h b/api/include/opentelemetry/plugin/tracer.h index 068b08071d..9f3c7c76d1 100644 --- a/api/include/opentelemetry/plugin/tracer.h +++ b/api/include/opentelemetry/plugin/tracer.h @@ -104,6 +104,8 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this{new (std::nothrow) Span{this->shared_from_this(), span}}; } +#if OPENTELEMETRY_ABI_VERSION_NO == 1 + void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override { tracer_handle_->tracer().ForceFlushWithMicroseconds(timeout); @@ -114,6 +116,8 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_thistracer().CloseWithMicroseconds(timeout); } +#endif /* OPENTELEMETRY_ABI_VERSION_NO */ + private: // Note: The order is important here. // diff --git a/api/include/opentelemetry/trace/noop.h b/api/include/opentelemetry/trace/noop.h index 407f3c1ac9..345c4ba0c9 100644 --- a/api/include/opentelemetry/trace/noop.h +++ b/api/include/opentelemetry/trace/noop.h @@ -102,9 +102,13 @@ class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer, return noop_span; } +#if OPENTELEMETRY_ABI_VERSION_NO == 1 + void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {} void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {} + +#endif /* OPENTELEMETRY_ABI_VERSION_NO */ }; /** diff --git a/api/include/opentelemetry/trace/tracer.h b/api/include/opentelemetry/trace/tracer.h index 82b4d0ea02..db8f003d8c 100644 --- a/api/include/opentelemetry/trace/tracer.h +++ b/api/include/opentelemetry/trace/tracer.h @@ -163,6 +163,13 @@ class Tracer } } +#if OPENTELEMETRY_ABI_VERSION_NO == 1 + + /* + * The following is removed from the API in ABI version 2. + * It belongs to the SDK. + */ + /** * Force any buffered spans to flush. * @param timeout to complete the flush @@ -188,6 +195,8 @@ class Tracer } virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0; + +#endif /* OPENTELEMETRY_ABI_VERSION_NO */ }; } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/singleton/singleton_test.cc b/api/test/singleton/singleton_test.cc index 0973a1427f..fb4551cfe9 100644 --- a/api/test/singleton/singleton_test.cc +++ b/api/test/singleton/singleton_test.cc @@ -250,9 +250,13 @@ class MyTracer : public trace::Tracer return result; } +#if OPENTELEMETRY_ABI_VERSION_NO == 1 + void ForceFlushWithMicroseconds(uint64_t /* timeout */) noexcept override {} void CloseWithMicroseconds(uint64_t /* timeout */) noexcept override {} + +#endif /* OPENTELEMETRY_ABI_VERSION_NO */ }; class MyTracerProvider : public trace::TracerProvider diff --git a/examples/otlp/file_main.cc b/examples/otlp/file_main.cc index e0a1264fb6..675ad47a75 100644 --- a/examples/otlp/file_main.cc +++ b/examples/otlp/file_main.cc @@ -28,25 +28,25 @@ namespace otlp = opentelemetry::exporter::otlp; namespace { opentelemetry::exporter::otlp::OtlpFileExporterOptions opts; + +std::shared_ptr sdk_provider; + void InitTracer() { // Create OTLP exporter instance auto exporter = otlp::OtlpFileExporterFactory::Create(opts); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + sdk_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + trace::Provider::SetTracerProvider(sdk_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); - if (provider) + if (sdk_provider) { - static_cast(provider.get())->ForceFlush(); + sdk_provider->ForceFlush(std::chrono::milliseconds(10000)); } std::shared_ptr none; diff --git a/examples/otlp/grpc_main.cc b/examples/otlp/grpc_main.cc index c238d9ad2a..37a3a7987f 100644 --- a/examples/otlp/grpc_main.cc +++ b/examples/otlp/grpc_main.cc @@ -5,7 +5,10 @@ #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/nostd/shared_ptr.h" // sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when // destroy and shutdown exporters.It's optional to users. @@ -24,25 +27,29 @@ namespace otlp = opentelemetry::exporter::otlp; namespace { opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts; + +std::shared_ptr sdk_provider; + void InitTracer() { // Create OTLP exporter instance auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = + sdk_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + + std::shared_ptr api_provider ( sdk_provider ); // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); - if (provider) + if (sdk_provider) { - static_cast(provider.get())->ForceFlush(); + sdk_provider->ForceFlush(); + sdk_provider = nullptr; } std::shared_ptr none; diff --git a/examples/plugin/plugin/tracer.h b/examples/plugin/plugin/tracer.h index af2a4c5e92..1480b44541 100644 --- a/examples/plugin/plugin/tracer.h +++ b/examples/plugin/plugin/tracer.h @@ -20,7 +20,11 @@ class Tracer final : public opentelemetry::trace::Tracer, const opentelemetry::trace::SpanContextKeyValueIterable & /*links*/, const opentelemetry::trace::StartSpanOptions & /*options */) noexcept override; +#if OPENTELEMETRY_ABI_VERSION_NO == 1 + void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {} void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {} + +#endif /* OPENTELEMETRY_ABI_VERSION_NO */ }; diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 228c5cc67f..36c0e82cb8 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -491,7 +491,7 @@ if(BUILD_TESTING) target_link_libraries( otlp_file_client_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB} opentelemetry_exporter_otlp_file - opentelemetry_otlp_recordable) + opentelemetry_otlp_recordable nlohmann_json::nlohmann_json) gtest_add_tests( TARGET otlp_file_client_test TEST_PREFIX exporter.otlp. diff --git a/exporters/otlp/test/otlp_file_exporter_test.cc b/exporters/otlp/test/otlp_file_exporter_test.cc index 497bb0b203..16a1306f88 100644 --- a/exporters/otlp/test/otlp_file_exporter_test.cc +++ b/exporters/otlp/test/otlp_file_exporter_test.cc @@ -88,7 +88,7 @@ class OtlpFileExporterTestPeer : public ::testing::Test auto processor = std::unique_ptr( new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); - auto provider = nostd::shared_ptr( + auto provider = nostd::shared_ptr( new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; @@ -110,7 +110,7 @@ class OtlpFileExporterTestPeer : public ::testing::Test child_span->End(); parent_span->End(); - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); { auto check_json = nlohmann::json::parse(output.str(), nullptr, false); 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 c0c04c577a..90827f576f 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -288,7 +288,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)))); @@ -331,7 +331,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/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 8f4b938b78..e4f69f502b 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -135,7 +135,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test auto processor = std::unique_ptr( new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); - auto provider = nostd::shared_ptr( + auto provider = nostd::shared_ptr( new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; @@ -190,7 +190,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test child_span->End(); parent_span->End(); - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); } # ifdef ENABLE_ASYNC_EXPORT @@ -226,7 +226,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test auto processor = std::unique_ptr( new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); - auto provider = nostd::shared_ptr( + auto provider = nostd::shared_ptr( new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; @@ -285,7 +285,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test child_span->End(); parent_span->End(); - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); } # endif @@ -321,7 +321,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test auto processor = std::unique_ptr( new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); - auto provider = nostd::shared_ptr( + auto provider = nostd::shared_ptr( new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; @@ -366,7 +366,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test child_span->End(); parent_span->End(); - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); } # ifdef ENABLE_ASYNC_EXPORT @@ -402,7 +402,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test auto processor = std::unique_ptr( new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); - auto provider = nostd::shared_ptr( + auto provider = nostd::shared_ptr( new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; @@ -452,7 +452,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test child_span->End(); parent_span->End(); - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); } # endif }; diff --git a/functional/otlp/func_http_main.cc b/functional/otlp/func_http_main.cc index d584a85687..9cd3585668 100644 --- a/functional/otlp/func_http_main.cc +++ b/functional/otlp/func_http_main.cc @@ -173,8 +173,6 @@ void payload() auto span = tracer->StartSpan(k_span_name); span->SetAttribute(k_attr_test_name, opt_test_name); span->End(); - - tracer->ForceFlushWithMicroseconds(1000000); } void cleanup() diff --git a/sdk/include/opentelemetry/sdk/trace/tracer.h b/sdk/include/opentelemetry/sdk/trace/tracer.h index 67e606ba97..9cf788dd44 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer.h @@ -44,9 +44,38 @@ class Tracer final : public opentelemetry::trace::Tracer, const opentelemetry::trace::SpanContextKeyValueIterable &links, const opentelemetry::trace::StartSpanOptions &options = {}) noexcept override; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 + /** + * Force any buffered spans to flush. + * @param timeout to complete the flush + */ + template + void ForceFlush(std::chrono::duration timeout) noexcept + { + this->ForceFlushWithMicroseconds(static_cast( + std::chrono::duration_cast(timeout).count())); + } + + void ForceFlushWithMicroseconds(uint64_t timeout) noexcept; + + /** + * ForceFlush any buffered spans and stop reporting spans. + * @param timeout to complete the flush + */ + template + void Close(std::chrono::duration timeout) noexcept + { + this->CloseWithMicroseconds(static_cast( + std::chrono::duration_cast(timeout).count())); + } + + void CloseWithMicroseconds(uint64_t timeout) noexcept; +#else + /* Exposed in the API in ABI version 1, but does not belong to the API */ void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override; void CloseWithMicroseconds(uint64_t timeout) noexcept override; +#endif /** Returns the configured span processor. */ SpanProcessor &GetProcessor() noexcept { return context_->GetProcessor(); } diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h index e9dfa82edb..20fb801b75 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h @@ -6,14 +6,10 @@ #include #include +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace trace -{ -class TracerProvider; -} // namespace trace - namespace sdk { namespace resource @@ -37,19 +33,19 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory public: /* Serie of builders with a single processor. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr processor); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler, @@ -57,19 +53,19 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory /* Serie of builders with a vector of processor. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors); - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource); - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler); - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler, @@ -77,7 +73,7 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory /* Create with a tracer context. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr context); }; diff --git a/sdk/src/trace/tracer_provider_factory.cc b/sdk/src/trace/tracer_provider_factory.cc index c9b02a13ad..95477c5cbc 100644 --- a/sdk/src/trace/tracer_provider_factory.cc +++ b/sdk/src/trace/tracer_provider_factory.cc @@ -8,23 +8,20 @@ #include "opentelemetry/sdk/trace/tracer_context.h" #include "opentelemetry/sdk/trace/tracer_provider.h" -namespace trace_api = opentelemetry::trace; -namespace trace_sdk = opentelemetry::sdk::trace; - OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace trace { -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::unique_ptr processor) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processor), resource); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource) { @@ -32,7 +29,7 @@ std::unique_ptr TracerProviderFactory::Create( return Create(std::move(processor), resource, std::move(sampler)); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler) @@ -41,25 +38,26 @@ std::unique_ptr TracerProviderFactory::Cre return Create(std::move(processor), resource, std::move(sampler), std::move(id_generator)); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler, std::unique_ptr id_generator) { - std::unique_ptr provider(new trace_sdk::TracerProvider( - std::move(processor), resource, std::move(sampler), std::move(id_generator))); + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(processor), resource, + std::move(sampler), std::move(id_generator))); return provider; } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::vector> &&processors) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processors), resource); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource) { @@ -67,7 +65,7 @@ std::unique_ptr TracerProviderFactory::Cre return Create(std::move(processors), resource, std::move(sampler)); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler) @@ -76,22 +74,23 @@ std::unique_ptr TracerProviderFactory::Cre return Create(std::move(processors), resource, std::move(sampler), std::move(id_generator)); } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource, std::unique_ptr sampler, std::unique_ptr id_generator) { - std::unique_ptr provider(new trace_sdk::TracerProvider( - std::move(processors), resource, std::move(sampler), std::move(id_generator))); + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(processors), resource, + std::move(sampler), std::move(id_generator))); return provider; } -std::unique_ptr TracerProviderFactory::Create( +std::unique_ptr TracerProviderFactory::Create( std::unique_ptr context) { - std::unique_ptr provider( - new trace_sdk::TracerProvider(std::move(context))); + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(context))); return provider; } From c2a2ef459719e2b85f0a7a8d810e9dccb82d9339 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 9 May 2024 15:58:35 +0200 Subject: [PATCH 02/18] Fixes #2506 --- examples/otlp/file_log_main.cc | 46 ++++++++--------- examples/otlp/file_main.cc | 13 +++-- examples/otlp/grpc_log_main.cc | 40 ++++++++------- examples/otlp/grpc_main.cc | 23 ++++----- examples/otlp/http_log_main.cc | 49 ++++++++++--------- examples/otlp/http_main.cc | 14 +++--- .../sdk/logs/event_logger_provider_factory.h | 9 ++-- .../sdk/logs/logger_provider_factory.h | 16 +++--- .../sdk/metrics/meter_provider_factory.h | 45 ++--------------- sdk/src/logs/event_logger_provider_factory.cc | 4 +- sdk/src/logs/logger_provider_factory.cc | 16 +++--- sdk/src/metrics/meter_provider_factory.cc | 20 +++----- 12 files changed, 128 insertions(+), 167 deletions(-) diff --git a/examples/otlp/file_log_main.cc b/examples/otlp/file_log_main.cc index f903449c56..f716887d80 100644 --- a/examples/otlp/file_log_main.cc +++ b/examples/otlp/file_log_main.cc @@ -8,20 +8,17 @@ #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" #include "opentelemetry/logs/provider.h" #include "opentelemetry/sdk/logs/exporter.h" +#include "opentelemetry/sdk/logs/logger_provider.h" #include "opentelemetry/sdk/logs/logger_provider_factory.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel -// running exportings when destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/logs/logger_provider.h" -#include "opentelemetry/sdk/trace/tracer_provider.h" - #include #include #include @@ -43,27 +40,31 @@ namespace { opentelemetry::exporter::otlp::OtlpFileExporterOptions opts; opentelemetry::exporter::otlp::OtlpFileLogRecordExporterOptions log_opts; + +std::shared_ptr tracer_provider; +std::shared_ptr logger_provider; + void InitTracer() { // Create OTLP exporter instance - auto exporter = otlp::OtlpFileExporterFactory::Create(opts); - auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + auto exporter = otlp::OtlpFileExporterFactory::Create(opts); + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = tracer_provider; + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); - if (provider) + if (tracer_provider) { - static_cast(provider.get())->ForceFlush(); + tracer_provider->ForceFlush(); } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } @@ -71,24 +72,23 @@ void CleanupTracer() void InitLogger() { // Create OTLP exporter instance - auto exporter = otlp::OtlpFileLogRecordExporterFactory::Create(log_opts); - auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - nostd::shared_ptr provider( - logs_sdk::LoggerProviderFactory::Create(std::move(processor))); + auto exporter = otlp::OtlpFileLogRecordExporterFactory::Create(log_opts); + auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); + logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor)); - opentelemetry::logs::Provider::SetLoggerProvider(provider); + std::shared_ptr api_provider = logger_provider; + opentelemetry::logs::Provider::SetLoggerProvider(api_provider); } void CleanupLogger() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - logs::Provider::GetLoggerProvider(); - if (provider) + if (logger_provider) { - static_cast(provider.get())->ForceFlush(); + logger_provider->ForceFlush(); } + logger_provider.reset(); nostd::shared_ptr none; opentelemetry::logs::Provider::SetLoggerProvider(none); } diff --git a/examples/otlp/file_main.cc b/examples/otlp/file_main.cc index 675ad47a75..cc911e07c4 100644 --- a/examples/otlp/file_main.cc +++ b/examples/otlp/file_main.cc @@ -29,26 +29,29 @@ namespace { opentelemetry::exporter::otlp::OtlpFileExporterOptions opts; -std::shared_ptr sdk_provider; +std::shared_ptr provider; void InitTracer() { // Create OTLP exporter instance auto exporter = otlp::OtlpFileExporterFactory::Create(opts); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - sdk_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + // Set the global trace provider - trace::Provider::SetTracerProvider(sdk_provider); + std::shared_ptr api_provider = provider; + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - if (sdk_provider) + if (provider) { - sdk_provider->ForceFlush(std::chrono::milliseconds(10000)); + provider->ForceFlush(std::chrono::milliseconds(10000)); } + provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } diff --git a/examples/otlp/grpc_log_main.cc b/examples/otlp/grpc_log_main.cc index 9d7399dbaf..2316289999 100644 --- a/examples/otlp/grpc_log_main.cc +++ b/examples/otlp/grpc_log_main.cc @@ -40,27 +40,31 @@ namespace { opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts; opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts; + +std::shared_ptr tracer_provider; +std::shared_ptr logger_provider; + void InitTracer() { // Create OTLP exporter instance - auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); - auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = tracer_provider; + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); - if (provider) + if (tracer_provider) { - static_cast(provider.get())->ForceFlush(); + tracer_provider->ForceFlush(); } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } @@ -68,24 +72,24 @@ void CleanupTracer() void InitLogger() { // Create OTLP exporter instance - auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts); - auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - nostd::shared_ptr provider( - logs_sdk::LoggerProviderFactory::Create(std::move(processor))); + auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts); + auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); + logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor)); - opentelemetry::logs::Provider::SetLoggerProvider(provider); + // Set the global logger provider + std::shared_ptr api_provider = logger_provider; + opentelemetry::logs::Provider::SetLoggerProvider(api_provider); } void CleanupLogger() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - logs::Provider::GetLoggerProvider(); - if (provider) + if (logger_provider) { - static_cast(provider.get())->ForceFlush(); + logger_provider->ForceFlush(); } + logger_provider.reset(); nostd::shared_ptr none; opentelemetry::logs::Provider::SetLoggerProvider(none); } diff --git a/examples/otlp/grpc_main.cc b/examples/otlp/grpc_main.cc index 37a3a7987f..9b29e16edd 100644 --- a/examples/otlp/grpc_main.cc +++ b/examples/otlp/grpc_main.cc @@ -2,17 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" +#include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" -#include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/sdk/trace/tracer_provider.h" -#include "opentelemetry/trace/tracer_provider.h" +#include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -#include "opentelemetry/nostd/shared_ptr.h" - -// sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when -// destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/tracer_provider.h" #ifdef BAZEL_BUILD # include "examples/common/foo_library/foo_library.h" @@ -28,30 +24,29 @@ namespace { opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts; -std::shared_ptr sdk_provider; +std::shared_ptr provider; void InitTracer() { // Create OTLP exporter instance auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - sdk_provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); - std::shared_ptr api_provider ( sdk_provider ); // Set the global trace provider + std::shared_ptr api_provider = provider; trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - if (sdk_provider) + if (provider) { - sdk_provider->ForceFlush(); - sdk_provider = nullptr; + provider->ForceFlush(); } + provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index 7b68ad44c7..82b9e47a51 100644 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -6,19 +6,16 @@ #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" #include "opentelemetry/logs/provider.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/logs/logger_provider.h" #include "opentelemetry/sdk/logs/logger_provider_factory.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel -// running exportings when destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/logs/logger_provider.h" -#include "opentelemetry/sdk/trace/tracer_provider.h" - #include #include @@ -40,6 +37,9 @@ namespace { opentelemetry::exporter::otlp::OtlpHttpExporterOptions trace_opts; + +std::shared_ptr tracer_provider; + void InitTracer() { if (trace_opts.url.size() > 9) @@ -58,53 +58,56 @@ void InitTracer() } } std::cout << "Using " << trace_opts.url << " to export trace spans." << std::endl; + // Create OTLP exporter instance - auto exporter = otlp::OtlpHttpExporterFactory::Create(trace_opts); - auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + auto exporter = otlp::OtlpHttpExporterFactory::Create(trace_opts); + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = tracer_provider; + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); - if (provider) + if (tracer_provider) { - static_cast(provider.get())->ForceFlush(); + tracer_provider->ForceFlush(); } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts; + +std::shared_ptr logger_provider; + void InitLogger() { std::cout << "Using " << logger_opts.url << " to export log records." << std::endl; logger_opts.console_debug = true; // Create OTLP exporter instance - auto exporter = otlp::OtlpHttpLogRecordExporterFactory::Create(logger_opts); - auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - logs_sdk::LoggerProviderFactory::Create(std::move(processor)); + auto exporter = otlp::OtlpHttpLogRecordExporterFactory::Create(logger_opts); + auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); + logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor)); - opentelemetry::logs::Provider::SetLoggerProvider(provider); + std::shared_ptr api_provider = logger_provider; + opentelemetry::logs::Provider::SetLoggerProvider(api_provider); } void CleanupLogger() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - logs::Provider::GetLoggerProvider(); - if (provider) + if (logger_provider) { - static_cast(provider.get())->ForceFlush(); + logger_provider->ForceFlush(); } + logger_provider.reset(); std::shared_ptr none; opentelemetry::logs::Provider::SetLoggerProvider(none); } diff --git a/examples/otlp/http_main.cc b/examples/otlp/http_main.cc index 6a0667b918..4918bf25de 100644 --- a/examples/otlp/http_main.cc +++ b/examples/otlp/http_main.cc @@ -30,27 +30,29 @@ namespace internal_log = opentelemetry::sdk::common::internal_log; namespace { opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts; + +std::shared_ptr provider; + void InitTracer() { // Create OTLP exporter instance auto exporter = otlp::OtlpHttpExporterFactory::Create(opts); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); // Set the global trace provider - trace::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = provider; + trace::Provider::SetTracerProvider(api_provider); } void CleanupTracer() { // We call ForceFlush to prevent to cancel running exportings, It's optional. - opentelemetry::nostd::shared_ptr provider = - trace::Provider::GetTracerProvider(); if (provider) { - static_cast(provider.get())->ForceFlush(); + provider->ForceFlush(); } + provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } diff --git a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h index 0b8c066fc6..abdec14b0a 100644 --- a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h @@ -4,14 +4,11 @@ #pragma once #include + +#include "opentelemetry/sdk/logs/event_logger_provider.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace logs -{ -class EventLoggerProvider; -} // namespace logs - namespace sdk { namespace logs @@ -26,7 +23,7 @@ class EventLoggerProviderFactory /** * Create a EventLoggerProvider. */ - static std::unique_ptr Create(); + static std::unique_ptr Create(); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index 987a09fe16..10cc3b37bb 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -6,14 +6,10 @@ #include #include +#include "opentelemetry/sdk/logs/logger_provider.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace logs -{ -class LoggerProvider; -} // namespace logs - namespace sdk { namespace resource @@ -35,33 +31,33 @@ class OPENTELEMETRY_EXPORT LoggerProviderFactory /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr &&processor); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr &&processor, const opentelemetry::sdk::resource::Resource &resource); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource); /** * Create a LoggerProvider. */ - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr context); }; diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 69e77ebf5e..d26fb9b438 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -11,6 +11,7 @@ #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/metrics/meter.h" #include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/meter_provider.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" @@ -20,55 +21,19 @@ namespace sdk namespace metrics { -/* - MAINTAINER: - - The best design is to return an API object: - std::unique_ptr - to shield the calling application from SDK implementation details. - - This however assumes that the SDK object can be created in one call, - instead of making multiple calls to the SDK to setup a meter provider. - - Because existing code, already advertised in examples: - - creates an instance of sdk::MeterProvider - - calls SDK methods on it to continue the setup, such as - MeterProvider::AddMetricReader() - MeterProvider::AddView() - existing applications will need to access the underlying - class sdk::MeterProvider. - - We need to decide whether to return: - - (1) std::unique_ptr - - (2) std::unique_ptr - from a Create() method. - - In the long term, (1) is better, but forces users to use a down cast, - to make additional calls to the SDK class, until such a time when - the builders can take all the necessary input at once, - for example using a std::vector to add all readers. - - Implementing (2) is forcing technical debt, and prevents the - calling application configuring the SDK to be decoupled from it, - making deployment of shared libraries much more difficult. - - The design choice here is to return (1) an API MeterProvider, - even if this forces, temporarily, existing applications to use a downcast. -*/ - class OPENTELEMETRY_EXPORT MeterProviderFactory { public: - static std::unique_ptr Create(); + static std::unique_ptr Create(); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr views); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr views, const opentelemetry::sdk::resource::Resource &resource); - static std::unique_ptr Create( + static std::unique_ptr Create( std::unique_ptr context); }; diff --git a/sdk/src/logs/event_logger_provider_factory.cc b/sdk/src/logs/event_logger_provider_factory.cc index 0c9eff4570..5118833345 100644 --- a/sdk/src/logs/event_logger_provider_factory.cc +++ b/sdk/src/logs/event_logger_provider_factory.cc @@ -11,9 +11,9 @@ namespace sdk namespace logs { -std::unique_ptr EventLoggerProviderFactory::Create() +std::unique_ptr EventLoggerProviderFactory::Create() { - return std::unique_ptr(new EventLoggerProvider()); + return std::unique_ptr(new EventLoggerProvider()); } } // namespace logs diff --git a/sdk/src/logs/logger_provider_factory.cc b/sdk/src/logs/logger_provider_factory.cc index 76e662a909..76c0eef29f 100644 --- a/sdk/src/logs/logger_provider_factory.cc +++ b/sdk/src/logs/logger_provider_factory.cc @@ -12,42 +12,42 @@ namespace sdk namespace logs { -std::unique_ptr LoggerProviderFactory::Create( +std::unique_ptr LoggerProviderFactory::Create( std::unique_ptr &&processor) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processor), resource); } -std::unique_ptr LoggerProviderFactory::Create( +std::unique_ptr LoggerProviderFactory::Create( std::unique_ptr &&processor, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr provider( + std::unique_ptr provider( new LoggerProvider(std::move(processor), resource)); return provider; } -std::unique_ptr LoggerProviderFactory::Create( +std::unique_ptr LoggerProviderFactory::Create( std::vector> &&processors) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(processors), resource); } -std::unique_ptr LoggerProviderFactory::Create( +std::unique_ptr LoggerProviderFactory::Create( std::vector> &&processors, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr provider( + std::unique_ptr provider( new LoggerProvider(std::move(processors), resource)); return provider; } -std::unique_ptr LoggerProviderFactory::Create( +std::unique_ptr LoggerProviderFactory::Create( std::unique_ptr context) { - std::unique_ptr provider( + std::unique_ptr provider( new LoggerProvider(std::move(context))); return provider; } diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc index bd6bbe932c..49abe00c37 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -13,43 +13,39 @@ #include -namespace resource = opentelemetry::sdk::resource; -namespace metrics_api = opentelemetry::metrics; -namespace metrics_sdk = opentelemetry::sdk::metrics; - OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { -std::unique_ptr MeterProviderFactory::Create() +std::unique_ptr MeterProviderFactory::Create() { auto views = ViewRegistryFactory::Create(); return Create(std::move(views)); } -std::unique_ptr MeterProviderFactory::Create( +std::unique_ptr MeterProviderFactory::Create( std::unique_ptr views) { auto resource = opentelemetry::sdk::resource::Resource::Create({}); return Create(std::move(views), resource); } -std::unique_ptr MeterProviderFactory::Create( +std::unique_ptr MeterProviderFactory::Create( std::unique_ptr views, const opentelemetry::sdk::resource::Resource &resource) { - std::unique_ptr provider( - new metrics_sdk::MeterProvider(std::move(views), resource)); + std::unique_ptr provider( + new opentelemetry::sdk::metrics::MeterProvider(std::move(views), resource)); return provider; } -std::unique_ptr MeterProviderFactory::Create( +std::unique_ptr MeterProviderFactory::Create( std::unique_ptr context) { - std::unique_ptr provider( - new metrics_sdk::MeterProvider(std::move(context))); + std::unique_ptr provider( + new opentelemetry::sdk::metrics::MeterProvider(std::move(context))); return provider; } From e88cb41248d6378cd968cfa50296a5cb76728c18 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 9 May 2024 16:30:32 +0200 Subject: [PATCH 03/18] DLL build on windows --- sdk/include/opentelemetry/sdk/logs/event_logger_provider.h | 3 ++- sdk/include/opentelemetry/sdk/logs/logger_provider.h | 2 +- sdk/include/opentelemetry/sdk/trace/tracer_provider.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/logs/event_logger_provider.h b/sdk/include/opentelemetry/sdk/logs/event_logger_provider.h index 2f3d2a0afd..4572647356 100644 --- a/sdk/include/opentelemetry/sdk/logs/event_logger_provider.h +++ b/sdk/include/opentelemetry/sdk/logs/event_logger_provider.h @@ -20,7 +20,8 @@ namespace logs class EventLogger; class Logger; -class EventLoggerProvider final : public opentelemetry::logs::EventLoggerProvider +class OPENTELEMETRY_EXPORT EventLoggerProvider final + : public opentelemetry::logs::EventLoggerProvider { public: EventLoggerProvider() noexcept; diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider.h b/sdk/include/opentelemetry/sdk/logs/logger_provider.h index 0ab5683d3e..bced6061f5 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider.h @@ -26,7 +26,7 @@ class Logger; class LoggerContext; class LogRecordProcessor; -class LoggerProvider final : public opentelemetry::logs::LoggerProvider +class OPENTELEMETRY_EXPORT LoggerProvider final : public opentelemetry::logs::LoggerProvider { public: /** diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h index ce2a6d4401..79458518c6 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h @@ -26,7 +26,7 @@ class SpanProcessor; class Tracer; class TracerContext; -class TracerProvider final : public opentelemetry::trace::TracerProvider +class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider { public: /** From 12f5837aee6a84128853c10dd4508ef94cba9384 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 9 May 2024 17:14:22 +0200 Subject: [PATCH 04/18] CHANGELOG --- CHANGELOG.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9381363300..a1e10913aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,52 @@ Increment the: * [SDK] Update ExemplarFilter and ExemplarReservoir for spec [#2372](https://github.com/open-telemetry/opentelemetry-cpp/pull/2372) +* [API/SDK] Provider cleanup + [#2664](https://github.com/open-telemetry/opentelemetry-cpp/pull/2664) + +Important changes: + +* [API/SDK] Provider cleanup + [#2664](https://github.com/open-telemetry/opentelemetry-cpp/pull/2664) + * Before this fix: + * The API class `opentelemetry::trace::Tracer` exposed methods such + as `ForceFlush()`, `ForceFlushWithMicroseconds()`, `Close()` + and `CloseWithMicroseconds()`. + * These methods are meant to be used when configuring the SDK, + and should not be part of the API. Exposing them was an oversight. + * Two of these methods are virtual, and therefore part of the ABI. + * After this fix: + * In `OPENTELEMETRY_ABI_VERSION_NO 1`, nothing is changed, + because removing this code would break the ABI. + * In `OPENTELEMETRY_ABI_VERSION_NO 2`, these methods are moved + from the API to the SDK. This is a breaking change for ABI version 2, + which is still experimental. + * In all cases, instrumenting an application should not + invoke flush or close on a tracer, do not use these methods. + +Breaking changes: + +* [API/SDK] Provider cleanup + [#2664](https://github.com/open-telemetry/opentelemetry-cpp/pull/2664) + * Before this fix: + * SDK factory methods such as: + * opentelemetry::sdk::trace::TracerProviderFactory::Create() + * opentelemetry::sdk::metrics::MeterProviderFactory::Create() + * opentelemetry::sdk::logs::LoggerProviderFactory::Create() + * opentelemetry::sdk::logs::EventLoggerProviderFactory::Create() + returned an API object (opentelemetry::trace::TracerProvider) + to the caller. + * After this fix, these methods return an SDK level object + (opentelemetry::sdk::trace::TracerProvider) to the caller. + * Returning an SDK object is necessary for the application to + cleanup invoke SDK level methods, such as ForceFlush(), + on a provider. + * The application code that configures the SDK, by calling + the various provider factories, may need adjustment. + * All the examples have been updated, and in particular no + longer perform static_cast do convert an API object to an SDK object. + Please refers to examples for guidance on how to adjust. + Notes on experimental features: * [#2372](https://github.com/open-telemetry/opentelemetry-cpp/issues/2372) From 8c26e6068f37ca626afe0936ac15cbc600062204 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 9 May 2024 17:32:33 +0200 Subject: [PATCH 05/18] Windows DDL build --- sdk/src/logs/event_logger_provider.cc | 2 +- sdk/src/logs/logger_provider.cc | 2 +- sdk/src/metrics/meter_provider.cc | 2 +- sdk/src/trace/tracer_provider.cc | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/src/logs/event_logger_provider.cc b/sdk/src/logs/event_logger_provider.cc index 28a88d82e2..acab6a9c63 100644 --- a/sdk/src/logs/event_logger_provider.cc +++ b/sdk/src/logs/event_logger_provider.cc @@ -23,7 +23,7 @@ EventLoggerProvider::EventLoggerProvider() noexcept OTEL_INTERNAL_LOG_DEBUG("[EventLoggerProvider] EventLoggerProvider created."); } -EventLoggerProvider::~EventLoggerProvider() {} +OPENTELEMETRY_EXPORT EventLoggerProvider::~EventLoggerProvider() {} nostd::shared_ptr EventLoggerProvider::CreateEventLogger( nostd::shared_ptr delegate_logger, diff --git a/sdk/src/logs/logger_provider.cc b/sdk/src/logs/logger_provider.cc index 46eaa01054..3eb01d5250 100644 --- a/sdk/src/logs/logger_provider.cc +++ b/sdk/src/logs/logger_provider.cc @@ -39,7 +39,7 @@ LoggerProvider::LoggerProvider(std::unique_ptr context) noexcept : context_(std::move(context)) {} -LoggerProvider::~LoggerProvider() +OPENTELEMETRY_EXPORT LoggerProvider::~LoggerProvider() { // Logger hold the shared pointer to the context. So we can not use destructor of LoggerContext to // Shutdown and flush all pending recordables when we have more than one loggers. These diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 943ae137bd..1b289d1ce1 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -138,7 +138,7 @@ bool MeterProvider::ForceFlush(std::chrono::microseconds timeout) noexcept * Shutdown MeterContext when MeterProvider is destroyed. * */ -MeterProvider::~MeterProvider() +OPENTELEMETRY_EXPORT MeterProvider::~MeterProvider() { if (context_) { diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 8569b63f23..97d09b2e5e 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -42,7 +42,7 @@ TracerProvider::TracerProvider(std::vector> &&pro std::move(id_generator)); } -TracerProvider::~TracerProvider() +OPENTELEMETRY_EXPORT TracerProvider::~TracerProvider() { // Tracer hold the shared pointer to the context. So we can not use destructor of TracerContext to // Shutdown and flush all pending recordables when we have more than one tracers.These recordables From 8ccd546bb21e3c61bcef08739c49d39c923b8446 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 9 May 2024 17:53:27 +0200 Subject: [PATCH 06/18] Typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1e10913aa..6e5bf62d46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,7 +56,7 @@ Breaking changes: * After this fix, these methods return an SDK level object (opentelemetry::sdk::trace::TracerProvider) to the caller. * Returning an SDK object is necessary for the application to - cleanup invoke SDK level methods, such as ForceFlush(), + cleanup and invoke SDK level methods, such as ForceFlush(), on a provider. * The application code that configures the SDK, by calling the various provider factories, may need adjustment. From 75b1b007a3b51f16b184e9e1a2c8b85965d2cb39 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Fri, 10 May 2024 15:52:53 +0200 Subject: [PATCH 07/18] Cleanup --- examples/logs_simple/main.cc | 23 ++++++++++++---------- examples/metrics_simple/metrics_ostream.cc | 19 +++++++++--------- examples/simple/main.cc | 6 ++++-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/examples/logs_simple/main.cc b/examples/logs_simple/main.cc index 54a872ad0e..ea65743422 100644 --- a/examples/logs_simple/main.cc +++ b/examples/logs_simple/main.cc @@ -1,19 +1,20 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/exporters/ostream/log_record_exporter.h" #include "opentelemetry/exporters/ostream/span_exporter_factory.h" +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/sdk/logs/logger_provider_factory.h" +#include "opentelemetry/sdk/logs/processor.h" +#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -#include "opentelemetry/exporters/ostream/log_record_exporter.h" -#include "opentelemetry/logs/provider.h" -#include "opentelemetry/sdk/logs/logger_provider_factory.h" -#include "opentelemetry/sdk/logs/processor.h" -#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" - #ifdef BAZEL_BUILD # include "examples/common/logs_foo_library/foo_library.h" #else @@ -35,11 +36,12 @@ void InitTracer() // Create ostream span exporter instance auto exporter = trace_exporter::OStreamSpanExporterFactory::Create(); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = + std::shared_ptr provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); // Set the global trace provider - trace_api::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = provider; + trace_api::Provider::SetTracerProvider(api_provider); } void CleanupTracer() @@ -54,11 +56,12 @@ void InitLogger() auto exporter = std::unique_ptr(new logs_exporter::OStreamLogRecordExporter); auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider( + std::shared_ptr provider( logs_sdk::LoggerProviderFactory::Create(std::move(processor))); // Set the global logger provider - logs_api::Provider::SetLoggerProvider(provider); + std::shared_ptr api_provider = provider; + logs_api::Provider::SetLoggerProvider(api_provider); } void CleanupLogger() diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 7248bc9148..da5efb9001 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -46,10 +46,9 @@ void InitMetrics(const std::string &name) auto reader = metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); - auto u_provider = metrics_sdk::MeterProviderFactory::Create(); - auto *p = static_cast(u_provider.get()); + auto provider = metrics_sdk::MeterProviderFactory::Create(); - p->AddMetricReader(std::move(reader)); + provider->AddMetricReader(std::move(reader)); // counter view std::string counter_name = name + "_counter"; @@ -63,7 +62,7 @@ void InitMetrics(const std::string &name) auto sum_view = metrics_sdk::ViewFactory::Create(name, "description", unit, metrics_sdk::AggregationType::kSum); - p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); + provider->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); // observable counter view std::string observable_counter_name = name + "_observable_counter"; @@ -76,8 +75,8 @@ void InitMetrics(const std::string &name) auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description", unit, metrics_sdk::AggregationType::kSum); - p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector), - std::move(observable_sum_view)); + provider->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector), + std::move(observable_sum_view)); // histogram view std::string histogram_name = name + "_histogram"; @@ -100,11 +99,11 @@ void InitMetrics(const std::string &name) auto histogram_view = metrics_sdk::ViewFactory::Create( name, "description", unit, metrics_sdk::AggregationType::kHistogram, aggregation_config); - p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), - std::move(histogram_view)); + provider->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), + std::move(histogram_view)); - std::shared_ptr provider(std::move(u_provider)); - metrics_api::Provider::SetMeterProvider(provider); + std::shared_ptr api_provider(std::move(provider)); + metrics_api::Provider::SetMeterProvider(api_provider); } void CleanupMetrics() diff --git a/examples/simple/main.cc b/examples/simple/main.cc index 48768b6448..d343a8d5e9 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -5,6 +5,7 @@ #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" @@ -24,11 +25,12 @@ void InitTracer() { auto exporter = trace_exporter::OStreamSpanExporterFactory::Create(); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = + std::shared_ptr provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); // Set the global trace provider - trace_api::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = provider; + trace_api::Provider::SetTracerProvider(api_provider); } void CleanupTracer() From cbf95f170e593f41028c77fcd090d85d8268ca19 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 13 May 2024 20:43:46 +0200 Subject: [PATCH 08/18] Partial revert --- sdk/src/logs/event_logger_provider.cc | 2 +- sdk/src/logs/logger_provider.cc | 2 +- sdk/src/metrics/meter_provider.cc | 2 +- sdk/src/trace/tracer_provider.cc | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/src/logs/event_logger_provider.cc b/sdk/src/logs/event_logger_provider.cc index acab6a9c63..28a88d82e2 100644 --- a/sdk/src/logs/event_logger_provider.cc +++ b/sdk/src/logs/event_logger_provider.cc @@ -23,7 +23,7 @@ EventLoggerProvider::EventLoggerProvider() noexcept OTEL_INTERNAL_LOG_DEBUG("[EventLoggerProvider] EventLoggerProvider created."); } -OPENTELEMETRY_EXPORT EventLoggerProvider::~EventLoggerProvider() {} +EventLoggerProvider::~EventLoggerProvider() {} nostd::shared_ptr EventLoggerProvider::CreateEventLogger( nostd::shared_ptr delegate_logger, diff --git a/sdk/src/logs/logger_provider.cc b/sdk/src/logs/logger_provider.cc index 3eb01d5250..46eaa01054 100644 --- a/sdk/src/logs/logger_provider.cc +++ b/sdk/src/logs/logger_provider.cc @@ -39,7 +39,7 @@ LoggerProvider::LoggerProvider(std::unique_ptr context) noexcept : context_(std::move(context)) {} -OPENTELEMETRY_EXPORT LoggerProvider::~LoggerProvider() +LoggerProvider::~LoggerProvider() { // Logger hold the shared pointer to the context. So we can not use destructor of LoggerContext to // Shutdown and flush all pending recordables when we have more than one loggers. These diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 1b289d1ce1..943ae137bd 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -138,7 +138,7 @@ bool MeterProvider::ForceFlush(std::chrono::microseconds timeout) noexcept * Shutdown MeterContext when MeterProvider is destroyed. * */ -OPENTELEMETRY_EXPORT MeterProvider::~MeterProvider() +MeterProvider::~MeterProvider() { if (context_) { diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 97d09b2e5e..8569b63f23 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -42,7 +42,7 @@ TracerProvider::TracerProvider(std::vector> &&pro std::move(id_generator)); } -OPENTELEMETRY_EXPORT TracerProvider::~TracerProvider() +TracerProvider::~TracerProvider() { // Tracer hold the shared pointer to the context. So we can not use destructor of TracerContext to // Shutdown and flush all pending recordables when we have more than one tracers.These recordables From 5fcbcade611c8940db89b44876fa71731f616995 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 13 May 2024 21:34:19 +0200 Subject: [PATCH 09/18] Export entire provider class --- ext/src/dll/input.src | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/src/dll/input.src b/ext/src/dll/input.src index 91ed42d303..2b1913387a 100644 --- a/ext/src/dll/input.src +++ b/ext/src/dll/input.src @@ -9,8 +9,8 @@ Create@LoggerProviderFactory@logs@sdk@v1@opentelemetry Create@BatchLogRecordProcessorFactory@logs@sdk@v1@opentelemetry Create@SimpleLogRecordProcessorFactory@logs@sdk@v1@opentelemetry Create@MultiLogRecordProcessorFactory@logs@sdk@v1@opentelemetry -ForceFlush@TracerProvider@trace@sdk@v1@opentelemetry -ForceFlush@LoggerProvider@logs@sdk@v1@opentelemetry +TracerProvider@trace@sdk@v1@opentelemetry +LoggerProvider@logs@sdk@v1@opentelemetry OStreamLogRecordExporter@logs@exporter@v1@opentelemetry Create@OStreamMetricExporterFactory@metrics@exporter@v1@opentelemetry Create@PeriodicExportingMetricReaderFactory@metrics@sdk@v1@opentelemetry @@ -20,7 +20,7 @@ Create@ViewFactory@metrics@sdk@v1@opentelemetry Create@MeterSelectorFactory@metrics@sdk@v1@opentelemetry Create@InstrumentSelectorFactory@metrics@sdk@v1@opentelemetry AddMetricReader@MeterContext@metrics@sdk@v1@opentelemetry -AddMetricReader@MeterProvider@metrics@sdk@v1@opentelemetry +MeterProvider@metrics@sdk@v1@opentelemetry AddView@MeterProvider@metrics@sdk@v1@opentelemetry #if defined(WITH_OTLP_GRPC) || defined(WITH_OTLP_HTTP) @@ -64,4 +64,4 @@ GetOtlpDefaultHttpTracesEndpoint@otlp@exporter@v1@opentelemetry GetOtlpDefaultHttpMetricsEndpoint@otlp@exporter@v1@opentelemetry GetOtlpDefaultHttpLogsEndpoint@otlp@exporter@v1@opentelemetry #endif // defined(WITH_OTLP_HTTP) -// clang-format on \ No newline at end of file +// clang-format on From f201ba16d8e539e1627c23013c657865dd3ac2f0 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 13 May 2024 22:05:24 +0200 Subject: [PATCH 10/18] Tentative fix for dll link --- ext/src/dll/input.src | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/src/dll/input.src b/ext/src/dll/input.src index 2b1913387a..98c4c140fe 100644 --- a/ext/src/dll/input.src +++ b/ext/src/dll/input.src @@ -20,6 +20,7 @@ Create@ViewFactory@metrics@sdk@v1@opentelemetry Create@MeterSelectorFactory@metrics@sdk@v1@opentelemetry Create@InstrumentSelectorFactory@metrics@sdk@v1@opentelemetry AddMetricReader@MeterContext@metrics@sdk@v1@opentelemetry +AddMetricReader@MeterProvider@metrics@sdk@v1@opentelemetry MeterProvider@metrics@sdk@v1@opentelemetry AddView@MeterProvider@metrics@sdk@v1@opentelemetry From c59187503301f820e68789214ca5b6de5c47fc0d Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 27 May 2024 21:57:03 +0200 Subject: [PATCH 11/18] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e5bf62d46..690e251f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,7 @@ Breaking changes: the various provider factories, may need adjustment. * All the examples have been updated, and in particular no longer perform static_cast do convert an API object to an SDK object. - Please refers to examples for guidance on how to adjust. + Please refer to examples for guidance on how to adjust. Notes on experimental features: From 4e73d69c7e8c363a0ac725f1cff418560f7e25d0 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 29 May 2024 23:37:02 +0200 Subject: [PATCH 12/18] Include-what-you-use cleanup. --- .../opentelemetry/sdk/logs/logger_provider_factory.h | 10 +++------- .../opentelemetry/sdk/metrics/meter_context_factory.h | 10 ++++------ sdk/src/logs/event_logger_provider_factory.cc | 2 +- sdk/src/logs/logger_provider_factory.cc | 4 +++- sdk/src/metrics/meter_provider_factory.cc | 5 ++++- sdk/src/trace/tracer_provider_factory.cc | 2 -- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index 10cc3b37bb..490393209c 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -6,21 +6,17 @@ #include #include +#include "opentelemetry/sdk/logs/logger_context.h" #include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/sdk/logs/processor.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { -namespace resource -{ -class Resource; -} // namespace resource - namespace logs { -class LoggerContext; -class LogRecordProcessor; /** * Factory class for LoggerProvider. diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h index 13e3a9f290..1185a6075a 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context_factory.h @@ -3,21 +3,19 @@ #pragma once +#include + +#include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/view/view_registry.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" -#include - OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { -// forward declaration -class MeterContext; -class ViewRegistry; - /** * Factory class for MeterContext. */ diff --git a/sdk/src/logs/event_logger_provider_factory.cc b/sdk/src/logs/event_logger_provider_factory.cc index 5118833345..daff8a8590 100644 --- a/sdk/src/logs/event_logger_provider_factory.cc +++ b/sdk/src/logs/event_logger_provider_factory.cc @@ -3,7 +3,7 @@ #include "opentelemetry/sdk/logs/event_logger_provider_factory.h" #include "opentelemetry/sdk/logs/event_logger_provider.h" -#include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/src/logs/logger_provider_factory.cc b/sdk/src/logs/logger_provider_factory.cc index 76c0eef29f..2aacbacf16 100644 --- a/sdk/src/logs/logger_provider_factory.cc +++ b/sdk/src/logs/logger_provider_factory.cc @@ -1,9 +1,11 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/sdk/logs/logger_provider_factory.h" +#include + #include "opentelemetry/sdk/logs/logger_context.h" #include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/sdk/logs/logger_provider_factory.h" #include "opentelemetry/sdk/resource/resource.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc index 074b4b4493..c5e368aca6 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -1,12 +1,15 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #include -#include "opentelemetry/metrics/meter.h" +#include "opentelemetry/sdk/metrics/meter_context.h" #include "opentelemetry/sdk/metrics/meter_provider.h" #include "opentelemetry/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/sdk/metrics/view/view_registry.h" #include "opentelemetry/sdk/metrics/view/view_registry_factory.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/sdk/src/trace/tracer_provider_factory.cc b/sdk/src/trace/tracer_provider_factory.cc index 09b5bc8ccb..eaeb669b91 100644 --- a/sdk/src/trace/tracer_provider_factory.cc +++ b/sdk/src/trace/tracer_provider_factory.cc @@ -14,8 +14,6 @@ #include "opentelemetry/sdk/trace/tracer_context.h" #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" -#include "opentelemetry/trace/span_id.h" -#include "opentelemetry/trace/tracer_provider.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE From 6f76fcc30b428d9f68ad5f85a2e0972d5c5c27ca Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 01:02:44 +0200 Subject: [PATCH 13/18] Implement option WITH_DEPRECATED_SDK_FACTORY --- CMakeLists.txt | 2 + api/CMakeLists.txt | 5 ++ examples/logs_simple/main.cc | 21 ++++- examples/metrics_simple/metrics_ostream.cc | 7 +- examples/otlp/grpc_log_main.cc | 20 +++-- examples/otlp/grpc_main.cc | 8 ++ examples/otlp/http_log_main.cc | 16 ++++ examples/otlp/http_main.cc | 13 ++- examples/simple/main.cc | 8 +- .../otlp_grpc_log_record_exporter_test.cc | 7 ++ .../sdk/logs/event_logger_provider_factory.h | 11 +++ .../sdk/logs/logger_provider_factory.h | 34 ++++++++ .../sdk/metrics/meter_provider_factory.h | 28 ++++++ .../sdk/trace/tracer_provider_factory.h | 65 ++++++++++++++ sdk/src/logs/event_logger_provider_factory.cc | 11 +++ sdk/src/logs/logger_provider_factory.cc | 46 ++++++++++ sdk/src/metrics/meter_provider_factory.cc | 36 ++++++++ sdk/src/trace/tracer_provider_factory.cc | 86 +++++++++++++++++++ 18 files changed, 409 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc085b1af4..dd86689b1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,8 @@ message(STATUS "OPENTELEMETRY_VERSION=${OPENTELEMETRY_VERSION}") option(WITH_NO_DEPRECATED_CODE "Do not include deprecated code" OFF) +option(WITH_DEPRECATED_SDK_FACTORY "Use deprecated SDK provider factory" OFF) + set(WITH_STL "OFF" CACHE STRING "Which version of the Standard Library for C++ to use") diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 78e97ad029..e07275efed 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -35,6 +35,11 @@ if(WITH_NO_DEPRECATED_CODE) INTERFACE OPENTELEMETRY_NO_DEPRECATED_CODE) endif() +if(WITH_DEPRECATED_SDK_FACTORY) + target_compile_definitions(opentelemetry_api + INTERFACE OPENTELEMETRY_DEPRECATED_SDK_FACTORY) +endif() + if(WITH_ABSEIL) target_compile_definitions(opentelemetry_api INTERFACE HAVE_ABSEIL) target_link_libraries( diff --git a/examples/logs_simple/main.cc b/examples/logs_simple/main.cc index ea65743422..05528b9697 100644 --- a/examples/logs_simple/main.cc +++ b/examples/logs_simple/main.cc @@ -36,8 +36,15 @@ void InitTracer() // Create ostream span exporter instance auto exporter = trace_exporter::OStreamSpanExporterFactory::Create(); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::shared_ptr provider = + opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); +#else + std::shared_ptr provider = + opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + // Set the global trace provider std::shared_ptr api_provider = provider; @@ -56,8 +63,14 @@ void InitLogger() auto exporter = std::unique_ptr(new logs_exporter::OStreamLogRecordExporter); auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); - std::shared_ptr provider( - logs_sdk::LoggerProviderFactory::Create(std::move(processor))); + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::shared_ptr provider( + opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor))); +#else + std::shared_ptr provider( + opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor))); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ // Set the global logger provider std::shared_ptr api_provider = provider; diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index da5efb9001..f609c2c677 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -46,7 +46,12 @@ void InitMetrics(const std::string &name) auto reader = metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options); - auto provider = metrics_sdk::MeterProviderFactory::Create(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + auto u_provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(); + auto *provider = static_cast(u_provider.get()); +#else + auto provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ provider->AddMetricReader(std::move(reader)); diff --git a/examples/otlp/grpc_log_main.cc b/examples/otlp/grpc_log_main.cc index 2316289999..24037f6190 100644 --- a/examples/otlp/grpc_log_main.cc +++ b/examples/otlp/grpc_log_main.cc @@ -7,20 +7,17 @@ #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h" #include "opentelemetry/logs/provider.h" #include "opentelemetry/sdk/logs/exporter.h" +#include "opentelemetry/sdk/logs/logger_provider.h" #include "opentelemetry/sdk/logs/logger_provider_factory.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -// sdk::TracerProvider and sdk::LoggerProvider is just used to call ForceFlush and prevent to cancel -// running exportings when destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/logs/logger_provider.h" -#include "opentelemetry/sdk/trace/tracer_provider.h" - #include #ifdef BAZEL_BUILD @@ -41,8 +38,13 @@ namespace opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts; opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr tracer_provider; +std::shared_ptr logger_provider; +#else std::shared_ptr tracer_provider; std::shared_ptr logger_provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -61,7 +63,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (tracer_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } tracer_provider.reset(); @@ -86,7 +92,11 @@ void CleanupLogger() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (logger_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } logger_provider.reset(); diff --git a/examples/otlp/grpc_main.cc b/examples/otlp/grpc_main.cc index 9b29e16edd..15499985af 100644 --- a/examples/otlp/grpc_main.cc +++ b/examples/otlp/grpc_main.cc @@ -24,7 +24,11 @@ namespace { opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr provider; +#else std::shared_ptr provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -43,7 +47,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } provider.reset(); diff --git a/examples/otlp/http_log_main.cc b/examples/otlp/http_log_main.cc index 82b9e47a51..e2b219de1c 100644 --- a/examples/otlp/http_log_main.cc +++ b/examples/otlp/http_log_main.cc @@ -38,7 +38,11 @@ namespace opentelemetry::exporter::otlp::OtlpHttpExporterOptions trace_opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr tracer_provider; +#else std::shared_ptr tracer_provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -74,7 +78,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (tracer_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } tracer_provider.reset(); @@ -84,7 +92,11 @@ void CleanupTracer() opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr logger_provider; +#else std::shared_ptr logger_provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitLogger() { @@ -104,7 +116,11 @@ void CleanupLogger() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (logger_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } logger_provider.reset(); diff --git a/examples/otlp/http_main.cc b/examples/otlp/http_main.cc index 4918bf25de..9ad476bd47 100644 --- a/examples/otlp/http_main.cc +++ b/examples/otlp/http_main.cc @@ -6,13 +6,10 @@ #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -// sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when -// destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/trace/tracer_provider.h" - #include #ifdef BAZEL_BUILD @@ -31,7 +28,11 @@ namespace { opentelemetry::exporter::otlp::OtlpHttpExporterOptions opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr provider; +#else std::shared_ptr provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -49,7 +50,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } provider.reset(); diff --git a/examples/simple/main.cc b/examples/simple/main.cc index d343a8d5e9..284ec2f693 100644 --- a/examples/simple/main.cc +++ b/examples/simple/main.cc @@ -25,8 +25,14 @@ void InitTracer() { auto exporter = trace_exporter::OStreamSpanExporterFactory::Create(); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::shared_ptr provider = + opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); +#else std::shared_ptr provider = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ // Set the global trace provider std::shared_ptr api_provider = provider; 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 90827f576f..4c7a9fd7f1 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -288,10 +288,17 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ExportIntegrationTest) std::unique_ptr trace_stub_interface( trace_mock_stub); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + auto trace_provider = opentelemetry::nostd::shared_ptr( + opentelemetry::sdk::trace::TracerProviderFactory::Create( + opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create( + GetExporter(trace_stub_interface)))); +#else auto trace_provider = opentelemetry::nostd::shared_ptr( opentelemetry::sdk::trace::TracerProviderFactory::Create( opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create( GetExporter(trace_stub_interface)))); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ // Trace and Logs should both receive datas when links static gRPC on ELF ABI. EXPECT_CALL(*trace_mock_stub, Export(_, _, _)) diff --git a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h index abdec14b0a..e75d1c1742 100644 --- a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h @@ -23,7 +23,18 @@ class EventLoggerProviderFactory /** * Create a EventLoggerProvider. */ + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create(); +#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ + +#else static std::unique_ptr Create(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index 490393209c..5ccf5096b2 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -24,6 +24,37 @@ namespace logs class OPENTELEMETRY_EXPORT LoggerProviderFactory { public: + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr &&processor); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr &&processor, + const opentelemetry::sdk::resource::Resource &resource); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr context); + +#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ + +#else + /** * Create a LoggerProvider. */ @@ -55,6 +86,9 @@ class OPENTELEMETRY_EXPORT LoggerProviderFactory */ static std::unique_ptr Create( std::unique_ptr context); + +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 499c87ae6a..6ad8af4915 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -21,6 +21,31 @@ namespace metrics class OPENTELEMETRY_EXPORT MeterProviderFactory { public: + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create(); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr views); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr context); + +#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ + +#else + static std::unique_ptr Create(); static std::unique_ptr Create( @@ -32,6 +57,9 @@ class OPENTELEMETRY_EXPORT MeterProviderFactory static std::unique_ptr Create( std::unique_ptr context); + +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h index ed451a8c99..6e99fbe144 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h @@ -28,6 +28,69 @@ namespace trace class OPENTELEMETRY_EXPORT TracerProviderFactory { public: + +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE + + /* Serie of builders with a single processor. */ + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr processor); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator); + + /* Serie of builders with a vector of processor. */ + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler); + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator); + + /* Create with a tracer context. */ + + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr context); + +#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ + +#else + /* Serie of builders with a single processor. */ static std::unique_ptr Create( @@ -72,6 +135,8 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory static std::unique_ptr Create( std::unique_ptr context); + +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ }; } // namespace trace diff --git a/sdk/src/logs/event_logger_provider_factory.cc b/sdk/src/logs/event_logger_provider_factory.cc index daff8a8590..7f5c8cadf1 100644 --- a/sdk/src/logs/event_logger_provider_factory.cc +++ b/sdk/src/logs/event_logger_provider_factory.cc @@ -11,11 +11,22 @@ namespace sdk namespace logs { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +std::unique_ptr EventLoggerProviderFactory::Create() +{ + return std::unique_ptr(new EventLoggerProvider()); +} + +#else + std::unique_ptr EventLoggerProviderFactory::Create() { return std::unique_ptr(new EventLoggerProvider()); } +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + } // namespace logs } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/logs/logger_provider_factory.cc b/sdk/src/logs/logger_provider_factory.cc index 2aacbacf16..8c169866ff 100644 --- a/sdk/src/logs/logger_provider_factory.cc +++ b/sdk/src/logs/logger_provider_factory.cc @@ -14,6 +14,50 @@ namespace sdk namespace logs { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +std::unique_ptr LoggerProviderFactory::Create( + std::unique_ptr &&processor) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(processor), resource); +} + +std::unique_ptr LoggerProviderFactory::Create( + std::unique_ptr &&processor, + const opentelemetry::sdk::resource::Resource &resource) +{ + std::unique_ptr provider( + new LoggerProvider(std::move(processor), resource)); + return provider; +} + +std::unique_ptr LoggerProviderFactory::Create( + std::vector> &&processors) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(processors), resource); +} + +std::unique_ptr LoggerProviderFactory::Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource) +{ + std::unique_ptr provider( + new LoggerProvider(std::move(processors), resource)); + return provider; +} + +std::unique_ptr LoggerProviderFactory::Create( + std::unique_ptr context) +{ + std::unique_ptr provider( + new LoggerProvider(std::move(context))); + return provider; +} + +#else + std::unique_ptr LoggerProviderFactory::Create( std::unique_ptr &&processor) { @@ -54,6 +98,8 @@ std::unique_ptr LoggerProviderFactory: return provider; } +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + } // namespace logs } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/meter_provider_factory.cc b/sdk/src/metrics/meter_provider_factory.cc index c5e368aca6..7749a13124 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -18,6 +18,40 @@ namespace sdk namespace metrics { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +std::unique_ptr MeterProviderFactory::Create() +{ + auto views = ViewRegistryFactory::Create(); + return Create(std::move(views)); +} + +std::unique_ptr MeterProviderFactory::Create( + std::unique_ptr views) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(views), resource); +} + +std::unique_ptr MeterProviderFactory::Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource) +{ + std::unique_ptr provider( + new opentelemetry::sdk::metrics::MeterProvider(std::move(views), resource)); + return provider; +} + +std::unique_ptr MeterProviderFactory::Create( + std::unique_ptr context) +{ + std::unique_ptr provider( + new opentelemetry::sdk::metrics::MeterProvider(std::move(context))); + return provider; +} + +#else + std::unique_ptr MeterProviderFactory::Create() { auto views = ViewRegistryFactory::Create(); @@ -48,6 +82,8 @@ std::unique_ptr MeterProviderFactory return provider; } +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/trace/tracer_provider_factory.cc b/sdk/src/trace/tracer_provider_factory.cc index eaeb669b91..eeb18a1e59 100644 --- a/sdk/src/trace/tracer_provider_factory.cc +++ b/sdk/src/trace/tracer_provider_factory.cc @@ -22,6 +22,90 @@ namespace sdk namespace trace { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + +std::unique_ptr TracerProviderFactory::Create( + std::unique_ptr processor) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(processor), resource); +} + +std::unique_ptr TracerProviderFactory::Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource) +{ + auto sampler = AlwaysOnSamplerFactory::Create(); + return Create(std::move(processor), resource, std::move(sampler)); +} + +std::unique_ptr TracerProviderFactory::Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler) +{ + auto id_generator = RandomIdGeneratorFactory::Create(); + return Create(std::move(processor), resource, std::move(sampler), std::move(id_generator)); +} + +std::unique_ptr TracerProviderFactory::Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator) +{ + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(processor), resource, + std::move(sampler), std::move(id_generator))); + return provider; +} + +std::unique_ptr TracerProviderFactory::Create( + std::vector> &&processors) +{ + auto resource = opentelemetry::sdk::resource::Resource::Create({}); + return Create(std::move(processors), resource); +} + +std::unique_ptr TracerProviderFactory::Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource) +{ + auto sampler = AlwaysOnSamplerFactory::Create(); + return Create(std::move(processors), resource, std::move(sampler)); +} + +std::unique_ptr TracerProviderFactory::Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler) +{ + auto id_generator = RandomIdGeneratorFactory::Create(); + return Create(std::move(processors), resource, std::move(sampler), std::move(id_generator)); +} + +std::unique_ptr TracerProviderFactory::Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler, + std::unique_ptr id_generator) +{ + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(processors), resource, + std::move(sampler), std::move(id_generator))); + return provider; +} + +std::unique_ptr TracerProviderFactory::Create( + std::unique_ptr context) +{ + std::unique_ptr provider( + new opentelemetry::sdk::trace::TracerProvider(std::move(context))); + return provider; +} + +#else + std::unique_ptr TracerProviderFactory::Create( std::unique_ptr processor) { @@ -102,6 +186,8 @@ std::unique_ptr TracerProviderFactory return provider; } +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + } // namespace trace } // namespace sdk OPENTELEMETRY_END_NAMESPACE From 4875b5062a77266ebaf1c6c5558dc714a2254cc0 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 01:56:09 +0200 Subject: [PATCH 14/18] Updated DEPRECATED and CHANGELOG --- CHANGELOG.md | 8 ++ DEPRECATED.md | 80 ++++++++++++++++++- examples/logs_simple/main.cc | 1 - examples/metrics_simple/metrics_ostream.cc | 2 +- .../sdk/logs/event_logger_provider_factory.h | 5 +- .../sdk/logs/logger_provider_factory.h | 6 +- .../sdk/metrics/meter_provider_factory.h | 6 +- .../sdk/trace/tracer_provider_factory.h | 5 +- 8 files changed, 96 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8ac6002c..cd92d15ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,14 @@ Breaking changes: * All the examples have been updated, and in particular no longer perform static_cast do convert an API object to an SDK object. Please refer to examples for guidance on how to adjust. + * If adjusting application code is impractical, + an alternate and temporary solution is to build with option + WITH_DEPRECATED_SDK_FACTORY=ON in CMake. + * Option WITH_DEPRECATED_SDK_FACTORY=ON will allow to build code + without application changes, posponing changes for later. + * WITH_DEPRECATED_SDK_FACTORY=ON is temporary, only to provide + an easier migration path. Expect this flag to be removed, + as early as by the next release. Notes on experimental features: diff --git a/DEPRECATED.md b/DEPRECATED.md index 0632f50503..7db5ba9d93 100644 --- a/DEPRECATED.md +++ b/DEPRECATED.md @@ -88,7 +88,85 @@ No date set yet for the Jaeger Propagator. ## [opentelemetry-cpp SDK] -N/A +### SDK ProviderFactory cleanup + +#### Announcement (SDK ProviderFactory cleanup) + +* Version: 1.15.0 +* Date: 2024-06-03 +* PR: [API/SDK] Provider cleanup + [#2664](https://github.com/open-telemetry/opentelemetry-cpp/pull/2664) + +This PR introduces changes to SDK ProviderFactory methods. + +#### Motivation (SDK ProviderFactory cleanup) + +SDK Factory methods for signal providers, such as: + +* opentelemetry::sdk::trace::TracerProviderFactory +* opentelemetry::sdk::metrics::MeterProviderFactory +* opentelemetry::sdk::logs::LoggerProviderFactory +* opentelemetry::sdk::logs::EventLoggerProviderFactory + +currently returns a unique pointer on a API class. + +This is incorrect, the proper return type should be +a unique pointer on a SDK class instead. + +#### Scope (SDK ProviderFactory cleanup) + +All the current Create methods in: + +* class opentelemetry::sdk::trace::TracerProviderFactory +* class opentelemetry::sdk::metrics::MeterProviderFactory +* class opentelemetry::sdk::logs::LoggerProviderFactory +* class opentelemetry::sdk::logs::EventLoggerProviderFactory + +are marked as deprecated, as they return an API object. + +Instead, another set of Create methods is provided, +with a different return type, an SDK object. + +Both sets can not be exposed at the same time, +as this would cause build breaks, +so a compilation flag is defined to select which methods to use. + +When OPENTELEMETRY_DEPRECATED_SDK_FACTORY is defined, +the old, deprecated, methods are available. + +When OPENTELEMETRY_DEPRECATED_SDK_FACTORY is not defined, +the new methods are available. + +The scope of this deprecation and removal, +is to remove the flag OPENTELEMETRY_DEPRECATED_SDK_FACTORY itself, +which implies that only the new set of Create() methods, +returning an SDK object, are supported. + +#### Mitigation (SDK ProviderFactory cleanup) + +Build without defining flag OPENTELEMETRY_DEPRECATED_SDK_FACTORY. + +Existing code, such as: + +```cpp + std::shared_ptr tracer_provider; + tracer_provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(...); +``` + +should be adjusted to: + +```cpp + std::shared_ptr tracer_provider; + tracer_provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(...); +``` + +#### Planned removal (SDK ProviderFactory cleanup) + +Flag OPENTELEMETRY_DEPRECATED_SDK_FACTORY is introduced in release 1.16.0, +to provide a migration path. + +This flag is meant to be temporary, and short lived. +Expect removal by release 1.17.0 ## [opentelemetry-cpp Exporter] diff --git a/examples/logs_simple/main.cc b/examples/logs_simple/main.cc index 05528b9697..e104724e2a 100644 --- a/examples/logs_simple/main.cc +++ b/examples/logs_simple/main.cc @@ -45,7 +45,6 @@ void InitTracer() opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); #endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ - // Set the global trace provider std::shared_ptr api_provider = provider; trace_api::Provider::SetTracerProvider(api_provider); diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index f609c2c677..a5acf556b6 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -48,7 +48,7 @@ void InitMetrics(const std::string &name) #ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY auto u_provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(); - auto *provider = static_cast(u_provider.get()); + auto *provider = static_cast(u_provider.get()); #else auto provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(); #endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ diff --git a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h index e75d1c1742..621cbea6c8 100644 --- a/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h @@ -26,15 +26,14 @@ class EventLoggerProviderFactory #ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY -#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE +# ifndef OPENTELEMETRY_NO_DEPRECATED_CODE OPENTELEMETRY_DEPRECATED static std::unique_ptr Create(); -#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ +# endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ #else static std::unique_ptr Create(); #endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ - }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index 5ccf5096b2..7b8f636e68 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -24,10 +24,9 @@ namespace logs class OPENTELEMETRY_EXPORT LoggerProviderFactory { public: - #ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY -#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE +# ifndef OPENTELEMETRY_NO_DEPRECATED_CODE OPENTELEMETRY_DEPRECATED static std::unique_ptr Create( @@ -51,7 +50,7 @@ class OPENTELEMETRY_EXPORT LoggerProviderFactory static std::unique_ptr Create( std::unique_ptr context); -#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ +# endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ #else @@ -88,7 +87,6 @@ class OPENTELEMETRY_EXPORT LoggerProviderFactory std::unique_ptr context); #endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ - }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 6ad8af4915..cb9106ddc8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -21,10 +21,9 @@ namespace metrics class OPENTELEMETRY_EXPORT MeterProviderFactory { public: - #ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY -#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE +# ifndef OPENTELEMETRY_NO_DEPRECATED_CODE OPENTELEMETRY_DEPRECATED static std::unique_ptr Create(); @@ -42,7 +41,7 @@ class OPENTELEMETRY_EXPORT MeterProviderFactory static std::unique_ptr Create( std::unique_ptr context); -#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ +# endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ #else @@ -59,7 +58,6 @@ class OPENTELEMETRY_EXPORT MeterProviderFactory std::unique_ptr context); #endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ - }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h index 6e99fbe144..7c4b6903de 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h @@ -28,10 +28,9 @@ namespace trace class OPENTELEMETRY_EXPORT TracerProviderFactory { public: - #ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY -#ifndef OPENTELEMETRY_NO_DEPRECATED_CODE +# ifndef OPENTELEMETRY_NO_DEPRECATED_CODE /* Serie of builders with a single processor. */ @@ -87,7 +86,7 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory static std::unique_ptr Create( std::unique_ptr context); -#endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ +# endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ #else From 75adfa0c4aea4c2ec059bb3f9d7d0d3f9d4da480 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 02:31:35 +0200 Subject: [PATCH 15/18] Cleanup --- examples/metrics_simple/metrics_ostream.cc | 5 +++++ examples/otlp/file_log_main.cc | 13 +++++++++++++ examples/otlp/file_main.cc | 15 ++++++++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index a5acf556b6..770b2db81a 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -107,7 +107,12 @@ void InitMetrics(const std::string &name) provider->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::shared_ptr api_provider(std::move(u_provider)); +#else std::shared_ptr api_provider(std::move(provider)); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + metrics_api::Provider::SetMeterProvider(api_provider); } diff --git a/examples/otlp/file_log_main.cc b/examples/otlp/file_log_main.cc index f716887d80..9254c9947f 100644 --- a/examples/otlp/file_log_main.cc +++ b/examples/otlp/file_log_main.cc @@ -41,8 +41,13 @@ namespace opentelemetry::exporter::otlp::OtlpFileExporterOptions opts; opentelemetry::exporter::otlp::OtlpFileLogRecordExporterOptions log_opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr tracer_provider; +std::shared_ptr logger_provider; +#else std::shared_ptr tracer_provider; std::shared_ptr logger_provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -61,7 +66,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (tracer_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } tracer_provider.reset(); @@ -85,7 +94,11 @@ void CleanupLogger() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (logger_provider) { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } logger_provider.reset(); diff --git a/examples/otlp/file_main.cc b/examples/otlp/file_main.cc index cc911e07c4..13db0e4ad1 100644 --- a/examples/otlp/file_main.cc +++ b/examples/otlp/file_main.cc @@ -4,13 +4,10 @@ #include "opentelemetry/exporters/otlp/otlp_file_exporter_factory.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/sdk/trace/tracer_provider_factory.h" #include "opentelemetry/trace/provider.h" -// sdk::TracerProvider is just used to call ForceFlush and prevent to cancel running exportings when -// destroy and shutdown exporters.It's optional to users. -#include "opentelemetry/sdk/trace/tracer_provider.h" - #include #include #include @@ -29,7 +26,11 @@ namespace { opentelemetry::exporter::otlp::OtlpFileExporterOptions opts; +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY +std::shared_ptr provider; +#else std::shared_ptr provider; +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ void InitTracer() { @@ -48,7 +49,11 @@ void CleanupTracer() // We call ForceFlush to prevent to cancel running exportings, It's optional. if (provider) { - provider->ForceFlush(std::chrono::milliseconds(10000)); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else + provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } provider.reset(); From c605b7368c6423f02a7ec81a4d18a650e7e8327b Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 02:44:11 +0200 Subject: [PATCH 16/18] WITH_DEPRECATED_SDK_FACTORY=ON by default --- CMakeLists.txt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd86689b1f..d701b0e294 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,7 +154,13 @@ message(STATUS "OPENTELEMETRY_VERSION=${OPENTELEMETRY_VERSION}") option(WITH_NO_DEPRECATED_CODE "Do not include deprecated code" OFF) -option(WITH_DEPRECATED_SDK_FACTORY "Use deprecated SDK provider factory" OFF) +# This option is temporary, and will be removed. +# Set WITH_DEPRECATED_SDK_FACTORY=OFF to migrate to the new SDK code. +option(WITH_DEPRECATED_SDK_FACTORY "Use deprecated SDK provider factory" ON) + +if(WITH_DEPRECATED_SDK_FACTORY) + message(WARNING "WITH_DEPRECATED_SDK_FACTORY=ON is temporary and deprecated") +endif() set(WITH_STL "OFF" From c41eb3de6e621f7468098b3b949b9c532e35fa5e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 03:01:48 +0200 Subject: [PATCH 17/18] Cleanup --- CMakeLists.txt | 4 ++-- ci/do_ci.sh | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d701b0e294..d01d7054ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,8 +154,8 @@ message(STATUS "OPENTELEMETRY_VERSION=${OPENTELEMETRY_VERSION}") option(WITH_NO_DEPRECATED_CODE "Do not include deprecated code" OFF) -# This option is temporary, and will be removed. -# Set WITH_DEPRECATED_SDK_FACTORY=OFF to migrate to the new SDK code. +# This option is temporary, and will be removed. Set +# WITH_DEPRECATED_SDK_FACTORY=OFF to migrate to the new SDK code. option(WITH_DEPRECATED_SDK_FACTORY "Use deprecated SDK provider factory" ON) if(WITH_DEPRECATED_SDK_FACTORY) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index b7c97a3800..2d089ef21e 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -130,6 +130,7 @@ elif [[ "$1" == "cmake.maintainer.sync.test" ]]; then -DWITH_ASYNC_EXPORT_PREVIEW=OFF \ -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ + -DWITH_DEPRECATED_SDK_FACTORY=OFF \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ ${IWYU} \ "${SRC_DIR}" @@ -152,6 +153,7 @@ elif [[ "$1" == "cmake.maintainer.async.test" ]]; then -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ + -DWITH_DEPRECATED_SDK_FACTORY=OFF \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ ${IWYU} \ "${SRC_DIR}" @@ -175,6 +177,7 @@ elif [[ "$1" == "cmake.maintainer.cpp11.async.test" ]]; then -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ + -DWITH_DEPRECATED_SDK_FACTORY=OFF \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ "${SRC_DIR}" make -k -j $(nproc) @@ -196,6 +199,7 @@ elif [[ "$1" == "cmake.maintainer.abiv2.test" ]]; then -DWITH_ASYNC_EXPORT_PREVIEW=OFF \ -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ + -DWITH_DEPRECATED_SDK_FACTORY=OFF \ -DWITH_ABI_VERSION_1=OFF \ -DWITH_ABI_VERSION_2=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ From 79595f57e9881f68bb549bd0d20cb08aa538634e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 08:57:18 +0200 Subject: [PATCH 18/18] Build cleanup --- ci/do_ci.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index bdab94ae04..f6a12e3877 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -133,6 +133,7 @@ switch ($action) { cmake $SRC_DIR ` -DOTELCPP_MAINTAINER_MODE=ON ` -DWITH_NO_DEPRECATED_CODE=ON ` + -DWITH_DEPRECATED_SDK_FACTORY=OFF ` -DVCPKG_TARGET_TRIPLET=x64-windows ` "-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake" $exit = $LASTEXITCODE @@ -157,6 +158,7 @@ switch ($action) { -DCMAKE_CXX_STANDARD=20 ` -DOTELCPP_MAINTAINER_MODE=ON ` -DWITH_NO_DEPRECATED_CODE=ON ` + -DWITH_DEPRECATED_SDK_FACTORY=OFF ` -DVCPKG_TARGET_TRIPLET=x64-windows ` "-DCMAKE_TOOLCHAIN_FILE=$VCPKG_DIR/scripts/buildsystems/vcpkg.cmake" $exit = $LASTEXITCODE