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

Add Aggregation as part of metrics SDK. #1178

Merged
merged 12 commits into from
Feb 5, 2022
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems InstrumentMonotonicityAwareAggregation should be a subclass of Aggretation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes or else we can also move this functionality within the Aggregation class. I thought of keeping it a separate class as not sure right now how to use it for Asynchronous aggregation. We can refactor this later once we have more complete implementation.

{
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;
Copy link
Member Author

@lalitb lalitb Jan 21, 2022

Choose a reason for hiding this comment

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

Preferred overloaded methods over the template ( for double and long type) to avoid having header-only metrics sdk. The template use had a cascading effect here, forcing to make all inter-related classes as templates. Since this is not a customer-facing API, we have the flexibility to change it later.


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
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