From 76e6e9b74d6302bb61ce373683108b5af919fb97 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:03:26 -0400 Subject: [PATCH 01/41] Delete TBD --- sdk/include/opentelemetry/sdk/metrics/TBD | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 sdk/include/opentelemetry/sdk/metrics/TBD diff --git a/sdk/include/opentelemetry/sdk/metrics/TBD b/sdk/include/opentelemetry/sdk/metrics/TBD deleted file mode 100644 index e69de29bb2..0000000000 From c93227e7c2d25124e0efef32cb9e433fd75adfeb Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:04:17 -0400 Subject: [PATCH 02/41] Create min_max_sum_count_aggregator.h --- .../metrics/min_max_sum_count_aggregator.h | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h new file mode 100644 index 0000000000..982e8b60a5 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h @@ -0,0 +1,129 @@ +#pragma once + +#include "opentelemetry/version.h" +#include "opentelemetry/metrics/instrument.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" + +#include +#include +#include +#include + +namespace metrics_api = opentelemetry::metrics; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * This aggregator stores and maintains a vector of + * type T where the contents in the vector are made + * up of the minimum value recorded to this instrument, + * the maximum value, the sum of all values, and the + * count of all values. + * + * @tparam T the type of values stored in this aggregator. + */ +template +class MinMaxSumCountAggregator : public Aggregator +{ +public: + explicit MinMaxSumCountAggregator(metrics_api::BoundInstrumentKind kind) + { + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + this->kind_ = kind; + this->values_ = std::vector(4, 0); // {min, max, sum count} + this->checkpoint_ = this->values_; + } + + /** + * Receives a captured value from the instrument and applies it to the current aggregator value. + * + * @param val, the raw value used in aggregation + */ + void update(T val) override + { + this->mu_.lock(); + + if (this->values_[3] == 0 || val < this->values_[0]) // set min + this->values_[0] = val; + if (this->values_[3] == 0 || val > this->values_[1]) // set max + this->values_[1] = val; + + this->values_[2] += val; // compute sum + this->values_[3]++; // increment count + + this->mu_.unlock(); + } + + /** + * Checkpoints the current value. This function will overwrite the current checkpoint with the + * current value. + * + */ + void checkpoint() override + { + this->mu_.lock(); + this->checkpoint_ = this->values_; + // Reset the values + this->values_[0] = 0; + this->values_[1] = 0; + this->values_[2] = 0; + this->values_[3] = 0; + this->mu_.unlock(); + } + + /** + * Merges two MinMaxSumCount aggregators together + * + * @param other the aggregator to merge with this aggregator + */ + void merge(const MinMaxSumCountAggregator &other) + { + if (this->kind_ == other.kind_) + { + this->mu_.lock(); + // set min + if (other.values_[0] < this->values_[0]) + this->values_[0] = other.values_[0]; + // set max + if (other.values_[1] > this->values_[1]) + this->values_[1] = other.values_[1]; + // set sum + this->values_[2] += other.values_[2]; + // set count + this->values_[3] += other.values_[3]; + this->mu_.unlock(); + } + else + { + // Log error + return; + } + } + + /** + * Returns the checkpointed value + * + * @return the value of the checkpoint + */ + std::vector get_checkpoint() override + { + return this->checkpoint_; + } + + /** + * Returns the values currently held by the aggregator + * + * @return the values held by the aggregator + */ + std::vector get_values() override + { + return this->values_; + } + +}; +} +} +OPENTELEMETRY_END_NAMESPACE From a407da3d2ce237328146d1455b80de4efc77e035 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:04:46 -0400 Subject: [PATCH 03/41] Create gauge_aggregator.h --- .../sdk/metrics/gauge_aggregator.h | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h new file mode 100644 index 0000000000..3517e8e064 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -0,0 +1,135 @@ +#pragma once + +#include "opentelemetry/version.h" +#include "opentelemetry/metrics/instrument.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +#include "opentelemetry/core/timestamp.h" + +#include +#include +#include +#include + +namespace metrics_api = opentelemetry::metrics; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * This aggregator stores and maintains a vector of + * type T where the contents of the vector simply + * include the last value recorded to the aggregator. + * The aggregator also maintains a timestamp of when + * the last value was recorded. + * + * @tparam T the type of values stored in this aggregator. + */ +template +class GaugeAggregator : public Aggregator +{ +public: + explicit GaugeAggregator(metrics_api::BoundInstrumentKind kind) + { + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + this->kind_ = kind; + this->values_ = std::vector(1, 0); + this->checkpoint_ = this->values_; + cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + } + + /** + * Receives a captured value from the instrument and applies it to the current aggregator value. + * + * @param val, the raw value used in aggregation + */ + void update(T val) override + { + this->mu_.lock(); + this->values_[0] = val; + cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + this->mu_.unlock(); + } + + /** + * Checkpoints the current value. This function will overwrite the current checkpoint with the + * current value. + * + * @return none + */ + + void checkpoint() override + { + this->mu_.lock(); + + this->checkpoint_ = this->values_; + + // Reset the values to default + this->values_[0] = 0; + checkpoint_timestamp = cur_timestamp; + cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + + this->mu_.unlock(); + } + + /** + * Merges two Gauge aggregators together + * + * @param other the aggregator to merge with this aggregator + */ + void merge(GaugeAggregator other) + { + if (this->kind_ == other.kind_) + { + this->mu_.lock(); + this->values_[0] = other.values_[0]; + cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + this->mu_.unlock(); + } + else + { + // Log error + return; + } + } + + /** + * @return the value of the latest checkpoint + */ + std::vector get_checkpoint() override + { + return this->checkpoint_; + } + + /** + * @return the latest checkpointed timestamp + */ + core::SystemTimestamp get_checkpoint_timestamp() + { + return checkpoint_timestamp; + } + + /** + * @return the values_ vector stored in this aggregator + */ + std::vector get_values() override + { + return this->values_; + } + + /** + * @return the timestamp of when the last value recorded + */ + core::SystemTimestamp get_timestamp() + { + return cur_timestamp; + } + +private: + core::SystemTimestamp cur_timestamp; + core::SystemTimestamp checkpoint_timestamp; +}; +} +} +OPENTELEMETRY_END_NAMESPACE From 7ba2880a9762e60624461d43606bafc272a83588 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:05:09 -0400 Subject: [PATCH 04/41] Create min_max_sum_count_aggregator_test.cc --- .../min_max_sum_count_aggregator_test.cc | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 sdk/test/metrics/min_max_sum_count_aggregator_test.cc diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc new file mode 100644 index 0000000000..b35278dec0 --- /dev/null +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -0,0 +1,193 @@ +#include +#include + +#include "opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h" + +using namespace opentelemetry::sdk::metrics; + +TEST(MinMaxSumCountAggregator, Update) +{ + // This tests that the aggregator updates the maintained value correctly + // after a call to the update() function. + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto value_set = agg->get_values(); + ASSERT_EQ(value_set[0], 0); + ASSERT_EQ(value_set[1], 0); + ASSERT_EQ(value_set[2], 0); + ASSERT_EQ(value_set[3], 0); + + // 1 + 2 + 3 + ... + 10 = 55 + for (int i = 1; i <= 10; ++i) + { + agg->update(i); + } + + value_set = agg->get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count +} + +TEST(MinMaxSumCountAggregator, FirstUpdate) +{ + // This tests that the aggregator appropriately maintains the min and + // max values after a single update call. + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + agg->update(1); + auto value_set = agg->get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 1); // max + ASSERT_EQ(value_set[2], 1); // sum + ASSERT_EQ(value_set[3], 1); // count +} + +TEST(MinMaxSumCountAggregator, Checkpoint) +{ + // This test verifies that the default checkpoint is set correctly + // and that the checkpoint values update correctly after a call + // to the checkpoint() function. + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + // Verify that the default checkpoint is set correctly. + auto checkpoint_set = agg->get_checkpoint(); + ASSERT_EQ(checkpoint_set[0], 0); // min + ASSERT_EQ(checkpoint_set[1], 0); // max + ASSERT_EQ(checkpoint_set[2], 0); // sum + ASSERT_EQ(checkpoint_set[3], 0); // count + + // 1 + 2 + 3 + ... + 10 = 55 + for (int i = 1; i <= 10; ++i) + { + agg->update(i); + } + + agg->checkpoint(); + + // Verify that the checkpoint values were updated. + checkpoint_set = agg->get_checkpoint(); + ASSERT_EQ(checkpoint_set[0], 1); // min + ASSERT_EQ(checkpoint_set[1], 10); // max + ASSERT_EQ(checkpoint_set[2], 55); // sum + ASSERT_EQ(checkpoint_set[3], 10); // count + + // Verify that the current values were reset to the default state. + auto value_set = agg->get_values(); + ASSERT_EQ(value_set[0], 0); // min + ASSERT_EQ(value_set[1], 0); // max + ASSERT_EQ(value_set[2], 0); // sum + ASSERT_EQ(value_set[3], 0); // count +} + +TEST(MinMaxSumCountAggregator, Merge) +{ + // This tests that the values_ vector is updated correctly after + // two aggregators are merged together. + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + // 1 + 2 + 3 + ... + 10 = 55 + for (int i = 1; i <= 10; ++i) + { + agg1->update(i); + } + + // 1 + 2 + 3 + ... + 20 = 210 + for (int i = 1; i <= 20; ++i) + { + agg2->update(i); + } + + agg1->merge(*agg2); + + // Verify that the current values were changed by the merge. + auto value_set = agg1->get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 20); // max + ASSERT_EQ(value_set[2], 265); // sum + ASSERT_EQ(value_set[3], 30); // count +} + +TEST(MinMaxSumCountAggregator, BadMerge) +{ + // This verifies that we encounter and error when we try to merge + // two aggregators of different numeric types together. + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); + + agg1->merge(*agg2); +} + +TEST(MinMaxSumCountAggregator, Types) +{ + // This test verifies that we do not encounter any errors when + // using various numeric types. + auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + for (int i = 1; i <= 10; ++i) + { + agg_int->update(i); + agg_long->update(i); + } + + for (float i = 1.0; i <= 10.0; i += 1) + { + agg_float->update(i); + agg_double->update(i); + } + + auto value_set = agg_int->get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set2 = agg_long->get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set3 = agg_float->get_values(); + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set4 = agg_double->get_values(); + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count +} + +static void callback(MinMaxSumCountAggregator &agg) +{ + // 1 + 2 + ... + 10000 = 50005000 + for (int i = 1; i <= 10000; ++i) + { + agg.update(i); + } +} + +TEST(MinMaxSumCountAggregator, Concurrency) +{ + // This test checks that the aggregator updates appropriately + // when called in a multi-threaded context. + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + std::thread first(&callback, std::ref(*agg)); + std::thread second(&callback, std::ref(*agg)); + + first.join(); + second.join(); + + auto value_set = agg->get_values(); + ASSERT_EQ(value_set[0], 1); + ASSERT_EQ(value_set[1], 10000); + ASSERT_EQ(value_set[2], 2 * 50005000); + ASSERT_EQ(value_set[3], 2 * 10000); +} From bf29610c314ebc07c1a47f7b98759797f1a0f7c4 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:05:26 -0400 Subject: [PATCH 05/41] Create gauge_aggregator_test.cc --- sdk/test/metrics/gauge_aggregator_test.cc | 120 ++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 sdk/test/metrics/gauge_aggregator_test.cc diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc new file mode 100644 index 0000000000..9db99d8b56 --- /dev/null +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -0,0 +1,120 @@ +#include +#include + +#include "opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h" + +using namespace opentelemetry::sdk::metrics; + +TEST(GaugeAggregator, Update) +{ + // This tests that the aggregator updates the maintained value correctly + // after a call to the update() function. + auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + // Verify default value + ASSERT_EQ(agg->get_values()[0], 0); + + // Verify that the value updates correctly + agg->update(1); + ASSERT_EQ(agg->get_values()[0], 1); + + // Verify that the value continually updates correctly + for (int i = 0; i < 10; ++i) + { + agg->update(i); + } + ASSERT_EQ(agg->get_values()[0], 9); +} + +TEST(GaugeAggregator, Checkpoint) +{ + // This tests that the aggregator correctly updates the + // checkpoint_ value after a call to update() followed + // by a call to checkpoint(). + auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + // Verify default checkpoint, before updates + ASSERT_EQ(agg->get_checkpoint()[0], 0); + + agg->update(10); + agg->checkpoint(); + + // Verify that the new checkpoint contains the update value + ASSERT_EQ(agg->get_checkpoint()[0], 10); +} + +TEST(GaugeAggregator, Merge) +{ + // This tests that the values_ vector is updated correctly after + // two aggregators are merged together. + auto agg1 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + agg1->update(1); + agg2->update(2); + + agg1->merge(*agg2); + + // Verify that the aggregators merged and the value was updated correctly + ASSERT_EQ(agg1->get_values()[0], 2); +} + +TEST(GaugeAggregator, BadMerge) +{ + // This verifies that we encounter and error when we try to merge + // two aggregators of different numeric types together. + auto agg1 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); + + agg1->merge(*agg2); +} + +TEST(GaugeAggregator, Types) +{ + // This test verifies that we do not encounter any errors when + // using various numeric types. + auto agg_int = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_long = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_float = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_double = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + for (int i = 1; i <= 10; ++i) + { + agg_int->update(i); + agg_long->update(i); + } + + for (float i = 1.0; i <= 10.0; i += 1) + { + agg_float->update(i); + agg_double->update(i); + } + + ASSERT_EQ(agg_int->get_values()[0], 10); + ASSERT_EQ(agg_long->get_values()[0], 10); + ASSERT_EQ(agg_float->get_values()[0], 10.0); + ASSERT_EQ(agg_double->get_values()[0], 10.0); +} + +static void callback(GaugeAggregator &agg) +{ + for (int i = 1; i <= 10000; ++i) + { + agg.update(i); + } +} + +TEST(GaugeAggregator, Concurrency) +{ + // This test checks that the aggregator updates appropriately + // when called in a multi-threaded context. + auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + + std::thread first(&callback, std::ref(*agg)); + std::thread second(&callback, std::ref(*agg)); + + first.join(); + second.join(); + + ASSERT_EQ(agg->get_values()[0], 10000); +} From 94924903f3e0fe599bc57de5794936099a9e0f89 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:05:47 -0400 Subject: [PATCH 06/41] Create CMakeLists.txt --- sdk/test/metrics/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 sdk/test/metrics/CMakeLists.txt diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt new file mode 100644 index 0000000000..13446fb180 --- /dev/null +++ b/sdk/test/metrics/CMakeLists.txt @@ -0,0 +1,7 @@ +foreach(testname meter_test counter_aggregator_test gauge_aggregator_test + histogram_aggregator_test min_max_sum_count_aggregator_test record_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) + gtest_add_tests(TARGET ${testname} TEST_PREFIX metrics. TEST_LIST ${testname}) +endforeach() From cc614a478f06cd65fc556c19d1b640c0d3027441 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:06:31 -0400 Subject: [PATCH 07/41] Add metrics subdir --- sdk/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/test/CMakeLists.txt b/sdk/test/CMakeLists.txt index 8c8b199946..607e98bc99 100644 --- a/sdk/test/CMakeLists.txt +++ b/sdk/test/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(common) add_subdirectory(trace) +add_subdirectory(metrics) From cf0acd861a3b55e7216823eb8474d182fd1f910e Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:07:07 -0400 Subject: [PATCH 08/41] Remove absent test names --- sdk/test/metrics/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 13446fb180..e90977fac6 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,5 +1,4 @@ -foreach(testname meter_test counter_aggregator_test gauge_aggregator_test - histogram_aggregator_test min_max_sum_count_aggregator_test record_test) +foreach(testname gauge_aggregator_test min_max_sum_count_aggregator_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) From 552f5b11f961184fbbf44ebbef6b0b19e8a3ac83 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:07:38 -0400 Subject: [PATCH 09/41] Create BUILD --- sdk/test/metrics/BUILD | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sdk/test/metrics/BUILD diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD new file mode 100644 index 0000000000..879d0b37cd --- /dev/null +++ b/sdk/test/metrics/BUILD @@ -0,0 +1,21 @@ +cc_test( + name = "gauge_aggregator_test", + srcs = [ + "gauge_aggregator_test.cc", + ], + deps = [ + "//sdk/src/metrics", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "min_max_sum_count_aggregator_test", + srcs = [ + "min_max_sum_count_aggregator_test.cc", + ], + deps = [ + "//sdk/src/metrics", + "@com_google_googletest//:gtest_main", + ], +) From b866cdc5a00a876fda7dda9e1e0dcee987fffbce Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:42:35 -0400 Subject: [PATCH 10/41] Add Assert check to BadMerge test --- sdk/test/metrics/gauge_aggregator_test.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 9db99d8b56..558f528948 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -65,8 +65,14 @@ TEST(GaugeAggregator, BadMerge) // two aggregators of different numeric types together. auto agg1 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg2 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); - + + agg1->update(1); + agg2->update(2); agg1->merge(*agg2); + + // Verify that the aggregators did NOT merge + std::vector correct{1}; + ASSERT_EQ(agg1->get_values(), correct); } TEST(GaugeAggregator, Types) From 8ff0aee4463ef00169e29999f33caa33a9ffbefb Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:45:43 -0400 Subject: [PATCH 11/41] Add Assert to BadMerge test --- sdk/test/metrics/min_max_sum_count_aggregator_test.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index b35278dec0..af1dffb41c 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -114,8 +114,18 @@ TEST(MinMaxSumCountAggregator, BadMerge) // two aggregators of different numeric types together. auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); + + agg1->update(1); + agg2->update(2); agg1->merge(*agg2); + + // Verify that the values did NOT merge + auto value_set = agg1->get_values(); + ASSERT_EQ(value[0], 1); // min + ASSERT_EQ(value[0], 1); // max + ASSERT_EQ(value[0], 1); // sum + ASSERT_EQ(value[0], 1); // count } TEST(MinMaxSumCountAggregator, Types) From faf74bc2c208b937cdbb16f404a2f1a8aa419eb2 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:51:29 -0400 Subject: [PATCH 12/41] Fix typos --- sdk/test/metrics/min_max_sum_count_aggregator_test.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index af1dffb41c..d7fbaf36c4 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -122,10 +122,10 @@ TEST(MinMaxSumCountAggregator, BadMerge) // Verify that the values did NOT merge auto value_set = agg1->get_values(); - ASSERT_EQ(value[0], 1); // min - ASSERT_EQ(value[0], 1); // max - ASSERT_EQ(value[0], 1); // sum - ASSERT_EQ(value[0], 1); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 1); // max + ASSERT_EQ(value_set[2], 1); // sum + ASSERT_EQ(value_set[3], 1); // count } TEST(MinMaxSumCountAggregator, Types) @@ -135,7 +135,7 @@ TEST(MinMaxSumCountAggregator, Types) auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); for (int i = 1; i <= 10; ++i) { From c13d7af3a8591afd7c8274faf6acf98e36a85751 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 16 Jul 2020 12:52:13 -0400 Subject: [PATCH 13/41] Fix type-o :) haha, it's a pun --- sdk/test/metrics/gauge_aggregator_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 558f528948..5ee40e2733 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -82,7 +82,7 @@ TEST(GaugeAggregator, Types) auto agg_int = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg_long = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); auto agg_float = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_double = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_double = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); for (int i = 1; i <= 10; ++i) { From 43b65dec52a7f57404af5506993bf1a5849cd324 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 09:34:03 -0400 Subject: [PATCH 14/41] Incorporate PR Feedback Also changed enum in ctor to InstrumentKind to support new version of the Aggregator class in PR #178 --- .../sdk/metrics/gauge_aggregator.h | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h index 3517e8e064..7e6e0f6628 100644 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -1,14 +1,14 @@ #pragma once -#include "opentelemetry/version.h" +#include "opentelemetry/core/timestamp.h" #include "opentelemetry/metrics/instrument.h" #include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -#include "opentelemetry/core/timestamp.h" +#include "opentelemetry/version.h" +#include +#include #include #include -#include -#include namespace metrics_api = opentelemetry::metrics; @@ -30,13 +30,13 @@ template class GaugeAggregator : public Aggregator { public: - explicit GaugeAggregator(metrics_api::BoundInstrumentKind kind) + explicit GaugeAggregator(metrics_api::InstrumentKind kind) { static_assert(std::is_arithmetic::value, "Not an arithmetic type"); this->kind_ = kind; this->values_ = std::vector(1, 0); this->checkpoint_ = this->values_; - cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); } /** @@ -48,7 +48,7 @@ class GaugeAggregator : public Aggregator { this->mu_.lock(); this->values_[0] = val; - cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); this->mu_.unlock(); } @@ -67,8 +67,8 @@ class GaugeAggregator : public Aggregator // Reset the values to default this->values_[0] = 0; - checkpoint_timestamp = cur_timestamp; - cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + checkpoint_timestamp_ = current_timestamp_; + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); this->mu_.unlock(); } @@ -84,7 +84,7 @@ class GaugeAggregator : public Aggregator { this->mu_.lock(); this->values_[0] = other.values_[0]; - cur_timestamp = core::SystemTimestamp(std::chrono::system_clock::now()); + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); this->mu_.unlock(); } else @@ -107,7 +107,7 @@ class GaugeAggregator : public Aggregator */ core::SystemTimestamp get_checkpoint_timestamp() { - return checkpoint_timestamp; + return checkpoint_timestamp_; } /** @@ -123,12 +123,12 @@ class GaugeAggregator : public Aggregator */ core::SystemTimestamp get_timestamp() { - return cur_timestamp; + return current_timestamp_; } private: - core::SystemTimestamp cur_timestamp; - core::SystemTimestamp checkpoint_timestamp; + core::SystemTimestamp current_timestamp_; + core::SystemTimestamp checkpoint_timestamp_; }; } } From 4d2bcb62d7f3653df7aba3a4c89255b92e7b99e2 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 09:35:37 -0400 Subject: [PATCH 15/41] Change enum in ctor from BoundInstrumentKind to InstrumentKind --- sdk/test/metrics/gauge_aggregator_test.cc | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 5ee40e2733..552600cb1b 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -9,7 +9,7 @@ TEST(GaugeAggregator, Update) { // This tests that the aggregator updates the maintained value correctly // after a call to the update() function. - auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); // Verify default value ASSERT_EQ(agg->get_values()[0], 0); @@ -31,7 +31,7 @@ TEST(GaugeAggregator, Checkpoint) // This tests that the aggregator correctly updates the // checkpoint_ value after a call to update() followed // by a call to checkpoint(). - auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); // Verify default checkpoint, before updates ASSERT_EQ(agg->get_checkpoint()[0], 0); @@ -47,8 +47,8 @@ TEST(GaugeAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); agg1->update(1); agg2->update(2); @@ -63,9 +63,9 @@ TEST(GaugeAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); - + auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + agg1->update(1); agg2->update(2); agg1->merge(*agg2); @@ -79,10 +79,10 @@ TEST(GaugeAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_long = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_float = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_double = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_double = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); for (int i = 1; i <= 10; ++i) { @@ -114,7 +114,7 @@ TEST(GaugeAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new GaugeAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); std::thread first(&callback, std::ref(*agg)); std::thread second(&callback, std::ref(*agg)); From ce7d74b6bffb3688fd74da34ff451b9baa2685c7 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 09:36:32 -0400 Subject: [PATCH 16/41] Incorporate PR feedback --- .../metrics/min_max_sum_count_aggregator.h | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h index 982e8b60a5..0356907155 100644 --- a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h @@ -1,13 +1,13 @@ #pragma once -#include "opentelemetry/version.h" #include "opentelemetry/metrics/instrument.h" #include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +#include "opentelemetry/version.h" +#include +#include #include #include -#include -#include namespace metrics_api = opentelemetry::metrics; @@ -16,6 +16,10 @@ namespace sdk { namespace metrics { +const int MinValueIndex = 0; +const int MaxValueIndex = 1; +const int SumValueIndex = 2; +const int CountValueIndex = 3; /** * This aggregator stores and maintains a vector of * type T where the contents in the vector are made @@ -29,12 +33,13 @@ template class MinMaxSumCountAggregator : public Aggregator { public: - explicit MinMaxSumCountAggregator(metrics_api::BoundInstrumentKind kind) + explicit MinMaxSumCountAggregator(metrics_api::InstrumentKind kind) { static_assert(std::is_arithmetic::value, "Not an arithmetic type"); this->kind_ = kind; - this->values_ = std::vector(4, 0); // {min, max, sum count} + this->values_ = std::vector(4, 0); // {min, max, sum, count} this->checkpoint_ = this->values_; + this->agg_kind_ = AggregatorKind::MinMaxSumCount; } /** @@ -46,13 +51,13 @@ class MinMaxSumCountAggregator : public Aggregator { this->mu_.lock(); - if (this->values_[3] == 0 || val < this->values_[0]) // set min - this->values_[0] = val; - if (this->values_[3] == 0 || val > this->values_[1]) // set max - this->values_[1] = val; + if (this->values_[CountValueIndex] == 0 || val < this->values_[MinValueIndex]) // set min + this->values_[MinValueIndex] = val; + if (this->values_[CountValueIndex] == 0 || val > this->values_[MaxValueIndex]) // set max + this->values_[MaxValueIndex] = val; - this->values_[2] += val; // compute sum - this->values_[3]++; // increment count + this->values_[SumValueIndex] += val; // compute sum + this->values_[CountValueIndex]++; // increment count this->mu_.unlock(); } @@ -67,10 +72,10 @@ class MinMaxSumCountAggregator : public Aggregator this->mu_.lock(); this->checkpoint_ = this->values_; // Reset the values - this->values_[0] = 0; - this->values_[1] = 0; - this->values_[2] = 0; - this->values_[3] = 0; + this->values_[MinValueIndex] = 0; + this->values_[MaxValueIndex] = 0; + this->values_[SumValueIndex] = 0; + this->values_[CountValueIndex] = 0; this->mu_.unlock(); } @@ -85,15 +90,15 @@ class MinMaxSumCountAggregator : public Aggregator { this->mu_.lock(); // set min - if (other.values_[0] < this->values_[0]) - this->values_[0] = other.values_[0]; + if (other.values_[MinValueIndex] < this->values_[MinValueIndex]) + this->values_[MinValueIndex] = other.values_[MinValueIndex]; // set max - if (other.values_[1] > this->values_[1]) - this->values_[1] = other.values_[1]; + if (other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) + this->values_[MaxValueIndex] = other.values_[MaxValueIndex]; // set sum - this->values_[2] += other.values_[2]; + this->values_[SumValueIndex] += other.values_[SumValueIndex]; // set count - this->values_[3] += other.values_[3]; + this->values_[CountValueIndex] += other.values_[CountValueIndex]; this->mu_.unlock(); } else @@ -122,7 +127,6 @@ class MinMaxSumCountAggregator : public Aggregator { return this->values_; } - }; } } From 60ec09d0ebd4a3d30ab78fca6e193845577b612d Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 09:37:12 -0400 Subject: [PATCH 17/41] Change enum in ctor from BoundInstrumentKind to InstrumentKind --- .../min_max_sum_count_aggregator_test.cc | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index d7fbaf36c4..58457d5bf6 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -9,7 +9,7 @@ TEST(MinMaxSumCountAggregator, Update) { // This tests that the aggregator updates the maintained value correctly // after a call to the update() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); auto value_set = agg->get_values(); ASSERT_EQ(value_set[0], 0); ASSERT_EQ(value_set[1], 0); @@ -33,7 +33,7 @@ TEST(MinMaxSumCountAggregator, FirstUpdate) { // This tests that the aggregator appropriately maintains the min and // max values after a single update call. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); agg->update(1); auto value_set = agg->get_values(); ASSERT_EQ(value_set[0], 1); // min @@ -47,7 +47,7 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // This test verifies that the default checkpoint is set correctly // and that the checkpoint values update correctly after a call // to the checkpoint() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); // Verify that the default checkpoint is set correctly. auto checkpoint_set = agg->get_checkpoint(); @@ -83,8 +83,8 @@ TEST(MinMaxSumCountAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) @@ -112,30 +112,30 @@ TEST(MinMaxSumCountAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntValueRecorder); - + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + agg1->update(1); agg2->update(2); agg1->merge(*agg2); - + // Verify that the values did NOT merge auto value_set = agg1->get_values(); ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 1); // max - ASSERT_EQ(value_set[2], 1); // sum - ASSERT_EQ(value_set[3], 1); // count + ASSERT_EQ(value_set[0], 1); // max + ASSERT_EQ(value_set[0], 1); // sum + ASSERT_EQ(value_set[0], 1); // count } TEST(MinMaxSumCountAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); - auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); for (int i = 1; i <= 10; ++i) { @@ -187,7 +187,7 @@ TEST(MinMaxSumCountAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::BoundInstrumentKind::BoundIntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); std::thread first(&callback, std::ref(*agg)); std::thread second(&callback, std::ref(*agg)); From 57be4935dc7ec8e190a02f476f9b956b841e4232 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 09:42:21 -0400 Subject: [PATCH 18/41] Set protected var agg_kind_ in ctor This is to keep up with the update Aggregator class in PR #178 --- sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h index 7e6e0f6628..407c3167db 100644 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -36,6 +36,7 @@ class GaugeAggregator : public Aggregator this->kind_ = kind; this->values_ = std::vector(1, 0); this->checkpoint_ = this->values_; + this->agg_kind_ = AggregatorKind::Gauge; current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); } From 5034c61a3a8e872691554dab80dbb0a5ee1bd229 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 10:37:48 -0400 Subject: [PATCH 19/41] Fix bad merge test Accidental copy-paste of InstrumentKind arg meant the test was not actually conducting a bad merge but rather, a perfectly valid merge. --- sdk/test/metrics/min_max_sum_count_aggregator_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index 58457d5bf6..26f063fcec 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -113,7 +113,7 @@ TEST(MinMaxSumCountAggregator, BadMerge) // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::DoubleCounter); agg1->update(1); agg2->update(2); From b3c523972dce6b739f4aacbfe1043f9fc32be85b Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Mon, 20 Jul 2020 10:38:09 -0400 Subject: [PATCH 20/41] Fix bad merge test Accidental copy-paste of InstrumentKind arg meant the test was not actually conducting a bad merge but rather, a perfectly valid merge. --- sdk/test/metrics/gauge_aggregator_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 552600cb1b..41f604328e 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -64,7 +64,7 @@ TEST(GaugeAggregator, BadMerge) // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::DoubleCounter); agg1->update(1); agg2->update(2); From d49d229c4f90c51f9336cd4214da4359c3f79e88 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Fri, 24 Jul 2020 15:59:09 -0400 Subject: [PATCH 21/41] Update get_checkpoint_timestamp to override --- sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h index 407c3167db..bdc0860013 100644 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -40,6 +40,8 @@ class GaugeAggregator : public Aggregator current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); } + ~GaugeAggregator() = default; + /** * Receives a captured value from the instrument and applies it to the current aggregator value. * @@ -106,7 +108,7 @@ class GaugeAggregator : public Aggregator /** * @return the latest checkpointed timestamp */ - core::SystemTimestamp get_checkpoint_timestamp() + core::SystemTimestamp get_checkpoint_timestamp() override { return checkpoint_timestamp_; } From 371202857c16ce7ab9206a71f69a1bf7cc87151f Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Fri, 24 Jul 2020 15:59:35 -0400 Subject: [PATCH 22/41] Update enums to stay in line with base aggregator --- sdk/test/metrics/gauge_aggregator_test.cc | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 41f604328e..b8846029c9 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -9,7 +9,7 @@ TEST(GaugeAggregator, Update) { // This tests that the aggregator updates the maintained value correctly // after a call to the update() function. - auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); // Verify default value ASSERT_EQ(agg->get_values()[0], 0); @@ -31,7 +31,7 @@ TEST(GaugeAggregator, Checkpoint) // This tests that the aggregator correctly updates the // checkpoint_ value after a call to update() followed // by a call to checkpoint(). - auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); // Verify default checkpoint, before updates ASSERT_EQ(agg->get_checkpoint()[0], 0); @@ -47,8 +47,8 @@ TEST(GaugeAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); agg1->update(1); agg2->update(2); @@ -63,8 +63,8 @@ TEST(GaugeAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::DoubleCounter); + auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); agg1->update(1); agg2->update(2); @@ -79,10 +79,10 @@ TEST(GaugeAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_double = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_double = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) { @@ -114,7 +114,7 @@ TEST(GaugeAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); std::thread first(&callback, std::ref(*agg)); std::thread second(&callback, std::ref(*agg)); From 5817b6157e8e2b0107da45f831d78af0f4610845 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Fri, 24 Jul 2020 16:03:33 -0400 Subject: [PATCH 23/41] Update enums to stay in line with Aggregator class --- .../min_max_sum_count_aggregator_test.cc | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index 26f063fcec..d8ddaaf23f 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -9,7 +9,7 @@ TEST(MinMaxSumCountAggregator, Update) { // This tests that the aggregator updates the maintained value correctly // after a call to the update() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); auto value_set = agg->get_values(); ASSERT_EQ(value_set[0], 0); ASSERT_EQ(value_set[1], 0); @@ -33,7 +33,7 @@ TEST(MinMaxSumCountAggregator, FirstUpdate) { // This tests that the aggregator appropriately maintains the min and // max values after a single update call. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); agg->update(1); auto value_set = agg->get_values(); ASSERT_EQ(value_set[0], 1); // min @@ -47,7 +47,7 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // This test verifies that the default checkpoint is set correctly // and that the checkpoint values update correctly after a call // to the checkpoint() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); // Verify that the default checkpoint is set correctly. auto checkpoint_set = agg->get_checkpoint(); @@ -83,8 +83,8 @@ TEST(MinMaxSumCountAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) @@ -112,8 +112,8 @@ TEST(MinMaxSumCountAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::DoubleCounter); + auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); agg1->update(1); agg2->update(2); @@ -132,10 +132,10 @@ TEST(MinMaxSumCountAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); - auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) { @@ -187,7 +187,7 @@ TEST(MinMaxSumCountAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::IntCounter); + auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); std::thread first(&callback, std::ref(*agg)); std::thread second(&callback, std::ref(*agg)); From 0584db671758a746ff69ca43cdfe8b4e7597be12 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Sun, 26 Jul 2020 00:04:41 -0400 Subject: [PATCH 24/41] Add copy constructor --- .../opentelemetry/sdk/metrics/gauge_aggregator.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h index bdc0860013..c5d8933944 100644 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -42,6 +42,16 @@ class GaugeAggregator : public Aggregator ~GaugeAggregator() = default; + GaugeAggregator(const GaugeAggregator &cp) + { + this->values_ = cp.values_; + this->checkpoint_ = cp.checkpoint_; + this->kind_ = cp.kind_; + this->agg_kind_ = cp.agg_kind_; + current_timestamp_ = cp.current_timestamp_; + // use default initialized mutex as they cannot be copied + } + /** * Receives a captured value from the instrument and applies it to the current aggregator value. * From b852bb273ef4d4ce288c83d18bdfe56da407009a Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Sun, 26 Jul 2020 00:05:03 -0400 Subject: [PATCH 25/41] Add copy constructor --- .../sdk/metrics/min_max_sum_count_aggregator.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h index 0356907155..1851739b7c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h @@ -42,6 +42,17 @@ class MinMaxSumCountAggregator : public Aggregator this->agg_kind_ = AggregatorKind::MinMaxSumCount; } + ~MinMaxSumCountAggregator() = default; + + MinMaxSumCountAggregator(const MinMaxSumCountAggregator &cp) + { + this->values_ = cp.values_; + this->checkpoint_ = cp.checkpoint_; + this->kind_ = cp.kind_; + this->agg_kind_ = cp.agg_kind_; + // use default initialized mutex as they cannot be copied + } + /** * Receives a captured value from the instrument and applies it to the current aggregator value. * From fd64f9d229a8ca2ec44516754aed494f59cf4bb5 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Sun, 26 Jul 2020 16:42:02 -0400 Subject: [PATCH 26/41] Fix merge test --- sdk/test/metrics/gauge_aggregator_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index b8846029c9..059ebca6de 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -48,7 +48,7 @@ TEST(GaugeAggregator, Merge) // This tests that the values_ vector is updated correctly after // two aggregators are merged together. auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); + auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); agg1->update(1); agg2->update(2); From 2ba5e688104883b1b2864cf44aa3f3382cfb9e54 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Tue, 28 Jul 2020 19:52:57 -0400 Subject: [PATCH 27/41] Merge checkpoints as well as values --- sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h index c5d8933944..afb95d6153 100644 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h @@ -96,7 +96,10 @@ class GaugeAggregator : public Aggregator if (this->kind_ == other.kind_) { this->mu_.lock(); + // First merge values this->values_[0] = other.values_[0]; + // Now merge checkpoints + this->checkpoint_[0] = other.checkpoint_[0]; current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); this->mu_.unlock(); } From 00ede0ee33de07570236c070a1c021a57f63c2e5 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Tue, 28 Jul 2020 19:53:29 -0400 Subject: [PATCH 28/41] Merge checkpoints as well as values --- .../sdk/metrics/min_max_sum_count_aggregator.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h index 1851739b7c..e26f7894be 100644 --- a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h @@ -100,6 +100,7 @@ class MinMaxSumCountAggregator : public Aggregator if (this->kind_ == other.kind_) { this->mu_.lock(); + // First merge values // set min if (other.values_[MinValueIndex] < this->values_[MinValueIndex]) this->values_[MinValueIndex] = other.values_[MinValueIndex]; @@ -110,6 +111,18 @@ class MinMaxSumCountAggregator : public Aggregator this->values_[SumValueIndex] += other.values_[SumValueIndex]; // set count this->values_[CountValueIndex] += other.values_[CountValueIndex]; + + // Now merge checkpoints + if (other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) + this->checkpoint_[MinValueIndex] = other.checkpoint_[MinValueIndex]; + // set max + if (other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) + this->checkpoint_[MaxValueIndex] = other.checkpoint_[MaxValueIndex]; + // set sum + this->checkpoint_[SumValueIndex] += other.checkpoint_[SumValueIndex]; + // set count + this->checkpoint_[CountValueIndex] += other.checkpoint_[CountValueIndex]; + this->mu_.unlock(); } else From 98349cce7d9feb6857a4875ba796dc33ceb4b1a1 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:00:51 -0400 Subject: [PATCH 29/41] Temporarily delete because placed in wrong dir --- .../sdk/metrics/gauge_aggregator.h | 151 ------------------ 1 file changed, 151 deletions(-) delete mode 100644 sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h deleted file mode 100644 index afb95d6153..0000000000 --- a/sdk/include/opentelemetry/sdk/metrics/gauge_aggregator.h +++ /dev/null @@ -1,151 +0,0 @@ -#pragma once - -#include "opentelemetry/core/timestamp.h" -#include "opentelemetry/metrics/instrument.h" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -#include "opentelemetry/version.h" - -#include -#include -#include -#include - -namespace metrics_api = opentelemetry::metrics; - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk -{ -namespace metrics -{ -/** - * This aggregator stores and maintains a vector of - * type T where the contents of the vector simply - * include the last value recorded to the aggregator. - * The aggregator also maintains a timestamp of when - * the last value was recorded. - * - * @tparam T the type of values stored in this aggregator. - */ -template -class GaugeAggregator : public Aggregator -{ -public: - explicit GaugeAggregator(metrics_api::InstrumentKind kind) - { - static_assert(std::is_arithmetic::value, "Not an arithmetic type"); - this->kind_ = kind; - this->values_ = std::vector(1, 0); - this->checkpoint_ = this->values_; - this->agg_kind_ = AggregatorKind::Gauge; - current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); - } - - ~GaugeAggregator() = default; - - GaugeAggregator(const GaugeAggregator &cp) - { - this->values_ = cp.values_; - this->checkpoint_ = cp.checkpoint_; - this->kind_ = cp.kind_; - this->agg_kind_ = cp.agg_kind_; - current_timestamp_ = cp.current_timestamp_; - // use default initialized mutex as they cannot be copied - } - - /** - * Receives a captured value from the instrument and applies it to the current aggregator value. - * - * @param val, the raw value used in aggregation - */ - void update(T val) override - { - this->mu_.lock(); - this->values_[0] = val; - current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); - this->mu_.unlock(); - } - - /** - * Checkpoints the current value. This function will overwrite the current checkpoint with the - * current value. - * - * @return none - */ - - void checkpoint() override - { - this->mu_.lock(); - - this->checkpoint_ = this->values_; - - // Reset the values to default - this->values_[0] = 0; - checkpoint_timestamp_ = current_timestamp_; - current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); - - this->mu_.unlock(); - } - - /** - * Merges two Gauge aggregators together - * - * @param other the aggregator to merge with this aggregator - */ - void merge(GaugeAggregator other) - { - if (this->kind_ == other.kind_) - { - this->mu_.lock(); - // First merge values - this->values_[0] = other.values_[0]; - // Now merge checkpoints - this->checkpoint_[0] = other.checkpoint_[0]; - current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); - this->mu_.unlock(); - } - else - { - // Log error - return; - } - } - - /** - * @return the value of the latest checkpoint - */ - std::vector get_checkpoint() override - { - return this->checkpoint_; - } - - /** - * @return the latest checkpointed timestamp - */ - core::SystemTimestamp get_checkpoint_timestamp() override - { - return checkpoint_timestamp_; - } - - /** - * @return the values_ vector stored in this aggregator - */ - std::vector get_values() override - { - return this->values_; - } - - /** - * @return the timestamp of when the last value recorded - */ - core::SystemTimestamp get_timestamp() - { - return current_timestamp_; - } - -private: - core::SystemTimestamp current_timestamp_; - core::SystemTimestamp checkpoint_timestamp_; -}; -} -} -OPENTELEMETRY_END_NAMESPACE From 2f6b895603036ff9715c06847382573c58bcb692 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:01:08 -0400 Subject: [PATCH 30/41] Create gauge_aggregator.h --- .../sdk/metrics/aggregator/gauge_aggregator.h | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h new file mode 100644 index 0000000000..b9f68091a2 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h @@ -0,0 +1,139 @@ +#pragma once + +#include "opentelemetry/core/timestamp.h" +#include "opentelemetry/metrics/instrument.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +#include "opentelemetry/version.h" + +#include +#include +#include +#include + +namespace metrics_api = opentelemetry::metrics; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * This aggregator stores and maintains a vector of + * type T where the contents of the vector simply + * include the last value recorded to the aggregator. + * The aggregator also maintains a timestamp of when + * the last value was recorded. + * + * @tparam T the type of values stored in this aggregator. + */ +template +class GaugeAggregator : public Aggregator +{ +public: + explicit GaugeAggregator(metrics_api::InstrumentKind kind) + { + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + this->kind_ = kind; + this->values_ = std::vector(1, 0); + this->checkpoint_ = this->values_; + this->agg_kind_ = AggregatorKind::Gauge; + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); + } + + ~GaugeAggregator() = default; + + GaugeAggregator(const GaugeAggregator &cp) + { + this->values_ = cp.values_; + this->checkpoint_ = cp.checkpoint_; + this->kind_ = cp.kind_; + this->agg_kind_ = cp.agg_kind_; + current_timestamp_ = cp.current_timestamp_; + // use default initialized mutex as they cannot be copied + } + + /** + * Receives a captured value from the instrument and applies it to the current aggregator value. + * + * @param val, the raw value used in aggregation + */ + void update(T val) override + { + this->mu_.lock(); + this->values_[0] = val; + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); + this->mu_.unlock(); + } + + /** + * Checkpoints the current value. This function will overwrite the current checkpoint with the + * current value. + * + * @return none + */ + + void checkpoint() override + { + this->mu_.lock(); + + this->checkpoint_ = this->values_; + + // Reset the values to default + this->values_[0] = 0; + checkpoint_timestamp_ = current_timestamp_; + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); + + this->mu_.unlock(); + } + + /** + * Merges two Gauge aggregators together + * + * @param other the aggregator to merge with this aggregator + */ + void merge(GaugeAggregator other) + { + if (this->kind_ == other.kind_) + { + this->mu_.lock(); + // First merge values + this->values_[0] = other.values_[0]; + // Now merge checkpoints + this->checkpoint_[0] = other.checkpoint_[0]; + current_timestamp_ = core::SystemTimestamp(std::chrono::system_clock::now()); + this->mu_.unlock(); + } + else + { + // Log error + return; + } + } + + /** + * @return the value of the latest checkpoint + */ + std::vector get_checkpoint() override { return this->checkpoint_; } + + /** + * @return the latest checkpointed timestamp + */ + core::SystemTimestamp get_checkpoint_timestamp() override { return checkpoint_timestamp_; } + + /** + * @return the values_ vector stored in this aggregator + */ + std::vector get_values() override { return this->values_; } + + /** + * @return the timestamp of when the last value recorded + */ + core::SystemTimestamp get_timestamp() { return current_timestamp_; } + +private: + core::SystemTimestamp current_timestamp_; + core::SystemTimestamp checkpoint_timestamp_; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From b7496201c2f1a21bd0eb9ae4f2560148ad927176 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:05:48 -0400 Subject: [PATCH 31/41] Create min_max_sum_count_aggregator.h --- .../aggregator/min_max_sum_count_aggregator.h | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h new file mode 100644 index 0000000000..831dcef06a --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h @@ -0,0 +1,151 @@ +#pragma once + +#include "opentelemetry/metrics/instrument.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +#include "opentelemetry/version.h" + +#include +#include +#include +#include + +namespace metrics_api = opentelemetry::metrics; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +const int MinValueIndex = 0; +const int MaxValueIndex = 1; +const int SumValueIndex = 2; +const int CountValueIndex = 3; +/** + * This aggregator stores and maintains a vector of + * type T where the contents in the vector are made + * up of the minimum value recorded to this instrument, + * the maximum value, the sum of all values, and the + * count of all values. + * + * @tparam T the type of values stored in this aggregator. + */ +template +class MinMaxSumCountAggregator : public Aggregator +{ +public: + explicit MinMaxSumCountAggregator(metrics_api::InstrumentKind kind) + { + static_assert(std::is_arithmetic::value, "Not an arithmetic type"); + this->kind_ = kind; + this->values_ = std::vector(4, 0); // {min, max, sum, count} + this->checkpoint_ = this->values_; + this->agg_kind_ = AggregatorKind::MinMaxSumCount; + } + + ~MinMaxSumCountAggregator() = default; + + MinMaxSumCountAggregator(const MinMaxSumCountAggregator &cp) + { + this->values_ = cp.values_; + this->checkpoint_ = cp.checkpoint_; + this->kind_ = cp.kind_; + this->agg_kind_ = cp.agg_kind_; + // use default initialized mutex as they cannot be copied + } + + /** + * Receives a captured value from the instrument and applies it to the current aggregator value. + * + * @param val, the raw value used in aggregation + */ + void update(T val) override + { + this->mu_.lock(); + + if (this->values_[CountValueIndex] == 0 || val < this->values_[MinValueIndex]) // set min + this->values_[MinValueIndex] = val; + if (this->values_[CountValueIndex] == 0 || val > this->values_[MaxValueIndex]) // set max + this->values_[MaxValueIndex] = val; + + this->values_[SumValueIndex] += val; // compute sum + this->values_[CountValueIndex]++; // increment count + + this->mu_.unlock(); + } + + /** + * Checkpoints the current value. This function will overwrite the current checkpoint with the + * current value. + * + */ + void checkpoint() override + { + this->mu_.lock(); + this->checkpoint_ = this->values_; + // Reset the values + this->values_[MinValueIndex] = 0; + this->values_[MaxValueIndex] = 0; + this->values_[SumValueIndex] = 0; + this->values_[CountValueIndex] = 0; + this->mu_.unlock(); + } + + /** + * Merges two MinMaxSumCount aggregators together + * + * @param other the aggregator to merge with this aggregator + */ + void merge(const MinMaxSumCountAggregator &other) + { + if (this->kind_ == other.kind_) + { + this->mu_.lock(); + // First merge values + // set min + if (other.values_[MinValueIndex] < this->values_[MinValueIndex]) + this->values_[MinValueIndex] = other.values_[MinValueIndex]; + // set max + if (other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) + this->values_[MaxValueIndex] = other.values_[MaxValueIndex]; + // set sum + this->values_[SumValueIndex] += other.values_[SumValueIndex]; + // set count + this->values_[CountValueIndex] += other.values_[CountValueIndex]; + + // Now merge checkpoints + if (other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) + this->checkpoint_[MinValueIndex] = other.checkpoint_[MinValueIndex]; + // set max + if (other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) + this->checkpoint_[MaxValueIndex] = other.checkpoint_[MaxValueIndex]; + // set sum + this->checkpoint_[SumValueIndex] += other.checkpoint_[SumValueIndex]; + // set count + this->checkpoint_[CountValueIndex] += other.checkpoint_[CountValueIndex]; + + this->mu_.unlock(); + } + else + { + // Log error + return; + } + } + + /** + * Returns the checkpointed value + * + * @return the value of the checkpoint + */ + std::vector get_checkpoint() override { return this->checkpoint_; } + + /** + * Returns the values currently held by the aggregator + * + * @return the values held by the aggregator + */ + std::vector get_values() override { return this->values_; } +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE From b9fe9acb641033988161559778b0fb93a08081b1 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:05:59 -0400 Subject: [PATCH 32/41] Delete as it is in wrong dir --- .../metrics/min_max_sum_count_aggregator.h | 157 ------------------ 1 file changed, 157 deletions(-) delete mode 100644 sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h deleted file mode 100644 index e26f7894be..0000000000 --- a/sdk/include/opentelemetry/sdk/metrics/min_max_sum_count_aggregator.h +++ /dev/null @@ -1,157 +0,0 @@ -#pragma once - -#include "opentelemetry/metrics/instrument.h" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -#include "opentelemetry/version.h" - -#include -#include -#include -#include - -namespace metrics_api = opentelemetry::metrics; - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace sdk -{ -namespace metrics -{ -const int MinValueIndex = 0; -const int MaxValueIndex = 1; -const int SumValueIndex = 2; -const int CountValueIndex = 3; -/** - * This aggregator stores and maintains a vector of - * type T where the contents in the vector are made - * up of the minimum value recorded to this instrument, - * the maximum value, the sum of all values, and the - * count of all values. - * - * @tparam T the type of values stored in this aggregator. - */ -template -class MinMaxSumCountAggregator : public Aggregator -{ -public: - explicit MinMaxSumCountAggregator(metrics_api::InstrumentKind kind) - { - static_assert(std::is_arithmetic::value, "Not an arithmetic type"); - this->kind_ = kind; - this->values_ = std::vector(4, 0); // {min, max, sum, count} - this->checkpoint_ = this->values_; - this->agg_kind_ = AggregatorKind::MinMaxSumCount; - } - - ~MinMaxSumCountAggregator() = default; - - MinMaxSumCountAggregator(const MinMaxSumCountAggregator &cp) - { - this->values_ = cp.values_; - this->checkpoint_ = cp.checkpoint_; - this->kind_ = cp.kind_; - this->agg_kind_ = cp.agg_kind_; - // use default initialized mutex as they cannot be copied - } - - /** - * Receives a captured value from the instrument and applies it to the current aggregator value. - * - * @param val, the raw value used in aggregation - */ - void update(T val) override - { - this->mu_.lock(); - - if (this->values_[CountValueIndex] == 0 || val < this->values_[MinValueIndex]) // set min - this->values_[MinValueIndex] = val; - if (this->values_[CountValueIndex] == 0 || val > this->values_[MaxValueIndex]) // set max - this->values_[MaxValueIndex] = val; - - this->values_[SumValueIndex] += val; // compute sum - this->values_[CountValueIndex]++; // increment count - - this->mu_.unlock(); - } - - /** - * Checkpoints the current value. This function will overwrite the current checkpoint with the - * current value. - * - */ - void checkpoint() override - { - this->mu_.lock(); - this->checkpoint_ = this->values_; - // Reset the values - this->values_[MinValueIndex] = 0; - this->values_[MaxValueIndex] = 0; - this->values_[SumValueIndex] = 0; - this->values_[CountValueIndex] = 0; - this->mu_.unlock(); - } - - /** - * Merges two MinMaxSumCount aggregators together - * - * @param other the aggregator to merge with this aggregator - */ - void merge(const MinMaxSumCountAggregator &other) - { - if (this->kind_ == other.kind_) - { - this->mu_.lock(); - // First merge values - // set min - if (other.values_[MinValueIndex] < this->values_[MinValueIndex]) - this->values_[MinValueIndex] = other.values_[MinValueIndex]; - // set max - if (other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) - this->values_[MaxValueIndex] = other.values_[MaxValueIndex]; - // set sum - this->values_[SumValueIndex] += other.values_[SumValueIndex]; - // set count - this->values_[CountValueIndex] += other.values_[CountValueIndex]; - - // Now merge checkpoints - if (other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) - this->checkpoint_[MinValueIndex] = other.checkpoint_[MinValueIndex]; - // set max - if (other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) - this->checkpoint_[MaxValueIndex] = other.checkpoint_[MaxValueIndex]; - // set sum - this->checkpoint_[SumValueIndex] += other.checkpoint_[SumValueIndex]; - // set count - this->checkpoint_[CountValueIndex] += other.checkpoint_[CountValueIndex]; - - this->mu_.unlock(); - } - else - { - // Log error - return; - } - } - - /** - * Returns the checkpointed value - * - * @return the value of the checkpoint - */ - std::vector get_checkpoint() override - { - return this->checkpoint_; - } - - /** - * Returns the values currently held by the aggregator - * - * @return the values held by the aggregator - */ - std::vector get_values() override - { - return this->values_; - } -}; -} -} -OPENTELEMETRY_END_NAMESPACE From 739773e9eb48ac795d0b73f2ba42953363b8e23b Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:06:59 -0400 Subject: [PATCH 33/41] Format --- sdk/test/metrics/gauge_aggregator_test.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 059ebca6de..79925ef713 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -79,9 +79,9 @@ TEST(GaugeAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); auto agg_double = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) From c781029995c862ef490afd6f15dea290eb986d93 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:07:28 -0400 Subject: [PATCH 34/41] Format --- .../min_max_sum_count_aggregator_test.cc | 100 +++++++++--------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index d8ddaaf23f..70c5c53458 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -23,10 +23,10 @@ TEST(MinMaxSumCountAggregator, Update) } value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count } TEST(MinMaxSumCountAggregator, FirstUpdate) @@ -36,10 +36,10 @@ TEST(MinMaxSumCountAggregator, FirstUpdate) auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); agg->update(1); auto value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 1); // max - ASSERT_EQ(value_set[2], 1); // sum - ASSERT_EQ(value_set[3], 1); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 1); // max + ASSERT_EQ(value_set[2], 1); // sum + ASSERT_EQ(value_set[3], 1); // count } TEST(MinMaxSumCountAggregator, Checkpoint) @@ -51,10 +51,10 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // Verify that the default checkpoint is set correctly. auto checkpoint_set = agg->get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 0); // min - ASSERT_EQ(checkpoint_set[1], 0); // max - ASSERT_EQ(checkpoint_set[2], 0); // sum - ASSERT_EQ(checkpoint_set[3], 0); // count + ASSERT_EQ(checkpoint_set[0], 0); // min + ASSERT_EQ(checkpoint_set[1], 0); // max + ASSERT_EQ(checkpoint_set[2], 0); // sum + ASSERT_EQ(checkpoint_set[3], 0); // count // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) @@ -66,17 +66,17 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // Verify that the checkpoint values were updated. checkpoint_set = agg->get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 1); // min - ASSERT_EQ(checkpoint_set[1], 10); // max - ASSERT_EQ(checkpoint_set[2], 55); // sum - ASSERT_EQ(checkpoint_set[3], 10); // count + ASSERT_EQ(checkpoint_set[0], 1); // min + ASSERT_EQ(checkpoint_set[1], 10); // max + ASSERT_EQ(checkpoint_set[2], 55); // sum + ASSERT_EQ(checkpoint_set[3], 10); // count // Verify that the current values were reset to the default state. auto value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 0); // min - ASSERT_EQ(value_set[1], 0); // max - ASSERT_EQ(value_set[2], 0); // sum - ASSERT_EQ(value_set[3], 0); // count + ASSERT_EQ(value_set[0], 0); // min + ASSERT_EQ(value_set[1], 0); // max + ASSERT_EQ(value_set[2], 0); // sum + ASSERT_EQ(value_set[3], 0); // count } TEST(MinMaxSumCountAggregator, Merge) @@ -102,10 +102,10 @@ TEST(MinMaxSumCountAggregator, Merge) // Verify that the current values were changed by the merge. auto value_set = agg1->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 20); // max - ASSERT_EQ(value_set[2], 265); // sum - ASSERT_EQ(value_set[3], 30); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 20); // max + ASSERT_EQ(value_set[2], 265); // sum + ASSERT_EQ(value_set[3], 30); // count } TEST(MinMaxSumCountAggregator, BadMerge) @@ -113,7 +113,8 @@ TEST(MinMaxSumCountAggregator, BadMerge) // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); + auto agg2 = + new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); agg1->update(1); agg2->update(2); @@ -122,10 +123,10 @@ TEST(MinMaxSumCountAggregator, BadMerge) // Verify that the values did NOT merge auto value_set = agg1->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[0], 1); // max - ASSERT_EQ(value_set[0], 1); // sum - ASSERT_EQ(value_set[0], 1); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[0], 1); // max + ASSERT_EQ(value_set[0], 1); // sum + ASSERT_EQ(value_set[0], 1); // count } TEST(MinMaxSumCountAggregator, Types) @@ -133,9 +134,12 @@ TEST(MinMaxSumCountAggregator, Types) // This test verifies that we do not encounter any errors when // using various numeric types. auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_long = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_float = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_double = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_long = + new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_float = + new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + auto agg_double = + new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) { @@ -150,28 +154,28 @@ TEST(MinMaxSumCountAggregator, Types) } auto value_set = agg_int->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set2 = agg_long->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set3 = agg_float->get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set4 = agg_double->get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count } static void callback(MinMaxSumCountAggregator &agg) From dca67160be462b08b7558c2ee63a7c578e7ece8d Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 16:08:31 -0400 Subject: [PATCH 35/41] Format --- sdk/test/metrics/CMakeLists.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index c0bcb806a7..3d906b6bcb 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,6 +1,7 @@ -foreach(testname meter_provider_sdk_test gauge_aggregator_test min_max_sum_count_aggregator_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) - gtest_add_tests(TARGET ${testname} TEST_PREFIX metrics. TEST_LIST ${testname}) +foreach(testname meter_provider_sdk_test gauge_aggregator_test + min_max_sum_count_aggregator_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) + gtest_add_tests(TARGET ${testname} TEST_PREFIX metrics. TEST_LIST ${testname}) endforeach() From 9ea5d4f8441913f1d307d581651d84c3c53ec2cc Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 18:05:06 -0400 Subject: [PATCH 36/41] Fix Merge function to set min and max correctly Needed to add check to see if the count is equal to 0 and, if so, set the min and max to the first value seen. --- .../sdk/metrics/aggregator/min_max_sum_count_aggregator.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h index 831dcef06a..e716e29d94 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h @@ -102,10 +102,10 @@ class MinMaxSumCountAggregator : public Aggregator this->mu_.lock(); // First merge values // set min - if (other.values_[MinValueIndex] < this->values_[MinValueIndex]) + if (this->values_[CountValueIndex] == 0 || other.values_[MinValueIndex] < this->values_[MinValueIndex]) this->values_[MinValueIndex] = other.values_[MinValueIndex]; // set max - if (other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) + if (this->values_[CountValueIndex] == 0 || other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) this->values_[MaxValueIndex] = other.values_[MaxValueIndex]; // set sum this->values_[SumValueIndex] += other.values_[SumValueIndex]; @@ -113,10 +113,10 @@ class MinMaxSumCountAggregator : public Aggregator this->values_[CountValueIndex] += other.values_[CountValueIndex]; // Now merge checkpoints - if (other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) + if (this->checkpoint_[CountValueIndex] == 0 || other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) this->checkpoint_[MinValueIndex] = other.checkpoint_[MinValueIndex]; // set max - if (other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) + if (this->checkpoint_[CountValueIndex] == 0 || other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) this->checkpoint_[MaxValueIndex] = other.checkpoint_[MaxValueIndex]; // set sum this->checkpoint_[SumValueIndex] += other.checkpoint_[SumValueIndex]; From fc78872569a147ac6c645afbf7f6a0d2ecbe71df Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Wed, 29 Jul 2020 18:09:30 -0400 Subject: [PATCH 37/41] Fix formatting --- .../aggregator/min_max_sum_count_aggregator.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h index e716e29d94..da6d3a446f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h @@ -102,10 +102,12 @@ class MinMaxSumCountAggregator : public Aggregator this->mu_.lock(); // First merge values // set min - if (this->values_[CountValueIndex] == 0 || other.values_[MinValueIndex] < this->values_[MinValueIndex]) + if (this->values_[CountValueIndex] == 0 || + other.values_[MinValueIndex] < this->values_[MinValueIndex]) this->values_[MinValueIndex] = other.values_[MinValueIndex]; // set max - if (this->values_[CountValueIndex] == 0 || other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) + if (this->values_[CountValueIndex] == 0 || + other.values_[MaxValueIndex] > this->values_[MaxValueIndex]) this->values_[MaxValueIndex] = other.values_[MaxValueIndex]; // set sum this->values_[SumValueIndex] += other.values_[SumValueIndex]; @@ -113,10 +115,12 @@ class MinMaxSumCountAggregator : public Aggregator this->values_[CountValueIndex] += other.values_[CountValueIndex]; // Now merge checkpoints - if (this->checkpoint_[CountValueIndex] == 0 || other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) + if (this->checkpoint_[CountValueIndex] == 0 || + other.checkpoint_[MinValueIndex] < this->checkpoint_[MinValueIndex]) this->checkpoint_[MinValueIndex] = other.checkpoint_[MinValueIndex]; // set max - if (this->checkpoint_[CountValueIndex] == 0 || other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) + if (this->checkpoint_[CountValueIndex] == 0 || + other.checkpoint_[MaxValueIndex] > this->checkpoint_[MaxValueIndex]) this->checkpoint_[MaxValueIndex] = other.checkpoint_[MaxValueIndex]; // set sum this->checkpoint_[SumValueIndex] += other.checkpoint_[SumValueIndex]; From 185bb3c84f68a2461b3bc9f46c13eeeae329b117 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 30 Jul 2020 09:29:20 -0400 Subject: [PATCH 38/41] Remove extraneuous include --- .../opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h index b9f68091a2..7925f5e205 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h @@ -7,7 +7,6 @@ #include #include -#include #include namespace metrics_api = opentelemetry::metrics; From 8a481753a90ce33607864d28c385b4dd8a168db4 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 30 Jul 2020 09:32:52 -0400 Subject: [PATCH 39/41] Remove include --- .../sdk/metrics/aggregator/min_max_sum_count_aggregator.h | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h index da6d3a446f..b47cf0df06 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/min_max_sum_count_aggregator.h @@ -6,7 +6,6 @@ #include #include -#include #include namespace metrics_api = opentelemetry::metrics; From 098c00a452c0c2392ff7242f47578c0861e7171a Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 30 Jul 2020 09:55:45 -0400 Subject: [PATCH 40/41] Allocate aggs on the stack --- sdk/test/metrics/gauge_aggregator_test.cc | 69 +++---- .../min_max_sum_count_aggregator_test.cc | 182 +++++++++--------- 2 files changed, 124 insertions(+), 127 deletions(-) diff --git a/sdk/test/metrics/gauge_aggregator_test.cc b/sdk/test/metrics/gauge_aggregator_test.cc index 79925ef713..7370d78f70 100644 --- a/sdk/test/metrics/gauge_aggregator_test.cc +++ b/sdk/test/metrics/gauge_aggregator_test.cc @@ -24,6 +24,7 @@ TEST(GaugeAggregator, Update) agg->update(i); } ASSERT_EQ(agg->get_values()[0], 9); + delete agg; } TEST(GaugeAggregator, Checkpoint) @@ -31,75 +32,75 @@ TEST(GaugeAggregator, Checkpoint) // This tests that the aggregator correctly updates the // checkpoint_ value after a call to update() followed // by a call to checkpoint(). - auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); // Verify default checkpoint, before updates - ASSERT_EQ(agg->get_checkpoint()[0], 0); + ASSERT_EQ(agg.get_checkpoint()[0], 0); - agg->update(10); - agg->checkpoint(); + agg.update(10); + agg.checkpoint(); // Verify that the new checkpoint contains the update value - ASSERT_EQ(agg->get_checkpoint()[0], 10); + ASSERT_EQ(agg.get_checkpoint()[0], 10); } TEST(GaugeAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg1(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg2(opentelemetry::metrics::InstrumentKind::Counter); - agg1->update(1); - agg2->update(2); + agg1.update(1); + agg2.update(2); - agg1->merge(*agg2); + agg1.merge(agg2); // Verify that the aggregators merged and the value was updated correctly - ASSERT_EQ(agg1->get_values()[0], 2); + ASSERT_EQ(agg1.get_values()[0], 2); } TEST(GaugeAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); + GaugeAggregator agg1(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg2(opentelemetry::metrics::InstrumentKind::ValueRecorder); - agg1->update(1); - agg2->update(2); - agg1->merge(*agg2); + agg1.update(1); + agg2.update(2); + agg1.merge(agg2); // Verify that the aggregators did NOT merge std::vector correct{1}; - ASSERT_EQ(agg1->get_values(), correct); + ASSERT_EQ(agg1.get_values(), correct); } TEST(GaugeAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_long = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_float = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_double = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg_int(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg_long(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg_float(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg_double(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) { - agg_int->update(i); - agg_long->update(i); + agg_int.update(i); + agg_long.update(i); } for (float i = 1.0; i <= 10.0; i += 1) { - agg_float->update(i); - agg_double->update(i); + agg_float.update(i); + agg_double.update(i); } - ASSERT_EQ(agg_int->get_values()[0], 10); - ASSERT_EQ(agg_long->get_values()[0], 10); - ASSERT_EQ(agg_float->get_values()[0], 10.0); - ASSERT_EQ(agg_double->get_values()[0], 10.0); + ASSERT_EQ(agg_int.get_values()[0], 10); + ASSERT_EQ(agg_long.get_values()[0], 10); + ASSERT_EQ(agg_float.get_values()[0], 10.0); + ASSERT_EQ(agg_double.get_values()[0], 10.0); } static void callback(GaugeAggregator &agg) @@ -114,13 +115,13 @@ TEST(GaugeAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new GaugeAggregator(opentelemetry::metrics::InstrumentKind::Counter); + GaugeAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); - std::thread first(&callback, std::ref(*agg)); - std::thread second(&callback, std::ref(*agg)); + std::thread first(&callback, std::ref(agg)); + std::thread second(&callback, std::ref(agg)); first.join(); second.join(); - ASSERT_EQ(agg->get_values()[0], 10000); -} + ASSERT_EQ(agg.get_values()[0], 10000); +} \ No newline at end of file diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index 70c5c53458..1b18b1965c 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -9,8 +9,8 @@ TEST(MinMaxSumCountAggregator, Update) { // This tests that the aggregator updates the maintained value correctly // after a call to the update() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto value_set = agg->get_values(); + MinMaxSumCountAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); + auto value_set = agg.get_values(); ASSERT_EQ(value_set[0], 0); ASSERT_EQ(value_set[1], 0); ASSERT_EQ(value_set[2], 0); @@ -19,27 +19,27 @@ TEST(MinMaxSumCountAggregator, Update) // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) { - agg->update(i); + agg.update(i); } - value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + value_set = agg.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count } TEST(MinMaxSumCountAggregator, FirstUpdate) { // This tests that the aggregator appropriately maintains the min and // max values after a single update call. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - agg->update(1); - auto value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 1); // max - ASSERT_EQ(value_set[2], 1); // sum - ASSERT_EQ(value_set[3], 1); // count + MinMaxSumCountAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); + agg.update(1); + auto value_set = agg.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 1); // max + ASSERT_EQ(value_set[2], 1); // sum + ASSERT_EQ(value_set[3], 1); // count } TEST(MinMaxSumCountAggregator, Checkpoint) @@ -47,135 +47,131 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // This test verifies that the default checkpoint is set correctly // and that the checkpoint values update correctly after a call // to the checkpoint() function. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); // Verify that the default checkpoint is set correctly. - auto checkpoint_set = agg->get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 0); // min - ASSERT_EQ(checkpoint_set[1], 0); // max - ASSERT_EQ(checkpoint_set[2], 0); // sum - ASSERT_EQ(checkpoint_set[3], 0); // count + auto checkpoint_set = agg.get_checkpoint(); + ASSERT_EQ(checkpoint_set[0], 0); // min + ASSERT_EQ(checkpoint_set[1], 0); // max + ASSERT_EQ(checkpoint_set[2], 0); // sum + ASSERT_EQ(checkpoint_set[3], 0); // count // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) { - agg->update(i); + agg.update(i); } - agg->checkpoint(); + agg.checkpoint(); // Verify that the checkpoint values were updated. - checkpoint_set = agg->get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 1); // min - ASSERT_EQ(checkpoint_set[1], 10); // max - ASSERT_EQ(checkpoint_set[2], 55); // sum - ASSERT_EQ(checkpoint_set[3], 10); // count + checkpoint_set = agg.get_checkpoint(); + ASSERT_EQ(checkpoint_set[0], 1); // min + ASSERT_EQ(checkpoint_set[1], 10); // max + ASSERT_EQ(checkpoint_set[2], 55); // sum + ASSERT_EQ(checkpoint_set[3], 10); // count // Verify that the current values were reset to the default state. - auto value_set = agg->get_values(); - ASSERT_EQ(value_set[0], 0); // min - ASSERT_EQ(value_set[1], 0); // max - ASSERT_EQ(value_set[2], 0); // sum - ASSERT_EQ(value_set[3], 0); // count + auto value_set = agg.get_values(); + ASSERT_EQ(value_set[0], 0); // min + ASSERT_EQ(value_set[1], 0); // max + ASSERT_EQ(value_set[2], 0); // sum + ASSERT_EQ(value_set[3], 0); // count } TEST(MinMaxSumCountAggregator, Merge) { // This tests that the values_ vector is updated correctly after // two aggregators are merged together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg1(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg2(opentelemetry::metrics::InstrumentKind::Counter); // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) { - agg1->update(i); + agg1.update(i); } // 1 + 2 + 3 + ... + 20 = 210 for (int i = 1; i <= 20; ++i) { - agg2->update(i); + agg2.update(i); } - agg1->merge(*agg2); + agg1.merge(agg2); // Verify that the current values were changed by the merge. - auto value_set = agg1->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 20); // max - ASSERT_EQ(value_set[2], 265); // sum - ASSERT_EQ(value_set[3], 30); // count + auto value_set = agg1.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 20); // max + ASSERT_EQ(value_set[2], 265); // sum + ASSERT_EQ(value_set[3], 30); // count } TEST(MinMaxSumCountAggregator, BadMerge) { // This verifies that we encounter and error when we try to merge // two aggregators of different numeric types together. - auto agg1 = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg2 = - new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::ValueRecorder); + MinMaxSumCountAggregator agg1(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg2(opentelemetry::metrics::InstrumentKind::ValueRecorder); - agg1->update(1); - agg2->update(2); + agg1.update(1); + agg2.update(2); - agg1->merge(*agg2); + agg1.merge(agg2); // Verify that the values did NOT merge - auto value_set = agg1->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[0], 1); // max - ASSERT_EQ(value_set[0], 1); // sum - ASSERT_EQ(value_set[0], 1); // count + auto value_set = agg1.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[0], 1); // max + ASSERT_EQ(value_set[0], 1); // sum + ASSERT_EQ(value_set[0], 1); // count } TEST(MinMaxSumCountAggregator, Types) { // This test verifies that we do not encounter any errors when // using various numeric types. - auto agg_int = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_long = - new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_float = - new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); - auto agg_double = - new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg_int(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg_long(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg_float(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg_double(opentelemetry::metrics::InstrumentKind::Counter); for (int i = 1; i <= 10; ++i) { - agg_int->update(i); - agg_long->update(i); + agg_int.update(i); + agg_long.update(i); } for (float i = 1.0; i <= 10.0; i += 1) { - agg_float->update(i); - agg_double->update(i); + agg_float.update(i); + agg_double.update(i); } - auto value_set = agg_int->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count - - auto value_set2 = agg_long->get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count - - auto value_set3 = agg_float->get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count - - auto value_set4 = agg_double->get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count + auto value_set = agg_int.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set2 = agg_long.get_values(); + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set3 = agg_float.get_values(); + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count + + auto value_set4 = agg_double.get_values(); + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count } static void callback(MinMaxSumCountAggregator &agg) @@ -191,17 +187,17 @@ TEST(MinMaxSumCountAggregator, Concurrency) { // This test checks that the aggregator updates appropriately // when called in a multi-threaded context. - auto agg = new MinMaxSumCountAggregator(opentelemetry::metrics::InstrumentKind::Counter); + MinMaxSumCountAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); - std::thread first(&callback, std::ref(*agg)); - std::thread second(&callback, std::ref(*agg)); + std::thread first(&callback, std::ref(agg)); + std::thread second(&callback, std::ref(agg)); first.join(); second.join(); - auto value_set = agg->get_values(); + auto value_set = agg.get_values(); ASSERT_EQ(value_set[0], 1); ASSERT_EQ(value_set[1], 10000); ASSERT_EQ(value_set[2], 2 * 50005000); ASSERT_EQ(value_set[3], 2 * 10000); -} +} \ No newline at end of file From 9d88ecd88f0ee20cdc151b5805414f775f6f7900 Mon Sep 17 00:00:00 2001 From: Brandon Kimberly Date: Thu, 30 Jul 2020 10:01:21 -0400 Subject: [PATCH 41/41] Format --- .../min_max_sum_count_aggregator_test.cc | 88 +++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc index 1b18b1965c..21d06ddcea 100644 --- a/sdk/test/metrics/min_max_sum_count_aggregator_test.cc +++ b/sdk/test/metrics/min_max_sum_count_aggregator_test.cc @@ -23,10 +23,10 @@ TEST(MinMaxSumCountAggregator, Update) } value_set = agg.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count } TEST(MinMaxSumCountAggregator, FirstUpdate) @@ -36,10 +36,10 @@ TEST(MinMaxSumCountAggregator, FirstUpdate) MinMaxSumCountAggregator agg(opentelemetry::metrics::InstrumentKind::Counter); agg.update(1); auto value_set = agg.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 1); // max - ASSERT_EQ(value_set[2], 1); // sum - ASSERT_EQ(value_set[3], 1); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 1); // max + ASSERT_EQ(value_set[2], 1); // sum + ASSERT_EQ(value_set[3], 1); // count } TEST(MinMaxSumCountAggregator, Checkpoint) @@ -51,10 +51,10 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // Verify that the default checkpoint is set correctly. auto checkpoint_set = agg.get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 0); // min - ASSERT_EQ(checkpoint_set[1], 0); // max - ASSERT_EQ(checkpoint_set[2], 0); // sum - ASSERT_EQ(checkpoint_set[3], 0); // count + ASSERT_EQ(checkpoint_set[0], 0); // min + ASSERT_EQ(checkpoint_set[1], 0); // max + ASSERT_EQ(checkpoint_set[2], 0); // sum + ASSERT_EQ(checkpoint_set[3], 0); // count // 1 + 2 + 3 + ... + 10 = 55 for (int i = 1; i <= 10; ++i) @@ -66,17 +66,17 @@ TEST(MinMaxSumCountAggregator, Checkpoint) // Verify that the checkpoint values were updated. checkpoint_set = agg.get_checkpoint(); - ASSERT_EQ(checkpoint_set[0], 1); // min - ASSERT_EQ(checkpoint_set[1], 10); // max - ASSERT_EQ(checkpoint_set[2], 55); // sum - ASSERT_EQ(checkpoint_set[3], 10); // count + ASSERT_EQ(checkpoint_set[0], 1); // min + ASSERT_EQ(checkpoint_set[1], 10); // max + ASSERT_EQ(checkpoint_set[2], 55); // sum + ASSERT_EQ(checkpoint_set[3], 10); // count // Verify that the current values were reset to the default state. auto value_set = agg.get_values(); - ASSERT_EQ(value_set[0], 0); // min - ASSERT_EQ(value_set[1], 0); // max - ASSERT_EQ(value_set[2], 0); // sum - ASSERT_EQ(value_set[3], 0); // count + ASSERT_EQ(value_set[0], 0); // min + ASSERT_EQ(value_set[1], 0); // max + ASSERT_EQ(value_set[2], 0); // sum + ASSERT_EQ(value_set[3], 0); // count } TEST(MinMaxSumCountAggregator, Merge) @@ -102,10 +102,10 @@ TEST(MinMaxSumCountAggregator, Merge) // Verify that the current values were changed by the merge. auto value_set = agg1.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 20); // max - ASSERT_EQ(value_set[2], 265); // sum - ASSERT_EQ(value_set[3], 30); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 20); // max + ASSERT_EQ(value_set[2], 265); // sum + ASSERT_EQ(value_set[3], 30); // count } TEST(MinMaxSumCountAggregator, BadMerge) @@ -122,10 +122,10 @@ TEST(MinMaxSumCountAggregator, BadMerge) // Verify that the values did NOT merge auto value_set = agg1.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[0], 1); // max - ASSERT_EQ(value_set[0], 1); // sum - ASSERT_EQ(value_set[0], 1); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[0], 1); // max + ASSERT_EQ(value_set[0], 1); // sum + ASSERT_EQ(value_set[0], 1); // count } TEST(MinMaxSumCountAggregator, Types) @@ -150,28 +150,28 @@ TEST(MinMaxSumCountAggregator, Types) } auto value_set = agg_int.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set2 = agg_long.get_values(); - ASSERT_EQ(value_set[0], 1); // min - ASSERT_EQ(value_set[1], 10); // max - ASSERT_EQ(value_set[2], 55); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1); // min + ASSERT_EQ(value_set[1], 10); // max + ASSERT_EQ(value_set[2], 55); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set3 = agg_float.get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count auto value_set4 = agg_double.get_values(); - ASSERT_EQ(value_set[0], 1.0); // min - ASSERT_EQ(value_set[1], 10.0); // max - ASSERT_EQ(value_set[2], 55.0); // sum - ASSERT_EQ(value_set[3], 10); // count + ASSERT_EQ(value_set[0], 1.0); // min + ASSERT_EQ(value_set[1], 10.0); // max + ASSERT_EQ(value_set[2], 55.0); // sum + ASSERT_EQ(value_set[3], 10); // count } static void callback(MinMaxSumCountAggregator &agg)