Skip to content

Commit

Permalink
Add Aggregation as part of metrics SDK. (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Feb 5, 2022
1 parent b6a28df commit 44795b6
Show file tree
Hide file tree
Showing 19 changed files with 790 additions and 59 deletions.
37 changes: 37 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation.h
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
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
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
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
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
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
6 changes: 2 additions & 4 deletions sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
# include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

class Aggregator
{
// TBD
};
{};

class NoOpAggregator : public Aggregator
{
Expand Down
37 changes: 37 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/data/metric_data.h
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
Loading

1 comment on commit 44795b6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

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.

Benchmark suite Current: 44795b6 Previous: b6a28df Ratio
BM_SetValueBaggageWithTenEntries 1851.2268069502627 ns/iter 797.4833581281193 ns/iter 2.32

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.