Skip to content

Commit

Permalink
Add MeterProvider api (open-telemetry#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
HudsonHumphries authored Jul 14, 2020
1 parent 4b8e7f0 commit 04c48a1
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
Empty file.
18 changes: 18 additions & 0 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE

namespace metrics
{

class Meter
{
public:
Meter() = default;

};

} // namespace metrics

OPENTELEMETRY_END_NAMESPACE
27 changes: 27 additions & 0 deletions api/include/opentelemetry/metrics/meter_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "opentelemetry/metrics/meter.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{
/**
* Creates new Meter instances.
*/
class MeterProvider
{
public:
virtual ~MeterProvider() = default;
/**
* Gets or creates a named Meter instance.
*
* Optionally a version can be passed to create a named and versioned Meter
* instance.
*/
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view library_name,
nostd::string_view library_version = "") = 0;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
42 changes: 42 additions & 0 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
// Please refer to provider.h for documentation on how to obtain a Meter object.
//
// This file is part of the internal implementation of OpenTelemetry. Nothing in this file should be
// used directly. Please refer to meter.h for documentation on these interfaces.

#include "opentelemetry/metrics/meter.h"
#include "opentelemetry/metrics/meter_provider.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/version.h"

#include <memory>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{
/**
* No-op implementation of a MeterProvider.
*/
class NoopMeter final : public Meter, public std::enable_shared_from_this<NoopMeter> {};

class NoopMeterProvider final : public opentelemetry::metrics::MeterProvider
{
public:
NoopMeterProvider()
: meter_{nostd::shared_ptr<opentelemetry::metrics::NoopMeter>(
new opentelemetry::metrics::NoopMeter)}
{}

nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view library_name,
nostd::string_view library_version) override
{
return meter_;
}

private:
nostd::shared_ptr<opentelemetry::metrics::Meter> meter_;
};
} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
60 changes: 60 additions & 0 deletions api/include/opentelemetry/metrics/provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <atomic>

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

OPENTELEMETRY_BEGIN_NAMESPACE
namespace metrics
{
/**
* Stores the singleton global MeterProvider.
*/
class Provider
{
public:
/**
* Returns the singleton MeterProvider.
*
* By default, a no-op MeterProvider is returned. This will never return a
* nullptr MeterProvider.
*/
static nostd::shared_ptr<MeterProvider> GetMeterProvider() noexcept
{
while (GetLock().test_and_set(std::memory_order_acquire))
;
auto provider = nostd::shared_ptr<MeterProvider>(GetProvider());
GetLock().clear(std::memory_order_release);

return provider;
}

/**
* Changes the singleton MeterProvider.
*/
static void SetMeterProvider(nostd::shared_ptr<MeterProvider> tp) noexcept
{
while (GetLock().test_and_set(std::memory_order_acquire))
;
GetProvider() = tp;
GetLock().clear(std::memory_order_release);
}

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

static std::atomic_flag &GetLock() noexcept
{
static std::atomic_flag lock = ATOMIC_FLAG_INIT;
return lock;
}
};

} // namespace metrics
OPENTELEMETRY_END_NAMESPACE
1 change: 1 addition & 0 deletions api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(core)
add_subdirectory(plugin)
add_subdirectory(nostd)
add_subdirectory(trace)
add_subdirectory(metrics)
12 changes: 12 additions & 0 deletions api/test/metrics/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")

cc_test(
name = "meter_provider_test",
srcs = [
"meter_provider_test.cc",
],
deps = [
"//api",
"@com_google_googletest//:gtest_main",
],
)
6 changes: 6 additions & 0 deletions api/test/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
foreach(testname meter_provider_test)
add_executable(${testname} "${testname}.cc")
target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
gtest_add_tests(TARGET ${testname} TEST_PREFIX metrics. TEST_LIST ${testname})
endforeach()
41 changes: 41 additions & 0 deletions api/test/metrics/meter_provider_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/nostd/shared_ptr.h"

#include <gtest/gtest.h>

using opentelemetry::metrics::Meter;
using opentelemetry::metrics::MeterProvider;
using opentelemetry::metrics::Provider;

class TestProvider : public MeterProvider
{
opentelemetry::nostd::shared_ptr<Meter> GetMeter(
opentelemetry::nostd::string_view library_name,
opentelemetry::nostd::string_view library_version) override
{
return opentelemetry::nostd::shared_ptr<Meter>(nullptr);
}
};

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

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

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

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

0 comments on commit 04c48a1

Please sign in to comment.