diff options
Diffstat (limited to 'src/jaegertracing/opentelemetry-cpp/api/test/_metrics')
5 files changed, 378 insertions, 0 deletions
diff --git a/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/BUILD b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/BUILD new file mode 100644 index 000000000..6e5b9009b --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/BUILD @@ -0,0 +1,50 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + +cc_test( + name = "meter_provider_test", + srcs = [ + "meter_provider_test.cc", + ], + tags = [ + "api", + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "noop_metrics_test", + srcs = [ + "noop_metrics_test.cc", + ], + tags = [ + "api", + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "noop_instrument_test", + srcs = [ + "noop_instrument_test.cc", + ], + linkstatic = 1, + tags = [ + "api", + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/CMakeLists.txt b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/CMakeLists.txt new file mode 100644 index 000000000..9d70c376e --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/CMakeLists.txt @@ -0,0 +1,9 @@ +foreach(testname noop_instrument_test meter_provider_test noop_metrics_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() diff --git a/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/meter_provider_test.cc b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/meter_provider_test.cc new file mode 100644 index 000000000..507081067 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/meter_provider_test.cc @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_METRICS_PREVIEW +# include <gtest/gtest.h> +# include "opentelemetry/_metrics/provider.h" +# include "opentelemetry/nostd/shared_ptr.h" + +using opentelemetry::metrics::Meter; +using opentelemetry::metrics::MeterProvider; +using opentelemetry::metrics::Provider; +namespace nostd = opentelemetry::nostd; + +class TestProvider : public MeterProvider +{ + nostd::shared_ptr<Meter> GetMeter(nostd::string_view library_name, + nostd::string_view library_version) override + { + return nostd::shared_ptr<Meter>(nullptr); + } +}; + +TEST(Provider, GetMeterProviderDefault) +{ + auto tf = Provider::GetMeterProvider(); + EXPECT_NE(nullptr, tf); +} + +TEST(Provider, SetMeterProvider) +{ + auto tf = nostd::shared_ptr<MeterProvider>(new TestProvider()); + Provider::SetMeterProvider(tf); + ASSERT_EQ(tf, Provider::GetMeterProvider()); +} + +TEST(Provider, MultipleMeterProviders) +{ + auto tf = nostd::shared_ptr<MeterProvider>(new TestProvider()); + Provider::SetMeterProvider(tf); + auto tf2 = nostd::shared_ptr<MeterProvider>(new TestProvider()); + Provider::SetMeterProvider(tf2); + + ASSERT_NE(Provider::GetMeterProvider(), tf); +} +#endif diff --git a/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_instrument_test.cc b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_instrument_test.cc new file mode 100644 index 000000000..83d8d3960 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_instrument_test.cc @@ -0,0 +1,191 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_METRICS_PREVIEW +# include <gtest/gtest.h> +# include <cstring> +# include <map> +# include <string> + +# include "opentelemetry/_metrics/noop.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +void noopIntCallback(ObserverResult<int> result) +{ + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + result.observe(1, labelkv); + result.observe(-1, labelkv); +} + +void noopDoubleCallback(ObserverResult<double> result) +{ + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + result.observe(1.5, labelkv); + result.observe(-1.5, labelkv); +} + +TEST(ValueObserver, Observe) +{ + NoopValueObserver<int> alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopValueObserver<double> beta("test", "none", "unitless", true, &noopDoubleCallback); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.5, labelkv); +} + +TEST(SumObserver, DefaultConstruction) +{ + NoopSumObserver<int> alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopSumObserver<double> beta("test", "none", "unitless", true, &noopDoubleCallback); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.5, labelkv); +} + +TEST(UpDownSumObserver, DefaultConstruction) +{ + NoopUpDownSumObserver<int> alpha("test", "none", "unitless", true, &noopIntCallback); + + NoopUpDownSumObserver<double> beta("test", "none", "unitless", true, &noopDoubleCallback); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.observe(1, labelkv); + beta.observe(1.0, labelkv); + alpha.observe(-1, labelkv); + beta.observe(-1.0, labelkv); +} + +TEST(Counter, DefaultConstruction) +{ + NoopCounter<int> alpha("test", "none", "unitless", true); + NoopCounter<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.bind(labelkv); + + auto gamma = alpha.bindNoopCounter(labelkv); + auto delta = beta.bindNoopCounter(labelkv); + + gamma->unbind(); + delta->unbind(); +} + +TEST(Counter, Add) +{ + NoopCounter<int> alpha("test", "none", "unitless", true); + NoopCounter<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.add(1, labelkv); + beta.add(1.5, labelkv); + + auto gamma = alpha.bindNoopCounter(labelkv); + auto delta = beta.bindNoopCounter(labelkv); + + gamma->add(1); + delta->add(1.5); + + gamma->unbind(); + delta->unbind(); +} + +TEST(UpDownCounter, DefaultConstruction) +{ + NoopUpDownCounter<int> alpha("test", "none", "unitless", true); + NoopUpDownCounter<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.bind(labelkv); + + auto gamma = alpha.bindNoopUpDownCounter(labelkv); + auto delta = beta.bindNoopUpDownCounter(labelkv); + + gamma->unbind(); + delta->unbind(); +} + +TEST(UpDownCounter, Add) +{ + NoopUpDownCounter<int> alpha("test", "none", "unitless", true); + NoopUpDownCounter<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.add(1, labelkv); + beta.add(1.5, labelkv); + + auto gamma = alpha.bindNoopUpDownCounter(labelkv); + auto delta = beta.bindNoopUpDownCounter(labelkv); + + gamma->add(1); + delta->add(1.0); + gamma->add(-1); + delta->add(-1.0); + + gamma->unbind(); + delta->unbind(); +} + +TEST(ValueRecorder, DefaultConstruction) +{ + NoopValueRecorder<int> alpha("test", "none", "unitless", true); + NoopValueRecorder<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.bind(labelkv); + + auto gamma = alpha.bindNoopValueRecorder(labelkv); + auto delta = beta.bindNoopValueRecorder(labelkv); + + gamma->unbind(); + delta->unbind(); +} + +TEST(ValueRecorder, Record) +{ + NoopValueRecorder<int> alpha("test", "none", "unitless", true); + NoopValueRecorder<double> beta("other", "none", "unitless", true); + + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + + alpha.record(1, labelkv); + beta.record(1.5, labelkv); + + auto gamma = alpha.bindNoopValueRecorder(labelkv); + auto delta = beta.bindNoopValueRecorder(labelkv); + + gamma->record(1); + delta->record(1.5); + + gamma->unbind(); + delta->unbind(); +} + +} // namespace metrics +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_metrics_test.cc b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_metrics_test.cc new file mode 100644 index 000000000..cc9d1f4b7 --- /dev/null +++ b/src/jaegertracing/opentelemetry-cpp/api/test/_metrics/noop_metrics_test.cc @@ -0,0 +1,83 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_METRICS_PREVIEW +# include <gtest/gtest.h> +# include "opentelemetry/_metrics/instrument.h" +# include "opentelemetry/_metrics/noop.h" +# include "opentelemetry/_metrics/observer_result.h" +# include "opentelemetry/_metrics/sync_instruments.h" + +# include <array> +# include <memory> + +OPENTELEMETRY_BEGIN_NAMESPACE + +using opentelemetry::metrics::Meter; +using opentelemetry::metrics::NoopMeter; + +void Callback(opentelemetry::metrics::ObserverResult<int> result) +{ + std::map<std::string, std::string> labels = {{"key", "value"}}; + auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels}; + result.observe(1, labelkv); +} + +TEST(NoopTest, CreateInstruments) +{ + auto m = std::unique_ptr<Meter>(new NoopMeter{}); + + // Test instrument constructors + m->NewIntCounter("Test counter", "For testing", "Unitless", true); + m->NewIntUpDownCounter("Test ud counter", "For testing", "Unitless", true); + m->NewIntValueRecorder("Test recorder", "For testing", "Unitless", true); + + m->NewIntSumObserver("Test sum obs", "For testing", "Unitless", true, &Callback); + m->NewIntUpDownSumObserver("Test udsum obs", "For testing", "Unitless", true, &Callback); + m->NewIntValueObserver("Test val obs", "For testing", "Unitless", true, &Callback); +} + +TEST(NoopMeter, RecordBatch) +{ + // Test BatchRecord with all supported types + // Create Counter and call RecordBatch for all four supported types: short, int, float, and double + + std::unique_ptr<Meter> m{std::unique_ptr<Meter>(new NoopMeter{})}; + + std::map<std::string, std::string> labels = {{"Key", "Value"}}; + auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels}; + + auto s = m->NewShortCounter("Test short counter", "For testing", "Unitless", true); + + std::array<metrics::SynchronousInstrument<short> *, 1> siarr{s.get()}; + std::array<short, 1> svarr{1}; + nostd::span<metrics::SynchronousInstrument<short> *> ssp{siarr}; + nostd::span<short> sval{svarr}; + m->RecordShortBatch(labelkv, ssp, sval); + + auto i = m->NewIntCounter("Test int counter", "For testing", "Unitless", true); + + std::array<metrics::SynchronousInstrument<int> *, 1> iiarr{i.get()}; + std::array<int, 1> ivarr{1}; + nostd::span<metrics::SynchronousInstrument<int> *> isp{iiarr}; + nostd::span<int> ival{ivarr}; + m->RecordIntBatch(labelkv, isp, ival); + + auto f = m->NewFloatCounter("Test int counter", "For testing", "Unitless", true); + + std::array<metrics::SynchronousInstrument<float> *, 1> fiarr{f.get()}; + std::array<float, 1> fvarr{1.0f}; + nostd::span<metrics::SynchronousInstrument<float> *> fsp{fiarr}; + nostd::span<float> fval{fvarr}; + m->RecordFloatBatch(labelkv, fsp, fval); + + auto d = m->NewDoubleCounter("Test int counter", "For testing", "Unitless", true); + + std::array<metrics::SynchronousInstrument<double> *, 1> diarr{d.get()}; + std::array<double, 1> dvarr{1.0f}; + nostd::span<metrics::SynchronousInstrument<double> *> dsp{diarr}; + nostd::span<double> dval{dvarr}; + m->RecordDoubleBatch(labelkv, dsp, dval); +} +OPENTELEMETRY_END_NAMESPACE +#endif |