diff --git a/api/include/opentelemetry/common/timestamp.h b/api/include/opentelemetry/common/timestamp.h index 54e7b7aa6c..8e45ef0c74 100644 --- a/api/include/opentelemetry/common/timestamp.h +++ b/api/include/opentelemetry/common/timestamp.h @@ -45,6 +45,18 @@ class SystemTimestamp : SystemTimestamp{time_point.time_since_epoch()} {} + /** + * @brief Reset a system timestamp based on a point in time. + * + * @param time_point A point in time. + */ + void Reset(const std::chrono::system_clock::time_point &time_point) noexcept + { + nanos_since_epoch_ = static_cast( + std::chrono::duration_cast(time_point.time_since_epoch()) + .count()); + } + /** * @brief Returns a time point for the time stamp. * diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h new file mode 100644 index 0000000000..83ccd2fd92 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h @@ -0,0 +1,180 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/metrics/data/point_data.h" +# include "opentelemetry/version.h" + +# include +# include +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class Accumulation +{ +public: + virtual void Record(long value) noexcept = 0; + virtual void Record(double value) noexcept = 0; +}; + +class LongSumAccumulation : public Accumulation +{ +public: + LongSumAccumulation(long value = 0) : sum_(value) {} + + void Record(long value) noexcept override { sum_ += value; } + void Record(double value) noexcept override {} + + LongSingularPointData ToPointData() { return LongSingularPointData(sum_); } + +private: + long sum_; +}; + +class DoubleSumAccumulation : public Accumulation +{ +public: + DoubleSumAccumulation(double value = 0) : sum_(value) {} + + void Record(double value) noexcept override { sum_ += value; } + void Record(long value) noexcept override {} + + DoubleSingularPointData ToPointData() { return DoubleSingularPointData(sum_); } + +private: + double sum_; +}; + +class LongLastValueAccumulation : public Accumulation +{ +public: + LongLastValueAccumulation( + long value = 0, + opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()) + : last_value_(value), ts_{ts} + {} + + void Record(long value) noexcept override + { + last_value_ = value; + ts_.Reset(std::chrono::system_clock::now()); + } + + void Record(double value) noexcept override {} + + const opentelemetry::common::SystemTimestamp &GetLastValueTimeStamp() const { return ts_; } + + LongSingularPointData ToPointData() { return LongSingularPointData(last_value_); } + +private: + long last_value_; + opentelemetry::common::SystemTimestamp ts_; +}; + +class DoubleLastValueAccumulation : public Accumulation +{ +public: + DoubleLastValueAccumulation( + long value = 0, + opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()) + : last_value_(value), ts_{ts} + {} + + void Record(double value) noexcept override + { + last_value_ = value; + ts_.Reset(std::chrono::system_clock::now()); + } + + void Record(long value) noexcept override {} + + const opentelemetry::common::SystemTimestamp &GetLastValueTimeStamp() const { return ts_; } + + DoubleSingularPointData ToPointData() { return DoubleSingularPointData(last_value_); } + +private: + double last_value_; + opentelemetry::common::SystemTimestamp ts_; +}; + +class LongHistogramAccumulation : public Accumulation +{ +public: + LongHistogramAccumulation(std::vector &boundaries) : histogram_(boundaries) {} + + void Record(long value) noexcept override + { + histogram_.count_ += 1; + histogram_.sum_ += value; + for (auto it = histogram_.boundaries_.begin(); it != histogram_.boundaries_.end(); ++it) + { + if (value < *it) + { + histogram_.counts_[std::distance(histogram_.boundaries_.begin(), it)] += 1; + return; + } + } + } + + void Record(double value) noexcept override {} + + LongHistogramPointData &ToPointData() { return histogram_; } + +private: + LongHistogramPointData histogram_; +}; + +class DoubleHistogramAccumulation : public Accumulation +{ +public: + DoubleHistogramAccumulation(std::vector &boundaries) : histogram_(boundaries) {} + + void Record(double value) noexcept override + { + histogram_.count_ += 1; + histogram_.sum_ += value; + for (auto it = histogram_.boundaries_.begin(); it != histogram_.boundaries_.end(); ++it) + { + if (value < *it) + { + histogram_.counts_[std::distance(histogram_.boundaries_.begin(), it)] += 1; + return; + } + } + } + + void Record(long value) noexcept override {} + + DoubleHistogramPointData &ToPointData() { return histogram_; } + +private: + DoubleHistogramPointData histogram_; +}; + +class DropAccumulation : public Accumulation +{ +public: + DropAccumulation() {} + + void Record(long value) noexcept override {} + void Record(double value) noexcept override {} + + DropPointData ToPointData() { return DropPointData(); } +}; + +template +struct AccumulationRecord +{ + T accumulation; + opentelemetry::sdk::common::AttributeMap attributes; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h index 73d7d20e71..ba83f4e1a9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h @@ -3,6 +3,8 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -12,14 +14,15 @@ namespace metrics class Aggregator { - // TBD -}; +public: + virtual std::unique_ptr CreateAccumulation() noexcept = 0; -class NoOpAggregator : public Aggregator -{ - // TBD -}; + virtual std::unique_ptr Merge(Accumulation &prev, + Accumulation ¤t) noexcept = 0; + virtual std::unique_ptr diff(Accumulation &prev, + Accumulation ¤t) noexcept = 0; +}; } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h new file mode 100644 index 0000000000..d33961dcdb --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h @@ -0,0 +1,54 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** Aggregators to drop the instruments. */ +class DropAggregator : public Aggregator +{ +public: + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new DropAccumulation())); + } + + /** Returns the result of the merge of the given accumulations.*/ + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move( + std::unique_ptr(std::unique_ptr(new DropAccumulation()))); + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move( + std::unique_ptr(std::unique_ptr(new DropAccumulation()))); + } + + DropMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + DropMetricData metrics_data; + return metrics_data; + } +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h new file mode 100644 index 0000000000..5a1f1b7693 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -0,0 +1,108 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * Basic aggregator which observes events and counts them in pre-defined buckets + * and provides the total sum and count of all observations. + */ +class LongHistogramAggregator : public Aggregator +{ + // TBD - This class is placeholder, and needs to be implemented +public: + LongHistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} + std::unique_ptr CreateAccumulation() noexcept override + { + // TBD + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); + } + + /** Returns the result of the merge of the given accumulations.*/ + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + // TBD + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); + } + + LongHistogramMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + LongHistogramMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + // TBD - Populate me + return metrics_data; + } + +private: + std::vector boundaries_; +}; + +class DoubleHistogramAggregator : public Aggregator +{ + // TBD - This class is placeholder, and needs to be implemented +public: + DoubleHistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new DoubleHistogramAccumulation(boundaries_))); + } + + /** Returns the result of the merge of the given accumulations.*/ + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + // TBD + return std::move(std::unique_ptr(new DoubleHistogramAccumulation(boundaries_))); + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + // TBD + return std::move(std::unique_ptr(new DoubleHistogramAccumulation(boundaries_))); + } + + DoubleHistogramMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + DoubleHistogramMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + // TBD - Populate me + return metrics_data; + } + +private: + std::vector boundaries_; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h new file mode 100644 index 0000000000..2d9ede4ba9 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h @@ -0,0 +1,129 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" + +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** Basic aggregator which calculates a last value from individual measurements. */ +class LongLastValueAggregator : public Aggregator +{ +public: + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new LongLastValueAccumulation())); + } + + /** Returns the result of the merge of the given accumulations.*/ + + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + auto prev_lvp = static_cast(&prev); + auto current_lvp = static_cast(¤t); + auto latest_accumulation = (current_lvp->GetLastValueTimeStamp().time_since_epoch() >= + prev_lvp->GetLastValueTimeStamp().time_since_epoch()) + ? current_lvp + : prev_lvp; + + return std::move(std::unique_ptr(new LongLastValueAccumulation( + prev_lvp->ToPointData().value_, latest_accumulation->GetLastValueTimeStamp()))); + } + + /** Returns a new DELTA aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + auto prev_lvp = static_cast(&prev); + auto current_lvp = static_cast(¤t); + return std::move(std::unique_ptr(new LongLastValueAccumulation( + prev_lvp->ToPointData().value_ - current_lvp->ToPointData().value_))); + } + + LongSingularMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + LongSingularMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + for (auto &acc_by_attr : accumulation_by_attributes) + { + opentelemetry::sdk::metrics::BasePointData base_point_data = {start_epoch_ns, end_epoch_ns, + acc_by_attr.attributes}; + auto point_data = acc_by_attr.accumulation.ToPointData(); + metrics_data.point_data_list.push_back(std::make_pair(base_point_data, point_data)); + } + return metrics_data; + } +}; + +class DoubleLastValueAggregator : public Aggregator +{ +public: + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new DoubleLastValueAccumulation())); + } + + /** Returns the result of the merge of the given accumulations.*/ + + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + auto prev_lvp = static_cast(&prev); + auto current_lvp = static_cast(¤t); + auto latest_accumulation = (current_lvp->GetLastValueTimeStamp().time_since_epoch() >= + prev_lvp->GetLastValueTimeStamp().time_since_epoch()) + ? current_lvp + : prev_lvp; + + return std::move(std::unique_ptr(new DoubleLastValueAccumulation( + prev_lvp->ToPointData().value_, latest_accumulation->GetLastValueTimeStamp()))); + } + + /** Returns a new DELTA aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + auto prev_lvp = static_cast(&prev); + auto current_lvp = static_cast(¤t); + return std::move(std::unique_ptr(new DoubleLastValueAccumulation( + prev_lvp->ToPointData().value_ - current_lvp->ToPointData().value_))); + } + + DoubleSingularMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + DoubleSingularMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + for (auto &acc_by_attr : accumulation_by_attributes) + { + opentelemetry::sdk::metrics::BasePointData base_point_data = {start_epoch_ns, end_epoch_ns, + acc_by_attr.attributes}; + auto point_data = acc_by_attr.accumulation.ToPointData(); + metrics_data.point_data_list.push_back(std::make_pair(base_point_data, point_data)); + } + return metrics_data; + } +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h new file mode 100644 index 0000000000..97ad05e379 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h @@ -0,0 +1,119 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" + +# include "opentelemetry/version.h" + +# include +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** Basic aggregator which calculates a Sum from individual measurements. */ +class LongSumAggregator : public Aggregator +{ +public: + LongSumAggregator(InstrumentDescriptor &instrument_descriptor) {} + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new LongSumAccumulation())); + } + + /** Returns the result of the merge of the given accumulations.*/ + + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new LongSumAccumulation( + static_cast(&prev)->ToPointData().value_ + + static_cast(¤t)->ToPointData().value_))); + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new LongSumAccumulation( + static_cast(&prev)->ToPointData().value_ - + static_cast(¤t)->ToPointData().value_))); + } + + LongSingularMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + LongSingularMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + for (auto &acc_by_attr : accumulation_by_attributes) + { + opentelemetry::sdk::metrics::BasePointData base_point_data = {start_epoch_ns, end_epoch_ns, + acc_by_attr.attributes}; + auto point_data = acc_by_attr.accumulation.ToPointData(); + metrics_data.point_data_list.push_back(std::make_pair(base_point_data, point_data)); + } + return metrics_data; + } +}; + +class DoubleSumAggregator : public Aggregator +{ +public: + DoubleSumAggregator(InstrumentDescriptor &instrument_descriptor) {} + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new DoubleSumAccumulation())); + } + + /** Returns the result of the merge of the given accumulations.*/ + + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new DoubleSumAccumulation( + static_cast(&prev)->ToPointData().value_ + + static_cast(¤t)->ToPointData().value_))); + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new DoubleSumAccumulation( + static_cast(&prev)->ToPointData().value_ - + static_cast(¤t)->ToPointData().value_))); + } + + DoubleSingularMetricData ToMetricData( + opentelemetry::sdk::resource::Resource *resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + std::vector> + &accumulation_by_attributes, + opentelemetry::common::SystemTimestamp &start_epoch_ns, + opentelemetry::common::SystemTimestamp &end_epoch_ns) + { + DoubleSingularMetricData metrics_data = {resource, instrumentation_library, + instrument_descriptor}; + for (auto &acc_by_attr : accumulation_by_attributes) + { + opentelemetry::sdk::metrics::BasePointData base_point_data = {start_epoch_ns, end_epoch_ns, + acc_by_attr.attributes}; + auto point_data = acc_by_attr.accumulation.ToPointData(); + metrics_data.point_data_list.push_back(std::make_pair(base_point_data, point_data)); + } + return metrics_data; + } +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/data/data.h b/sdk/include/opentelemetry/sdk/metrics/data/data.h new file mode 100644 index 0000000000..14e66e70c6 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/data.h @@ -0,0 +1,26 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/point_data.h" +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * A collection of data points associated to a metric. + */ +template +typename std::enable_if::value>::type class Data : public T +{ +public: + virtual std::vector GetPoints() = 0; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h new file mode 100644 index 0000000000..5026c33116 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -0,0 +1,67 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# 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" + +# include +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/*struct MetricData +{ + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; +};*/ + +struct LongSingularMetricData +{ +public: + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_data_list; +}; + +struct DoubleSingularMetricData +{ +public: + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_data_list; +}; + +class LongHistogramMetricData +{ +public: + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_datas_list; +}; + +class DoubleHistogramMetricData +{ +public: + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_datas_list; +}; + +class DropMetricData +{}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h new file mode 100644 index 0000000000..acd3b8ee6b --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -0,0 +1,177 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +using PointAttributes = opentelemetry::sdk::common::AttributeMap; + +/** + * A point in the "Metric stream" data model. + */ + +struct BasePointData +{ + opentelemetry::common::SystemTimestamp start_epoch_nanos_; + opentelemetry::common::SystemTimestamp end_epoch_nanos_; + PointAttributes attributes_; +}; + +class PointData +{}; + +struct LongSingularPointData : public PointData +{ + LongSingularPointData(long value) : value_(value) {} + long value_; +}; + +struct DoubleSingularPointData : public PointData +{ + DoubleSingularPointData(long value) : value_(value) {} + long value_; +}; + +struct LongHistogramPointData : public PointData +{ + LongHistogramPointData(std::vector &boundaries) + : boundaries_{boundaries}, counts_(boundaries.size() + 1, 0), sum_(0), count_(0) + {} + std::vector boundaries_; + std::vector counts_; + long sum_; + uint64_t count_; +}; + +struct DoubleHistogramPointData : public PointData +{ + DoubleHistogramPointData(std::vector &boundaries) + : boundaries_{boundaries}, counts_(boundaries.size() + 1, 0), sum_(0), count_(0) + {} + std::vector boundaries_; + std::vector counts_; + double sum_; + uint64_t count_; +}; + +struct DropPointData : public PointData +{}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif + +/*class PointData { +public: + public: + PointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + opentelemetry::common::SystemTimestamp& epoch_nanos, + PointAttributes& attributes): start_epoch_nanos_(start_epoch_nanos), + epoch_nanos_(epoch_nanos), epoch_nanos_{attributes} + {} + //Returns the start epoch timestamp + opentelemetry::common::SystemTimestamp GetStartEpochNanos() noexcept + { + return start_epoch_nanos_; + } + + //Returns the epoch timestamp in nanos when data were collected + opentelemetry::common::SystemTimestamp GetEpochNanos() noexcept + { + return epoch_nanos_; + } + + //Returns the attributes associated with this Point + PointAttributes& GetAttributes() noexcept + { + return attributes_; + } + + private: + opentelemetry::common::SystemTimestamp start_epoch_nanos_; + opentelemetry::common::SystemTimestamp epoch_nanos_; + PointAttributes attributes_; +}; + +template +class SingularPointData: public PointData { + public: + SingularPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + opentelemetry::common::SystemTimestamp& epoch_nanos, + PointAttributes& attributes, + T value): + PointData(start_epoch_nanos, epoch_nanos, attributes), + value_(value) + {} + + T GetValue() const noexcept { + return value_; + } + + private: + T value_; +}; + +template +class SummaryPointData: public PointData { + public: + SummaryPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + opentelemetry::common::SystemTimestamp& epoch_nanos, + PointAttributes& attributes, + uint64_t count, + T sum): + PointData(start_epoch_nanos, epoch_nanos, attributes), + sum_(sum), count_(count) + {} + T GetSum() const noexcept { + return sum_; + } + + uint64_t GetCount() const noexcept { + return count_; + } + + private: + T sum_; + uint64_t count_; +}; + +template +class HistogramPointData : public PointData { + HistogramPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + opentelemetry::common::SystemTimestamp& epoch_nanos, + PointAttributes& attributes, + T sum, + std::vector boundaries, + std::vector counts): + PointData(start_epoch_nanos, epoch_nanos, attributes), + sum_(sum), boundaries_{boundaries}, counts_{count} + {} + T GetSum() const noexcept { + return sum_; + } + std::vector GetBoundaries() { + return boundaries_; + } + std::vector GetCounts() { + return counts_; + } + private: + T sum_; + std::vector boundaries_; + std::vector counts_; +};*/ +//} // namespace metrics +//} // namespace sdk +// OPENTELEMETRY_END_NAMESPACE +//#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index 8d101090c7..b1b9ab3fa4 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -21,9 +21,7 @@ enum class InstrumentType enum class InstrumentValueType { - kInt, kLong, - kFloat, kDouble }; diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index b1a4e304d7..0f7fe43026 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -6,7 +6,9 @@ # include # include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/meter_context.h" +# include "opentelemetry/sdk/metrics/state/writable_metric_storage.h" # include "opentelemetry/sdk/resource/resource.h" # include "opentelemetry/version.h" @@ -98,10 +100,9 @@ class Meter final : public opentelemetry::metrics::Meter const noexcept; private: - // order of declaration is important here - instrumentation library should destroy after - // meter-context. std::shared_ptr instrumentation_library_; std::shared_ptr context_; + // WritableMetricStorage &RegisterMetricStorage(InstrumentDescriptor &); }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context.h b/sdk/include/opentelemetry/sdk/metrics/meter_context.h index 9b0331a150..9a6d57f6ed 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -46,6 +46,12 @@ class MeterContext */ const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; + /** + * Obtain the View Registry associated with this meter context. + * @return The view registry for this meter context + */ + const opentelemetry::sdk::metrics::ViewRegistry &GetViewRegistry() const noexcept; + /** * Attaches a metric exporter to list of configured exporters for this Meter context. * @param exporter The metric exporter for this meter context. This diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h new file mode 100644 index 0000000000..47ad7c5861 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/" +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +/** Represents a storage from which we can collect metrics. + * + */ +class MetricStorage +{ + /* + * Collects the metrics from this storage. + */ + virtual MetricData &collect(CollectionInfo &collection_info, + std::vector &collection_infos, + opentelemetry::sdk::resource::Resource &resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibraryInfo + &instrumentationLibraryInfo, + opentelemetry::common::SystemTimestamp &start_epoch_nanos, + opentelemetry::common::SystemTimestamp &epoch_nanos) noexcept = 0; +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h new file mode 100644 index 0000000000..d657da83be --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/* + * Stores and aggregates {@link MetricData} for synchronous instruments. + */ + +template +class SyncMetricStorage : public WritableMetricStorage, MetricsStorage +{ + + void Record(T value) noexcept override {} + + void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} + + /* Collects the metrics from this storage.*/ + MetricData &Collect( + CollectionInfo &collection_info, + std::vector &collection_infos, + opentelemetry::sdk::resource::Resource &resource, + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, + opentelemetry::common::SystemTimestamp &start_epoch_nanos, + opentelemetry::common::SystemTimestamp &epoch_nanos) + {} +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h new file mode 100644 index 0000000000..dc4b213dac --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/key_value_iterable.h" +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +template +class WritableMetricStorage +{ +public: + virtual void Record(T value) noexcept = 0; + virtual void Record(T value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; +}; + +template +class NoopWritableMetricsStorage : public WritableMetricStorage +{ +public: + void Record(T value) noexcept override {} + void Record(T value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override + {} +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h new file mode 100644 index 0000000000..1a3e340f45 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -0,0 +1,54 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/metrics/sync_instruments.h" +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/state/writable_metrics_storage.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +template +class Counter : public openetelemetry::metrics::Counter +{ +public: + Counter(std::unique_ptr &&instrument_descriptor, + std::unique_ptr> &&writable_storage) + : instrument_descriptor_{std::move(instrument_descriptor)}, + writable_storage_{std::move(writable_storage)} + {} + void Add(T value) noexcept override + { + if (value < 0) + { + // TBD -warning + return; + } + writable_storage_->Record(value); + } + void Add(T value, const common::KeyValueIterable &attributes) noexcept override + { + if (value < 0) + { + // TBD -warning + return; + } + writable_storage_->Record(value, attributes); + } + +private: + std::unique_ptr instrument_descriptor_; + std::unique_ptr> writable_storage_; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h index de851e10fa..50ee020dac 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h @@ -12,35 +12,13 @@ namespace sdk { namespace metrics { + class Aggregation { public: virtual ~Aggregation() = default; - virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept = 0; -}; - -class NoOpAggregation : public Aggregation -{ - - opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static opentelemetry::sdk::metrics::NoOpAggregator noop_aggregator; - return noop_aggregator; - } -}; - -class DefaultAggregation : public Aggregation -{ - - opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - // TBD - This shouldn't return noop_aggregator - static opentelemetry::sdk::metrics::NoOpAggregator noop_aggregator; - return noop_aggregator; - } + virtual std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept = 0; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h new file mode 100644 index 0000000000..e375d601bb --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/view/aggregation.h" + +# include +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class DefaultAggregation : public Aggregation +{ +public: + std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + +private: + opentelemetry::sdk::metrics::Aggregation &resolve( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/view/drop_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/drop_aggregation.h new file mode 100644 index 0000000000..75510ebb96 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/drop_aggregation.h @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/view/aggregation.h" + +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class DropAggregation : public Aggregation +{ +public: + std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + + static Aggregation &GetInstance(); + +private: + DropAggregation() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h new file mode 100644 index 0000000000..84e8ec305b --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/view/aggregation.h" + +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class HistogramAggregation : public Aggregation +{ +public: + virtual std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + + static Aggregation &GetInstance() noexcept; + +private: + HistogramAggregation() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/view/lastvalue_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/lastvalue_aggregation.h new file mode 100644 index 0000000000..2587d880ca --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/lastvalue_aggregation.h @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/view/aggregation.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class LastValueAggregation : public Aggregation +{ +public: + std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + + static Aggregation &GetInstance() noexcept; + +private: + LastValueAggregation() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h new file mode 100644 index 0000000000..5157172dda --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/view/aggregation.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class SumAggregation : public Aggregation +{ +public: + std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + + static Aggregation &GetInstance() noexcept; + +private: + SumAggregation() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view.h b/sdk/include/opentelemetry/sdk/metrics/view/view.h index c131b4fc04..96545077c1 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view.h @@ -4,8 +4,8 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/nostd/string_view.h" -# include "opentelemetry/sdk/metrics/view/aggregation.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" +# include "opentelemetry/sdk/metrics/view/default_aggregation.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index d1d7ae97bc..e531980a78 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -1,4 +1,13 @@ -add_library(opentelemetry_metrics meter_provider.cc meter.cc meter_context.cc) +add_library( + opentelemetry_metrics + meter_provider.cc + meter.cc + meter_context.cc + view/sum_aggregation.cc + view/lastvalue_aggregation.cc + view/histogram_aggregation.cc + view/drop_aggregation.cc + view/default_aggregation.cc) set_target_properties(opentelemetry_metrics PROPERTIES EXPORT_NAME metrics) diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 6c759158a4..028ec42f30 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -29,6 +29,10 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::string_view description, nostd::string_view unit) noexcept { + InstrumentDescriptor instrument_descriptor = {name.data(), description.data(), unit.data(), + InstrumentType::kCounter, + InstrumentValueType::kLong}; + // auto storage = registerMetricStorage(instrument_descriptor); OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongCounter] Not Implemented - Returns Noop."); return nostd::shared_ptr>{ new metrics::NoopCounter(name, description, unit)}; @@ -158,6 +162,14 @@ const sdk::instrumentationlibrary::InstrumentationLibrary &Meter::GetInstrumenta return *instrumentation_library_; } +// WritableMatricsStorage &Meter::RegisterMetricsStorage(InstrumentDescriptor +// &instrument_descriptor) +//{ +// std::vector < std::vector views; +// context_.GetViewRegistry().FindViews(instrument_descriptor, *instrumentation_library_, +// [](const View &view) { views.push_back(view); }); +//} + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/meter_context.cc b/sdk/src/metrics/meter_context.cc index b3f4f5c6bf..fcc647d35d 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -28,6 +28,11 @@ const resource::Resource &MeterContext::GetResource() const noexcept return resource_; } +const opentelemetry::sdk::metrics::ViewRegistry &MeterContext::GetViewRegistry() const noexcept +{ + return *views_.get(); +} + void MeterContext::AddMetricExporter(std::unique_ptr exporter) noexcept { exporters_.push_back(std::move(exporter)); diff --git a/sdk/src/metrics/view/default_aggregation.cc b/sdk/src/metrics/view/default_aggregation.cc new file mode 100644 index 0000000000..376e8d9306 --- /dev/null +++ b/sdk/src/metrics/view/default_aggregation.cc @@ -0,0 +1,52 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/view/default_aggregation.h" +# include "opentelemetry/sdk/metrics/view/drop_aggregation.h" +# include "opentelemetry/sdk/metrics/view/histogram_aggregation.h" +# include "opentelemetry/sdk/metrics/view/lastvalue_aggregation.h" +# include "opentelemetry/sdk/metrics/view/sum_aggregation.h" + +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr DefaultAggregation::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + return resolve(instrument_descriptor).CreateAggregator(instrument_descriptor); +} + +opentelemetry::sdk::metrics::Aggregation &DefaultAggregation::resolve( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + switch (instrument_descriptor.type_) + { + case InstrumentType::kCounter: + case InstrumentType::kUpDownCounter: + case InstrumentType::kObservableUpDownCounter: + return SumAggregation::GetInstance(); + break; + case InstrumentType::kHistogram: + return HistogramAggregation::GetInstance(); + break; + case InstrumentType::kObservableGauge: + return LastValueAggregation::GetInstance(); + break; + default: + // TBD - Return drop aggregation + OTEL_INTERNAL_LOG_WARN( + "[METRICS] Aggregation: Invalid Instrument Type - Using DropAggregation "); + return DropAggregation::GetInstance(); + }; +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/view/drop_aggregation.cc b/sdk/src/metrics/view/drop_aggregation.cc new file mode 100644 index 0000000000..d2fe9575f7 --- /dev/null +++ b/sdk/src/metrics/view/drop_aggregation.cc @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/view/drop_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregator/drop_aggregator.h" +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr DropAggregation::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + return std::move(std::unique_ptr(new DropAggregator())); +} + +Aggregation &DropAggregation::GetInstance() +{ + static DropAggregation drop_aggregation; + return drop_aggregation; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/view/histogram_aggregation.cc b/sdk/src/metrics/view/histogram_aggregation.cc new file mode 100644 index 0000000000..285653adc3 --- /dev/null +++ b/sdk/src/metrics/view/histogram_aggregation.cc @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/view/histogram_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h" +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr HistogramAggregation::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + // default boundaries - + // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#explicit-bucket-histogram-aggregation + const std::vector boundaries = {0, 5, 10, 25, 50, 75, 100, 250, 500, 1000}; + std::unique_ptr aggregator = nullptr; + if (instrument_descriptor.valueType_ == InstrumentValueType::kLong) + { + const std::vector boundaries = {0, 5, 10, 25, 50, 75, 100, 250, 500, 1000}; + aggregator.reset(new LongHistogramAggregator(boundaries)); + } + else + { + const std::vector boundaries = {0, 5, 10, 25, 50, 75, 100, 250, 500, 1000}; + aggregator.reset(new DoubleHistogramAggregator(boundaries)); + } + return std::move(aggregator); +} + +Aggregation &HistogramAggregation::GetInstance() noexcept +{ + static HistogramAggregation histogram_aggregation; + return histogram_aggregation; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/src/metrics/view/lastvalue_aggregation.cc b/sdk/src/metrics/view/lastvalue_aggregation.cc new file mode 100644 index 0000000000..4853a474cf --- /dev/null +++ b/sdk/src/metrics/view/lastvalue_aggregation.cc @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/view/lastvalue_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h" +# include "opentelemetry/sdk/metrics/instruments.h" + +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr LastValueAggregation::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + auto aggregator = (instrument_descriptor.valueType_ == InstrumentValueType::kLong) + ? std::unique_ptr(new LongLastValueAggregator()) + : std::unique_ptr(new DoubleLastValueAggregator()); + return std::move(aggregator); +} + +Aggregation &LastValueAggregation::GetInstance() noexcept +{ + static LastValueAggregation lastvalue_aggregation; + return lastvalue_aggregation; +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/view/sum_aggregation.cc b/sdk/src/metrics/view/sum_aggregation.cc new file mode 100644 index 0000000000..25c27bca70 --- /dev/null +++ b/sdk/src/metrics/view/sum_aggregation.cc @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/view/sum_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.h" +# include "opentelemetry/sdk/metrics/instruments.h" + +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +std::unique_ptr SumAggregation::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + auto aggregator = + (instrument_descriptor.valueType_ == InstrumentValueType::kLong) + ? std::unique_ptr(new LongSumAggregator(instrument_descriptor)) + : std::unique_ptr(new DoubleSumAggregator(instrument_descriptor)); + return std::move(aggregator); +} + +Aggregation &SumAggregation::GetInstance() noexcept +{ + static SumAggregation sum_aggregation; + return sum_aggregation; +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 21c3d10716..1999adc479 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -8,6 +8,8 @@ # include "opentelemetry/sdk/metrics/view/instrument_selector.h" # include "opentelemetry/sdk/metrics/view/meter_selector.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" + using namespace opentelemetry::sdk::metrics; class MockMetricExporter : public MetricExporter