Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metrics SDK] Change boundry type to double for Explicit Bucket Histogram Aggregation, and change default bucket range #1626

Merged
merged 4 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
11 changes: 1 addition & 10 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,7 @@ void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::Po
}

sout_ << "\n buckets : ";
if (nostd::holds_alternative<std::list<double>>(histogram_point_data.boundaries_))
{
auto &double_boundaries = nostd::get<std::list<double>>(histogram_point_data.boundaries_);
printVec(sout_, double_boundaries);
}
else if (nostd::holds_alternative<std::list<long>>(histogram_point_data.boundaries_))
{
auto &long_boundaries = nostd::get<std::list<long>>(histogram_point_data.boundaries_);
printVec(sout_, long_boundaries);
}
printVec(sout_, histogram_point_data.boundaries_);

sout_ << "\n counts : ";
printVec(sout_, histogram_point_data.counts_);
Expand Down
2 changes: 1 addition & 1 deletion exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
histogram_point_data.min_ = 1.8;
histogram_point_data.max_ = 12.0;
metric_sdk::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.boundaries_ = std::list<double>{10.0, 20.0, 30.0};
lalitb marked this conversation as resolved.
Show resolved Hide resolved
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = 900l;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template <typename T>
class HistogramAggregationConfig : public AggregationConfig
{
public:
std::list<T> boundaries_;
std::list<double> boundaries_;
bool record_min_max_ = true;
};
} // namespace metrics
Expand Down
5 changes: 2 additions & 3 deletions sdk/include/opentelemetry/sdk/metrics/data/point_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace metrics
{

using ValueType = nostd::variant<long, double>;
using ListType = nostd::variant<std::list<long>, std::list<double>>;

// TODO: remove ctors and initializers from below classes when GCC<5 stops shipping on Ubuntu

Expand Down Expand Up @@ -55,8 +54,8 @@ class HistogramPointData
HistogramPointData &operator=(HistogramPointData &&) = default;
HistogramPointData(const HistogramPointData &) = default;
HistogramPointData() = default;

ListType boundaries_ = {};
HistogramPointData(std::list<double> &boundaries) : boundaries_(boundaries) {}
std::list<double> boundaries_ = {};
ValueType sum_ = {};
ValueType min_ = {};
ValueType max_ = {};
Expand Down
15 changes: 6 additions & 9 deletions sdk/src/metrics/aggregation/histogram_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ LongHistogramAggregation::LongHistogramAggregation(
}
else
{
point_data_.boundaries_ = std::list<long>{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l};
point_data_.boundaries_ =
std::list<double>{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0};
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}
if (aggregation_config)
{
record_min_max_ = aggregation_config->record_min_max_;
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<long>>(point_data_.boundaries_).size() + 1, 0);
point_data_.counts_ = std::vector<uint64_t>(point_data_.boundaries_.size() + 1, 0);
point_data_.sum_ = 0l;
point_data_.count_ = 0;
point_data_.record_min_max_ = record_min_max_;
Expand Down Expand Up @@ -59,8 +59,7 @@ void LongHistogramAggregation::Aggregate(long value,
point_data_.max_ = std::max(nostd::get<long>(point_data_.max_), value);
}
size_t index = 0;
for (auto it = nostd::get<std::list<long>>(point_data_.boundaries_).begin();
it != nostd::get<std::list<long>>(point_data_.boundaries_).end(); ++it)
for (auto it = point_data_.boundaries_.begin(); it != point_data_.boundaries_.end(); ++it)
{
if (value < *it)
{
Expand Down Expand Up @@ -114,8 +113,7 @@ DoubleHistogramAggregation::DoubleHistogramAggregation(
{
record_min_max_ = aggregation_config->record_min_max_;
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<double>>(point_data_.boundaries_).size() + 1, 0);
point_data_.counts_ = std::vector<uint64_t>(point_data_.boundaries_.size() + 1, 0);
point_data_.sum_ = 0.0;
point_data_.count_ = 0;
point_data_.record_min_max_ = record_min_max_;
Expand Down Expand Up @@ -143,8 +141,7 @@ void DoubleHistogramAggregation::Aggregate(double value,
point_data_.max_ = std::max(nostd::get<double>(point_data_.max_), value);
}
size_t index = 0;
for (auto it = nostd::get<std::list<double>>(point_data_.boundaries_).begin();
it != nostd::get<std::list<double>>(point_data_.boundaries_).end(); ++it)
for (auto it = point_data_.boundaries_.begin(); it != point_data_.boundaries_.end(); ++it)
{
if (value < *it)
{
Expand Down
13 changes: 5 additions & 8 deletions sdk/test/metrics/aggregation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ TEST(Aggregation, LongHistogramAggregation)
ASSERT_TRUE(nostd::holds_alternative<HistogramPointData>(data));
auto histogram_data = nostd::get<HistogramPointData>(data);
ASSERT_TRUE(nostd::holds_alternative<long>(histogram_data.sum_));
ASSERT_TRUE(nostd::holds_alternative<std::list<long>>(histogram_data.boundaries_));
EXPECT_EQ(nostd::get<long>(histogram_data.sum_), 0);
EXPECT_EQ(histogram_data.count_, 0);
aggr.Aggregate(12l, {}); // lies in fourth bucket
Expand Down Expand Up @@ -134,14 +133,14 @@ TEST(Aggregation, LongHistogramAggregationBoundaries)
{
nostd::shared_ptr<opentelemetry::sdk::metrics::HistogramAggregationConfig<long>>
aggregation_config{new opentelemetry::sdk::metrics::HistogramAggregationConfig<long>};
std::list<long> user_boundaries = {0, 50, 100, 250, 500, 750, 1000, 2500, 5000, 10000};
aggregation_config->boundaries_ = user_boundaries;
std::list<double> user_boundaries = {0.0, 50.0, 100.0, 250.0, 500.0,
750.0, 1000.0, 2500.0, 5000.0, 10000.0};
aggregation_config->boundaries_ = user_boundaries;
LongHistogramAggregation aggr{aggregation_config.get()};
auto data = aggr.ToPoint();
ASSERT_TRUE(nostd::holds_alternative<HistogramPointData>(data));
auto histogram_data = nostd::get<HistogramPointData>(data);
ASSERT_TRUE(nostd::holds_alternative<std::list<long>>(histogram_data.boundaries_));
EXPECT_EQ(nostd::get<std::list<long>>(histogram_data.boundaries_), user_boundaries);
EXPECT_EQ(histogram_data.boundaries_, user_boundaries);
}

TEST(Aggregation, DoubleHistogramAggregationBoundaries)
Expand All @@ -155,8 +154,7 @@ TEST(Aggregation, DoubleHistogramAggregationBoundaries)
auto data = aggr.ToPoint();
ASSERT_TRUE(nostd::holds_alternative<HistogramPointData>(data));
auto histogram_data = nostd::get<HistogramPointData>(data);
ASSERT_TRUE(nostd::holds_alternative<std::list<double>>(histogram_data.boundaries_));
EXPECT_EQ(nostd::get<std::list<double>>(histogram_data.boundaries_), user_boundaries);
EXPECT_EQ(histogram_data.boundaries_, user_boundaries);
}

TEST(Aggregation, DoubleHistogramAggregation)
Expand All @@ -166,7 +164,6 @@ TEST(Aggregation, DoubleHistogramAggregation)
ASSERT_TRUE(nostd::holds_alternative<HistogramPointData>(data));
auto histogram_data = nostd::get<HistogramPointData>(data);
ASSERT_TRUE(nostd::holds_alternative<double>(histogram_data.sum_));
ASSERT_TRUE(nostd::holds_alternative<std::list<double>>(histogram_data.boundaries_));
EXPECT_EQ(nostd::get<double>(histogram_data.sum_), 0);
EXPECT_EQ(histogram_data.count_, 0);
aggr.Aggregate(12.0, {}); // lies in fourth bucket
Expand Down