Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
76e6e9b
Delete TBD
Brandon-Kimberly Jul 16, 2020
c93227e
Create min_max_sum_count_aggregator.h
Brandon-Kimberly Jul 16, 2020
a407da3
Create gauge_aggregator.h
Brandon-Kimberly Jul 16, 2020
7ba2880
Create min_max_sum_count_aggregator_test.cc
Brandon-Kimberly Jul 16, 2020
bf29610
Create gauge_aggregator_test.cc
Brandon-Kimberly Jul 16, 2020
9492490
Create CMakeLists.txt
Brandon-Kimberly Jul 16, 2020
cc614a4
Add metrics subdir
Brandon-Kimberly Jul 16, 2020
cf0acd8
Remove absent test names
Brandon-Kimberly Jul 16, 2020
552f5b1
Create BUILD
Brandon-Kimberly Jul 16, 2020
b866cdc
Add Assert check to BadMerge test
Brandon-Kimberly Jul 16, 2020
8ff0aee
Add Assert to BadMerge test
Brandon-Kimberly Jul 16, 2020
faf74bc
Fix typos
Brandon-Kimberly Jul 16, 2020
c13d7af
Fix type-o
Brandon-Kimberly Jul 16, 2020
43b65de
Incorporate PR Feedback
Brandon-Kimberly Jul 20, 2020
4d2bcb6
Change enum in ctor from BoundInstrumentKind to InstrumentKind
Brandon-Kimberly Jul 20, 2020
ce7d74b
Incorporate PR feedback
Brandon-Kimberly Jul 20, 2020
60ec09d
Change enum in ctor from BoundInstrumentKind to InstrumentKind
Brandon-Kimberly Jul 20, 2020
57be493
Set protected var agg_kind_ in ctor
Brandon-Kimberly Jul 20, 2020
5034c61
Fix bad merge test
Brandon-Kimberly Jul 20, 2020
b3c5239
Fix bad merge test
Brandon-Kimberly Jul 20, 2020
d49d229
Update get_checkpoint_timestamp to override
Brandon-Kimberly Jul 24, 2020
3712028
Update enums to stay in line with base aggregator
Brandon-Kimberly Jul 24, 2020
5817b61
Update enums to stay in line with Aggregator class
Brandon-Kimberly Jul 24, 2020
0584db6
Add copy constructor
Brandon-Kimberly Jul 26, 2020
b852bb2
Add copy constructor
Brandon-Kimberly Jul 26, 2020
fd64f9d
Fix merge test
Brandon-Kimberly Jul 26, 2020
2ba5e68
Merge checkpoints as well as values
Brandon-Kimberly Jul 28, 2020
00ede0e
Merge checkpoints as well as values
Brandon-Kimberly Jul 28, 2020
48caac5
Merge branch 'master' into mmsc-gauge-agg
Brandon-Kimberly Jul 29, 2020
98349cc
Temporarily delete because placed in wrong dir
Brandon-Kimberly Jul 29, 2020
2f6b895
Create gauge_aggregator.h
Brandon-Kimberly Jul 29, 2020
b749620
Create min_max_sum_count_aggregator.h
Brandon-Kimberly Jul 29, 2020
b9fe9ac
Delete as it is in wrong dir
Brandon-Kimberly Jul 29, 2020
739773e
Format
Brandon-Kimberly Jul 29, 2020
c781029
Format
Brandon-Kimberly Jul 29, 2020
dca6716
Format
Brandon-Kimberly Jul 29, 2020
9ea5d4f
Fix Merge function to set min and max correctly
Brandon-Kimberly Jul 29, 2020
fc78872
Fix formatting
Brandon-Kimberly Jul 29, 2020
b58df87
Merge branch 'master' into mmsc-gauge-agg
reyang Jul 30, 2020
185bb3c
Remove extraneuous include
Brandon-Kimberly Jul 30, 2020
8a48175
Remove include
Brandon-Kimberly Jul 30, 2020
098c00a
Allocate aggs on the stack
Brandon-Kimberly Jul 30, 2020
9d88ecd
Format
Brandon-Kimberly Jul 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
138 changes: 138 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#pragma once

#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/metrics/instrument.h"
#include "opentelemetry/sdk/metrics/aggregator/aggregator.h"
#include "opentelemetry/version.h"

#include <memory>
#include <mutex>
#include <vector>

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 T>
class GaugeAggregator : public Aggregator<T>
{
public:
explicit GaugeAggregator<T>(metrics_api::InstrumentKind kind)
{
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
this->kind_ = kind;
this->values_ = std::vector<T>(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<T> 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<T> 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<T> 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#pragma once

#include "opentelemetry/metrics/instrument.h"
#include "opentelemetry/sdk/metrics/aggregator/aggregator.h"
#include "opentelemetry/version.h"

#include <memory>
#include <mutex>
#include <vector>

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 T>
class MinMaxSumCountAggregator : public Aggregator<T>
{
public:
explicit MinMaxSumCountAggregator(metrics_api::InstrumentKind kind)
{
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");
this->kind_ = kind;
this->values_ = std::vector<T>(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 (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])
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 (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])
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<T> get_checkpoint() override { return this->checkpoint_; }

/**
* Returns the values currently held by the aggregator
*
* @return the values held by the aggregator
*/
std::vector<T> get_values() override { return this->values_; }
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
22 changes: 22 additions & 0 deletions sdk/test/metrics/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
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",
],
)

cc_test(
name = "meter_provider_sdk_test",
srcs = [
Expand Down
8 changes: 4 additions & 4 deletions sdk/test/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
foreach(testname meter_provider_sdk_test)
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_common opentelemetry_metrics)
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()
Loading