Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC RFC] header only API for singletons #1525

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions api/include/opentelemetry/_metrics/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@

# 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"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{

class ProviderSingleton
{
public:
ProviderSingleton() : lock(), provider(new NoopMeterProvider()) {}

common::SpinLockMutex lock;
nostd::shared_ptr<MeterProvider> provider;

ProviderSingleton(const ProviderSingleton &) = delete;
ProviderSingleton &operator=(const ProviderSingleton &) = delete;
ProviderSingleton(ProviderSingleton &&) = delete;
ProviderSingleton &operator=(ProviderSingleton &&) = delete;
};

/**
* Stores the singleton global MeterProvider.
*/
Expand All @@ -27,33 +43,26 @@ class Provider
*/
static nostd::shared_ptr<MeterProvider> GetMeterProvider() noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
return nostd::shared_ptr<MeterProvider>(GetProvider());
std::lock_guard<common::SpinLockMutex> guard(s.lock);
nostd::shared_ptr<MeterProvider> result(s.provider);
return result;
}

/**
* Changes the singleton MeterProvider.
*/
static void SetMeterProvider(nostd::shared_ptr<MeterProvider> tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetProvider() = tp;
std::lock_guard<common::SpinLockMutex> guard(s.lock);
s.provider = tp;
}

private:
static nostd::shared_ptr<MeterProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<MeterProvider> provider(new NoopMeterProvider);
return provider;
}

static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
}
OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s;
};

OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s;

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif
12 changes: 7 additions & 5 deletions api/include/opentelemetry/baggage/baggage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cctype>

#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"
Expand Down Expand Up @@ -34,11 +35,7 @@ class Baggage
: kv_properties_(new opentelemetry::common::KeyValueProperties(keys_and_values))
{}

static nostd::shared_ptr<Baggage> GetDefault()
{
static nostd::shared_ptr<Baggage> baggage{new Baggage()};
return baggage;
}
static nostd::shared_ptr<Baggage> GetDefault() { return default_baggage; }

/* Get value for key in the baggage
@returns true if key is found, false otherwise
Expand Down Expand Up @@ -292,8 +289,13 @@ class Baggage
private:
// Store entries in a C-style array to avoid using std::array or std::vector.
nostd::unique_ptr<opentelemetry::common::KeyValueProperties> kv_properties_;

OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr<Baggage> default_baggage;
};

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<Baggage> Baggage::default_baggage(
new Baggage());

} // namespace baggage

OPENTELEMETRY_END_NAMESPACE
28 changes: 28 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,31 @@
#else
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg)
#endif

/**
@def OPENTELEMETRY_DECLARE_API_SINGLETON
Declare a weak symbol with visibility default.
*/
#if defined(__clang__)
# define OPENTELEMETRY_DECLARE_API_SINGLETON __attribute__((visibility("default"), weak))
#elif defined(__GNUC__)
# define OPENTELEMETRY_DECLARE_API_SINGLETON __attribute__((visibility("default"), weak))
#elif defined(_MSC_VER)
# define OPENTELEMETRY_DECLARE_API_SINGLETON
#else
# define OPENTELEMETRY_DECLARE_API_SINGLETON
#endif

/**
@def OPENTELEMETRY_DEFINE_API_SINGLETON
Define a weak symbol with visibility default.
*/
#if defined(__clang__)
# define OPENTELEMETRY_DEFINE_API_SINGLETON __attribute__((visibility("default"), weak))
#elif defined(__GNUC__)
# define OPENTELEMETRY_DEFINE_API_SINGLETON __attribute__((visibility("default"), weak))
#elif defined(_MSC_VER)
# define OPENTELEMETRY_DEFINE_API_SINGLETON __declspec(selectany)
#else
# define OPENTELEMETRY_DEFINE_API_SINGLETON
#endif
43 changes: 26 additions & 17 deletions api/include/opentelemetry/context/propagation/global_propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -19,37 +20,45 @@ namespace context
namespace propagation
{

/* Stores the singleton TextMapPropagator */
class PropagatorSingleton
{
public:
PropagatorSingleton() : lock(), propagator(new NoOpPropagator()) {}

common::SpinLockMutex lock;
nostd::shared_ptr<TextMapPropagator> propagator;

PropagatorSingleton(const PropagatorSingleton &) = delete;
PropagatorSingleton &operator=(const PropagatorSingleton &) = delete;
PropagatorSingleton(PropagatorSingleton &&) = delete;
PropagatorSingleton &operator=(PropagatorSingleton &&) = delete;
};

/**
* Stores the singleton TextMapPropagator.
*/
class GlobalTextMapPropagator
{
public:
static nostd::shared_ptr<TextMapPropagator> GetGlobalPropagator() noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
return nostd::shared_ptr<TextMapPropagator>(GetPropagator());
std::lock_guard<common::SpinLockMutex> guard(s.lock);
nostd::shared_ptr<TextMapPropagator> result(s.propagator);
return result;
}

static void SetGlobalPropagator(nostd::shared_ptr<TextMapPropagator> prop) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetPropagator() = prop;
std::lock_guard<common::SpinLockMutex> guard(s.lock);
s.propagator = prop;
}

private:
static nostd::shared_ptr<TextMapPropagator> &GetPropagator() noexcept
{
static nostd::shared_ptr<TextMapPropagator> propagator(new NoOpPropagator());
return propagator;
}

static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
}
OPENTELEMETRY_DECLARE_API_SINGLETON static PropagatorSingleton s;
};

OPENTELEMETRY_DEFINE_API_SINGLETON PropagatorSingleton GlobalTextMapPropagator::s;

} // namespace propagation
} // namespace context
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
28 changes: 20 additions & 8 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "opentelemetry/common/macros.h"
#include "opentelemetry/context/context.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -17,7 +18,7 @@ class Token
public:
bool operator==(const Context &other) const noexcept { return context_ == other; }

~Token();
~Token() noexcept;

private:
friend class RuntimeContextStorage;
Expand Down Expand Up @@ -74,6 +75,19 @@ class RuntimeContextStorage
*/
static RuntimeContextStorage *GetDefaultStorage() noexcept;

class RuntimeContextSingleton
{
public:
RuntimeContextSingleton() : context_storage(GetDefaultStorage()) {}

nostd::shared_ptr<RuntimeContextStorage> 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.
Expand Down Expand Up @@ -145,7 +159,7 @@ class RuntimeContext
*/
static void SetRuntimeContextStorage(nostd::shared_ptr<RuntimeContextStorage> storage) noexcept
{
GetStorage() = storage;
s.context_storage = storage;
}

/**
Expand All @@ -163,16 +177,14 @@ class RuntimeContext
private:
static nostd::shared_ptr<RuntimeContextStorage> GetRuntimeContextStorage() noexcept
{
return GetStorage();
return s.context_storage;
}

static nostd::shared_ptr<RuntimeContextStorage> &GetStorage() noexcept
{
static nostd::shared_ptr<RuntimeContextStorage> context(GetDefaultStorage());
return context;
}
OPENTELEMETRY_DECLARE_API_SINGLETON static RuntimeContextSingleton s;
};

OPENTELEMETRY_DEFINE_API_SINGLETON RuntimeContextSingleton RuntimeContext::s;

inline Token::~Token() noexcept
{
context::RuntimeContext::Detach(*this);
Expand Down
39 changes: 24 additions & 15 deletions api/include/opentelemetry/logs/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# include <mutex>

# include "opentelemetry/common/macros.h"
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/logs/logger_provider.h"
# include "opentelemetry/logs/noop.h"
Expand All @@ -14,6 +15,21 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace logs
{

class ProviderSingleton
{
public:
ProviderSingleton() : lock(), provider(new NoopLoggerProvider()) {}

common::SpinLockMutex lock;
nostd::shared_ptr<LoggerProvider> provider;

ProviderSingleton(const ProviderSingleton &) = delete;
ProviderSingleton &operator=(const ProviderSingleton &) = delete;
ProviderSingleton(ProviderSingleton &&) = delete;
ProviderSingleton &operator=(ProviderSingleton &&) = delete;
};

/**
* Stores the singleton global LoggerProvider.
*/
Expand All @@ -28,33 +44,26 @@ class Provider
*/
static nostd::shared_ptr<LoggerProvider> GetLoggerProvider() noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
return nostd::shared_ptr<LoggerProvider>(GetProvider());
std::lock_guard<common::SpinLockMutex> guard(s.lock);
nostd::shared_ptr<LoggerProvider> result(s.provider);
return result;
}

/**
* Changes the singleton LoggerProvider.
*/
static void SetLoggerProvider(nostd::shared_ptr<LoggerProvider> tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetProvider() = tp;
std::lock_guard<common::SpinLockMutex> guard(s.lock);
s.provider = tp;
}

private:
static nostd::shared_ptr<LoggerProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<LoggerProvider> provider(new NoopLoggerProvider);
return provider;
}

static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
}
OPENTELEMETRY_DECLARE_API_SINGLETON static ProviderSingleton s;
};

OPENTELEMETRY_DEFINE_API_SINGLETON ProviderSingleton Provider::s;

} // namespace logs
OPENTELEMETRY_END_NAMESPACE
#endif
Loading