-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Aggregation as part of metrics SDK. (#1178)
- Loading branch information
Showing
19 changed files
with
790 additions
and
59 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/nostd/string_view.h" | ||
# include "opentelemetry/sdk/metrics/data/metric_data.h" | ||
# include "opentelemetry/sdk/metrics/data/point_data.h" | ||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
class InstrumentMonotonicityAwareAggregation | ||
{ | ||
public: | ||
InstrumentMonotonicityAwareAggregation(bool is_monotonic) : is_monotonic_(is_monotonic) {} | ||
bool IsMonotonic() { return is_monotonic_; } | ||
|
||
private: | ||
bool is_monotonic_; | ||
}; | ||
|
||
class Aggregation | ||
{ | ||
public: | ||
virtual void Aggregate(long value, const PointAttributes &attributes = {}) noexcept = 0; | ||
|
||
virtual void Aggregate(double value, const PointAttributes &attributes = {}) noexcept = 0; | ||
|
||
virtual PointType Collect() noexcept = 0; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
55 changes: 55 additions & 0 deletions
55
sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/instruments.h" | ||
|
||
# include <mutex> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
class DefaultAggregation | ||
{ | ||
public: | ||
static std::unique_ptr<Aggregation> CreateAggregation( | ||
const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) | ||
{ | ||
switch (instrument_descriptor.type_) | ||
{ | ||
case InstrumentType::kCounter: | ||
case InstrumentType::kUpDownCounter: | ||
case InstrumentType::kObservableUpDownCounter: | ||
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) | ||
? std::move(std::unique_ptr<Aggregation>(new LongSumAggregation(true))) | ||
: std::move(std::unique_ptr<Aggregation>(new DoubleSumAggregation(true))); | ||
break; | ||
case InstrumentType::kHistogram: | ||
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) | ||
? std::move(std::unique_ptr<Aggregation>(new LongHistogramAggregation())) | ||
: std::move(std::unique_ptr<Aggregation>(new DoubleHistogramAggregation())); | ||
break; | ||
case InstrumentType::kObservableGauge: | ||
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) | ||
? std::move(std::unique_ptr<Aggregation>(new LongLastValueAggregation())) | ||
: std::move(std::unique_ptr<Aggregation>(new DoubleLastValueAggregation())); | ||
break; | ||
default: | ||
return std::move(std::unique_ptr<Aggregation>(new DropAggregation())); | ||
}; | ||
} | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
31 changes: 31 additions & 0 deletions
31
sdk/include/opentelemetry/sdk/metrics/aggregation/drop_aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
|
||
# include <mutex> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
class DropAggregation : public Aggregation | ||
{ | ||
public: | ||
DropAggregation() = default; | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
PointType Collect() noexcept override { return DropPointData(); } | ||
}; | ||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
72 changes: 72 additions & 0 deletions
72
sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
|
||
# include <mutex> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
template <class T> | ||
static inline void PopulateHistogramDataPoint(HistogramPointData &histogram, | ||
opentelemetry::common::SystemTimestamp epoch_nanos, | ||
T sum, | ||
uint64_t count, | ||
std::vector<uint64_t> &counts, | ||
std::vector<T> boundaries) | ||
{ | ||
histogram.epoch_nanos_ = epoch_nanos; | ||
histogram.boundaries_ = boundaries; | ||
histogram.sum_ = sum; | ||
histogram.counts_ = counts; | ||
histogram.count_ = count; | ||
} | ||
|
||
class LongHistogramAggregation : public Aggregation | ||
{ | ||
public: | ||
LongHistogramAggregation(); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
std::vector<long> boundaries_; | ||
long sum_; | ||
std::vector<uint64_t> counts_; | ||
uint64_t count_; | ||
}; | ||
|
||
class DoubleHistogramAggregation : public Aggregation | ||
{ | ||
public: | ||
DoubleHistogramAggregation(); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
std::vector<double> boundaries_; | ||
double sum_; | ||
std::vector<uint64_t> counts_; | ||
uint64_t count_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
53 changes: 53 additions & 0 deletions
53
sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
|
||
# include <mutex> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
class LongLastValueAggregation : public Aggregation | ||
{ | ||
public: | ||
LongLastValueAggregation(); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
long value_; | ||
bool is_lastvalue_valid_; | ||
}; | ||
|
||
class DoubleLastValueAggregation : public Aggregation | ||
{ | ||
public: | ||
DoubleLastValueAggregation(); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
double value_; | ||
bool is_lastvalue_valid_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
68 changes: 68 additions & 0 deletions
68
sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/spin_lock_mutex.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
|
||
# include <mutex> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
template <class T> | ||
static inline void PopulateSumPointData(SumPointData &sum, | ||
opentelemetry::common::SystemTimestamp start_ts, | ||
opentelemetry::common::SystemTimestamp end_ts, | ||
T value, | ||
bool is_monotonic) | ||
{ | ||
sum.start_epoch_nanos_ = start_ts; | ||
sum.end_epoch_nanos_ = end_ts; | ||
sum.value_ = value; | ||
sum.is_monotonic_ = is_monotonic; | ||
sum.aggregation_temporarily_ = AggregationTemporarily::kDelta; | ||
} | ||
|
||
class LongSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation | ||
{ | ||
public: | ||
LongSumAggregation(bool is_monotonic); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
opentelemetry::common::SystemTimestamp start_epoch_nanos_; | ||
long sum_; | ||
}; | ||
|
||
class DoubleSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation | ||
{ | ||
public: | ||
DoubleSumAggregation(bool is_monotonic); | ||
|
||
void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {} | ||
|
||
void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override; | ||
|
||
PointType Collect() noexcept override; | ||
|
||
private: | ||
opentelemetry::common::SpinLockMutex lock_; | ||
opentelemetry::common::SystemTimestamp start_epoch_nanos_; | ||
double sum_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/nostd/variant.h" | ||
# include "opentelemetry/sdk/common/attribute_utils.h" | ||
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" | ||
# include "opentelemetry/sdk/metrics/data/point_data.h" | ||
# include "opentelemetry/sdk/metrics/instruments.h" | ||
# include "opentelemetry/sdk/resource/resource.h" | ||
# include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
using PointAttributes = opentelemetry::sdk::common::AttributeMap; | ||
using PointType = opentelemetry::nostd:: | ||
variant<SumPointData, HistogramPointData, LastValuePointData, DropPointData>; | ||
|
||
class MetricData | ||
{ | ||
public: | ||
opentelemetry::sdk::resource::Resource *resource_; | ||
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; | ||
PointAttributes attributes_; | ||
InstrumentDescriptor instrument_descriptor; | ||
PointType point_data_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
Oops, something went wrong.
44795b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.BM_SetValueBaggageWithTenEntries
1851.2268069502627
ns/iter797.4833581281193
ns/iter2.32
This comment was automatically generated by workflow using github-action-benchmark.