Skip to content

Commit

Permalink
Synchronous Metric collection (Delta , Cumulative) (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Mar 30, 2022
1 parent c1b9590 commit a7e814a
Show file tree
Hide file tree
Showing 28 changed files with 804 additions and 268 deletions.
57 changes: 28 additions & 29 deletions exporters/ostream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,35 @@ cc_library(
],
)

cc_library(
name = "ostream_metric_exporter",
srcs = [
"src/metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/metric_exporter.h",
],
strip_include_prefix = "include",
tags = [
"metrics",
"ostream",
],
deps = [
"//sdk/src/metrics",
],
)
#TODO MetricData is still changing, uncomment once it is final
# srcs = [
# "src/metric_exporter.cc",
# ],
# hdrs = [
# "include/opentelemetry/exporters/ostream/metric_exporter.h",
# ],
# strip_include_prefix = "include",
# tags = [
# "metrics",
# "ostream",
# ],
# deps = [
# "//sdk/src/metrics",
# ],
#)

cc_test(
name = "ostream_metric_test",
srcs = ["test/ostream_metric_test.cc"],
tags = [
"ostream",
"test",
],
deps = [
":ostream_metric_exporter",
"@com_google_googletest//:gtest_main",
],
)
#cc_test(
# name = "ostream_metric_test",
# srcs = ["test/ostream_metric_test.cc"],
# tags = [
# "ostream",
# "test",
# ],
# deps = [
# ":ostream_metric_exporter",
# "@com_google_googletest//:gtest_main",
#],
#)

cc_test(
name = "ostream_metrics_test_deprecated",
Expand Down
40 changes: 29 additions & 11 deletions sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,42 @@ 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;
/**
* Returns the result of the merge of the two aggregations.
*
* This should always assume that the aggregations do not overlap and merge together for a new
* cumulative report.
*
* @param delta the newly captured (delta) aggregation
* @return the result of the merge of the given aggregation.
*/

virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept = 0;

/**
* Returns a new delta aggregation by comparing two cumulative measurements.
*
* @param next the newly captured (cumulative) aggregation.
* @return The resulting delta aggregation.
*/
virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept = 0;

/**
* Returns the point data that the aggregation will produce.
*
* @return PointType
*/

virtual PointType ToPoint() const noexcept = 0;

virtual ~Aggregation() = default;
};

} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class DefaultAggregation
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)));
? std::move(std::unique_ptr<Aggregation>(new LongSumAggregation()))
: std::move(std::unique_ptr<Aggregation>(new DoubleSumAggregation()));
break;
case InstrumentType::kHistogram:
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
Expand Down Expand Up @@ -79,11 +79,11 @@ class DefaultAggregation
case AggregationType::kSum:
if (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
{
return std::unique_ptr<Aggregation>(new LongSumAggregation(true));
return std::unique_ptr<Aggregation>(new LongSumAggregation());
}
else
{
return std::unique_ptr<Aggregation>(new DoubleSumAggregation(true));
return std::unique_ptr<Aggregation>(new DoubleSumAggregation());
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ namespace sdk
namespace metrics
{

/**
* A null Aggregation which denotes no aggregation should occur.
*/

class DropAggregation : public Aggregation
{
public:
Expand All @@ -23,7 +27,21 @@ class DropAggregation : public Aggregation

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {}

PointType Collect() noexcept override { return DropPointData(); }
std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override
{
return std::unique_ptr<Aggregation>(new DropAggregation());
}

std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override
{
return std::unique_ptr<Aggregation>(new DropAggregation());
}

PointType ToPoint() const noexcept override
{
static DropPointData point_data;
return point_data;
}
};
} // namespace metrics
} // namespace sdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,47 @@ 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();
LongHistogramAggregation(HistogramPointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override;

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {}

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
std::vector<long> boundaries_;
long sum_;
std::vector<uint64_t> counts_;
uint64_t count_;
HistogramPointData point_data_;
};

class DoubleHistogramAggregation : public Aggregation
{
public:
DoubleHistogramAggregation();
DoubleHistogramAggregation(HistogramPointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {}

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override;

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
std::vector<double> boundaries_;
double sum_;
std::vector<uint64_t> counts_;
uint64_t count_;
mutable opentelemetry::common::SpinLockMutex lock_;
mutable HistogramPointData point_data_;
};

} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,42 @@ class LongLastValueAggregation : public Aggregation
{
public:
LongLastValueAggregation();
LongLastValueAggregation(LastValuePointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override;

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {}

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
long value_;
bool is_lastvalue_valid_;
LastValuePointData point_data_;
};

class DoubleLastValueAggregation : public Aggregation
{
public:
DoubleLastValueAggregation();
DoubleLastValueAggregation(LastValuePointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {}

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override;

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
double value_;
bool is_lastvalue_valid_;
mutable opentelemetry::common::SpinLockMutex lock_;
mutable LastValuePointData point_data_;
};

} // namespace metrics
Expand Down
44 changes: 19 additions & 25 deletions sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,46 @@ 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_temporality_ = AggregationTemporality::kDelta;
}

class LongSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation
class LongSumAggregation : public Aggregation
{
public:
LongSumAggregation(bool is_monotonic);
LongSumAggregation();
LongSumAggregation(SumPointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override;

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override {}

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
opentelemetry::common::SystemTimestamp start_epoch_nanos_;
long sum_;
SumPointData point_data_;
};

class DoubleSumAggregation : public Aggregation, InstrumentMonotonicityAwareAggregation
class DoubleSumAggregation : public Aggregation
{
public:
DoubleSumAggregation(bool is_monotonic);
DoubleSumAggregation();
DoubleSumAggregation(SumPointData &&);

void Aggregate(long value, const PointAttributes &attributes = {}) noexcept override {}

void Aggregate(double value, const PointAttributes &attributes = {}) noexcept override;

PointType Collect() noexcept override;
virtual std::unique_ptr<Aggregation> Merge(const Aggregation &delta) const noexcept override;

virtual std::unique_ptr<Aggregation> Diff(const Aggregation &next) const noexcept override;

PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
opentelemetry::common::SystemTimestamp start_epoch_nanos_;
double sum_;
mutable opentelemetry::common::SpinLockMutex lock_;
SumPointData point_data_;
};

} // namespace metrics
Expand Down
Loading

0 comments on commit a7e814a

Please sign in to comment.