forked from open-telemetry/opentelemetry-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MeterProvider api (open-telemetry#165)
- Loading branch information
1 parent
4b8e7f0
commit 04c48a1
Showing
9 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |