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 (Windows) #1595

Closed
5 changes: 3 additions & 2 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 Down Expand Up @@ -41,13 +42,13 @@ class Provider
}

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

static common::SpinLockMutex &GetLock() noexcept
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
Expand Down
3 changes: 2 additions & 1 deletion 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,7 +35,7 @@ class Baggage
: kv_properties_(new opentelemetry::common::KeyValueProperties(keys_and_values))
{}

static nostd::shared_ptr<Baggage> GetDefault()
OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<Baggage> GetDefault()
{
static nostd::shared_ptr<Baggage> baggage{new Baggage()};
return baggage;
Expand Down
15 changes: 15 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@
#else
# define OPENTELEMETRY_DEPRECATED_MESSAGE(msg)
#endif

/**
@def OPENTELEMETRY_API_SINGLETON
Declare a symbol with visibility default.
*/
#if defined(__clang__)
# define OPENTELEMETRY_API_SINGLETON __attribute__((visibility("default")))
#elif defined(__GNUC__)
# define OPENTELEMETRY_API_SINGLETON __attribute__((visibility("default")))
#elif defined(_MSC_VER)
/* Tentative fix */
# define OPENTELEMETRY_API_SINGLETON __declspec(selectany)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why __declspec(selectany) is removed?Doesn't it work with MSVC on Windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owent

__declspec(selectany) works when applied to a member.

This PR tested applying it to a method, which failed to compile.

See places where OPENTELEMETRY_API_SINGLETON is used in this patch,
which are different compared to PR#1525

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add __declspec(selectany) to variables, so we can also support dll on Windows with MSVC?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owent, see file macros.h in the latest push.

#else
# define OPENTELEMETRY_API_SINGLETON
#endif
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 Down Expand Up @@ -37,13 +38,13 @@ class GlobalTextMapPropagator
}

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

static common::SpinLockMutex &GetLock() noexcept
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
Expand All @@ -52,4 +53,4 @@ class GlobalTextMapPropagator

} // namespace propagation
} // namespace context
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
5 changes: 3 additions & 2 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 @@ -166,7 +167,7 @@ class RuntimeContext
return GetStorage();
}

static nostd::shared_ptr<RuntimeContextStorage> &GetStorage() noexcept
OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<RuntimeContextStorage> &GetStorage() noexcept
{
static nostd::shared_ptr<RuntimeContextStorage> context(GetDefaultStorage());
return context;
Expand Down Expand Up @@ -315,7 +316,7 @@ class ThreadLocalContextStorage : public RuntimeContextStorage
Context *base_;
};

Stack &GetStack()
OPENTELEMETRY_API_SINGLETON Stack &GetStack()
{
static thread_local Stack stack_ = Stack();
return stack_;
Expand Down
5 changes: 3 additions & 2 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 Down Expand Up @@ -42,13 +43,13 @@ class Provider
}

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

static common::SpinLockMutex &GetLock() noexcept
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
Expand Down
7 changes: 4 additions & 3 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 Down Expand Up @@ -42,13 +43,13 @@ class Provider
}

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

static common::SpinLockMutex &GetLock() noexcept
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
Expand All @@ -57,4 +58,4 @@ class Provider

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
#endif
#endif
5 changes: 3 additions & 2 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 Down Expand Up @@ -41,13 +42,13 @@ class Provider
}

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

static common::SpinLockMutex &GetLock() noexcept
OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
{
static common::SpinLockMutex lock;
return lock;
Expand Down
3 changes: 2 additions & 1 deletion 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,7 +42,7 @@ class TraceState
static constexpr auto kKeyValueSeparator = '=';
static constexpr auto kMembersSeparator = ',';

static nostd::shared_ptr<TraceState> GetDefault()
OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<TraceState> GetDefault()
{
static nostd::shared_ptr<TraceState> ts{new TraceState()};
return ts;
Expand Down
1 change: 1 addition & 0 deletions api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ if(WITH_LOGS_PREVIEW)
endif()
add_subdirectory(common)
add_subdirectory(baggage)
add_subdirectory(singleton)
142 changes: 142 additions & 0 deletions api/test/singleton/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# FIXME: visibility("hidden") / visibility("default") compiling options.

DEFAULT_WIN_COPTS = [
]

# gcc and clang, assumed to be used on this platform
DEFAULT_NOWIN_COPTS = [
"-fvisibility=default",
]

HIDDEN_WIN_COPTS = [
]

# gcc and clang, assumed to be used on this platform
HIDDEN_NOWIN_COPTS = [
"-fvisibility=hidden",
]

cc_library(
name = "component_a",
srcs = [
"component_a.cc",
],
hdrs = [
"component_a.h",
],
linkstatic = True,
deps = [
"//api",
],
)

cc_library(
name = "component_b",
srcs = [
"component_b.cc",
],
hdrs = [
"component_b.h",
],
linkstatic = True,
deps = [
"//api",
],
)

cc_library(
name = "component_c",
srcs = [
"component_c.cc",
],
hdrs = [
"component_c.h",
],
copts = select({
"//bazel:windows": DEFAULT_WIN_COPTS,
"//conditions:default": DEFAULT_NOWIN_COPTS,
}),
linkstatic = False,
deps = [
"//api",
],
)

cc_library(
name = "component_d",
srcs = [
"component_d.cc",
],
hdrs = [
"component_d.h",
],
copts = select({
"//bazel:windows": HIDDEN_WIN_COPTS,
"//conditions:default": HIDDEN_NOWIN_COPTS,
}),
linkstatic = False,
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,
}),
linkstatic = False,
deps = [
"//api",
],
)

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,
}),
linkstatic = False,
deps = [
"//api",
],
)

cc_test(
name = "singleton_test",
srcs = [
"singleton_test.cc",
],
linkstatic = False,
tags = [
"api",
"test",
],
deps = [
"component_a",
"component_b",
"component_c",
"component_d",
"component_e",
"component_f",
"//api",
"@com_google_googletest//:gtest_main",
],
)
Loading