Skip to content

Commit

Permalink
Implement OTEL_SDK_DISABLED
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Jan 15, 2025
1 parent 62b7197 commit 19a8e4c
Show file tree
Hide file tree
Showing 45 changed files with 725 additions and 150 deletions.
33 changes: 23 additions & 10 deletions api/include/opentelemetry/logs/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace logs
{
class Provider;
}
} // namespace sdk

namespace logs
{

Expand All @@ -38,12 +46,13 @@ class OPENTELEMETRY_EXPORT Provider

/**
* Changes the singleton LoggerProvider.
* While declared in the API class opentelemetry::logs::Provider,
* this method is actually part of the SDK, not API.
* An application **MUST** link with the opentelemetry-cpp SDK,
* to install a logs provider, when configuring OpenTelemetry.
* An instrumented library does not need to invoke this method.
*/
static void SetLoggerProvider(const nostd::shared_ptr<LoggerProvider> &tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetProvider() = tp;
}
static void SetLoggerProvider(const nostd::shared_ptr<LoggerProvider> &lp) noexcept;

/**
* Returns the singleton EventLoggerProvider.
Expand All @@ -59,14 +68,18 @@ class OPENTELEMETRY_EXPORT Provider

/**
* Changes the singleton EventLoggerProvider.
* While declared in the API class opentelemetry::logs::Provider,
* this method is actually part of the SDK, not API.
* An application **MUST** link with the opentelemetry-cpp SDK,
* to install an event logs provider, when configuring OpenTelemetry.
* An instrumented library does not need to invoke this method.
*/
static void SetEventLoggerProvider(const nostd::shared_ptr<EventLoggerProvider> &tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetEventProvider() = tp;
}
static void SetEventLoggerProvider(const nostd::shared_ptr<EventLoggerProvider> &lp) noexcept;

private:
/* The SDK is allowed to change the singleton in the API. */
friend class opentelemetry::sdk::logs::Provider;

OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<LoggerProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<LoggerProvider> provider(new NoopLoggerProvider);
Expand Down
22 changes: 17 additions & 5 deletions api/include/opentelemetry/metrics/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class Provider;
}
} // namespace sdk

namespace metrics
{

Expand All @@ -37,14 +45,18 @@ class Provider

/**
* Changes the singleton MeterProvider.
* While declared in the API class opentelemetry::metrics::Provider,
* this method is actually part of the SDK, not API.
* An application **MUST** link with the opentelemetry-cpp SDK,
* to install a metrics provider, when configuring OpenTelemetry.
* An instrumented library does not need to invoke this method.
*/
static void SetMeterProvider(const nostd::shared_ptr<MeterProvider> &tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetProvider() = tp;
}
static void SetMeterProvider(const nostd::shared_ptr<MeterProvider> &mp) noexcept;

private:
/* The SDK is allowed to change the singleton in the API. */
friend class opentelemetry::sdk::metrics::Provider;

OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<MeterProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<MeterProvider> provider(new NoopMeterProvider);
Expand Down
22 changes: 17 additions & 5 deletions api/include/opentelemetry/trace/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
class Provider;
}
} // namespace sdk

namespace trace
{

Expand All @@ -35,14 +43,18 @@ class OPENTELEMETRY_EXPORT Provider

/**
* Changes the singleton TracerProvider.
* While declared in the API class opentelemetry::trace::Provider,
* this method is actually part of the SDK, not API.
* An application **MUST** link with the opentelemetry-cpp SDK,
* to install a tracer provider, when configuring OpenTelemetry.
* An instrumented library does not need to invoke this method.
*/
static void SetTracerProvider(const nostd::shared_ptr<TracerProvider> &tp) noexcept
{
std::lock_guard<common::SpinLockMutex> guard(GetLock());
GetProvider() = tp;
}
static void SetTracerProvider(const nostd::shared_ptr<TracerProvider> &tp) noexcept;

private:
/* The SDK is allowed to change the singleton in the API. */
friend class opentelemetry::sdk::trace::Provider;

OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<TracerProvider> &GetProvider() noexcept
{
static nostd::shared_ptr<TracerProvider> provider(new NoopTracerProvider);
Expand Down
4 changes: 0 additions & 4 deletions api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@ add_subdirectory(metrics)
add_subdirectory(logs)
add_subdirectory(common)
add_subdirectory(baggage)

if(NOT OPENTELEMETRY_SKIP_DYNAMIC_LOADING_TESTS)
add_subdirectory(singleton)
endif()
41 changes: 0 additions & 41 deletions api/test/logs/logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,44 +192,3 @@ TEST(Logger, EventLogMethodOverloads)
opentelemetry::common::MakeAttributes(vec));
}

// Define a basic Logger class
class TestLogger : public Logger
{
const nostd::string_view GetName() noexcept override { return "test logger"; }

nostd::unique_ptr<opentelemetry::logs::LogRecord> CreateLogRecord() noexcept override
{
return nullptr;
}

using Logger::EmitLogRecord;

void EmitLogRecord(nostd::unique_ptr<opentelemetry::logs::LogRecord> &&) noexcept override {}
};

// Define a basic LoggerProvider class that returns an instance of the logger class defined above
class TestProvider : public LoggerProvider
{
nostd::shared_ptr<Logger> GetLogger(nostd::string_view /* logger_name */,
nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */,
const common::KeyValueIterable & /* attributes */) override
{
return nostd::shared_ptr<Logger>(new TestLogger());
}
};

TEST(Logger, PushLoggerImplementation)
{
// Push the new loggerprovider class into the global singleton
auto test_provider = shared_ptr<LoggerProvider>(new TestProvider());
Provider::SetLoggerProvider(test_provider);

auto lp = Provider::GetLoggerProvider();

// Check that the implementation was pushed by calling TestLogger's GetName()
nostd::string_view schema_url{"https://opentelemetry.io/schemas/1.11.0"};
auto logger = lp->GetLogger("TestLogger", "opentelelemtry_library", "", schema_url);
ASSERT_EQ("test logger", logger->GetName());
}
34 changes: 0 additions & 34 deletions api/test/logs/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@ TEST(Provider, GetLoggerProviderDefault)
EXPECT_NE(nullptr, tf);
}

TEST(Provider, SetLoggerProvider)
{
auto tf = shared_ptr<LoggerProvider>(new TestProvider());
Provider::SetLoggerProvider(tf);
ASSERT_EQ(tf, Provider::GetLoggerProvider());
}

TEST(Provider, MultipleLoggerProviders)
{
auto tf = shared_ptr<LoggerProvider>(new TestProvider());
Provider::SetLoggerProvider(tf);
auto tf2 = shared_ptr<LoggerProvider>(new TestProvider());
Provider::SetLoggerProvider(tf2);

ASSERT_NE(Provider::GetLoggerProvider(), tf);
}

TEST(Provider, GetLogger)
{
auto tf = shared_ptr<LoggerProvider>(new TestProvider());
Expand Down Expand Up @@ -88,23 +71,6 @@ TEST(Provider, GetEventLoggerProviderDefault)
EXPECT_NE(nullptr, tf);
}

TEST(Provider, SetEventLoggerProvider)
{
auto tf = nostd::shared_ptr<EventLoggerProvider>(new TestEventLoggerProvider());
Provider::SetEventLoggerProvider(tf);
ASSERT_EQ(tf, Provider::GetEventLoggerProvider());
}

TEST(Provider, MultipleEventLoggerProviders)
{
auto tf = nostd::shared_ptr<EventLoggerProvider>(new TestEventLoggerProvider());
Provider::SetEventLoggerProvider(tf);
auto tf2 = nostd::shared_ptr<EventLoggerProvider>(new TestEventLoggerProvider());
Provider::SetEventLoggerProvider(tf2);

ASSERT_NE(Provider::GetEventLoggerProvider(), tf);
}

TEST(Provider, CreateEventLogger)
{
auto tf = nostd::shared_ptr<TestEventLoggerProvider>(new TestEventLoggerProvider());
Expand Down
19 changes: 0 additions & 19 deletions api/test/metrics/meter_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,14 @@

#include <gtest/gtest.h>

#include "opentelemetry/metrics/noop.h"
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/nostd/shared_ptr.h"

using opentelemetry::metrics::MeterProvider;
using opentelemetry::metrics::NoopMeterProvider;
using opentelemetry::metrics::Provider;

TEST(Provider, GetMeterProviderDefault)
{
auto tf = Provider::GetMeterProvider();
EXPECT_NE(nullptr, tf);
}

TEST(Provider, SetMeterProvider)
{
auto tf = opentelemetry::nostd::shared_ptr<MeterProvider>(new NoopMeterProvider());
Provider::SetMeterProvider(tf);
ASSERT_EQ(tf, Provider::GetMeterProvider());
}

TEST(Provider, MultipleMeterProviders)
{
auto tf = opentelemetry::nostd::shared_ptr<MeterProvider>(new NoopMeterProvider());
Provider::SetMeterProvider(tf);
auto tf2 = opentelemetry::nostd::shared_ptr<MeterProvider>(new NoopMeterProvider());
Provider::SetMeterProvider(tf2);

ASSERT_NE(Provider::GetMeterProvider(), tf);
}
29 changes: 0 additions & 29 deletions api/test/trace/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,8 @@ using opentelemetry::trace::TracerProvider;

namespace nostd = opentelemetry::nostd;

class TestProvider : public TracerProvider
{

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<Tracer> GetTracer(
nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */,
const opentelemetry::common::KeyValueIterable * /* attributes */) noexcept override
{
return nostd::shared_ptr<Tracer>(nullptr);
}
#else
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */) noexcept override
{
return nostd::shared_ptr<Tracer>(nullptr);
}
#endif
};

TEST(Provider, GetTracerProviderDefault)
{
auto tf = Provider::GetTracerProvider();
EXPECT_NE(nullptr, tf);
}

TEST(Provider, SetTracerProvider)
{
auto tf = nostd::shared_ptr<TracerProvider>(new TestProvider());
Provider::SetTracerProvider(tf);
ASSERT_EQ(tf, Provider::GetTracerProvider());
}
18 changes: 18 additions & 0 deletions sdk/include/opentelemetry/sdk/common/disabled.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace common
{

bool GetSdkDisabled();

} // namespace common
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
39 changes: 39 additions & 0 deletions sdk/include/opentelemetry/sdk/logs/provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <mutex>

#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{

namespace logs
{

/**
* Changes the singleton global LoggerProvider.
*/
class Provider
{
public:
/**
* Changes the singleton LoggerProvider.
*/
static void SetLoggerProvider(
const nostd::shared_ptr<opentelemetry::logs::LoggerProvider> &lp) noexcept;

/**
* Changes the singleton EventLoggerProvider.
*/
static void SetEventLoggerProvider(
const nostd::shared_ptr<opentelemetry::logs::EventLoggerProvider> &lp) noexcept;
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 19a8e4c

Please sign in to comment.