Skip to content

Commit b62565f

Browse files
euroelessarethandmd
authored andcommitted
add base2 expo histo aggregation
1 parent 6175aa0 commit b62565f

File tree

7 files changed

+106
-4
lines changed

7 files changed

+106
-4
lines changed

sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation_config.h

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ class HistogramAggregationConfig : public AggregationConfig
2424
std::vector<double> boundaries_;
2525
bool record_min_max_ = true;
2626
};
27+
28+
class Base2ExponentialHistogramAggregationConfig : public AggregationConfig
29+
{
30+
public:
31+
size_t max_buckets_ = 160;
32+
int32_t max_scale_ = 20;
33+
bool record_min_max_ = true;
34+
};
35+
2736
} // namespace metrics
2837
} // namespace sdk
2938
OPENTELEMETRY_END_NAMESPACE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include "opentelemetry/common/spin_lock_mutex.h"
7+
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
8+
#include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
9+
#include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h"
10+
11+
#include <memory>
12+
#include <mutex>
13+
14+
OPENTELEMETRY_BEGIN_NAMESPACE
15+
namespace sdk
16+
{
17+
namespace metrics
18+
{
19+
20+
class Base2ExponentialHistogramAggregation : public Aggregation
21+
{
22+
public:
23+
Base2ExponentialHistogramAggregation(const AggregationConfig *aggregation_config = nullptr);
24+
Base2ExponentialHistogramAggregation(const Base2ExponentialHistogramPointData &point_data);
25+
Base2ExponentialHistogramAggregation(Base2ExponentialHistogramPointData &&point_data);
26+
27+
28+
void Aggregate(int64_t value, const PointAttributes &attributes = {}) noexcept override;
29+
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override;
30+
31+
/* Returns the result of merge of the existing aggregation with delta
32+
* aggregation with same boundaries */
33+
std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;
34+
35+
/* Returns the new delta aggregation by comparing existing aggregation with
36+
* next aggregation with same boundaries. Data points for `next` aggregation
37+
* (sum , bucket-counts) should be more than the current aggregation - which
38+
* is the normal scenario as measurements values are monotonic increasing.
39+
*/
40+
std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;
41+
42+
PointType ToPoint() const noexcept override;
43+
44+
private:
45+
void AggregateIntoBuckets(AdaptingCircularBufferCounter *buckets, double value) noexcept;
46+
void Downscale(uint32_t by) noexcept;
47+
48+
mutable opentelemetry::common::SpinLockMutex lock_;
49+
Base2ExponentialHistogramPointData point_data_;
50+
Base2ExponentialHistogramIndexer indexer_;
51+
bool record_min_max_ = true;
52+
};
53+
54+
} // namespace metrics
55+
} // namespace sdk
56+
OPENTELEMETRY_END_NAMESPACE

sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <memory>
77

88
#include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
9+
#include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h"
910
#include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h"
1011
#include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
1112
#include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h"
@@ -80,6 +81,10 @@ class DefaultAggregation
8081
return std::unique_ptr<Aggregation>(new DoubleHistogramAggregation(aggregation_config));
8182
}
8283
break;
84+
case AggregationType::kBase2ExponentialHistogram:
85+
return std::unique_ptr<Aggregation>(
86+
new Base2ExponentialHistogramAggregation(aggregation_config));
87+
break;
8388
case AggregationType::kLastValue:
8489
if (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
8590
{
@@ -139,6 +144,9 @@ class DefaultAggregation
139144
return std::unique_ptr<Aggregation>(
140145
new DoubleHistogramAggregation(nostd::get<HistogramPointData>(point_data)));
141146
}
147+
case AggregationType::kBase2ExponentialHistogram:
148+
return std::unique_ptr<Aggregation>(new Base2ExponentialHistogramAggregation(
149+
nostd::get<Base2ExponentialHistogramPointData>(point_data)));
142150
case AggregationType::kLastValue:
143151
if (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
144152
{

sdk/include/opentelemetry/sdk/metrics/data/metric_data.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ namespace metrics
1818
{
1919

2020
using PointAttributes = opentelemetry::sdk::common::OrderedAttributeMap;
21-
using PointType = opentelemetry::nostd::
22-
variant<SumPointData, HistogramPointData, LastValuePointData, DropPointData>;
21+
using PointType = opentelemetry::nostd::variant<SumPointData,
22+
HistogramPointData,
23+
Base2ExponentialHistogramPointData,
24+
LastValuePointData,
25+
DropPointData>;
2326

2427
struct PointDataAttributes
2528
{

sdk/include/opentelemetry/sdk/metrics/data/point_data.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "opentelemetry/common/timestamp.h"
99
#include "opentelemetry/nostd/variant.h"
10+
#include "opentelemetry/sdk/metrics/data/circular_buffer.h"
1011
#include "opentelemetry/version.h"
1112

1213
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -17,7 +18,8 @@ namespace metrics
1718

1819
using ValueType = nostd::variant<int64_t, double>;
1920

20-
// TODO: remove ctors and initializers from below classes when GCC<5 stops shipping on Ubuntu
21+
// TODO: remove ctors and initializers from below classes when GCC<5 stops
22+
// shipping on Ubuntu
2123

2224
class SumPointData
2325
{
@@ -64,6 +66,28 @@ class HistogramPointData
6466
bool record_min_max_ = true;
6567
};
6668

69+
class Base2ExponentialHistogramPointData
70+
{
71+
public:
72+
// TODO: remove ctors and initializers when GCC<5 stops shipping on Ubuntu
73+
Base2ExponentialHistogramPointData(Base2ExponentialHistogramPointData &&) = default;
74+
Base2ExponentialHistogramPointData &operator=(Base2ExponentialHistogramPointData &&) = default;
75+
Base2ExponentialHistogramPointData(const Base2ExponentialHistogramPointData &) = default;
76+
Base2ExponentialHistogramPointData() = default;
77+
78+
uint64_t count_ = {};
79+
double sum_ = {};
80+
int32_t scale_ = {};
81+
uint64_t zero_count_ = {};
82+
AdaptingCircularBufferCounter positive_buckets_{0};
83+
AdaptingCircularBufferCounter negative_buckets_{0};
84+
double min_ = {};
85+
double max_ = {};
86+
double zero_threshold_ = {};
87+
bool record_min_max_ = true;
88+
size_t max_buckets_ = {};
89+
};
90+
6791
class DropPointData
6892
{
6993
public:

sdk/include/opentelemetry/sdk/metrics/instruments.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ enum class AggregationType
4545
kHistogram,
4646
kLastValue,
4747
kSum,
48-
kDefault
48+
kDefault,
49+
kBase2ExponentialHistogram
4950
};
5051

5152
enum class AggregationTemporality

sdk/src/metrics/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_library(
2020
state/observable_registry.cc
2121
state/sync_metric_storage.cc
2222
state/temporal_metric_storage.cc
23+
aggregation/base2_exponential_histogram_aggregation.cc
2324
aggregation/base2_exponential_histogram_indexer.cc
2425
aggregation/histogram_aggregation.cc
2526
aggregation/lastvalue_aggregation.cc

0 commit comments

Comments
 (0)