From fa7c2cc625731afed2d6f345c6052afac7715b01 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 28 Jul 2022 10:37:04 +0200 Subject: [PATCH 01/15] Proof of concept. Fix header only API for singletons (#1520) --- api/include/opentelemetry/baggage/baggage.h | 11 +++--- api/include/opentelemetry/common/macros.h | 14 ++++++++ .../context/propagation/global_propagator.h | 34 +++++++++---------- .../opentelemetry/context/runtime_context.h | 14 ++++---- api/include/opentelemetry/logs/provider.h | 26 +++++++------- api/include/opentelemetry/metrics/provider.h | 28 +++++++-------- api/include/opentelemetry/trace/provider.h | 26 +++++++------- api/include/opentelemetry/trace/trace_state.h | 12 ++++--- 8 files changed, 88 insertions(+), 77 deletions(-) diff --git a/api/include/opentelemetry/baggage/baggage.h b/api/include/opentelemetry/baggage/baggage.h index eb5e4dcc7c..d8f8cc2374 100644 --- a/api/include/opentelemetry/baggage/baggage.h +++ b/api/include/opentelemetry/baggage/baggage.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/common/kv_properties.h" +#include "opentelemetry/common/macros.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/version.h" @@ -34,11 +35,7 @@ class Baggage : kv_properties_(new opentelemetry::common::KeyValueProperties(keys_and_values)) {} - static nostd::shared_ptr GetDefault() - { - static nostd::shared_ptr baggage{new Baggage()}; - return baggage; - } + static nostd::shared_ptr GetDefault() { return default_baggage; } /* Get value for key in the baggage @returns true if key is found, false otherwise @@ -292,8 +289,12 @@ class Baggage private: // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; + + static nostd::shared_ptr default_baggage; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr Baggage::default_baggage(new Baggage()); + } // namespace baggage OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/common/macros.h b/api/include/opentelemetry/common/macros.h index 204c1ed04e..89735d8d7b 100644 --- a/api/include/opentelemetry/common/macros.h +++ b/api/include/opentelemetry/common/macros.h @@ -89,3 +89,17 @@ #else # define OPENTELEMETRY_DEPRECATED_MESSAGE(msg) #endif + +/** + @def OPENTELEMETRY_EXPORT + Declare a weak symbol with visibility default. +*/ +#if defined(__clang__) +# define OPENTELEMETRY_EXPORT __attribute__((visibility("default"), weak)) +#elif defined(__GNUC__) +# define OPENTELEMETRY_EXPORT __attribute__((visibility("default"), weak)) +#elif defined(_MSC_VER) +# define OPENTELEMETRY_EXPORT __declspec(selectany) +#else +# define OPENTELEMETRY_EXPORT +#endif diff --git a/api/include/opentelemetry/context/propagation/global_propagator.h b/api/include/opentelemetry/context/propagation/global_propagator.h index b4e49325cd..964c881e73 100644 --- a/api/include/opentelemetry/context/propagation/global_propagator.h +++ b/api/include/opentelemetry/context/propagation/global_propagator.h @@ -8,6 +8,7 @@ #include "opentelemetry/context/propagation/noop_propagator.h" #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/common/macros.h" #include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/shared_ptr.h" @@ -19,37 +20,36 @@ namespace context namespace propagation { -/* Stores the singleton TextMapPropagator */ - +/** + * Stores the singleton TextMapPropagator. + */ class GlobalTextMapPropagator { public: static nostd::shared_ptr GetGlobalPropagator() noexcept { - std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetPropagator()); + std::lock_guard guard(lock); + nostd::shared_ptr result(propagator); + return result; } static void SetGlobalPropagator(nostd::shared_ptr prop) noexcept { - std::lock_guard guard(GetLock()); - GetPropagator() = prop; + std::lock_guard guard(lock); + propagator = prop; } private: - static nostd::shared_ptr &GetPropagator() noexcept - { - static nostd::shared_ptr propagator(new NoOpPropagator()); - return propagator; - } + static nostd::shared_ptr propagator; - static common::SpinLockMutex &GetLock() noexcept - { - static common::SpinLockMutex lock; - return lock; - } + static common::SpinLockMutex lock; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr GlobalTextMapPropagator::propagator( + new NoOpPropagator()); + +OPENTELEMETRY_EXPORT common::SpinLockMutex GlobalTextMapPropagator::lock; + } // namespace propagation } // namespace context -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 167a928f10..c1c08b8bc1 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -3,6 +3,7 @@ #pragma once +#include "opentelemetry/common/macros.h" #include "opentelemetry/context/context.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -145,7 +146,7 @@ class RuntimeContext */ static void SetRuntimeContextStorage(nostd::shared_ptr storage) noexcept { - GetStorage() = storage; + context_storage = storage; } /** @@ -163,16 +164,15 @@ class RuntimeContext private: static nostd::shared_ptr GetRuntimeContextStorage() noexcept { - return GetStorage(); + return context_storage; } - static nostd::shared_ptr &GetStorage() noexcept - { - static nostd::shared_ptr context(GetDefaultStorage()); - return context; - } + static nostd::shared_ptr context_storage; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr RuntimeContext::context_storage( + GetDefaultStorage()); + inline Token::~Token() noexcept { context::RuntimeContext::Detach(*this); diff --git a/api/include/opentelemetry/logs/provider.h b/api/include/opentelemetry/logs/provider.h index 38186bbe75..8855d3e334 100644 --- a/api/include/opentelemetry/logs/provider.h +++ b/api/include/opentelemetry/logs/provider.h @@ -6,6 +6,7 @@ # include +# include "opentelemetry/common/macros.h" # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/logs/logger_provider.h" # include "opentelemetry/logs/noop.h" @@ -28,8 +29,9 @@ class Provider */ static nostd::shared_ptr GetLoggerProvider() noexcept { - std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetProvider()); + std::lock_guard guard(lock); + nostd::shared_ptr result(provider); + return result; } /** @@ -37,24 +39,20 @@ class Provider */ static void SetLoggerProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(GetLock()); - GetProvider() = tp; + std::lock_guard guard(lock); + provider = tp; } private: - static nostd::shared_ptr &GetProvider() noexcept - { - static nostd::shared_ptr provider(new NoopLoggerProvider); - return provider; - } + static nostd::shared_ptr provider; - static common::SpinLockMutex &GetLock() noexcept - { - static common::SpinLockMutex lock; - return lock; - } + static common::SpinLockMutex lock; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopLoggerProvider); + +OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; + } // namespace logs OPENTELEMETRY_END_NAMESPACE #endif diff --git a/api/include/opentelemetry/metrics/provider.h b/api/include/opentelemetry/metrics/provider.h index 3bacac9760..5c6f2e9b54 100644 --- a/api/include/opentelemetry/metrics/provider.h +++ b/api/include/opentelemetry/metrics/provider.h @@ -6,6 +6,7 @@ # include +# include "opentelemetry/common/macros.h" # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/metrics/meter_provider.h" # include "opentelemetry/metrics/noop.h" @@ -28,8 +29,9 @@ class Provider */ static nostd::shared_ptr GetMeterProvider() noexcept { - std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetProvider()); + std::lock_guard guard(lock); + nostd::shared_ptr result(provider); + return result; } /** @@ -37,24 +39,20 @@ class Provider */ static void SetMeterProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(GetLock()); - GetProvider() = tp; + std::lock_guard guard(lock); + provider = tp; } private: - static nostd::shared_ptr &GetProvider() noexcept - { - static nostd::shared_ptr provider(new NoopMeterProvider); - return provider; - } + static nostd::shared_ptr provider; - static common::SpinLockMutex &GetLock() noexcept - { - static common::SpinLockMutex lock; - return lock; - } + static common::SpinLockMutex lock; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopMeterProvider); + +OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; + } // namespace metrics OPENTELEMETRY_END_NAMESPACE -#endif \ No newline at end of file +#endif diff --git a/api/include/opentelemetry/trace/provider.h b/api/include/opentelemetry/trace/provider.h index 6267cbd4d2..430ef3e417 100644 --- a/api/include/opentelemetry/trace/provider.h +++ b/api/include/opentelemetry/trace/provider.h @@ -5,6 +5,7 @@ #include +#include "opentelemetry/common/macros.h" #include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/noop.h" @@ -27,8 +28,9 @@ class Provider */ static nostd::shared_ptr GetTracerProvider() noexcept { - std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetProvider()); + std::lock_guard guard(lock); + nostd::shared_ptr result(provider); + return result; } /** @@ -36,23 +38,19 @@ class Provider */ static void SetTracerProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(GetLock()); - GetProvider() = tp; + std::lock_guard guard(lock); + provider = tp; } private: - static nostd::shared_ptr &GetProvider() noexcept - { - static nostd::shared_ptr provider(new NoopTracerProvider); - return provider; - } + static nostd::shared_ptr provider; - static common::SpinLockMutex &GetLock() noexcept - { - static common::SpinLockMutex lock; - return lock; - } + static common::SpinLockMutex lock; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopTracerProvider); + +OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; + } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/trace_state.h b/api/include/opentelemetry/trace/trace_state.h index 0343637cfa..96fbf132ac 100644 --- a/api/include/opentelemetry/trace/trace_state.h +++ b/api/include/opentelemetry/trace/trace_state.h @@ -15,6 +15,7 @@ #endif #include "opentelemetry/common/kv_properties.h" +#include "opentelemetry/common/macros.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" @@ -41,11 +42,7 @@ class TraceState static constexpr auto kKeyValueSeparator = '='; static constexpr auto kMembersSeparator = ','; - static nostd::shared_ptr GetDefault() - { - static nostd::shared_ptr ts{new TraceState()}; - return ts; - } + static nostd::shared_ptr GetDefault() { return default_ts; } /** * Returns shared_ptr to a newly created TraceState parsed from the header provided. @@ -315,6 +312,11 @@ class TraceState private: // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; + + static nostd::shared_ptr default_ts; }; + +OPENTELEMETRY_EXPORT nostd::shared_ptr TraceState::default_ts(new TraceState()); + } // namespace trace OPENTELEMETRY_END_NAMESPACE From 63259f666dd16d3304ba7d031b4e230f11b80b77 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 28 Jul 2022 18:09:51 +0200 Subject: [PATCH 02/15] Fix header only API for singletons (#1520) Fixed asan build --- api/include/opentelemetry/_metrics/provider.h | 26 +++++++++---------- api/include/opentelemetry/baggage/baggage.h | 2 +- .../context/propagation/global_propagator.h | 4 +-- .../opentelemetry/context/runtime_context.h | 2 +- api/include/opentelemetry/logs/provider.h | 4 +-- api/include/opentelemetry/metrics/provider.h | 4 +-- api/include/opentelemetry/trace/provider.h | 4 +-- api/include/opentelemetry/trace/trace_state.h | 2 +- 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/api/include/opentelemetry/_metrics/provider.h b/api/include/opentelemetry/_metrics/provider.h index e079b34d5f..4cf1a392fb 100644 --- a/api/include/opentelemetry/_metrics/provider.h +++ b/api/include/opentelemetry/_metrics/provider.h @@ -7,6 +7,7 @@ # include "opentelemetry/_metrics/meter_provider.h" # include "opentelemetry/_metrics/noop.h" +# include "opentelemetry/common/macros.h" # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/nostd/shared_ptr.h" @@ -27,8 +28,9 @@ class Provider */ static nostd::shared_ptr GetMeterProvider() noexcept { - std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetProvider()); + std::lock_guard guard(lock); + nostd::shared_ptr result(provider); + return result; } /** @@ -36,24 +38,20 @@ class Provider */ static void SetMeterProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(GetLock()); - GetProvider() = tp; + std::lock_guard guard(lock); + provider = tp; } private: - static nostd::shared_ptr &GetProvider() noexcept - { - static nostd::shared_ptr provider(new NoopMeterProvider); - return provider; - } + OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; - static common::SpinLockMutex &GetLock() noexcept - { - static common::SpinLockMutex lock; - return lock; - } + OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; }; +OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopMeterProvider); + +OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; + } // namespace metrics OPENTELEMETRY_END_NAMESPACE #endif diff --git a/api/include/opentelemetry/baggage/baggage.h b/api/include/opentelemetry/baggage/baggage.h index d8f8cc2374..5548f544e1 100644 --- a/api/include/opentelemetry/baggage/baggage.h +++ b/api/include/opentelemetry/baggage/baggage.h @@ -290,7 +290,7 @@ class Baggage // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - static nostd::shared_ptr default_baggage; + OPENTELEMETRY_EXPORT static nostd::shared_ptr default_baggage; }; OPENTELEMETRY_EXPORT nostd::shared_ptr Baggage::default_baggage(new Baggage()); diff --git a/api/include/opentelemetry/context/propagation/global_propagator.h b/api/include/opentelemetry/context/propagation/global_propagator.h index 964c881e73..946d45ef2c 100644 --- a/api/include/opentelemetry/context/propagation/global_propagator.h +++ b/api/include/opentelemetry/context/propagation/global_propagator.h @@ -40,9 +40,9 @@ class GlobalTextMapPropagator } private: - static nostd::shared_ptr propagator; + OPENTELEMETRY_EXPORT static nostd::shared_ptr propagator; - static common::SpinLockMutex lock; + OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; }; OPENTELEMETRY_EXPORT nostd::shared_ptr GlobalTextMapPropagator::propagator( diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index c1c08b8bc1..44d69c48bf 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -167,7 +167,7 @@ class RuntimeContext return context_storage; } - static nostd::shared_ptr context_storage; + OPENTELEMETRY_EXPORT static nostd::shared_ptr context_storage; }; OPENTELEMETRY_EXPORT nostd::shared_ptr RuntimeContext::context_storage( diff --git a/api/include/opentelemetry/logs/provider.h b/api/include/opentelemetry/logs/provider.h index 8855d3e334..ed2d1d2312 100644 --- a/api/include/opentelemetry/logs/provider.h +++ b/api/include/opentelemetry/logs/provider.h @@ -44,9 +44,9 @@ class Provider } private: - static nostd::shared_ptr provider; + OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; - static common::SpinLockMutex lock; + OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; }; OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopLoggerProvider); diff --git a/api/include/opentelemetry/metrics/provider.h b/api/include/opentelemetry/metrics/provider.h index 5c6f2e9b54..909ffa89a1 100644 --- a/api/include/opentelemetry/metrics/provider.h +++ b/api/include/opentelemetry/metrics/provider.h @@ -44,9 +44,9 @@ class Provider } private: - static nostd::shared_ptr provider; + OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; - static common::SpinLockMutex lock; + OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; }; OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopMeterProvider); diff --git a/api/include/opentelemetry/trace/provider.h b/api/include/opentelemetry/trace/provider.h index 430ef3e417..332e524cb6 100644 --- a/api/include/opentelemetry/trace/provider.h +++ b/api/include/opentelemetry/trace/provider.h @@ -43,9 +43,9 @@ class Provider } private: - static nostd::shared_ptr provider; + OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; - static common::SpinLockMutex lock; + OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; }; OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopTracerProvider); diff --git a/api/include/opentelemetry/trace/trace_state.h b/api/include/opentelemetry/trace/trace_state.h index 96fbf132ac..8525b3cbaf 100644 --- a/api/include/opentelemetry/trace/trace_state.h +++ b/api/include/opentelemetry/trace/trace_state.h @@ -313,7 +313,7 @@ class TraceState // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - static nostd::shared_ptr default_ts; + OPENTELEMETRY_EXPORT static nostd::shared_ptr default_ts; }; OPENTELEMETRY_EXPORT nostd::shared_ptr TraceState::default_ts(new TraceState()); From 0a64f86862809199689829f98e1be978bf89f86d Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 28 Jul 2022 18:57:36 +0200 Subject: [PATCH 03/15] Fix header only API for singletons (#1520) Code cleanup. --- api/include/opentelemetry/_metrics/provider.h | 9 +++---- api/include/opentelemetry/baggage/baggage.h | 4 ++-- api/include/opentelemetry/common/macros.h | 24 +++++++++++++++---- .../context/propagation/global_propagator.h | 10 ++++---- .../opentelemetry/context/runtime_context.h | 6 ++--- api/include/opentelemetry/logs/provider.h | 9 +++---- api/include/opentelemetry/metrics/provider.h | 9 +++---- api/include/opentelemetry/trace/provider.h | 9 +++---- api/include/opentelemetry/trace/trace_state.h | 4 ++-- 9 files changed, 51 insertions(+), 33 deletions(-) diff --git a/api/include/opentelemetry/_metrics/provider.h b/api/include/opentelemetry/_metrics/provider.h index 4cf1a392fb..bfa93ecb16 100644 --- a/api/include/opentelemetry/_metrics/provider.h +++ b/api/include/opentelemetry/_metrics/provider.h @@ -43,14 +43,15 @@ class Provider } private: - OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; - OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopMeterProvider); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( + new NoopMeterProvider); -OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/baggage/baggage.h b/api/include/opentelemetry/baggage/baggage.h index 5548f544e1..81e269abfa 100644 --- a/api/include/opentelemetry/baggage/baggage.h +++ b/api/include/opentelemetry/baggage/baggage.h @@ -290,10 +290,10 @@ class Baggage // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - OPENTELEMETRY_EXPORT static nostd::shared_ptr default_baggage; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr default_baggage; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr Baggage::default_baggage(new Baggage()); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Baggage::default_baggage(new Baggage()); } // namespace baggage diff --git a/api/include/opentelemetry/common/macros.h b/api/include/opentelemetry/common/macros.h index 89735d8d7b..c99ed53119 100644 --- a/api/include/opentelemetry/common/macros.h +++ b/api/include/opentelemetry/common/macros.h @@ -91,15 +91,29 @@ #endif /** - @def OPENTELEMETRY_EXPORT + @def OPENTELEMETRY_DECLARE_EXPORT Declare a weak symbol with visibility default. */ #if defined(__clang__) -# define OPENTELEMETRY_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DECLARE_EXPORT __attribute__((visibility("default"), weak)) #elif defined(__GNUC__) -# define OPENTELEMETRY_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DECLARE_EXPORT __attribute__((visibility("default"), weak)) #elif defined(_MSC_VER) -# define OPENTELEMETRY_EXPORT __declspec(selectany) +# define OPENTELEMETRY_DECLARE_EXPORT #else -# define OPENTELEMETRY_EXPORT +# define OPENTELEMETRY_DECLARE_EXPORT +#endif + +/** + @def OPENTELEMETRY_DEFINE_EXPORT + Declare a weak symbol with visibility default. +*/ +#if defined(__clang__) +# define OPENTELEMETRY_DEFINE_EXPORT __attribute__((visibility("default"), weak)) +#elif defined(__GNUC__) +# define OPENTELEMETRY_DEFINE_EXPORT __attribute__((visibility("default"), weak)) +#elif defined(_MSC_VER) +# define OPENTELEMETRY_DEFINE_EXPORT __declspec(selectany) +#else +# define OPENTELEMETRY_DEFINE_EXPORT #endif diff --git a/api/include/opentelemetry/context/propagation/global_propagator.h b/api/include/opentelemetry/context/propagation/global_propagator.h index 946d45ef2c..bc8c6c6406 100644 --- a/api/include/opentelemetry/context/propagation/global_propagator.h +++ b/api/include/opentelemetry/context/propagation/global_propagator.h @@ -40,15 +40,15 @@ class GlobalTextMapPropagator } private: - OPENTELEMETRY_EXPORT static nostd::shared_ptr propagator; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr propagator; - OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr GlobalTextMapPropagator::propagator( - new NoOpPropagator()); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr + GlobalTextMapPropagator::propagator(new NoOpPropagator()); -OPENTELEMETRY_EXPORT common::SpinLockMutex GlobalTextMapPropagator::lock; +OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex GlobalTextMapPropagator::lock; } // namespace propagation } // namespace context diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 44d69c48bf..31ced2c233 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -167,11 +167,11 @@ class RuntimeContext return context_storage; } - OPENTELEMETRY_EXPORT static nostd::shared_ptr context_storage; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr context_storage; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr RuntimeContext::context_storage( - GetDefaultStorage()); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr + RuntimeContext::context_storage(GetDefaultStorage()); inline Token::~Token() noexcept { diff --git a/api/include/opentelemetry/logs/provider.h b/api/include/opentelemetry/logs/provider.h index ed2d1d2312..a7b0b64f3b 100644 --- a/api/include/opentelemetry/logs/provider.h +++ b/api/include/opentelemetry/logs/provider.h @@ -44,14 +44,15 @@ class Provider } private: - OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; - OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopLoggerProvider); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( + new NoopLoggerProvider); -OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; } // namespace logs OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/provider.h b/api/include/opentelemetry/metrics/provider.h index 909ffa89a1..38853649dc 100644 --- a/api/include/opentelemetry/metrics/provider.h +++ b/api/include/opentelemetry/metrics/provider.h @@ -44,14 +44,15 @@ class Provider } private: - OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; - OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopMeterProvider); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( + new NoopMeterProvider); -OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/provider.h b/api/include/opentelemetry/trace/provider.h index 332e524cb6..6777036556 100644 --- a/api/include/opentelemetry/trace/provider.h +++ b/api/include/opentelemetry/trace/provider.h @@ -43,14 +43,15 @@ class Provider } private: - OPENTELEMETRY_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; - OPENTELEMETRY_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr Provider::provider(new NoopTracerProvider); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( + new NoopTracerProvider); -OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/trace_state.h b/api/include/opentelemetry/trace/trace_state.h index 8525b3cbaf..f4c110f41f 100644 --- a/api/include/opentelemetry/trace/trace_state.h +++ b/api/include/opentelemetry/trace/trace_state.h @@ -313,10 +313,10 @@ class TraceState // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - OPENTELEMETRY_EXPORT static nostd::shared_ptr default_ts; + OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr default_ts; }; -OPENTELEMETRY_EXPORT nostd::shared_ptr TraceState::default_ts(new TraceState()); +OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr TraceState::default_ts(new TraceState()); } // namespace trace OPENTELEMETRY_END_NAMESPACE From b1e865b02d36ebd078513e2b176cf254cb913ef7 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 22 Aug 2022 17:50:35 +0200 Subject: [PATCH 04/15] WIP --- api/include/opentelemetry/_metrics/provider.h | 8 +++---- api/include/opentelemetry/baggage/baggage.h | 5 +++-- api/include/opentelemetry/common/macros.h | 22 +++++++++---------- .../context/propagation/global_propagator.h | 8 +++---- .../opentelemetry/context/runtime_context.h | 5 +++-- api/include/opentelemetry/logs/provider.h | 8 +++---- api/include/opentelemetry/metrics/provider.h | 8 +++---- api/include/opentelemetry/trace/provider.h | 8 +++---- api/include/opentelemetry/trace/trace_state.h | 5 +++-- 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/api/include/opentelemetry/_metrics/provider.h b/api/include/opentelemetry/_metrics/provider.h index bfa93ecb16..b73f253e58 100644 --- a/api/include/opentelemetry/_metrics/provider.h +++ b/api/include/opentelemetry/_metrics/provider.h @@ -43,15 +43,15 @@ class Provider } private: - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( new NoopMeterProvider); -OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/baggage/baggage.h b/api/include/opentelemetry/baggage/baggage.h index 81e269abfa..a36fd9bd91 100644 --- a/api/include/opentelemetry/baggage/baggage.h +++ b/api/include/opentelemetry/baggage/baggage.h @@ -290,10 +290,11 @@ class Baggage // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr default_baggage; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr default_baggage; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Baggage::default_baggage(new Baggage()); +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Baggage::default_baggage( + new Baggage()); } // namespace baggage diff --git a/api/include/opentelemetry/common/macros.h b/api/include/opentelemetry/common/macros.h index c99ed53119..68abafc5f7 100644 --- a/api/include/opentelemetry/common/macros.h +++ b/api/include/opentelemetry/common/macros.h @@ -91,29 +91,29 @@ #endif /** - @def OPENTELEMETRY_DECLARE_EXPORT + @def OPENTELEMETRY_DECLARE_API_SINGLETON Declare a weak symbol with visibility default. */ #if defined(__clang__) -# define OPENTELEMETRY_DECLARE_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DECLARE_API_SINGLETON __attribute__((visibility("default"), weak)) #elif defined(__GNUC__) -# define OPENTELEMETRY_DECLARE_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DECLARE_API_SINGLETON __attribute__((visibility("default"), weak)) #elif defined(_MSC_VER) -# define OPENTELEMETRY_DECLARE_EXPORT +# define OPENTELEMETRY_DECLARE_API_SINGLETON #else -# define OPENTELEMETRY_DECLARE_EXPORT +# define OPENTELEMETRY_DECLARE_API_SINGLETON #endif /** - @def OPENTELEMETRY_DEFINE_EXPORT - Declare a weak symbol with visibility default. + @def OPENTELEMETRY_DEFINE_API_SINGLETON + Define a weak symbol with visibility default. */ #if defined(__clang__) -# define OPENTELEMETRY_DEFINE_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DEFINE_API_SINGLETON __attribute__((visibility("default"), weak)) #elif defined(__GNUC__) -# define OPENTELEMETRY_DEFINE_EXPORT __attribute__((visibility("default"), weak)) +# define OPENTELEMETRY_DEFINE_API_SINGLETON __attribute__((visibility("default"), weak)) #elif defined(_MSC_VER) -# define OPENTELEMETRY_DEFINE_EXPORT __declspec(selectany) +# define OPENTELEMETRY_DEFINE_API_SINGLETON __declspec(selectany) #else -# define OPENTELEMETRY_DEFINE_EXPORT +# define OPENTELEMETRY_DEFINE_API_SINGLETON #endif diff --git a/api/include/opentelemetry/context/propagation/global_propagator.h b/api/include/opentelemetry/context/propagation/global_propagator.h index bc8c6c6406..35059e33e2 100644 --- a/api/include/opentelemetry/context/propagation/global_propagator.h +++ b/api/include/opentelemetry/context/propagation/global_propagator.h @@ -40,15 +40,15 @@ class GlobalTextMapPropagator } private: - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr propagator; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr propagator; - OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr GlobalTextMapPropagator::propagator(new NoOpPropagator()); -OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex GlobalTextMapPropagator::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex GlobalTextMapPropagator::lock; } // namespace propagation } // namespace context diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 31ced2c233..9c5f346b49 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -167,10 +167,11 @@ class RuntimeContext return context_storage; } - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr context_storage; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr + context_storage; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr RuntimeContext::context_storage(GetDefaultStorage()); inline Token::~Token() noexcept diff --git a/api/include/opentelemetry/logs/provider.h b/api/include/opentelemetry/logs/provider.h index a7b0b64f3b..94f1d7d02c 100644 --- a/api/include/opentelemetry/logs/provider.h +++ b/api/include/opentelemetry/logs/provider.h @@ -44,15 +44,15 @@ class Provider } private: - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( new NoopLoggerProvider); -OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; } // namespace logs OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/provider.h b/api/include/opentelemetry/metrics/provider.h index 38853649dc..d133471a6c 100644 --- a/api/include/opentelemetry/metrics/provider.h +++ b/api/include/opentelemetry/metrics/provider.h @@ -44,15 +44,15 @@ class Provider } private: - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( new NoopMeterProvider); -OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/provider.h b/api/include/opentelemetry/trace/provider.h index 6777036556..bc99b42668 100644 --- a/api/include/opentelemetry/trace/provider.h +++ b/api/include/opentelemetry/trace/provider.h @@ -43,15 +43,15 @@ class Provider } private: - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr provider; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - OPENTELEMETRY_DECLARE_EXPORT static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr Provider::provider( +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( new NoopTracerProvider); -OPENTELEMETRY_DEFINE_EXPORT common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/trace_state.h b/api/include/opentelemetry/trace/trace_state.h index f4c110f41f..e4ce9cba67 100644 --- a/api/include/opentelemetry/trace/trace_state.h +++ b/api/include/opentelemetry/trace/trace_state.h @@ -313,10 +313,11 @@ class TraceState // Store entries in a C-style array to avoid using std::array or std::vector. nostd::unique_ptr kv_properties_; - OPENTELEMETRY_DECLARE_EXPORT static nostd::shared_ptr default_ts; + OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr default_ts; }; -OPENTELEMETRY_DEFINE_EXPORT nostd::shared_ptr TraceState::default_ts(new TraceState()); +OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr TraceState::default_ts( + new TraceState()); } // namespace trace OPENTELEMETRY_END_NAMESPACE From 049e2507bbd4179a5d5f4fcae2ff085e281c51fb Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 31 Aug 2022 21:24:20 +0200 Subject: [PATCH 05/15] WIP Added unit test to check singleton uniqueness --- api/test/CMakeLists.txt | 1 + api/test/singleton/CMakeLists.txt | 21 +++ api/test/singleton/component_a.cc | 37 +++++ api/test/singleton/component_a.h | 5 + api/test/singleton/component_b.cc | 37 +++++ api/test/singleton/component_b.h | 5 + api/test/singleton/component_c.cc | 37 +++++ api/test/singleton/component_c.h | 5 + api/test/singleton/component_d.cc | 37 +++++ api/test/singleton/component_d.h | 5 + api/test/singleton/singleton_test.cc | 231 +++++++++++++++++++++++++++ 11 files changed, 421 insertions(+) create mode 100644 api/test/singleton/CMakeLists.txt create mode 100644 api/test/singleton/component_a.cc create mode 100644 api/test/singleton/component_a.h create mode 100644 api/test/singleton/component_b.cc create mode 100644 api/test/singleton/component_b.h create mode 100644 api/test/singleton/component_c.cc create mode 100644 api/test/singleton/component_c.h create mode 100644 api/test/singleton/component_d.cc create mode 100644 api/test/singleton/component_d.h create mode 100644 api/test/singleton/singleton_test.cc diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt index 41125005ee..06f29d7365 100644 --- a/api/test/CMakeLists.txt +++ b/api/test/CMakeLists.txt @@ -13,3 +13,4 @@ if(WITH_LOGS_PREVIEW) endif() add_subdirectory(common) add_subdirectory(baggage) +add_subdirectory(singleton) diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt new file mode 100644 index 0000000000..58629e8e68 --- /dev/null +++ b/api/test/singleton/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +include(GoogleTest) + +add_library(component_a STATIC component_a.cc) +add_library(component_b STATIC component_b.cc) +add_library(component_c SHARED component_c.cc) +add_library(component_d SHARED component_d.cc) + +add_executable(singleton_test singleton_test.cc) + +target_link_libraries(singleton_test + component_a component_b component_c component_d + ${GTEST_BOTH_LIBRARIES} opentelemetry_api) + +gtest_add_tests( + TARGET singleton_test + TEST_PREFIX singleton. + TEST_LIST singleton_test) + diff --git a/api/test/singleton/component_a.cc b/api/test/singleton/component_a.cc new file mode 100644 index 0000000000..654b904023 --- /dev/null +++ b/api/test/singleton/component_a.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" + +#include "component_a.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("A", "10.1"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("A::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("A::f2")); + + f1(); + f1(); +} + +void do_something_in_a() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("A::library")); + + f2(); +} diff --git a/api/test/singleton/component_a.h b/api/test/singleton/component_a.h new file mode 100644 index 0000000000..ad6d559f5c --- /dev/null +++ b/api/test/singleton/component_a.h @@ -0,0 +1,5 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +void do_something_in_a(); + diff --git a/api/test/singleton/component_b.cc b/api/test/singleton/component_b.cc new file mode 100644 index 0000000000..e506cbff16 --- /dev/null +++ b/api/test/singleton/component_b.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" + +#include "component_b.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("B", "20.2"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("B::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("B::f2")); + + f1(); + f1(); +} + +void do_something_in_b() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("B::library")); + + f2(); +} diff --git a/api/test/singleton/component_b.h b/api/test/singleton/component_b.h new file mode 100644 index 0000000000..a5c14aeeff --- /dev/null +++ b/api/test/singleton/component_b.h @@ -0,0 +1,5 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +void do_something_in_b(); + diff --git a/api/test/singleton/component_c.cc b/api/test/singleton/component_c.cc new file mode 100644 index 0000000000..4aec4937c8 --- /dev/null +++ b/api/test/singleton/component_c.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" + +#include "component_c.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("C", "30.3"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("C::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("C::f2")); + + f1(); + f1(); +} + +void do_something_in_c() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("C::library")); + + f2(); +} diff --git a/api/test/singleton/component_c.h b/api/test/singleton/component_c.h new file mode 100644 index 0000000000..f84a7ccb7e --- /dev/null +++ b/api/test/singleton/component_c.h @@ -0,0 +1,5 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +void do_something_in_c(); + diff --git a/api/test/singleton/component_d.cc b/api/test/singleton/component_d.cc new file mode 100644 index 0000000000..53a3aa9b2c --- /dev/null +++ b/api/test/singleton/component_d.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" + +#include "component_d.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("D", "40.4"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("D::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("D::f2")); + + f1(); + f1(); +} + +void do_something_in_d() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("D::library")); + + f2(); +} diff --git a/api/test/singleton/component_d.h b/api/test/singleton/component_d.h new file mode 100644 index 0000000000..1c797d4f9b --- /dev/null +++ b/api/test/singleton/component_d.h @@ -0,0 +1,5 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +void do_something_in_d(); + diff --git a/api/test/singleton/singleton_test.cc b/api/test/singleton/singleton_test.cc new file mode 100644 index 0000000000..66f1324704 --- /dev/null +++ b/api/test/singleton/singleton_test.cc @@ -0,0 +1,231 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include +#include + +#include "component_a.h" +#include "component_b.h" +#include "component_c.h" +#include "component_d.h" + +#include "opentelemetry/trace/default_span.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_context_kv_iterable.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/trace/tracer_provider.h" + +using namespace opentelemetry; + +void do_something() +{ + do_something_in_a(); + do_something_in_b(); + do_something_in_c(); + do_something_in_d(); +} + +int span_a_lib_count = 0; +int span_a_f1_count = 0; +int span_a_f2_count = 0; +int span_b_lib_count = 0; +int span_b_f1_count = 0; +int span_b_f2_count = 0; +int span_c_lib_count = 0; +int span_c_f1_count = 0; +int span_c_f2_count = 0; +int span_d_lib_count = 0; +int span_d_f1_count = 0; +int span_d_f2_count = 0; +int unknown_span_count = 0; + +void reset_counts() +{ + span_a_lib_count = 0; + span_a_f1_count = 0; + span_a_f2_count = 0; + span_b_lib_count = 0; + span_b_f1_count = 0; + span_b_f2_count = 0; + span_c_lib_count = 0; + span_c_f1_count = 0; + span_c_f2_count = 0; + span_d_lib_count = 0; + span_d_f1_count = 0; + span_d_f2_count = 0; + unknown_span_count = 0; +} + +class MyTracer : public trace::Tracer +{ +public: + nostd::shared_ptr StartSpan(nostd::string_view name, + const common::KeyValueIterable &attributes, + const trace::SpanContextKeyValueIterable &links, + const trace::StartSpanOptions &options) noexcept override + { + nostd::shared_ptr result(new trace::DefaultSpan(trace::SpanContext::GetInvalid())); + + /* + Unit test code, no need to be fancy. + */ + + if (name == "A::library") + { + span_a_lib_count++; + } + else if (name == "A::f1") + { + span_a_f1_count++; + } + else if (name == "A::f2") + { + span_a_f2_count++; + } + else if (name == "B::library") + { + span_b_lib_count++; + } + else if (name == "B::f1") + { + span_b_f1_count++; + } + else if (name == "B::f2") + { + span_b_f2_count++; + } + else if (name == "C::library") + { + span_c_lib_count++; + } + else if (name == "C::f1") + { + span_c_f1_count++; + } + else if (name == "C::f2") + { + span_c_f2_count++; + } + else if (name == "D::library") + { + span_d_lib_count++; + } + else if (name == "D::f1") + { + span_d_f1_count++; + } + else if (name == "D::f2") + { + span_d_f2_count++; + } + else + { + unknown_span_count++; + } + + return result; + } + + void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override {} + + void CloseWithMicroseconds(uint64_t timeout) noexcept override {} +}; + +class MyTracerProvider : public trace::TracerProvider +{ +public: + static std::shared_ptr Create() + { + std::shared_ptr result(new MyTracerProvider()); + return result; + } + + nostd::shared_ptr GetTracer(nostd::string_view library_name, + nostd::string_view library_version, + nostd::string_view schema_url) noexcept override + { + nostd::shared_ptr result(new MyTracer()); + return result; + } +}; + +void setup_otel() +{ + std::shared_ptr provider = MyTracerProvider::Create(); + + // The whole point of this test is to make sure + // that the API singleton behind SetTracerProvider() + // works for all components, static and dynamic. + + // Set the global tracer provider + trace_api::Provider::SetTracerProvider(provider); +} + +void cleanup_otel() +{ + std::shared_ptr provider( + new opentelemetry::trace::NoopTracerProvider()); + + // Set the global tracer provider + trace_api::Provider::SetTracerProvider(provider); +} + +TEST(SingletonTest, Uniqueness) +{ + do_something(); + + EXPECT_EQ(span_a_lib_count, 0); + EXPECT_EQ(span_a_f1_count, 0); + EXPECT_EQ(span_a_f2_count, 0); + EXPECT_EQ(span_b_lib_count, 0); + EXPECT_EQ(span_b_f1_count, 0); + EXPECT_EQ(span_b_f2_count, 0); + EXPECT_EQ(span_c_lib_count, 0); + EXPECT_EQ(span_c_f1_count, 0); + EXPECT_EQ(span_c_f2_count, 0); + EXPECT_EQ(span_d_lib_count, 0); + EXPECT_EQ(span_d_f1_count, 0); + EXPECT_EQ(span_d_f2_count, 0); + EXPECT_EQ(unknown_span_count, 0); + + reset_counts(); + setup_otel(); + + do_something(); + + EXPECT_EQ(span_a_lib_count, 1); + EXPECT_EQ(span_a_f1_count, 2); + EXPECT_EQ(span_a_f2_count, 1); + EXPECT_EQ(span_b_lib_count, 1); + EXPECT_EQ(span_b_f1_count, 2); + EXPECT_EQ(span_b_f2_count, 1); + EXPECT_EQ(span_c_lib_count, 1); + EXPECT_EQ(span_c_f1_count, 2); + EXPECT_EQ(span_c_f2_count, 1); + EXPECT_EQ(span_d_lib_count, 1); + EXPECT_EQ(span_d_f1_count, 2); + EXPECT_EQ(span_d_f2_count, 1); + EXPECT_EQ(unknown_span_count, 0); + + reset_counts(); + cleanup_otel(); + + do_something(); + + EXPECT_EQ(span_a_lib_count, 0); + EXPECT_EQ(span_a_f1_count, 0); + EXPECT_EQ(span_a_f2_count, 0); + EXPECT_EQ(span_b_lib_count, 0); + EXPECT_EQ(span_b_f1_count, 0); + EXPECT_EQ(span_b_f2_count, 0); + EXPECT_EQ(span_c_lib_count, 0); + EXPECT_EQ(span_c_f1_count, 0); + EXPECT_EQ(span_c_f2_count, 0); + EXPECT_EQ(span_d_lib_count, 0); + EXPECT_EQ(span_d_f1_count, 0); + EXPECT_EQ(span_d_f2_count, 0); + EXPECT_EQ(unknown_span_count, 0); +} From aeb7d63363e9e6bf6801ccc1e5c24f99ef95f522 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 31 Aug 2022 21:29:36 +0200 Subject: [PATCH 06/15] Format. --- api/test/singleton/CMakeLists.txt | 11 ++++++++--- api/test/singleton/component_a.cc | 2 +- api/test/singleton/component_a.h | 1 - api/test/singleton/component_b.cc | 2 +- api/test/singleton/component_b.h | 1 - api/test/singleton/component_c.cc | 2 +- api/test/singleton/component_c.h | 1 - api/test/singleton/component_d.cc | 2 +- api/test/singleton/component_d.h | 1 - 9 files changed, 12 insertions(+), 11 deletions(-) diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index 58629e8e68..4c32be9679 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -10,9 +10,14 @@ add_library(component_d SHARED component_d.cc) add_executable(singleton_test singleton_test.cc) -target_link_libraries(singleton_test - component_a component_b component_c component_d - ${GTEST_BOTH_LIBRARIES} opentelemetry_api) +target_link_libraries( + singleton_test + component_a + component_b + component_c + component_d + ${GTEST_BOTH_LIBRARIES} + opentelemetry_api) gtest_add_tests( TARGET singleton_test diff --git a/api/test/singleton/component_a.cc b/api/test/singleton/component_a.cc index 654b904023..bdf03cb699 100644 --- a/api/test/singleton/component_a.cc +++ b/api/test/singleton/component_a.cc @@ -1,9 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/version.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" #include "component_a.h" diff --git a/api/test/singleton/component_a.h b/api/test/singleton/component_a.h index ad6d559f5c..2e06edf9ca 100644 --- a/api/test/singleton/component_a.h +++ b/api/test/singleton/component_a.h @@ -2,4 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 void do_something_in_a(); - diff --git a/api/test/singleton/component_b.cc b/api/test/singleton/component_b.cc index e506cbff16..966d9c461a 100644 --- a/api/test/singleton/component_b.cc +++ b/api/test/singleton/component_b.cc @@ -1,9 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/version.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" #include "component_b.h" diff --git a/api/test/singleton/component_b.h b/api/test/singleton/component_b.h index a5c14aeeff..2e526e21d5 100644 --- a/api/test/singleton/component_b.h +++ b/api/test/singleton/component_b.h @@ -2,4 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 void do_something_in_b(); - diff --git a/api/test/singleton/component_c.cc b/api/test/singleton/component_c.cc index 4aec4937c8..b3ef27e143 100644 --- a/api/test/singleton/component_c.cc +++ b/api/test/singleton/component_c.cc @@ -1,9 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/version.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" #include "component_c.h" diff --git a/api/test/singleton/component_c.h b/api/test/singleton/component_c.h index f84a7ccb7e..96444b2a54 100644 --- a/api/test/singleton/component_c.h +++ b/api/test/singleton/component_c.h @@ -2,4 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 void do_something_in_c(); - diff --git a/api/test/singleton/component_d.cc b/api/test/singleton/component_d.cc index 53a3aa9b2c..5954aee629 100644 --- a/api/test/singleton/component_d.cc +++ b/api/test/singleton/component_d.cc @@ -1,9 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/version.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" #include "component_d.h" diff --git a/api/test/singleton/component_d.h b/api/test/singleton/component_d.h index 1c797d4f9b..7d8ca0f9d1 100644 --- a/api/test/singleton/component_d.h +++ b/api/test/singleton/component_d.h @@ -2,4 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 void do_something_in_d(); - From 76999571551e6e9b3d76374e80fc8602089df087 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 31 Aug 2022 22:16:43 +0200 Subject: [PATCH 07/15] WIP, added bazel build --- api/test/singleton/BUILD | 76 +++++++++++++++++++++++++++++++ api/test/singleton/CMakeLists.txt | 8 ++++ api/test/singleton/component_b.h | 4 ++ api/test/singleton/component_d.h | 4 ++ 4 files changed, 92 insertions(+) create mode 100644 api/test/singleton/BUILD diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD new file mode 100644 index 0000000000..35252e0a97 --- /dev/null +++ b/api/test/singleton/BUILD @@ -0,0 +1,76 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +# FIXME: visibility("hidden") / visibility("default") compiling options. + +cc_library( + name = "component_a", + srcs = [ + "component_a.cc", + ], + hdrs = [ + "component_a.h", + ], + deps = [ + "//api", + ], +) + +cc_library( + name = "component_b", + srcs = [ + "component_b.cc", + ], + hdrs = [ + "component_b.h", + ], + deps = [ + "//api", + ], +) + +cc_library( + name = "component_c", + srcs = [ + "component_c.cc", + ], + hdrs = [ + "component_c.h", + ], + deps = [ + "//api", + ], +) + +cc_library( + name = "component_d", + srcs = [ + "component_d.cc", + ], + hdrs = [ + "component_d.h", + ], + deps = [ + "//api", + ], +) + +cc_test( + name = "singleton_test", + srcs = [ + "singleton_test.cc", + ], + tags = [ + "api", + "test", + ], + deps = [ + "//api", + "component_a", + "component_b", + "component_c", + "component_d", + "@com_google_googletest//:gtest_main", + ], +) + diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index 4c32be9679..b8fba22441 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -4,9 +4,16 @@ include(GoogleTest) add_library(component_a STATIC component_a.cc) +set_target_properties(component_a PROPERTIES CXX_VISIBILITY_PRESET default) + add_library(component_b STATIC component_b.cc) +set_target_properties(component_b PROPERTIES CXX_VISIBILITY_PRESET hidden) + add_library(component_c SHARED component_c.cc) +set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default) + add_library(component_d SHARED component_d.cc) +set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden) add_executable(singleton_test singleton_test.cc) @@ -17,6 +24,7 @@ target_link_libraries( component_c component_d ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) gtest_add_tests( diff --git a/api/test/singleton/component_b.h b/api/test/singleton/component_b.h index 2e526e21d5..48cb41f6ed 100644 --- a/api/test/singleton/component_b.h +++ b/api/test/singleton/component_b.h @@ -1,4 +1,8 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +// component_b is compiled with visibility("hidden"), +// so make the entry point visible + +__attribute__((visibility("default"))) void do_something_in_b(); diff --git a/api/test/singleton/component_d.h b/api/test/singleton/component_d.h index 7d8ca0f9d1..a36813a874 100644 --- a/api/test/singleton/component_d.h +++ b/api/test/singleton/component_d.h @@ -1,4 +1,8 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +// component_d is compiled with visibility("hidden"), +// so make the entry point visible + +__attribute__((visibility("default"))) void do_something_in_d(); From fe1565d33042f68b3e8b5aa87f8003b23f7567f2 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 31 Aug 2022 22:22:09 +0200 Subject: [PATCH 08/15] format cleanup --- api/test/singleton/BUILD | 2 +- api/test/singleton/CMakeLists.txt | 1 - api/test/singleton/component_b.h | 3 +-- api/test/singleton/component_d.h | 3 +-- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index 35252e0a97..1b2c314b32 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -65,11 +65,11 @@ cc_test( "test", ], deps = [ - "//api", "component_a", "component_b", "component_c", "component_d", + "//api", "@com_google_googletest//:gtest_main", ], ) diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index b8fba22441..3839d600e7 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -31,4 +31,3 @@ gtest_add_tests( TARGET singleton_test TEST_PREFIX singleton. TEST_LIST singleton_test) - diff --git a/api/test/singleton/component_b.h b/api/test/singleton/component_b.h index 48cb41f6ed..90431e457a 100644 --- a/api/test/singleton/component_b.h +++ b/api/test/singleton/component_b.h @@ -4,5 +4,4 @@ // component_b is compiled with visibility("hidden"), // so make the entry point visible -__attribute__((visibility("default"))) -void do_something_in_b(); +__attribute__((visibility("default"))) void do_something_in_b(); diff --git a/api/test/singleton/component_d.h b/api/test/singleton/component_d.h index a36813a874..37fbffc0e3 100644 --- a/api/test/singleton/component_d.h +++ b/api/test/singleton/component_d.h @@ -4,5 +4,4 @@ // component_d is compiled with visibility("hidden"), // so make the entry point visible -__attribute__((visibility("default"))) -void do_something_in_d(); +__attribute__((visibility("default"))) void do_something_in_d(); From 38649b4fd53879fe6a1e85bea7c2b362ca1204fe Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Sep 2022 10:39:20 +0200 Subject: [PATCH 09/15] cleanup --- api/test/singleton/BUILD | 1 - api/test/singleton/CMakeLists.txt | 2 -- api/test/singleton/component_b.h | 5 +---- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index 1b2c314b32..1c99e6c628 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -73,4 +73,3 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) - diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index 3839d600e7..bb8f84f210 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -4,10 +4,8 @@ include(GoogleTest) add_library(component_a STATIC component_a.cc) -set_target_properties(component_a PROPERTIES CXX_VISIBILITY_PRESET default) add_library(component_b STATIC component_b.cc) -set_target_properties(component_b PROPERTIES CXX_VISIBILITY_PRESET hidden) add_library(component_c SHARED component_c.cc) set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default) diff --git a/api/test/singleton/component_b.h b/api/test/singleton/component_b.h index 90431e457a..2e526e21d5 100644 --- a/api/test/singleton/component_b.h +++ b/api/test/singleton/component_b.h @@ -1,7 +1,4 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -// component_b is compiled with visibility("hidden"), -// so make the entry point visible - -__attribute__((visibility("default"))) void do_something_in_b(); +void do_something_in_b(); From 42ab6e5a0b21fd8e80a1f4f4ea8bb7600cc68f90 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Sep 2022 12:26:45 +0200 Subject: [PATCH 10/15] Work in progress, tentative windows fix. --- api/test/singleton/BUILD | 10 ++++++++++ api/test/singleton/component_d.cc | 2 ++ api/test/singleton/component_d.h | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index 1c99e6c628..d246f0ec4c 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -3,6 +3,14 @@ # FIXME: visibility("hidden") / visibility("default") compiling options. +DEFAULT_COPTS = [ + "-fvisibility=default", +] + +HIDDEN_COPTS = [ + "-fvisibility=hidden", +] + cc_library( name = "component_a", srcs = [ @@ -37,6 +45,7 @@ cc_library( hdrs = [ "component_c.h", ], + copts = DEFAULT_COPTS, deps = [ "//api", ], @@ -50,6 +59,7 @@ cc_library( hdrs = [ "component_d.h", ], + copts = HIDDEN_COPTS, deps = [ "//api", ], diff --git a/api/test/singleton/component_d.cc b/api/test/singleton/component_d.cc index 5954aee629..313fd1d23a 100644 --- a/api/test/singleton/component_d.cc +++ b/api/test/singleton/component_d.cc @@ -5,6 +5,8 @@ #include "opentelemetry/trace/provider.h" #include "opentelemetry/version.h" +#define BUILD_COMPONENT_D + #include "component_d.h" namespace trace = opentelemetry::trace; diff --git a/api/test/singleton/component_d.h b/api/test/singleton/component_d.h index 37fbffc0e3..be65f4b5ac 100644 --- a/api/test/singleton/component_d.h +++ b/api/test/singleton/component_d.h @@ -1,7 +1,21 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +// Make the entry point visible, loaded dynamically + +#if defined(_MSC_VER) +// component_d is a DDL + +# ifdef BUILD_COMPONENT_D +__declspec(dllexport) +# else +__declspec(dllimport) +# endif + +#else +// component_d is a shared library (*.so) // component_d is compiled with visibility("hidden"), -// so make the entry point visible +__attribute__((visibility("default"))) +#endif -__attribute__((visibility("default"))) void do_something_in_d(); + void do_something_in_d(); From ef0e0a3a562edf4ea072f46658ea52702bb7aefd Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Sep 2022 15:25:57 +0200 Subject: [PATCH 11/15] WIP, continued --- .../opentelemetry/context/runtime_context.h | 2 +- api/test/singleton/BUILD | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 9c5f346b49..08ea72b82b 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -18,7 +18,7 @@ class Token public: bool operator==(const Context &other) const noexcept { return context_ == other; } - ~Token(); + ~Token() noexcept; private: friend class RuntimeContextStorage; diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index d246f0ec4c..d1fbcdb516 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -3,11 +3,19 @@ # FIXME: visibility("hidden") / visibility("default") compiling options. -DEFAULT_COPTS = [ +DEFAULT_WIN_COPTS = [ +] + +# gcc and clang, assumed to be used on this platform +DEFAULT_NOWIN_COPTS = [ "-fvisibility=default", ] -HIDDEN_COPTS = [ +HIDDEN_WIN_COPTS = [ +] + +# gcc and clang, assumed to be used on this platform +HIDDEN_NOWIN_COPTS = [ "-fvisibility=hidden", ] @@ -45,7 +53,10 @@ cc_library( hdrs = [ "component_c.h", ], - copts = DEFAULT_COPTS, + copts = select({ + "//bazel:windows": DEFAULT_WIN_COPTS, + "//conditions:default": DEFAULT_NOWIN_COPTS, + }), deps = [ "//api", ], @@ -59,7 +70,10 @@ cc_library( hdrs = [ "component_d.h", ], - copts = HIDDEN_COPTS, + copts = select({ + "//bazel:windows": DEFAULT_WIN_COPTS, + "//conditions:default": DEFAULT_NOWIN_COPTS, + }), deps = [ "//api", ], From 3f50d3d52dd3311729f0f7be89b9120447985346 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Sep 2022 16:13:59 +0200 Subject: [PATCH 12/15] Expand singleton_test to have 2 libraries of each kind. --- api/test/singleton/BUILD | 36 +++++++++++++++++ api/test/singleton/CMakeLists.txt | 8 ++++ api/test/singleton/component_e.cc | 37 ++++++++++++++++++ api/test/singleton/component_e.h | 4 ++ api/test/singleton/component_f.cc | 39 +++++++++++++++++++ api/test/singleton/component_f.h | 21 ++++++++++ api/test/singleton/singleton_test.cc | 58 ++++++++++++++++++++++++++++ 7 files changed, 203 insertions(+) create mode 100644 api/test/singleton/component_e.cc create mode 100644 api/test/singleton/component_e.h create mode 100644 api/test/singleton/component_f.cc create mode 100644 api/test/singleton/component_f.h diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index d1fbcdb516..c0d3fcfd2c 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -70,6 +70,23 @@ cc_library( hdrs = [ "component_d.h", ], + copts = select({ + "//bazel:windows": HIDDEN_WIN_COPTS, + "//conditions:default": HIDDEN_NOWIN_COPTS, + }), + deps = [ + "//api", + ], +) + +cc_library( + name = "component_e", + srcs = [ + "component_e.cc", + ], + hdrs = [ + "component_e.h", + ], copts = select({ "//bazel:windows": DEFAULT_WIN_COPTS, "//conditions:default": DEFAULT_NOWIN_COPTS, @@ -79,6 +96,23 @@ cc_library( ], ) +cc_library( + name = "component_f", + srcs = [ + "component_f.cc", + ], + hdrs = [ + "component_f.h", + ], + copts = select({ + "//bazel:windows": HIDDEN_WIN_COPTS, + "//conditions:default": HIDDEN_NOWIN_COPTS, + }), + deps = [ + "//api", + ], +) + cc_test( name = "singleton_test", srcs = [ @@ -93,6 +127,8 @@ cc_test( "component_b", "component_c", "component_d", + "component_e", + "component_f", "//api", "@com_google_googletest//:gtest_main", ], diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index bb8f84f210..ef475bb34d 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -13,6 +13,12 @@ set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default) add_library(component_d SHARED component_d.cc) set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden) +add_library(component_e SHARED component_e.cc) +set_target_properties(component_e PROPERTIES CXX_VISIBILITY_PRESET default) + +add_library(component_f SHARED component_f.cc) +set_target_properties(component_f PROPERTIES CXX_VISIBILITY_PRESET hidden) + add_executable(singleton_test singleton_test.cc) target_link_libraries( @@ -21,6 +27,8 @@ target_link_libraries( component_b component_c component_d + component_e + component_f ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) diff --git a/api/test/singleton/component_e.cc b/api/test/singleton/component_e.cc new file mode 100644 index 0000000000..e5b2a2bfe6 --- /dev/null +++ b/api/test/singleton/component_e.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" + +#include "component_e.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("E", "50.5"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("E::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("E::f2")); + + f1(); + f1(); +} + +void do_something_in_e() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("E::library")); + + f2(); +} diff --git a/api/test/singleton/component_e.h b/api/test/singleton/component_e.h new file mode 100644 index 0000000000..c36ef40c4e --- /dev/null +++ b/api/test/singleton/component_e.h @@ -0,0 +1,4 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +void do_something_in_e(); diff --git a/api/test/singleton/component_f.cc b/api/test/singleton/component_f.cc new file mode 100644 index 0000000000..33b0af2fbd --- /dev/null +++ b/api/test/singleton/component_f.cc @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/version.h" + +#define BUILD_COMPONENT_F + +#include "component_f.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +static nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("F", "60.6"); +} + +static void f1() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("F::f1")); +} + +static void f2() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("F::f2")); + + f1(); + f1(); +} + +void do_something_in_f() +{ + auto scoped_span = trace::Scope(get_tracer()->StartSpan("F::library")); + + f2(); +} diff --git a/api/test/singleton/component_f.h b/api/test/singleton/component_f.h new file mode 100644 index 0000000000..775a6cf758 --- /dev/null +++ b/api/test/singleton/component_f.h @@ -0,0 +1,21 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Make the entry point visible, loaded dynamically + +#if defined(_MSC_VER) +// component_d is a DDL + +# ifdef BUILD_COMPONENT_F +__declspec(dllexport) +# else +__declspec(dllimport) +# endif + +#else +// component_d is a shared library (*.so) +// component_d is compiled with visibility("hidden"), +__attribute__((visibility("default"))) +#endif + + void do_something_in_f(); diff --git a/api/test/singleton/singleton_test.cc b/api/test/singleton/singleton_test.cc index 66f1324704..f9942c2f65 100644 --- a/api/test/singleton/singleton_test.cc +++ b/api/test/singleton/singleton_test.cc @@ -10,6 +10,8 @@ #include "component_b.h" #include "component_c.h" #include "component_d.h" +#include "component_e.h" +#include "component_f.h" #include "opentelemetry/trace/default_span.h" #include "opentelemetry/trace/provider.h" @@ -26,6 +28,8 @@ void do_something() do_something_in_b(); do_something_in_c(); do_something_in_d(); + do_something_in_e(); + do_something_in_f(); } int span_a_lib_count = 0; @@ -40,6 +44,12 @@ int span_c_f2_count = 0; int span_d_lib_count = 0; int span_d_f1_count = 0; int span_d_f2_count = 0; +int span_e_lib_count = 0; +int span_e_f1_count = 0; +int span_e_f2_count = 0; +int span_f_lib_count = 0; +int span_f_f1_count = 0; +int span_f_f2_count = 0; int unknown_span_count = 0; void reset_counts() @@ -56,6 +66,12 @@ void reset_counts() span_d_lib_count = 0; span_d_f1_count = 0; span_d_f2_count = 0; + span_e_lib_count = 0; + span_e_f1_count = 0; + span_e_f2_count = 0; + span_f_lib_count = 0; + span_f_f1_count = 0; + span_f_f2_count = 0; unknown_span_count = 0; } @@ -121,6 +137,30 @@ class MyTracer : public trace::Tracer { span_d_f2_count++; } + else if (name == "E::library") + { + span_e_lib_count++; + } + else if (name == "E::f1") + { + span_e_f1_count++; + } + else if (name == "E::f2") + { + span_e_f2_count++; + } + else if (name == "F::library") + { + span_f_lib_count++; + } + else if (name == "F::f1") + { + span_f_f1_count++; + } + else if (name == "F::f2") + { + span_f_f2_count++; + } else { unknown_span_count++; @@ -189,6 +229,12 @@ TEST(SingletonTest, Uniqueness) EXPECT_EQ(span_d_lib_count, 0); EXPECT_EQ(span_d_f1_count, 0); EXPECT_EQ(span_d_f2_count, 0); + EXPECT_EQ(span_e_lib_count, 0); + EXPECT_EQ(span_e_f1_count, 0); + EXPECT_EQ(span_e_f2_count, 0); + EXPECT_EQ(span_f_lib_count, 0); + EXPECT_EQ(span_f_f1_count, 0); + EXPECT_EQ(span_f_f2_count, 0); EXPECT_EQ(unknown_span_count, 0); reset_counts(); @@ -208,6 +254,12 @@ TEST(SingletonTest, Uniqueness) EXPECT_EQ(span_d_lib_count, 1); EXPECT_EQ(span_d_f1_count, 2); EXPECT_EQ(span_d_f2_count, 1); + EXPECT_EQ(span_e_lib_count, 1); + EXPECT_EQ(span_e_f1_count, 2); + EXPECT_EQ(span_e_f2_count, 1); + EXPECT_EQ(span_f_lib_count, 1); + EXPECT_EQ(span_f_f1_count, 2); + EXPECT_EQ(span_f_f2_count, 1); EXPECT_EQ(unknown_span_count, 0); reset_counts(); @@ -227,5 +279,11 @@ TEST(SingletonTest, Uniqueness) EXPECT_EQ(span_d_lib_count, 0); EXPECT_EQ(span_d_f1_count, 0); EXPECT_EQ(span_d_f2_count, 0); + EXPECT_EQ(span_e_lib_count, 0); + EXPECT_EQ(span_e_f1_count, 0); + EXPECT_EQ(span_e_f2_count, 0); + EXPECT_EQ(span_f_lib_count, 0); + EXPECT_EQ(span_f_f1_count, 0); + EXPECT_EQ(span_f_f2_count, 0); EXPECT_EQ(unknown_span_count, 0); } From 6b1add1db60fc18ae7e9b116af2f49fb98bf520f Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Thu, 1 Sep 2022 17:11:13 +0200 Subject: [PATCH 13/15] Fixed bazel link --- api/test/singleton/BUILD | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index c0d3fcfd2c..f7d8a80346 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -30,6 +30,7 @@ cc_library( deps = [ "//api", ], + linkstatic = True, ) cc_library( @@ -43,6 +44,7 @@ cc_library( deps = [ "//api", ], + linkstatic = True, ) cc_library( @@ -60,6 +62,7 @@ cc_library( deps = [ "//api", ], + linkstatic = False, ) cc_library( @@ -77,6 +80,7 @@ cc_library( deps = [ "//api", ], + linkstatic = False, ) cc_library( @@ -94,6 +98,7 @@ cc_library( deps = [ "//api", ], + linkstatic = False, ) cc_library( @@ -111,6 +116,7 @@ cc_library( deps = [ "//api", ], + linkstatic = False, ) cc_test( @@ -132,4 +138,5 @@ cc_test( "//api", "@com_google_googletest//:gtest_main", ], + linkstatic = False, ) From 4a817ff493170707751ff6ace1dea8f592c9f9a5 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 5 Sep 2022 09:14:39 +0200 Subject: [PATCH 14/15] WIP --- api/include/opentelemetry/_metrics/provider.h | 32 ++++++++++++------- .../context/propagation/global_propagator.h | 31 +++++++++++------- .../opentelemetry/context/runtime_context.h | 23 +++++++++---- api/include/opentelemetry/logs/provider.h | 32 ++++++++++++------- api/include/opentelemetry/metrics/provider.h | 32 ++++++++++++------- api/include/opentelemetry/trace/provider.h | 32 ++++++++++++------- api/test/singleton/BUILD | 14 ++++---- 7 files changed, 128 insertions(+), 68 deletions(-) diff --git a/api/include/opentelemetry/_metrics/provider.h b/api/include/opentelemetry/_metrics/provider.h index b73f253e58..3604bf6d8c 100644 --- a/api/include/opentelemetry/_metrics/provider.h +++ b/api/include/opentelemetry/_metrics/provider.h @@ -14,6 +14,21 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { + +class ProviderSingleton +{ +public: + ProviderSingleton() : lock(), provider(new NoopMeterProvider()) {} + + common::SpinLockMutex lock; + nostd::shared_ptr provider; + + ProviderSingleton(const ProviderSingleton &) = delete; + ProviderSingleton &operator=(const ProviderSingleton &) = delete; + ProviderSingleton(ProviderSingleton &&) = delete; + ProviderSingleton &operator=(ProviderSingleton &&) = delete; +}; + /** * Stores the singleton global MeterProvider. */ @@ -28,8 +43,8 @@ class Provider */ static nostd::shared_ptr GetMeterProvider() noexcept { - std::lock_guard guard(lock); - nostd::shared_ptr result(provider); + std::lock_guard guard(s.lock); + nostd::shared_ptr result(s.provider); return result; } @@ -38,20 +53,15 @@ class Provider */ static void SetMeterProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(lock); - provider = tp; + std::lock_guard guard(s.lock); + s.provider = tp; } private: - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - - OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( - new NoopMeterProvider); - -OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/propagation/global_propagator.h b/api/include/opentelemetry/context/propagation/global_propagator.h index 35059e33e2..690ccaad97 100644 --- a/api/include/opentelemetry/context/propagation/global_propagator.h +++ b/api/include/opentelemetry/context/propagation/global_propagator.h @@ -20,6 +20,20 @@ namespace context namespace propagation { +class PropagatorSingleton +{ +public: + PropagatorSingleton() : lock(), propagator(new NoOpPropagator()) {} + + common::SpinLockMutex lock; + nostd::shared_ptr propagator; + + PropagatorSingleton(const PropagatorSingleton &) = delete; + PropagatorSingleton &operator=(const PropagatorSingleton &) = delete; + PropagatorSingleton(PropagatorSingleton &&) = delete; + PropagatorSingleton &operator=(PropagatorSingleton &&) = delete; +}; + /** * Stores the singleton TextMapPropagator. */ @@ -28,27 +42,22 @@ class GlobalTextMapPropagator public: static nostd::shared_ptr GetGlobalPropagator() noexcept { - std::lock_guard guard(lock); - nostd::shared_ptr result(propagator); + std::lock_guard guard(s.lock); + nostd::shared_ptr result(s.propagator); return result; } static void SetGlobalPropagator(nostd::shared_ptr prop) noexcept { - std::lock_guard guard(lock); - propagator = prop; + std::lock_guard guard(s.lock); + s.propagator = prop; } private: - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr propagator; - - OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static PropagatorSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr - GlobalTextMapPropagator::propagator(new NoOpPropagator()); - -OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex GlobalTextMapPropagator::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON PropagatorSingleton GlobalTextMapPropagator::s; } // namespace propagation } // namespace context diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 08ea72b82b..cb89344e5a 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -75,6 +75,19 @@ class RuntimeContextStorage */ static RuntimeContextStorage *GetDefaultStorage() noexcept; +class RuntimeContextSingleton +{ +public: + RuntimeContextSingleton() : context_storage(GetDefaultStorage()) {} + + nostd::shared_ptr context_storage; + + RuntimeContextSingleton(const RuntimeContextSingleton &) = delete; + RuntimeContextSingleton &operator=(const RuntimeContextSingleton &) = delete; + RuntimeContextSingleton(RuntimeContextSingleton &&) = delete; + RuntimeContextSingleton &operator=(RuntimeContextSingleton &&) = delete; +}; + // Provides a wrapper for propagating the context object globally. // // By default, a thread-local runtime context storage is used. @@ -146,7 +159,7 @@ class RuntimeContext */ static void SetRuntimeContextStorage(nostd::shared_ptr storage) noexcept { - context_storage = storage; + s.context_storage = storage; } /** @@ -164,15 +177,13 @@ class RuntimeContext private: static nostd::shared_ptr GetRuntimeContextStorage() noexcept { - return context_storage; + return s.context_storage; } - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr - context_storage; + OPENTELEMETRY_DECLARE_API_SINGLETON static RuntimeContextSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr - RuntimeContext::context_storage(GetDefaultStorage()); +OPENTELEMETRY_DEFINE_API_SINGLETON RuntimeContextSingleton RuntimeContext::s; inline Token::~Token() noexcept { diff --git a/api/include/opentelemetry/logs/provider.h b/api/include/opentelemetry/logs/provider.h index 94f1d7d02c..c75d6697e3 100644 --- a/api/include/opentelemetry/logs/provider.h +++ b/api/include/opentelemetry/logs/provider.h @@ -15,6 +15,21 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace logs { + +class ProviderSingleton +{ +public: + ProviderSingleton() : lock(), provider(new NoopLoggerProvider()) {} + + common::SpinLockMutex lock; + nostd::shared_ptr provider; + + ProviderSingleton(const ProviderSingleton &) = delete; + ProviderSingleton &operator=(const ProviderSingleton &) = delete; + ProviderSingleton(ProviderSingleton &&) = delete; + ProviderSingleton &operator=(ProviderSingleton &&) = delete; +}; + /** * Stores the singleton global LoggerProvider. */ @@ -29,8 +44,8 @@ class Provider */ static nostd::shared_ptr GetLoggerProvider() noexcept { - std::lock_guard guard(lock); - nostd::shared_ptr result(provider); + std::lock_guard guard(s.lock); + nostd::shared_ptr result(s.provider); return result; } @@ -39,20 +54,15 @@ class Provider */ static void SetLoggerProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(lock); - provider = tp; + std::lock_guard guard(s.lock); + s.provider = tp; } private: - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - - OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( - new NoopLoggerProvider); - -OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s; } // namespace logs OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/provider.h b/api/include/opentelemetry/metrics/provider.h index d133471a6c..3ec9e08fa9 100644 --- a/api/include/opentelemetry/metrics/provider.h +++ b/api/include/opentelemetry/metrics/provider.h @@ -15,6 +15,21 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics { + +class ProviderSingleton +{ +public: + ProviderSingleton() : lock(), provider(new NoopMeterProvider()) {} + + common::SpinLockMutex lock; + nostd::shared_ptr provider; + + ProviderSingleton(const ProviderSingleton &) = delete; + ProviderSingleton &operator=(const ProviderSingleton &) = delete; + ProviderSingleton(ProviderSingleton &&) = delete; + ProviderSingleton &operator=(ProviderSingleton &&) = delete; +}; + /** * Stores the singleton global MeterProvider. */ @@ -29,8 +44,8 @@ class Provider */ static nostd::shared_ptr GetMeterProvider() noexcept { - std::lock_guard guard(lock); - nostd::shared_ptr result(provider); + std::lock_guard guard(s.lock); + nostd::shared_ptr result(s.provider); return result; } @@ -39,20 +54,15 @@ class Provider */ static void SetMeterProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(lock); - provider = tp; + std::lock_guard guard(s.lock); + s.provider = tp; } private: - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - - OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( - new NoopMeterProvider); - -OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/provider.h b/api/include/opentelemetry/trace/provider.h index bc99b42668..7c1311df02 100644 --- a/api/include/opentelemetry/trace/provider.h +++ b/api/include/opentelemetry/trace/provider.h @@ -14,6 +14,21 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { + +class ProviderSingleton +{ +public: + ProviderSingleton() : lock(), provider(new NoopTracerProvider()) {} + + common::SpinLockMutex lock; + nostd::shared_ptr provider; + + ProviderSingleton(const ProviderSingleton &) = delete; + ProviderSingleton &operator=(const ProviderSingleton &) = delete; + ProviderSingleton(ProviderSingleton &&) = delete; + ProviderSingleton &operator=(ProviderSingleton &&) = delete; +}; + /** * Stores the singleton global TracerProvider. */ @@ -28,8 +43,8 @@ class Provider */ static nostd::shared_ptr GetTracerProvider() noexcept { - std::lock_guard guard(lock); - nostd::shared_ptr result(provider); + std::lock_guard guard(s.lock); + nostd::shared_ptr result(s.provider); return result; } @@ -38,20 +53,15 @@ class Provider */ static void SetTracerProvider(nostd::shared_ptr tp) noexcept { - std::lock_guard guard(lock); - provider = tp; + std::lock_guard guard(s.lock); + s.provider = tp; } private: - OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr provider; - - OPENTELEMETRY_DECLARE_API_SINGLETON static common::SpinLockMutex lock; + OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s; }; -OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr Provider::provider( - new NoopTracerProvider); - -OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock; +OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s; } // namespace trace OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/singleton/BUILD b/api/test/singleton/BUILD index f7d8a80346..a8f5450532 100644 --- a/api/test/singleton/BUILD +++ b/api/test/singleton/BUILD @@ -27,10 +27,10 @@ cc_library( hdrs = [ "component_a.h", ], + linkstatic = True, deps = [ "//api", ], - linkstatic = True, ) cc_library( @@ -41,10 +41,10 @@ cc_library( hdrs = [ "component_b.h", ], + linkstatic = True, deps = [ "//api", ], - linkstatic = True, ) cc_library( @@ -59,10 +59,10 @@ cc_library( "//bazel:windows": DEFAULT_WIN_COPTS, "//conditions:default": DEFAULT_NOWIN_COPTS, }), + linkstatic = False, deps = [ "//api", ], - linkstatic = False, ) cc_library( @@ -77,10 +77,10 @@ cc_library( "//bazel:windows": HIDDEN_WIN_COPTS, "//conditions:default": HIDDEN_NOWIN_COPTS, }), + linkstatic = False, deps = [ "//api", ], - linkstatic = False, ) cc_library( @@ -95,10 +95,10 @@ cc_library( "//bazel:windows": DEFAULT_WIN_COPTS, "//conditions:default": DEFAULT_NOWIN_COPTS, }), + linkstatic = False, deps = [ "//api", ], - linkstatic = False, ) cc_library( @@ -113,10 +113,10 @@ cc_library( "//bazel:windows": HIDDEN_WIN_COPTS, "//conditions:default": HIDDEN_NOWIN_COPTS, }), + linkstatic = False, deps = [ "//api", ], - linkstatic = False, ) cc_test( @@ -124,6 +124,7 @@ cc_test( srcs = [ "singleton_test.cc", ], + linkstatic = False, tags = [ "api", "test", @@ -138,5 +139,4 @@ cc_test( "//api", "@com_google_googletest//:gtest_main", ], - linkstatic = False, ) From 162ba3ce7e08488a7ce3e04df812cea64d733a0a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Mon, 5 Sep 2022 10:56:18 +0200 Subject: [PATCH 15/15] Fixed import/export for windows ddl. --- api/test/singleton/component_c.cc | 2 ++ api/test/singleton/component_c.h | 13 ++++++++++++- api/test/singleton/component_e.cc | 2 ++ api/test/singleton/component_e.h | 13 ++++++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/api/test/singleton/component_c.cc b/api/test/singleton/component_c.cc index b3ef27e143..cd87ceb727 100644 --- a/api/test/singleton/component_c.cc +++ b/api/test/singleton/component_c.cc @@ -5,6 +5,8 @@ #include "opentelemetry/trace/provider.h" #include "opentelemetry/version.h" +#define BUILD_COMPONENT_C + #include "component_c.h" namespace trace = opentelemetry::trace; diff --git a/api/test/singleton/component_c.h b/api/test/singleton/component_c.h index 96444b2a54..31193da2df 100644 --- a/api/test/singleton/component_c.h +++ b/api/test/singleton/component_c.h @@ -1,4 +1,15 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -void do_something_in_c(); +#if defined(_MSC_VER) +// component_c is a DDL + +# ifdef BUILD_COMPONENT_C +__declspec(dllexport) +# else +__declspec(dllimport) +# endif + +#endif + + void do_something_in_c(); diff --git a/api/test/singleton/component_e.cc b/api/test/singleton/component_e.cc index e5b2a2bfe6..9c88d27732 100644 --- a/api/test/singleton/component_e.cc +++ b/api/test/singleton/component_e.cc @@ -5,6 +5,8 @@ #include "opentelemetry/trace/provider.h" #include "opentelemetry/version.h" +#define BUILD_COMPONENT_E + #include "component_e.h" namespace trace = opentelemetry::trace; diff --git a/api/test/singleton/component_e.h b/api/test/singleton/component_e.h index c36ef40c4e..53782d76f7 100644 --- a/api/test/singleton/component_e.h +++ b/api/test/singleton/component_e.h @@ -1,4 +1,15 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -void do_something_in_e(); +#if defined(_MSC_VER) +// component_e is a DDL + +# ifdef BUILD_COMPONENT_E +__declspec(dllexport) +# else +__declspec(dllimport) +# endif + +#endif + + void do_something_in_e();