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 5 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
27 changes: 13 additions & 14 deletions api/include/opentelemetry/_metrics/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -27,33 +28,31 @@ 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(lock);
nostd::shared_ptr<MeterProvider> result(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(lock);
provider = tp;
}

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

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<MeterProvider> Provider::provider(
new NoopMeterProvider);

OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock;

} // 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
34 changes: 17 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,36 @@ namespace context
namespace propagation
{

/* Stores the singleton TextMapPropagator */

/**
* 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(lock);
nostd::shared_ptr<TextMapPropagator> result(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(lock);
propagator = prop;
}

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

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<TextMapPropagator>
GlobalTextMapPropagator::propagator(new NoOpPropagator());

OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex GlobalTextMapPropagator::lock;

} // namespace propagation
} // namespace context
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
15 changes: 8 additions & 7 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 Down Expand Up @@ -145,7 +146,7 @@ class RuntimeContext
*/
static void SetRuntimeContextStorage(nostd::shared_ptr<RuntimeContextStorage> storage) noexcept
{
GetStorage() = storage;
context_storage = storage;
}

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

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<RuntimeContextStorage>
RuntimeContext::context_storage(GetDefaultStorage());

inline Token::~Token() noexcept
{
context::RuntimeContext::Detach(*this);
Expand Down
27 changes: 13 additions & 14 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 @@ -28,33 +29,31 @@ 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(lock);
nostd::shared_ptr<LoggerProvider> result(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(lock);
provider = tp;
}

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

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<LoggerProvider> Provider::provider(
new NoopLoggerProvider);

OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock;

} // namespace logs
OPENTELEMETRY_END_NAMESPACE
#endif
29 changes: 14 additions & 15 deletions api/include/opentelemetry/metrics/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/metrics/meter_provider.h"
# include "opentelemetry/metrics/noop.h"
Expand All @@ -28,33 +29,31 @@ 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(lock);
nostd::shared_ptr<MeterProvider> result(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(lock);
provider = tp;
}

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

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<MeterProvider> Provider::provider(
new NoopMeterProvider);

OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock;

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif
#endif
27 changes: 13 additions & 14 deletions api/include/opentelemetry/trace/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <mutex>

#include "opentelemetry/common/macros.h"
#include "opentelemetry/common/spin_lock_mutex.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/trace/noop.h"
Expand All @@ -27,32 +28,30 @@ class Provider
*/
static nostd::shared_ptr<TracerProvider> GetTracerProvider() noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
return nostd::shared_ptr<TracerProvider>(GetProvider());
std::lock_guard<common::SpinLockMutex> guard(lock);
nostd::shared_ptr<TracerProvider> result(provider);
return result;
}

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

private:
static nostd::shared_ptr<TracerProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<TracerProvider> provider(new NoopTracerProvider);
return provider;
}
OPENTELEMETRY_DECLARE_API_SINGLETON static nostd::shared_ptr<TracerProvider> provider;

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

OPENTELEMETRY_DEFINE_API_SINGLETON nostd::shared_ptr<TracerProvider> Provider::provider(
new NoopTracerProvider);

OPENTELEMETRY_DEFINE_API_SINGLETON common::SpinLockMutex Provider::lock;

} // namespace trace
OPENTELEMETRY_END_NAMESPACE
Loading