Skip to content

Commit

Permalink
Proof of concept.
Browse files Browse the repository at this point in the history
Fix header only API for singletons (open-telemetry#1520)
  • Loading branch information
marcalff committed Jul 28, 2022
1 parent 3a8f913 commit fa7c2cc
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 77 deletions.
11 changes: 6 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,12 @@ 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_;

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

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

} // namespace baggage

OPENTELEMETRY_END_NAMESPACE
14 changes: 14 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
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;
}
static nostd::shared_ptr<TextMapPropagator> propagator;

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

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

OPENTELEMETRY_EXPORT common::SpinLockMutex GlobalTextMapPropagator::lock;

} // namespace propagation
} // namespace context
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
14 changes: 7 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,15 @@ 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;
}
static nostd::shared_ptr<RuntimeContextStorage> context_storage;
};

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

inline Token::~Token() noexcept
{
context::RuntimeContext::Detach(*this);
Expand Down
26 changes: 12 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,30 @@ 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;
}
static nostd::shared_ptr<LoggerProvider> provider;

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

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

OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock;

} // namespace logs
OPENTELEMETRY_END_NAMESPACE
#endif
28 changes: 13 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,30 @@ 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;
}
static nostd::shared_ptr<MeterProvider> provider;

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

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

OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock;

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif
#endif
26 changes: 12 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,29 @@ 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;
}
static nostd::shared_ptr<TracerProvider> provider;

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

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

OPENTELEMETRY_EXPORT common::SpinLockMutex Provider::lock;

} // namespace trace
OPENTELEMETRY_END_NAMESPACE
12 changes: 7 additions & 5 deletions api/include/opentelemetry/trace/trace_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,11 +42,7 @@ class TraceState
static constexpr auto kKeyValueSeparator = '=';
static constexpr auto kMembersSeparator = ',';

static nostd::shared_ptr<TraceState> GetDefault()
{
static nostd::shared_ptr<TraceState> ts{new TraceState()};
return ts;
}
static nostd::shared_ptr<TraceState> GetDefault() { return default_ts; }

/**
* Returns shared_ptr to a newly created TraceState parsed from the header provided.
Expand Down Expand Up @@ -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<opentelemetry::common::KeyValueProperties> kv_properties_;

static nostd::shared_ptr<TraceState> default_ts;
};

OPENTELEMETRY_EXPORT nostd::shared_ptr<TraceState> TraceState::default_ts(new TraceState());

} // namespace trace
OPENTELEMETRY_END_NAMESPACE

0 comments on commit fa7c2cc

Please sign in to comment.