From 2535c70c4e4c1a92cb535413283b7c022c65554e Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 3 Jun 2024 22:46:08 +0200 Subject: [PATCH 1/5] [API/SDK] Provider cleanup (#2664) --- CHANGELOG.md | 54 +++++++++ CMakeLists.txt | 8 ++ DEPRECATED.md | 80 ++++++++++++- api/CMakeLists.txt | 5 + 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 + ci/do_ci.ps1 | 2 + ci/do_ci.sh | 4 + examples/logs_simple/main.cc | 39 ++++-- examples/metrics_simple/metrics_ostream.cc | 29 +++-- examples/otlp/file_log_main.cc | 59 ++++++---- examples/otlp/file_main.cc | 28 +++-- examples/otlp/grpc_log_main.cc | 60 ++++++---- examples/otlp/grpc_main.cc | 30 +++-- examples/otlp/http_log_main.cc | 65 ++++++---- examples/otlp/http_main.cc | 27 +++-- examples/plugin/plugin/tracer.h | 4 + examples/simple/main.cc | 12 +- .../otlp/test/otlp_file_exporter_test.cc | 4 +- .../otlp_grpc_log_record_exporter_test.cc | 9 +- .../otlp/test/otlp_http_exporter_test.cc | 16 +-- ext/src/dll/input.src | 7 +- functional/otlp/func_http_main.cc | 2 - .../sdk/logs/event_logger_provider.h | 3 +- .../sdk/logs/event_logger_provider_factory.h | 17 ++- .../opentelemetry/sdk/logs/logger_provider.h | 2 +- .../sdk/logs/logger_provider_factory.h | 58 ++++++--- .../sdk/metrics/meter_context_factory.h | 10 +- .../sdk/metrics/meter_provider_factory.h | 61 ++++------ sdk/include/opentelemetry/sdk/trace/tracer.h | 29 +++++ .../opentelemetry/sdk/trace/tracer_provider.h | 2 +- .../sdk/trace/tracer_provider_factory.h | 65 ++++++++++ sdk/src/logs/event_logger_provider_factory.cc | 13 +- sdk/src/logs/logger_provider_factory.cc | 50 +++++++- sdk/src/metrics/meter_provider_factory.cc | 48 +++++++- sdk/src/trace/tracer_provider_factory.cc | 111 +++++++++++++++--- 38 files changed, 806 insertions(+), 228 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27c379349f..cd92d15ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,60 @@ Increment the: * [CI] Upgrade to clang-format 18 [#2684](https://github.com/open-telemetry/opentelemetry-cpp/pull/2684) +* [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 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. + * 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: * [#2372](https://github.com/open-telemetry/opentelemetry-cpp/issues/2372) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc085b1af4..d01d7054ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,14 @@ 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. +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" CACHE STRING "Which version of the Standard Library for C++ to use") 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/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/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 7fc758511f..c10fff6c60 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/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 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 \ diff --git a/examples/logs_simple/main.cc b/examples/logs_simple/main.cc index 54a872ad0e..e104724e2a 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,18 @@ 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 - trace_api::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = provider; + trace_api::Provider::SetTracerProvider(api_provider); } void CleanupTracer() @@ -54,11 +62,18 @@ 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 - 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..770b2db81a 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -46,10 +46,14 @@ 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()); +#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 */ - p->AddMetricReader(std::move(reader)); + provider->AddMetricReader(std::move(reader)); // counter view std::string counter_name = name + "_counter"; @@ -63,7 +67,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 +80,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 +104,16 @@ 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)); + +#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 */ - std::shared_ptr provider(std::move(u_provider)); - metrics_api::Provider::SetMeterProvider(provider); + metrics_api::Provider::SetMeterProvider(api_provider); } void CleanupMetrics() diff --git a/examples/otlp/file_log_main.cc b/examples/otlp/file_log_main.cc index f903449c56..9254c9947f 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,40 @@ 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() { // 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else + tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } @@ -71,24 +81,27 @@ 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else + logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + 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 e0a1264fb6..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 @@ -28,27 +25,38 @@ namespace otlp = opentelemetry::exporter::otlp; 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() { // 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)); + 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else + provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + 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..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 @@ -40,27 +37,40 @@ 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() { // 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else + tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } @@ -68,24 +78,28 @@ 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else + logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + 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 c238d9ad2a..15499985af 100644 --- a/examples/otlp/grpc_main.cc +++ b/examples/otlp/grpc_main.cc @@ -2,14 +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.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 "opentelemetry/trace/tracer_provider.h" #ifdef BAZEL_BUILD # include "examples/common/foo_library/foo_library.h" @@ -24,27 +23,38 @@ namespace otlp = opentelemetry::exporter::otlp; 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() { // 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)); + 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else + provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + 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..e2b219de1c 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,13 @@ 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() { if (trace_opts.url.size() > 9) @@ -58,53 +62,68 @@ 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(tracer_provider.get())->ForceFlush(); +#else + tracer_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + tracer_provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(none); } 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() { 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(logger_provider.get())->ForceFlush(); +#else + logger_provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + 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..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 @@ -30,27 +27,37 @@ namespace internal_log = opentelemetry::sdk::common::internal_log; 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() { // 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(); +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + static_cast(provider.get())->ForceFlush(); +#else + provider->ForceFlush(); +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ } + provider.reset(); std::shared_ptr none; trace::Provider::SetTracerProvider(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/examples/simple/main.cc b/examples/simple/main.cc index 48768b6448..284ec2f693 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,18 @@ 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 = - trace_sdk::TracerProviderFactory::Create(std::move(processor)); + 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 - trace_api::Provider::SetTracerProvider(provider); + std::shared_ptr api_provider = provider; + trace_api::Provider::SetTracerProvider(api_provider); } void CleanupTracer() 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..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(_, _, _)) @@ -331,7 +338,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/ext/src/dll/input.src b/ext/src/dll/input.src index 91ed42d303..98c4c140fe 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 @@ -21,6 +21,7 @@ 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 +65,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 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/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/event_logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/event_logger_provider_factory.h index 0b8c066fc6..621cbea6c8 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,17 @@ 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.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/logs/logger_provider_factory.h b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h index 987a09fe16..7b8f636e68 100644 --- a/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/logger_provider_factory.h @@ -6,25 +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 logs -{ -class LoggerProvider; -} // namespace logs - namespace sdk { -namespace resource -{ -class Resource; -} // namespace resource - namespace logs { -class LoggerContext; -class LogRecordProcessor; /** * Factory class for LoggerProvider. @@ -32,37 +24,69 @@ class LogRecordProcessor; 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. */ - 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); + +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ }; } // namespace logs 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/include/opentelemetry/sdk/metrics/meter_provider_factory.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h index 15aa13badc..cb9106ddc8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider_factory.h @@ -7,6 +7,7 @@ #include "opentelemetry/metrics/meter_provider.h" #include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/meter_provider.h" #include "opentelemetry/sdk/metrics/view/view_registry.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" @@ -17,56 +18,46 @@ namespace sdk namespace metrics { -/* - MAINTAINER: +class OPENTELEMETRY_EXPORT MeterProviderFactory +{ +public: +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY - The best design is to return an API object: - std::unique_ptr - to shield the calling application from SDK implementation details. +# ifndef OPENTELEMETRY_NO_DEPRECATED_CODE - 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. + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create(); - 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. + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr views); - We need to decide whether to return: - - (1) std::unique_ptr - - (2) std::unique_ptr - from a Create() method. + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr views, + const opentelemetry::sdk::resource::Resource &resource); - 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. + OPENTELEMETRY_DEPRECATED + static std::unique_ptr Create( + std::unique_ptr context); - 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. +# endif /* OPENTELEMETRY_NO_DEPRECATED_CODE */ - The design choice here is to return (1) an API MeterProvider, - even if this forces, temporarily, existing applications to use a downcast. -*/ +#else -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); + +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/trace/tracer.h b/sdk/include/opentelemetry/sdk/trace/tracer.h index 778ffe8173..fb8342c1d5 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer.h @@ -45,9 +45,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.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h index e0f3ce0c4c..0d05a94043 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider.h @@ -28,7 +28,7 @@ namespace sdk namespace trace { -class TracerProvider final : public opentelemetry::trace::TracerProvider +class OPENTELEMETRY_EXPORT TracerProvider final : public opentelemetry::trace::TracerProvider { public: /** diff --git a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h index 17f7ea395a..7c4b6903de 100644 --- a/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/tracer_provider_factory.h @@ -11,6 +11,7 @@ #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/sampler.h" #include "opentelemetry/sdk/trace/tracer_context.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/tracer_provider.h" #include "opentelemetry/version.h" @@ -27,20 +28,28 @@ 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, @@ -49,18 +58,22 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory /* 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, @@ -69,8 +82,60 @@ class OPENTELEMETRY_EXPORT TracerProviderFactory /* 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( + std::unique_ptr processor); + + static std::unique_ptr Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource); + + static std::unique_ptr Create( + std::unique_ptr processor, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler); + + 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. */ + + static std::unique_ptr Create( + std::vector> &&processors); + + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource); + + static std::unique_ptr Create( + std::vector> &&processors, + const opentelemetry::sdk::resource::Resource &resource, + std::unique_ptr sampler); + + 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. */ + + 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 0c9eff4570..7f5c8cadf1 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 @@ -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 76e662a909..8c169866ff 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 @@ -12,6 +14,8 @@ namespace sdk namespace logs { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::unique_ptr LoggerProviderFactory::Create( std::unique_ptr &&processor) { @@ -52,6 +56,50 @@ std::unique_ptr LoggerProviderFactory::Crea return provider; } +#else + +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; +} + +#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 43c060e5f9..7749a13124 100644 --- a/sdk/src/metrics/meter_provider_factory.cc +++ b/sdk/src/metrics/meter_provider_factory.cc @@ -1,23 +1,25 @@ // 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" -namespace resource = opentelemetry::sdk::resource; -namespace metrics_sdk = opentelemetry::sdk::metrics; - OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { +#ifdef OPENTELEMETRY_DEPRECATED_SDK_FACTORY + std::unique_ptr MeterProviderFactory::Create() { auto views = ViewRegistryFactory::Create(); @@ -36,7 +38,7 @@ std::unique_ptr MeterProviderFactory::Cre const opentelemetry::sdk::resource::Resource &resource) { std::unique_ptr provider( - new metrics_sdk::MeterProvider(std::move(views), resource)); + new opentelemetry::sdk::metrics::MeterProvider(std::move(views), resource)); return provider; } @@ -44,10 +46,44 @@ std::unique_ptr MeterProviderFactory::Cre std::unique_ptr context) { std::unique_ptr provider( - new metrics_sdk::MeterProvider(std::move(context))); + new opentelemetry::sdk::metrics::MeterProvider(std::move(context))); + return provider; +} + +#else + +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; +} + +#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 d22330b866..eeb18a1e59 100644 --- a/sdk/src/trace/tracer_provider_factory.cc +++ b/sdk/src/trace/tracer_provider_factory.cc @@ -14,27 +14,24 @@ #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" -namespace trace_api = opentelemetry::trace; -namespace trace_sdk = opentelemetry::sdk::trace; - OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace trace { -std::unique_ptr TracerProviderFactory::Create( +#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 TracerProviderFactory::Create( std::unique_ptr processor, const opentelemetry::sdk::resource::Resource &resource) { @@ -57,8 +54,9 @@ std::unique_ptr TracerProviderFactory::Cre 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; } @@ -92,19 +90,104 @@ std::unique_ptr TracerProviderFactory::Cre 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 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) +{ + 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 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; } +#endif /* OPENTELEMETRY_DEPRECATED_SDK_FACTORY */ + } // namespace trace } // namespace sdk OPENTELEMETRY_END_NAMESPACE From cb9cd1d1d13a5c6b6a99287dcb8b0d7bb62f277e Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Wed, 5 Jun 2024 10:52:19 -0700 Subject: [PATCH 2/5] [ETW] Add table name mapping for Logs other than the default Log table (#2691) --- .../opentelemetry/exporters/etw/etw_config.h | 8 +++- .../opentelemetry/exporters/etw/etw_logger.h | 29 ++++++++--- exporters/etw/test/etw_logger_test.cc | 48 +++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_config.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_config.h index dae8f5b075..d343589699 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_config.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_config.h @@ -23,8 +23,9 @@ namespace etw /** * @brief TelemetryProvider Options passed via SDK API. */ -using TelemetryProviderOptions = - std::map>; +using TelemetryProviderOptions = std::map< + std::string, + nostd::variant>>; /** * @brief TelemetryProvider runtime configuration class. Internal representation @@ -41,6 +42,9 @@ typedef struct bool enableAutoParent; // Start new spans as children of current active span, Not used for Logs ETWProvider::EventFormat encoding; // Event encoding to use for this provider (TLD, MsgPack, XML, etc.). + bool enableTableNameMappings; // Map instrumentation scope name to table name with + // `tableNameMappings` + std::map tableNameMappings; } TelemetryProviderConfiguration; /** diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h index 494211ea0b..c464d572d5 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_logger.h @@ -145,6 +145,8 @@ class Logger : public opentelemetry::logs::Logger */ std::string provId; + std::string eventName_; + /** * @brief Encoding (Manifest, MessagePack or XML) */ @@ -179,10 +181,12 @@ class Logger : public opentelemetry::logs::Logger * @param encoding ETW encoding format to use. */ Logger(etw::LoggerProvider &parent, + nostd::string_view eventName, nostd::string_view providerId = "", ETWProvider::EventFormat encoding = ETWProvider::EventFormat::ETW_MANIFEST) : opentelemetry::logs::Logger(), loggerProvider_(parent), + eventName_(eventName), provId(providerId.data(), providerId.size()), encoding(encoding), provHandle(initProvHandle()) @@ -271,7 +275,7 @@ class Logger : public opentelemetry::logs::Logger #endif // defined(ENABLE_ENV_PROPERTIES) // Populate Etw.EventName attribute at envelope level - evt[ETW_FIELD_NAME] = ETW_VALUE_LOG; + evt[ETW_FIELD_NAME] = eventName_.data(); #ifdef HAVE_FIELD_TIME { @@ -347,6 +351,8 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider GetOption(options, "enableTraceId", config_.enableTraceId, true); GetOption(options, "enableSpanId", config_.enableSpanId, true); GetOption(options, "enableActivityId", config_.enableActivityId, false); + GetOption(options, "enableTableNameMappings", config_.enableTableNameMappings, false); + GetOption(options, "tableNameMappings", config_.tableNameMappings, {}); // Determines what encoding to use for ETW events: TraceLogging Dynamic, MsgPack, XML, etc. config_.encoding = GetEncoding(options); @@ -359,19 +365,30 @@ class LoggerProvider : public opentelemetry::logs::LoggerProvider nostd::shared_ptr GetLogger( opentelemetry::nostd::string_view logger_name, - opentelemetry::nostd::string_view library_name, - opentelemetry::nostd::string_view version = "", - opentelemetry::nostd::string_view schema_url = "", + opentelemetry::nostd::string_view library_name = "", + opentelemetry::nostd::string_view version = "", + opentelemetry::nostd::string_view schema_url = "", const opentelemetry::common::KeyValueIterable &attributes = opentelemetry::common::NoopKeyValueIterable()) override { - UNREFERENCED_PARAMETER(library_name); UNREFERENCED_PARAMETER(version); UNREFERENCED_PARAMETER(schema_url); UNREFERENCED_PARAMETER(attributes); + + std::string event_name{ETW_VALUE_LOG}; + if (config_.enableTableNameMappings) + { + auto it = + config_.tableNameMappings.find(std::string(library_name.data(), library_name.size())); + if (it != config_.tableNameMappings.end()) + { + event_name = it->second; + } + } + ETWProvider::EventFormat evtFmt = config_.encoding; return nostd::shared_ptr{ - new (std::nothrow) etw::Logger(*this, logger_name, evtFmt)}; + new (std::nothrow) etw::Logger(*this, event_name, logger_name, evtFmt)}; } }; diff --git a/exporters/etw/test/etw_logger_test.cc b/exporters/etw/test/etw_logger_test.cc index f27d47880b..c64769d6ee 100644 --- a/exporters/etw/test/etw_logger_test.cc +++ b/exporters/etw/test/etw_logger_test.cc @@ -14,6 +14,7 @@ using namespace OPENTELEMETRY_NAMESPACE; using namespace opentelemetry::exporter::etw; +// The ETW provider ID is {4533CB59-77E2-54E9-E340-F0F0549058B7} const char *kGlobalProviderName = "OpenTelemetry-ETW-TLD"; /** @@ -98,4 +99,51 @@ TEST(ETWLogger, LoggerCheckWithAttributes) opentelemetry::common::MakeAttributes(attribs))); } +/** + * @brief Logger Test with structured attributes + * + * Example Event for below test: + * { + * "Timestamp": "2024-06-02T15:04:15.4227815-07:00", + * "ProviderName": "OpenTelemetry-ETW-TLD", + * "Id": 1, + * "Message": null, + * "ProcessId": 37696, + * "Level": "Always", + * "Keywords": "0x0000000000000000", + * "EventName": "table1", + * "ActivityID": null, + * "RelatedActivityID": null, + * "Payload": { + * "SpanId": "0000000000000000", + * "Timestamp": "2021-09-30T22:04:15.066411500Z", + * "TraceId": "00000000000000000000000000000000", + * "_name": "table1", + * "attrib1": 1, + * "attrib2": "value2", + * "body": "This is a debug log body", + * "severityNumber": 5, + * "severityText": "DEBUG" + * } + * } + * + */ + +TEST(ETWLogger, LoggerCheckWithTableNameMappings) +{ + std::string providerName = kGlobalProviderName; // supply unique instrumentation name here + std::map tableNameMappings = {{"name1", "table1"}, {"name2", "table2"}}; + exporter::etw::TelemetryProviderOptions options = {{"enableTableNameMappings", true}, + {"tableNameMappings", tableNameMappings}}; + exporter::etw::LoggerProvider lp{options}; + + auto logger = lp.GetLogger(providerName, "name1"); + + // Log attributes + Properties attribs = {{"attrib1", 1}, {"attrib2", "value2"}}; + + EXPECT_NO_THROW( + logger->Debug("This is a debug log body", opentelemetry::common::MakeAttributes(attribs))); +} + #endif // _WIN32 From bf8fa53ac13e513eb50845e5e22fcbce94587094 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Thu, 6 Jun 2024 08:47:45 -0700 Subject: [PATCH 3/5] [BUILD] Remove benchmark overlay for vcpkg (#2695) --- ci/ports/benchmark/portfile.cmake | 36 ----------------------------- ci/ports/benchmark/vcpkg.json | 19 --------------- ci/setup_windows_ci_environment.ps1 | 4 ++-- 3 files changed, 2 insertions(+), 57 deletions(-) delete mode 100644 ci/ports/benchmark/portfile.cmake delete mode 100644 ci/ports/benchmark/vcpkg.json diff --git a/ci/ports/benchmark/portfile.cmake b/ci/ports/benchmark/portfile.cmake deleted file mode 100644 index efb5a2fce7..0000000000 --- a/ci/ports/benchmark/portfile.cmake +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright The OpenTelemetry Authors -# SPDX-License-Identifier: Apache-2.0 - -if(VCPKG_CMAKE_SYSTEM_NAME STREQUAL "WindowsStore") - message(FATAL_ERROR "${PORT} does not currently support UWP") -endif() - -# Make sure vs2019 compiled binaries are compat with vs2017 -vcpkg_check_linkage(ONLY_STATIC_LIBRARY) - -vcpkg_from_github( - OUT_SOURCE_PATH SOURCE_PATH - REPO google/benchmark - REF v1.7.1 - SHA512 396af1c1d3eaa2b78c6d23b1472f6088db85a294056ae1c2366dc5c0becdc8f141ba8fc3a235033324ab0a41c2298f5d242ef09b9b6f69d9877de6bcb2062efd - HEAD_REF master -) - -vcpkg_cmake_configure( - SOURCE_PATH ${SOURCE_PATH} - OPTIONS - -DBENCHMARK_ENABLE_TESTING=OFF -) - -vcpkg_cmake_install() -vcpkg_copy_pdbs() - -vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/benchmark) - -vcpkg_fixup_pkgconfig() - -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share") - -# Handle copyright -file(INSTALL "${SOURCE_PATH}/LICENSE" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright) diff --git a/ci/ports/benchmark/vcpkg.json b/ci/ports/benchmark/vcpkg.json deleted file mode 100644 index 605f5af9f0..0000000000 --- a/ci/ports/benchmark/vcpkg.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "$comment": "https://github.com/google/benchmark/issues/661 describes the missing UWP support upstream", - "name": "benchmark", - "version-semver": "1.7.1", - "description": "A library to support the benchmarking of functions, similar to unit-tests.", - "homepage": "https://github.com/google/benchmark", - "license": "Apache-2.0", - "supports": "!uwp", - "dependencies": [ - { - "name": "vcpkg-cmake", - "host": true - }, - { - "name": "vcpkg-cmake-config", - "host": true - } - ] - } \ No newline at end of file diff --git a/ci/setup_windows_ci_environment.ps1 b/ci/setup_windows_ci_environment.ps1 index 6d003bd0ef..7ad57c3464 100755 --- a/ci/setup_windows_ci_environment.ps1 +++ b/ci/setup_windows_ci_environment.ps1 @@ -10,8 +10,8 @@ $VCPKG_DIR = (Get-Item -Path ".\").FullName ./bootstrap-vcpkg.bat ./vcpkg integrate install -# Patched Google Benchmark can be shared between vs2017 and vs2019 compilers -./vcpkg "--vcpkg-root=$VCPKG_DIR" install --overlay-ports="$PSScriptRoot\ports" benchmark:x64-windows +# Google Benchmark +./vcpkg "--vcpkg-root=$VCPKG_DIR" install benchmark:x64-windows # Google Test ./vcpkg "--vcpkg-root=$VCPKG_DIR" install gtest:x64-windows From 1d8a7b5b256cfa0010681ab9be1d918a85133c0a Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Thu, 6 Jun 2024 09:24:23 -0700 Subject: [PATCH 4/5] [BUILD] Remove the incorrect set of CMAKE_MSVC_RUNTIME_LIBRARY for vcpkg (#2696) --- CMakeLists.txt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d01d7054ef..0304beec50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -343,12 +343,6 @@ if(MSVC) # __cplusplus flag is not supported by Visual Studio 2015 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus") endif() - # When using vcpkg, all targets build with the same runtime - if(VCPKG_TOOLCHAIN) - set(CMAKE_MSVC_RUNTIME_LIBRARY - "MultiThreaded$<$:Debug>$<$:DLL>" - CACHE STRING "") - endif() endif() # include GNUInstallDirs before include cmake/opentelemetry-proto.cmake because From 436a981d81c1094c66b7e647b5380f23c099efed Mon Sep 17 00:00:00 2001 From: Thomas Braun Date: Thu, 6 Jun 2024 19:34:47 +0200 Subject: [PATCH 5/5] CMakeLists.txt: Enable CMAKE_MSVC_RUNTIME_LIBRARY support (#2652) The documentation for CMAKE_MSVC_RUNTIME_LIBRARY states [1]: > This variable has effect only when policy CMP0091 is set to NEW prior to > the first project() or enable_language() command that enables a language > using a compiler targeting the MSVC ABI. so the current usage of CMAKE_MSVC_RUNTIME_LIBRARY for vcpkg does not work at all. Let's fix that by setting policy 91 to new if present. [1]: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html Co-authored-by: Tom Tan --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0304beec50..2e61faed35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,11 @@ if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12") cmake_policy(SET CMP0074 NEW) endif() +# Prefer CMAKE_MSVC_RUNTIME_LIBRARY if possible +if(POLICY CMP0091) + cmake_policy(SET CMP0091 NEW) +endif() + project(opentelemetry-cpp) # Mark variables as used so cmake doesn't complain about them