From 04ef417aa9bc27d0225c88a3e097e72a78410d88 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 14 Jan 2022 01:07:54 -0800 Subject: [PATCH 1/5] aggregator sdk --- api/include/opentelemetry/common/timestamp.h | 11 ++ .../sdk/metrics/aggregator/accumulation.h | 125 +++++++++++++ .../sdk/metrics/aggregator/aggregator.h | 13 +- .../metrics/aggregator/histogram_aggregator.h | 60 +++++++ .../aggregator/last_value_aggregator.h | 60 +++++++ .../sdk/metrics/aggregator/noop_aggregator.h | 51 ++++++ .../sdk/metrics/aggregator/sum_aggregator.h | 57 ++++++ .../opentelemetry/sdk/metrics/data/data.h | 29 +++ .../sdk/metrics/data/metric_data.h | 44 +++++ .../sdk/metrics/data/point_data.h | 169 ++++++++++++++++++ sdk/include/opentelemetry/sdk/metrics/meter.h | 5 +- .../opentelemetry/sdk/metrics/meter_context.h | 6 + .../sdk/metrics/state/metric_storage.h | 35 ++++ .../sdk/metrics/state/sync_metric_storage.h | 46 +++++ .../metrics/state/writable_metric_storage.h | 30 ++++ .../sdk/metrics/sync_instruments.h | 50 ++++++ .../sdk/metrics/view/aggregation.h | 46 ++++- sdk/src/metrics/meter.cc | 12 ++ sdk/src/metrics/meter_context.cc | 5 + sdk/test/metrics/meter_provider_sdk_test.cc | 2 + 20 files changed, 841 insertions(+), 15 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/data/data.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/data/metric_data.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/data/point_data.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/sync_instruments.h diff --git a/api/include/opentelemetry/common/timestamp.h b/api/include/opentelemetry/common/timestamp.h index 54e7b7aa6c..dfa5359d99 100644 --- a/api/include/opentelemetry/common/timestamp.h +++ b/api/include/opentelemetry/common/timestamp.h @@ -45,6 +45,17 @@ 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..be5c62fbb4 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h @@ -0,0 +1,125 @@ +// 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 +{ + +template +class Accumulation +{ + public: + virtual void Record(T value) noexcept = 0; +}; + +template +class SumAccumulation: public Accumulation +{ + public: + SumAccumulation(T value = 0):sum_(value){ + } + + void Record(T value) noexcept override { + sum_ += value; + } + + SingularPointData ToPointData(){ + return SingularPointData(sum_); + } + private: + T sum_; +}; + +template +class LastValueAccumulation: public Accumulation +{ + public: + LastValueAccumulation(T value = 0, opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()): + last_value_(value), ts_{ts} + { + } + + void Record(T value) noexcept override { + last_value_ = value; + ts_.Reset(std::chrono::system_clock::now()); + } + + const opentelemetry::common::SystemTimestamp& GetLastValueTimeStamp() const + { + return ts_; + } + + SingularPointData ToPointData() { + return SingularPointData(last_value_); + } + + private: + T last_value_; + opentelemetry::common::SystemTimestamp ts_; +}; + +template +class HistogramAccumulation: public Accumulation +{ + public: + HistogramAccumulation(std::vector& boundaries): + histogram_(boundaries){ + } + + void Record(T 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; + } + } + } + + HistogramPointData& ToPointData() { + return histogram_; + } + + private: + HistogramPointData histogram_; +}; + +template +class NoopAccumulation: public Accumulation +{ + public: + NoopAccumulation() { + } + + void Record(T value) noexcept override { + } + + NoopPointData ToPointData(){ + return NoopPointData(); + } + private: + T sum_; +}; + +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..daad53390d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h @@ -10,16 +10,17 @@ namespace sdk namespace metrics { +template class Aggregator { - // TBD -}; + public: + virtual T CreateAccumulation() noexcept = 0; -class NoOpAggregator : public Aggregator -{ - // TBD -}; + virtual T Merge(T& prev, T& current ) noexcept = 0; + virtual T diff(T& prev, T& current) noexcept = 0; + +}; } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE 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..4e0e0edb8c --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -0,0 +1,60 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +#include "opentelemetry/sdk/metrics/data/metric_data.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. + */ + template + class HistogramAggregator: public Aggregator> + { + // TBD - This clas is placeholder, and needs to be implemented + public: + HistogramAggregator(std::vector& boundaries):boundaries_(boundaries){} + HistogramAccumulation CreateAccumulation() noexcept override { + return HistogramAccumulation(boundaries_); + } + + /** Returns the result of the merge of the given accumulations.*/ + HistogramAccumulation Merge(HistogramAccumulation& prev, HistogramAccumulation& current ) noexcept override { + return HistogramAccumulation(boundaries_); + + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + HistogramAccumulation diff(HistogramAccumulation& prev, HistogramAccumulation& current) noexcept override { + return HistogramAccumulation(boundaries_) + } + + HistogramMetricsData 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 + ){ + HistogramMetricsData 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; + } + }; +} +} +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h new file mode 100644 index 0000000000..ba8eb7be20 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h @@ -0,0 +1,60 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +#include "opentelemetry/sdk/metrics/data/metric_data.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + /** Basic aggregator which calculates a last value from individual measurements. */ + template + class LastValueAggregator: public Aggregator> + { + public: + + LastValueAccumulation CreateAccumulation() noexcept override { + return new LastValueAccumulation(); + } + + /** Returns the result of the merge of the given accumulations.*/ + + LastValueAccumulation Merge(LastValueAccumulation& previous, LastValueAccumulation& current ) noexcept override { + auto latest_accumulation = + (current.GetLastValueTimeStamp().time_since_epoch() >= previous.GetLastValueTimeStamp().time_since_epoch())? + current : previous; + + return new LastValueAccumulation(latest_accumulation.toPointData().value_, latest_accumulation.GetLastValueTimeStamp()); + } + + /** Returns a new DELTA aggregation by comparing two cumulative measurements.*/ + LastValueAccumulation diff(LastValueAccumulation& prev, LastValueAccumulation& current) noexcept override { + return LastValueAccumulation(prev.ToPoint() - current.ToPoint()); + } + + SingularMetricsData 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 + ){ + SingularMetricsData 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; + } + }; +} +} +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h new file mode 100644 index 0000000000..421e39d3b8 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h @@ -0,0 +1,51 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +#include "opentelemetry/sdk/metrics/data/metric_data.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + /** Basic aggregator which calculates a Sum from individual measurements. */ + template + class NoopAggregator: public Aggregator> + { + public: + + NoopAccumulation CreateAccumulation() noexcept override { + return NoopAccumulation(); + } + + /** Returns the result of the merge of the given accumulations.*/ + NoopAccumulation Merge(NoopAccumulation& prev, NoopAccumulation& current ) noexcept override { + return SumAccumulation(); + + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + NoopAccumulation diff(NoopAccumulation& prev, NoopAccumulation& current) noexcept override { + return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + } + + NoopMetricData 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 + ){ + NoopMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor }; + return metrics_data; + } + }; +} +} +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..5d93f3bc05 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h @@ -0,0 +1,57 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +#include "opentelemetry/sdk/metrics/data/metric_data.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + /** Basic aggregator which calculates a Sum from individual measurements. */ + template + class SumAggregator: public Aggregator> + { + public: + + SumAccumulation CreateAccumulation() noexcept override { + return SumAccumulation(); + } + + /** Returns the result of the merge of the given accumulations.*/ + + SumAccumulation Merge(SumAccumulation& prev, SumAccumulation& current ) noexcept override { + return SumAccumulation(prev.ToPointData().value_ + current.ToPointData().value_); + + } + + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + SumAccumulation diff(SumAccumulation& prev, SumAccumulation& current) noexcept override { + return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + } + + SingularMetricsData 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 + ){ + SingularMetricsData 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; + } + }; +} +} +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..dbbcf0c0bf --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/data.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/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; + + }; + + +} +} +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..c83bd74bc5 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -0,0 +1,44 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +#include "opentelemetry/sdk/metrics/data/point_data.h" +# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.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 +{ + template + struct MetricData { + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + }; + + template + struct SingularMetricsData : public MetricData { + public: + std::vector>> point_data_list; + }; + + template + class HistogramMetricData: public MetricData { + public: + std::vector>> point_datas_list; + }; + + template + class NoopMetricData: public MetricData + { + }; +} +} +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..ae829acb7a --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -0,0 +1,169 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +#include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/common/attribute_utils.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_; +}; + +template +class AbstractPointData +{ +}; + +template +struct SingularPointData : public AbstractPointData +{ + SingularPointData(T value):value_(value) {} + T value_; +}; + +template +struct HistogramPointData: public AbstractPointData +{ + HistogramPointData(std::vector& boundaries): + boundaries_{boundaries}, + counts_(boundaries.size() + 1, 0), + sum_(0), count_(0) + { + } + std::vector boundaries_; + std::vector counts_; + T sum_; + uint64_t count_; +}; + +template +struct NoopPointData: public AbstractPointData +{ +}; + +} // 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/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index b1a4e304d7..a2c8f531b2 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -6,6 +6,8 @@ # include # include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/state/writable_metric_storage.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/meter_context.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_; + WritableMatricStorage& 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..02685b34e3 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..4e8b56eabc --- /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/version.h" +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.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; +} +} +} +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..3d36a66e7d --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -0,0 +1,46 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/version.h" +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.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 + ) + { + + } +} +} +} +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..3e5f98b973 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# 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 common::KeyValueIterable &attributes) noexcept = 0; +}; + +template +class NoopWritableMetricsStorage: public WritableMetricStorage { + public: + void Record(T value) noexcept override {} + void Record(T value, const 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..5296eba17b --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -0,0 +1,50 @@ +// 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/state/writable_metrics_storage.h" +# include "opentelemetry/sdk/metrics/instruments.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 +} // 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..03f7bae17a 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h @@ -6,43 +6,75 @@ # include # include "opentelemetry/sdk/metrics/aggregator/aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h" +#include "opentelemetry/sdk/metrics/aggregator/noop_aggregator.h" + # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { + +template class Aggregation { public: virtual ~Aggregation() = default; - virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( + virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept = 0; }; -class NoOpAggregation : public Aggregation +template +class SumAggregation: public Aggregation +{ + public: + virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override + { + static SumAggregator sum_aggregator; + return sum_aggregator; + } +}; + +template +class LastValueAggregation: public Aggregation { + public: + virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override + { + static LastValueAggregator last_value_aggregator; + return last_value_aggregator; + } +}; - opentelemetry::sdk::metrics::Aggregator &CreateAggregator( +template +class NoopAggregation : public Aggregation +{ + opentelemetry::sdk::metrics::Aggregator &CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override { - static opentelemetry::sdk::metrics::NoOpAggregator noop_aggregator; + static NoopAggregator noop_aggregator; return noop_aggregator; } }; -class DefaultAggregation : public Aggregation +template +class DefaultAggregation : public Aggregation { - opentelemetry::sdk::metrics::Aggregator &CreateAggregator( + 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; + static NoopAggregator noop_aggregator; return noop_aggregator; } }; + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 6c759158a4..52cb8561d2 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -29,6 +29,8 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::string_view description, nostd::string_view unit) noexcept { + InstrumentDescriptor instrument_descriptor = {name, description, unit, 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 +160,16 @@ const sdk::instrumentationlibrary::InstrumentationLibrary &Meter::GetInstrumenta return *instrumentation_library_; } +WritableMatricsStorage& Meter::RegisterMetricsStorage(InstrumentDescriptor& instrument_descriptor) { + 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..490a3b9965 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& GetViewRegistry() const noexcept +{ + return *views_.get(); +} + void MeterContext::AddMetricExporter(std::unique_ptr exporter) noexcept { exporters_.push_back(std::move(exporter)); diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 21c3d10716..b14712621e 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 From 67e81d45b1a5569815c2016502ceba28a6ac4f69 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 14 Jan 2022 01:13:15 -0800 Subject: [PATCH 2/5] format --- api/include/opentelemetry/common/timestamp.h | 7 +- .../sdk/metrics/aggregator/accumulation.h | 159 ++++++++---------- .../sdk/metrics/aggregator/aggregator.h | 11 +- .../metrics/aggregator/histogram_aggregator.h | 88 +++++----- .../aggregator/last_value_aggregator.h | 96 ++++++----- .../sdk/metrics/aggregator/noop_aggregator.h | 71 ++++---- .../sdk/metrics/aggregator/sum_aggregator.h | 82 ++++----- .../opentelemetry/sdk/metrics/data/data.h | 27 ++- .../sdk/metrics/data/metric_data.h | 52 +++--- .../sdk/metrics/data/point_data.h | 57 +++---- sdk/include/opentelemetry/sdk/metrics/meter.h | 6 +- .../opentelemetry/sdk/metrics/meter_context.h | 2 +- .../sdk/metrics/state/metric_storage.h | 32 ++-- .../sdk/metrics/state/sync_metric_storage.h | 37 ++-- .../metrics/state/writable_metric_storage.h | 18 +- .../sdk/metrics/sync_instruments.h | 42 ++--- .../sdk/metrics/view/aggregation.h | 31 ++-- sdk/src/metrics/meter.cc | 20 +-- sdk/src/metrics/meter_context.cc | 2 +- sdk/test/metrics/meter_provider_sdk_test.cc | 2 +- 20 files changed, 421 insertions(+), 421 deletions(-) diff --git a/api/include/opentelemetry/common/timestamp.h b/api/include/opentelemetry/common/timestamp.h index dfa5359d99..8e45ef0c74 100644 --- a/api/include/opentelemetry/common/timestamp.h +++ b/api/include/opentelemetry/common/timestamp.h @@ -45,15 +45,16 @@ 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()); + nanos_since_epoch_ = static_cast( + std::chrono::duration_cast(time_point.time_since_epoch()) + .count()); } /** diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h index be5c62fbb4..61ca443c67 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h @@ -3,12 +3,12 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -#include "opentelemetry/common/timestamp.h" -#include "opentelemetry/sdk/metrics/data/point_data.h" -#include "opentelemetry/version.h" +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/sdk/metrics/data/point_data.h" +# include "opentelemetry/version.h" -#include -#include +# include +# include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { @@ -18,105 +18,94 @@ namespace metrics template class Accumulation { - public: - virtual void Record(T value) noexcept = 0; +public: + virtual void Record(T value) noexcept = 0; }; -template -class SumAccumulation: public Accumulation +template +class SumAccumulation : public Accumulation { - public: - SumAccumulation(T value = 0):sum_(value){ - } - - void Record(T value) noexcept override { - sum_ += value; - } - - SingularPointData ToPointData(){ - return SingularPointData(sum_); - } - private: - T sum_; +public: + SumAccumulation(T value = 0) : sum_(value) {} + + void Record(T value) noexcept override { sum_ += value; } + + SingularPointData ToPointData() { return SingularPointData(sum_); } + +private: + T sum_; }; template -class LastValueAccumulation: public Accumulation +class LastValueAccumulation : public Accumulation { - public: - LastValueAccumulation(T value = 0, opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()): - last_value_(value), ts_{ts} - { - } - - void Record(T value) noexcept override { - last_value_ = value; - ts_.Reset(std::chrono::system_clock::now()); - } - - const opentelemetry::common::SystemTimestamp& GetLastValueTimeStamp() const - { - return ts_; - } - - SingularPointData ToPointData() { - return SingularPointData(last_value_); - } - - private: - T last_value_; - opentelemetry::common::SystemTimestamp ts_; +public: + LastValueAccumulation( + T value = 0, + opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()) + : last_value_(value), ts_{ts} + {} + + void Record(T value) noexcept override + { + last_value_ = value; + ts_.Reset(std::chrono::system_clock::now()); + } + + const opentelemetry::common::SystemTimestamp &GetLastValueTimeStamp() const { return ts_; } + + SingularPointData ToPointData() { return SingularPointData(last_value_); } + +private: + T last_value_; + opentelemetry::common::SystemTimestamp ts_; }; template -class HistogramAccumulation: public Accumulation +class HistogramAccumulation : public Accumulation { - public: - HistogramAccumulation(std::vector& boundaries): - histogram_(boundaries){ - } - - void Record(T 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; - } - } - } - - HistogramPointData& ToPointData() { - return histogram_; - } - - private: - HistogramPointData histogram_; +public: + HistogramAccumulation(std::vector &boundaries) : histogram_(boundaries) {} + + void Record(T 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; + } + } + } + + HistogramPointData &ToPointData() { return histogram_; } + +private: + HistogramPointData histogram_; }; -template -class NoopAccumulation: public Accumulation +template +class NoopAccumulation : public Accumulation { - public: - NoopAccumulation() { - } - - void Record(T value) noexcept override { - } - - NoopPointData ToPointData(){ - return NoopPointData(); - } - private: - T sum_; +public: + NoopAccumulation() {} + + void Record(T value) noexcept override {} + + NoopPointData ToPointData() { return NoopPointData(); } + +private: + T sum_; }; template struct AccumulationRecord { - T accumulation; - opentelemetry::sdk::common::AttributeMap attributes; + T accumulation; + opentelemetry::sdk::common::AttributeMap attributes; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h index daad53390d..aba63eb310 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h @@ -10,16 +10,15 @@ namespace sdk namespace metrics { -template +template class Aggregator { - public: - virtual T CreateAccumulation() noexcept = 0; +public: + virtual T CreateAccumulation() noexcept = 0; - virtual T Merge(T& prev, T& current ) noexcept = 0; - - virtual T diff(T& prev, T& current) noexcept = 0; + virtual T Merge(T &prev, T ¤t) noexcept = 0; + virtual T diff(T &prev, T ¤t) noexcept = 0; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h index 4e0e0edb8c..3081bf3348 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -3,58 +3,64 @@ #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" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" -#include "opentelemetry/sdk/metrics/data/metric_data.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. */ - template - class HistogramAggregator: public Aggregator> - { - // TBD - This clas is placeholder, and needs to be implemented - public: - HistogramAggregator(std::vector& boundaries):boundaries_(boundaries){} - HistogramAccumulation CreateAccumulation() noexcept override { - return HistogramAccumulation(boundaries_); - } - - /** Returns the result of the merge of the given accumulations.*/ - HistogramAccumulation Merge(HistogramAccumulation& prev, HistogramAccumulation& current ) noexcept override { - return HistogramAccumulation(boundaries_); +template +class HistogramAggregator : public Aggregator> +{ + // TBD - This clas is placeholder, and needs to be implemented +public: + HistogramAggregator(std::vector &boundaries) : boundaries_(boundaries) {} + HistogramAccumulation CreateAccumulation() noexcept override + { + return HistogramAccumulation(boundaries_); + } - } + /** Returns the result of the merge of the given accumulations.*/ + HistogramAccumulation Merge(HistogramAccumulation &prev, + HistogramAccumulation ¤t) noexcept override + { + return HistogramAccumulation(boundaries_); + } - /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - HistogramAccumulation diff(HistogramAccumulation& prev, HistogramAccumulation& current) noexcept override { - return HistogramAccumulation(boundaries_) - } + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + HistogramAccumulation diff(HistogramAccumulation &prev, + HistogramAccumulation ¤t) noexcept override{ + return HistogramAccumulation(boundaries_)} - HistogramMetricsData 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 - ){ - HistogramMetricsData 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; - } - }; -} -} + HistogramMetricsData 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) + { + HistogramMetricsData 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/last_value_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h index ba8eb7be20..7b5884629d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h @@ -3,58 +3,68 @@ #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" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" -#include "opentelemetry/sdk/metrics/data/metric_data.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { - /** Basic aggregator which calculates a last value from individual measurements. */ - template - class LastValueAggregator: public Aggregator> - { - public: - - LastValueAccumulation CreateAccumulation() noexcept override { - return new LastValueAccumulation(); - } +/** Basic aggregator which calculates a last value from individual measurements. */ +template +class LastValueAggregator : public Aggregator> +{ +public: + LastValueAccumulation CreateAccumulation() noexcept override + { + return new LastValueAccumulation(); + } - /** Returns the result of the merge of the given accumulations.*/ + /** Returns the result of the merge of the given accumulations.*/ - LastValueAccumulation Merge(LastValueAccumulation& previous, LastValueAccumulation& current ) noexcept override { - auto latest_accumulation = - (current.GetLastValueTimeStamp().time_since_epoch() >= previous.GetLastValueTimeStamp().time_since_epoch())? - current : previous; - - return new LastValueAccumulation(latest_accumulation.toPointData().value_, latest_accumulation.GetLastValueTimeStamp()); - } + LastValueAccumulation Merge(LastValueAccumulation &previous, + LastValueAccumulation ¤t) noexcept override + { + auto latest_accumulation = (current.GetLastValueTimeStamp().time_since_epoch() >= + previous.GetLastValueTimeStamp().time_since_epoch()) + ? current + : previous; - /** Returns a new DELTA aggregation by comparing two cumulative measurements.*/ - LastValueAccumulation diff(LastValueAccumulation& prev, LastValueAccumulation& current) noexcept override { - return LastValueAccumulation(prev.ToPoint() - current.ToPoint()); - } + return new LastValueAccumulation(latest_accumulation.toPointData().value_, + latest_accumulation.GetLastValueTimeStamp()); + } - SingularMetricsData 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 - ){ - SingularMetricsData 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; - } - }; -} -} + /** Returns a new DELTA aggregation by comparing two cumulative measurements.*/ + LastValueAccumulation diff(LastValueAccumulation &prev, + LastValueAccumulation ¤t) noexcept override + { + return LastValueAccumulation(prev.ToPoint() - current.ToPoint()); + } + + SingularMetricsData 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) + { + SingularMetricsData 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/noop_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h index 421e39d3b8..83b40773d9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h @@ -3,49 +3,50 @@ #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" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" -#include "opentelemetry/sdk/metrics/data/metric_data.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { - /** Basic aggregator which calculates a Sum from individual measurements. */ - template - class NoopAggregator: public Aggregator> - { - public: - - NoopAccumulation CreateAccumulation() noexcept override { - return NoopAccumulation(); - } - - /** Returns the result of the merge of the given accumulations.*/ - NoopAccumulation Merge(NoopAccumulation& prev, NoopAccumulation& current ) noexcept override { - return SumAccumulation(); +/** Basic aggregator which calculates a Sum from individual measurements. */ +template +class NoopAggregator : public Aggregator> +{ +public: + NoopAccumulation CreateAccumulation() noexcept override { return NoopAccumulation(); } - } + /** Returns the result of the merge of the given accumulations.*/ + NoopAccumulation Merge(NoopAccumulation &prev, + NoopAccumulation ¤t) noexcept override + { + return SumAccumulation(); + } - /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - NoopAccumulation diff(NoopAccumulation& prev, NoopAccumulation& current) noexcept override { - return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); - } + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + NoopAccumulation diff(NoopAccumulation &prev, + NoopAccumulation ¤t) noexcept override + { + return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + } - NoopMetricData 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 - ){ - NoopMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor }; - return metrics_data; - } - }; -} -} + NoopMetricData 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) + { + NoopMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; + 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 index 5d93f3bc05..4af9fb1337 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h @@ -3,55 +3,57 @@ #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" -#include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" -#include "opentelemetry/sdk/metrics/data/metric_data.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { - /** Basic aggregator which calculates a Sum from individual measurements. */ - template - class SumAggregator: public Aggregator> - { - public: - - SumAccumulation CreateAccumulation() noexcept override { - return SumAccumulation(); - } - - /** Returns the result of the merge of the given accumulations.*/ +/** Basic aggregator which calculates a Sum from individual measurements. */ +template +class SumAggregator : public Aggregator> +{ +public: + SumAccumulation CreateAccumulation() noexcept override { return SumAccumulation(); } - SumAccumulation Merge(SumAccumulation& prev, SumAccumulation& current ) noexcept override { - return SumAccumulation(prev.ToPointData().value_ + current.ToPointData().value_); + /** Returns the result of the merge of the given accumulations.*/ - } + SumAccumulation Merge(SumAccumulation &prev, SumAccumulation ¤t) noexcept override + { + return SumAccumulation(prev.ToPointData().value_ + current.ToPointData().value_); + } - /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - SumAccumulation diff(SumAccumulation& prev, SumAccumulation& current) noexcept override { - return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); - } + /** Returns a new delta aggregation by comparing two cumulative measurements.*/ + SumAccumulation diff(SumAccumulation &prev, SumAccumulation ¤t) noexcept override + { + return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + } - SingularMetricsData 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 - ){ - SingularMetricsData 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; - } - }; -} -} + SingularMetricsData 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) + { + SingularMetricsData 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 index dbbcf0c0bf..14e66e70c6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/data.h @@ -3,27 +3,24 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -#include "opentelemetry/sdk/metrics/point_data.h" +# 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; - - }; +/** + * 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 index c83bd74bc5..88fccc9d2e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -3,42 +3,44 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -#include "opentelemetry/sdk/metrics/data/point_data.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" -# include "opentelemetry/sdk/metrics/instruments.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 +# include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { - template - struct MetricData { - opentelemetry::sdk::resource::Resource *resource_; - opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; - InstrumentDescriptor instrument_descriptor; - }; +template +struct MetricData +{ + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; +}; - template - struct SingularMetricsData : public MetricData { - public: - std::vector>> point_data_list; - }; +template +struct SingularMetricsData : public MetricData +{ +public: + std::vector>> point_data_list; +}; - template - class HistogramMetricData: public MetricData { - public: - std::vector>> point_datas_list; - }; +template +class HistogramMetricData : public MetricData +{ +public: + std::vector>> point_datas_list; +}; - template - class NoopMetricData: public MetricData - { - }; -} -} +template +class NoopMetricData : public MetricData +{}; +} // 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 index ae829acb7a..eadfa8bedb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -3,10 +3,9 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/version.h" -#include "opentelemetry/common/timestamp.h" +# include "opentelemetry/common/timestamp.h" # include "opentelemetry/sdk/common/attribute_utils.h" - +# include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -20,43 +19,39 @@ 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_; +struct BasePointData +{ + opentelemetry::common::SystemTimestamp start_epoch_nanos_; + opentelemetry::common::SystemTimestamp end_epoch_nanos_; + PointAttributes attributes_; }; template -class AbstractPointData -{ -}; +class AbstractPointData +{}; template struct SingularPointData : public AbstractPointData { - SingularPointData(T value):value_(value) {} - T value_; + SingularPointData(T value) : value_(value) {} + T value_; }; template -struct HistogramPointData: public AbstractPointData +struct HistogramPointData : public AbstractPointData { - HistogramPointData(std::vector& boundaries): - boundaries_{boundaries}, - counts_(boundaries.size() + 1, 0), - sum_(0), count_(0) - { - } - std::vector boundaries_; - std::vector counts_; - T sum_; - uint64_t count_; + HistogramPointData(std::vector &boundaries) + : boundaries_{boundaries}, counts_(boundaries.size() + 1, 0), sum_(0), count_(0) + {} + std::vector boundaries_; + std::vector counts_; + T sum_; + uint64_t count_; }; template -struct NoopPointData: public AbstractPointData -{ -}; +struct NoopPointData : public AbstractPointData +{}; } // namespace metrics } // namespace sdk @@ -86,7 +81,7 @@ OPENTELEMETRY_END_NAMESPACE //Returns the attributes associated with this Point PointAttributes& GetAttributes() noexcept { - return attributes_; + return attributes_; } private: @@ -98,7 +93,7 @@ OPENTELEMETRY_END_NAMESPACE template class SingularPointData: public PointData { public: - SingularPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + SingularPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, opentelemetry::common::SystemTimestamp& epoch_nanos, PointAttributes& attributes, T value): @@ -117,7 +112,7 @@ class SingularPointData: public PointData { template class SummaryPointData: public PointData { public: - SummaryPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + SummaryPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, opentelemetry::common::SystemTimestamp& epoch_nanos, PointAttributes& attributes, uint64_t count, @@ -140,7 +135,7 @@ class SummaryPointData: public PointData { template class HistogramPointData : public PointData { - HistogramPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, + HistogramPointData(opentelemetry::common::SystemTimestamp& start_epoch_nanos, opentelemetry::common::SystemTimestamp& epoch_nanos, PointAttributes& attributes, T sum, @@ -165,5 +160,5 @@ class HistogramPointData : public PointData { };*/ //} // namespace metrics //} // namespace sdk -//OPENTELEMETRY_END_NAMESPACE +// OPENTELEMETRY_END_NAMESPACE //#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index a2c8f531b2..ec7877abd8 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -6,9 +6,9 @@ # include # include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" -# include "opentelemetry/sdk/metrics/state/writable_metric_storage.h" -# include "opentelemetry/sdk/metrics/instruments.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" @@ -102,7 +102,7 @@ class Meter final : public opentelemetry::metrics::Meter private: std::shared_ptr instrumentation_library_; std::shared_ptr context_; - WritableMatricStorage& RegisterMetricStorage(InstrumentDescriptor&); + WritableMatricStorage &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 02685b34e3..9a6d57f6ed 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -50,7 +50,7 @@ class MeterContext * 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; + const opentelemetry::sdk::metrics::ViewRegistry &GetViewRegistry() const noexcept; /** * Attaches a metric exporter to list of configured exporters for this Meter context. diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index 4e8b56eabc..47ad7c5861 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -3,9 +3,9 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/metrics/" +# include "opentelemetry/sdk/metrics/" +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" # include "opentelemetry/version.h" -# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -16,20 +16,20 @@ 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; -} -} +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 index 3d36a66e7d..d657da83be 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -3,15 +3,15 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" # include "opentelemetry/version.h" -# include "opentelemetry/sdk/metrics/state/sync_metrics_storage.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { - /* +/* * Stores and aggregates {@link MetricData} for synchronous instruments. */ @@ -19,28 +19,21 @@ template class SyncMetricStorage : public WritableMetricStorage, MetricsStorage { - void Record(T value) noexcept override { + void Record(T value) noexcept override {} - } + void Record(T value, const common::KeyValueIterable &attributes) 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 - ) - { - - } -} -} + /* 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 index 3e5f98b973..533e4637cf 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h @@ -11,17 +11,19 @@ namespace metrics { template -class WritableMetricStorage{ - public: - virtual void Record(T value) noexcept = 0; - virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; +class WritableMetricStorage +{ +public: + virtual void Record(T value) noexcept = 0; + virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; }; template -class NoopWritableMetricsStorage: public WritableMetricStorage { - public: - void Record(T value) noexcept override {} - void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} +class NoopWritableMetricsStorage : public WritableMetricStorage +{ +public: + void Record(T value) noexcept override {} + void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index 5296eba17b..1a3e340f45 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -5,11 +5,10 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/metrics/sync_instruments.h" -# include "opentelemetry/sdk/metrics/state/writable_metrics_storage.h" -# include "opentelemetry/sdk/metrics/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 { @@ -20,31 +19,36 @@ 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)} + 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 + void Add(T value) noexcept override { - if (value < 0){ - //TBD -warning - return; - } - writable_storage_->Record(value); + if (value < 0) + { + // TBD -warning + return; + } + writable_storage_->Record(value); } - void Add(T value, const common::KeyValueIterable &attributes) noexcept override + void Add(T value, const common::KeyValueIterable &attributes) noexcept override { - if (value < 0){ - //TBD -warning - return; - } - writable_storage_->Record(value, attributes); + if (value < 0) + { + // TBD -warning + return; + } + writable_storage_->Record(value, attributes); } + private: std::unique_ptr instrument_descriptor_; std::unique_ptr> writable_storage_; -}; +}; } // namespace metrics -} // sdk +} // 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 03f7bae17a..1eea0d9d0b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h @@ -6,9 +6,9 @@ # include # include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h" -#include "opentelemetry/sdk/metrics/aggregator/noop_aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/noop_aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.h" # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -27,27 +27,27 @@ class Aggregation }; template -class SumAggregation: public Aggregation +class SumAggregation : public Aggregation { - public: +public: virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static SumAggregator sum_aggregator; - return sum_aggregator; - } + { + static SumAggregator sum_aggregator; + return sum_aggregator; + } }; template -class LastValueAggregation: public Aggregation +class LastValueAggregation : public Aggregation { - public: +public: virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static LastValueAggregator last_value_aggregator; - return last_value_aggregator; - } + { + static LastValueAggregator last_value_aggregator; + return last_value_aggregator; + } }; template @@ -74,7 +74,6 @@ class DefaultAggregation : public Aggregation } }; - } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 52cb8561d2..6dadc11ed8 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -29,8 +29,9 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::string_view description, nostd::string_view unit) noexcept { - InstrumentDescriptor instrument_descriptor = {name, description, unit, InstrumentType::kCounter, InstrumentValueType::kLong }; - auto storage = registerMetricStorage(instrument_descriptor); + InstrumentDescriptor instrument_descriptor = {name, description, unit, 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)}; @@ -160,16 +161,15 @@ const sdk::instrumentationlibrary::InstrumentationLibrary &Meter::GetInstrumenta return *instrumentation_library_; } -WritableMatricsStorage& Meter::RegisterMetricsStorage(InstrumentDescriptor& instrument_descriptor) { - std::vector views; - context_.GetViewRegistry().FindViews(instrument_descriptor, *instrumentation_library_, - [](const View &view) { - views.push_back(view); - - }); +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 490a3b9965..fff7b1b292 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -28,7 +28,7 @@ const resource::Resource &MeterContext::GetResource() const noexcept return resource_; } -const opentelemetry::sdk::metrics::ViewRegistry& GetViewRegistry() const noexcept +const opentelemetry::sdk::metrics::ViewRegistry &GetViewRegistry() const noexcept { return *views_.get(); } diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index b14712621e..1999adc479 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -8,7 +8,7 @@ # include "opentelemetry/sdk/metrics/view/instrument_selector.h" # include "opentelemetry/sdk/metrics/view/meter_selector.h" -#include "opentelemetry/sdk/metrics/aggregator/accumulation.h" +# include "opentelemetry/sdk/metrics/aggregator/accumulation.h" using namespace opentelemetry::sdk::metrics; From 7c9164f2a2670f973c8ada31e0aac73a9ac41041 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 17 Jan 2022 23:49:50 -0800 Subject: [PATCH 3/5] more changes --- .../sdk/metrics/aggregator/accumulation.h | 8 +-- .../sdk/metrics/aggregator/aggregator.h | 1 + .../{noop_aggregator.h => drop_aggregator.h} | 24 ++++---- .../metrics/aggregator/histogram_aggregator.h | 2 +- ...ue_aggregator.h => lastvalue_aggregator.h} | 0 .../sdk/metrics/aggregator/sum_aggregator.h | 1 + .../sdk/metrics/data/metric_data.h | 2 +- .../sdk/metrics/data/point_data.h | 2 +- .../opentelemetry/sdk/metrics/instruments.h | 2 - .../sdk/metrics/view/aggregation.h | 57 +------------------ .../sdk/metrics/view/default_aggregation.h | 25 ++++++++ .../sdk/metrics/view/drop_aggregation.h | 33 +++++++++++ .../sdk/metrics/view/histogram_aggregation.h | 32 +++++++++++ .../sdk/metrics/view/lastvalue_aggregation.h | 32 +++++++++++ .../sdk/metrics/view/sum_aggregation.h | 32 +++++++++++ sdk/src/metrics/CMakeLists.txt | 11 +++- sdk/src/metrics/meter.cc | 4 +- sdk/src/metrics/view/default_aggregation.cc | 53 +++++++++++++++++ sdk/src/metrics/view/drop_aggregation.cc | 31 ++++++++++ sdk/src/metrics/view/histogram_aggregation.cc | 37 ++++++++++++ sdk/src/metrics/view/lastvalue_aggregtion.cc | 33 +++++++++++ sdk/src/metrics/view/sum_aggregation.cc | 34 +++++++++++ 22 files changed, 377 insertions(+), 79 deletions(-) rename sdk/include/opentelemetry/sdk/metrics/aggregator/{noop_aggregator.h => drop_aggregator.h} (63%) rename sdk/include/opentelemetry/sdk/metrics/aggregator/{last_value_aggregator.h => lastvalue_aggregator.h} (100%) create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/drop_aggregation.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/lastvalue_aggregation.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h create mode 100644 sdk/src/metrics/view/default_aggregation.cc create mode 100644 sdk/src/metrics/view/drop_aggregation.cc create mode 100644 sdk/src/metrics/view/histogram_aggregation.cc create mode 100644 sdk/src/metrics/view/lastvalue_aggregtion.cc create mode 100644 sdk/src/metrics/view/sum_aggregation.cc diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h index 61ca443c67..f082600ccc 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h @@ -65,7 +65,7 @@ template class HistogramAccumulation : public Accumulation { public: - HistogramAccumulation(std::vector &boundaries) : histogram_(boundaries) {} + HistogramAccumulation(const std::vector &boundaries) : histogram_(boundaries) {} void Record(T value) noexcept override { @@ -88,14 +88,14 @@ class HistogramAccumulation : public Accumulation }; template -class NoopAccumulation : public Accumulation +class DropAccumulation : public Accumulation { public: - NoopAccumulation() {} + DropAccumulation() {} void Record(T value) noexcept override {} - NoopPointData ToPointData() { return NoopPointData(); } + DropPointData ToPointData() { return DropPointData(); } private: T sum_; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h index aba63eb310..85c4780d31 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h @@ -3,6 +3,7 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h similarity index 63% rename from sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h rename to sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h index 83b40773d9..f628b15eee 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/noop_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h @@ -12,37 +12,37 @@ namespace sdk { namespace metrics { -/** Basic aggregator which calculates a Sum from individual measurements. */ +/** Aggregators to drop the instruments. */ template -class NoopAggregator : public Aggregator> +class DropAggregator : public Aggregator> { public: - NoopAccumulation CreateAccumulation() noexcept override { return NoopAccumulation(); } + DropAccumulation CreateAccumulation() noexcept override { return DropAccumulation(); } /** Returns the result of the merge of the given accumulations.*/ - NoopAccumulation Merge(NoopAccumulation &prev, - NoopAccumulation ¤t) noexcept override + DropAccumulation Merge(DropAccumulation &prev, + DropAccumulation ¤t) noexcept override { - return SumAccumulation(); + return DropAccumulation(); } /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - NoopAccumulation diff(NoopAccumulation &prev, - NoopAccumulation ¤t) noexcept override + DropAccumulation diff(DropAccumulation &prev, + DropAccumulation ¤t) noexcept override { - return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + return DropAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); } - NoopMetricData ToMetricData( + DropMetricData ToMetricData( opentelemetry::sdk::resource::Resource *resource, opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - std::vector>> + std::vector>> &accumulation_by_attributes, opentelemetry::common::SystemTimestamp &start_epoch_ns, opentelemetry::common::SystemTimestamp &end_epoch_ns) { - NoopMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; + DropMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; return metrics_data; } }; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h index 3081bf3348..c78a4293d6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -21,7 +21,7 @@ class HistogramAggregator : public Aggregator> { // TBD - This clas is placeholder, and needs to be implemented public: - HistogramAggregator(std::vector &boundaries) : boundaries_(boundaries) {} + HistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} HistogramAccumulation CreateAccumulation() noexcept override { return HistogramAccumulation(boundaries_); diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h similarity index 100% rename from sdk/include/opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h rename to sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h index 4af9fb1337..b20e152846 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h @@ -17,6 +17,7 @@ template class SumAggregator : public Aggregator> { public: + SumAggregator(InstrumentDescriptor &instrument_descriptor) {} SumAccumulation CreateAccumulation() noexcept override { return SumAccumulation(); } /** Returns the result of the merge of the given accumulations.*/ diff --git a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h index 88fccc9d2e..4455911785 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -38,7 +38,7 @@ class HistogramMetricData : public MetricData }; template -class NoopMetricData : public MetricData +class DropMetricData : public MetricData {}; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h index eadfa8bedb..aa0e5ebaf1 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -50,7 +50,7 @@ struct HistogramPointData : public AbstractPointData }; template -struct NoopPointData : public AbstractPointData +struct DropPointData : public AbstractPointData {}; } // namespace metrics 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/view/aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h index 1eea0d9d0b..0bc1b39693 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h @@ -6,10 +6,8 @@ # include # include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/last_value_aggregator.h" +# include "opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h" # include "opentelemetry/sdk/metrics/aggregator/noop_aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.h" - # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -17,61 +15,12 @@ namespace sdk namespace metrics { -template class Aggregation { public: virtual ~Aggregation() = default; - virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept = 0; -}; - -template -class SumAggregation : public Aggregation -{ -public: - virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static SumAggregator sum_aggregator; - return sum_aggregator; - } -}; - -template -class LastValueAggregation : public Aggregation -{ -public: - virtual opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static LastValueAggregator last_value_aggregator; - return last_value_aggregator; - } -}; - -template -class NoopAggregation : public Aggregation -{ - opentelemetry::sdk::metrics::Aggregator &CreateAggregator( - opentelemetry::sdk::metrics::InstrumentDescriptor instrument_descriptor) noexcept override - { - static NoopAggregator noop_aggregator; - return noop_aggregator; - } -}; - -template -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 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..61c7bdb2a9 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h @@ -0,0 +1,25 @@ +// 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 + +class DefaultAggregation : public Aggregation +{ +public: + std::unique_ptr CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; + +private: + static opentelemetry::sdk::metrics::Aggregation &resolve( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept; +}; +} +} +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..532c13283d --- /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: + 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/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..8473f34433 --- /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(); + +private: + SumAggregation() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index d1d7ae97bc..efdf6cbb70 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 + sum_aggregation.cc + lastvalue_aggregation.cc + histogram_aggregation.cc + drop_aggregation.cc + 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 6dadc11ed8..bcc339e2fb 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -165,9 +165,7 @@ WritableMatricsStorage &Meter::RegisterMetricsStorage(InstrumentDescriptor &inst { std::vector < std::vector views; context_.GetViewRegistry().FindViews(instrument_descriptor, *instrumentation_library_, - [](const View &view) { - views.push_back(view); - }); + [](const View &view) { views.push_back(view); }); } } // namespace metrics diff --git a/sdk/src/metrics/view/default_aggregation.cc b/sdk/src/metrics/view/default_aggregation.cc new file mode 100644 index 0000000000..d262c64264 --- /dev/null +++ b/sdk/src/metrics/view/default_aggregation.cc @@ -0,0 +1,53 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/view/drop_aggregation.h" +# include "opentelemetry/sdk/view/histogram_aggregation.h" +# include "opentelemetry/sdk/view/lastvalue_aggregation.h" +# include "opentelemetry/sdk/view/sum_aggregation.h" + +# include "opentelemetry/sdk_config.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +std::unique_ptr DefaultAggregator::CreateAggregator( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept +{ + return resolve(instrument_descriptor).CreateAggregator(instrument_descriptor); +} + +static opentelemetry::sdk::metrics::Aggregation &DefaultAggregator::resolve( + opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept; +{ + switch (instrument_descriptor.type_) + { + case InstrumentType.kCounter: + case InstrumentType.kUpDownCounter: + 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_WARNING("[METRICS] Aggregation: Instrument Type - " + << static_cast(instrument_descriptor.type_)) + << " - not supported. NoopAggregation will be used." return DropAggregation::GetInstance() + .CreateAggregator(instrument_descriptor); + }; +} +} // 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..39a6a5c5df --- /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/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(instrument_descriptor))); +} + +static 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..80201eb608 --- /dev/null +++ b/sdk/src/metrics/view/histogram_aggregation.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/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}; + auto aggregator = (instrument_descriptor.valueType_ == InstrumentValueType.kLong) + ? std::unique_ptr(new HitogramAggregation(boundaries)) + : std::unique_ptr(new HistogramAggregation(boundaries)); + return std::move(aggregator); +} + +static Aggregation &HistogramAggregation::GetInstance() noexcept +{ + static LastValueAggregation lastvalue_aggregation; + return lastvalue_aggregation; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/src/metrics/view/lastvalue_aggregtion.cc b/sdk/src/metrics/view/lastvalue_aggregtion.cc new file mode 100644 index 0000000000..6cd29f2d3a --- /dev/null +++ b/sdk/src/metrics/view/lastvalue_aggregtion.cc @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h" +# include "opentelemetry/sdk/view/lastvalue_aggregation.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 LastValueAggregation()) + : std::unique_ptr(new LastValueAggregation()); + return std::move(aggregator); +} + +static 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..8cdd1cba35 --- /dev/null +++ b/sdk/src/metrics/view/sum_aggregation.cc @@ -0,0 +1,34 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/view/sum_aggregation.h" +# include "opentelemetry/sdk/metrics/aggregator/sum_aggregator.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 SumAggregator(instrument_descriptor)) + : std::unique_ptr(new SumAggregator(instrument_descriptor)); + return std::move(aggregator); +} + +static Aggregation &SumAggregation::GetInstance() noexecept +{ + static SumAggregation sum_aggregation; + return sum_aggregation; +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file From 5a37512a9bf7895f2ce4aec27d07ad5339bdde83 Mon Sep 17 00:00:00 2001 From: Lalit Date: Tue, 18 Jan 2022 22:38:49 -0800 Subject: [PATCH 4/5] fix build --- .../sdk/metrics/aggregator/accumulation.h | 120 ++++++++++++++---- .../sdk/metrics/aggregator/aggregator.h | 10 +- .../sdk/metrics/aggregator/drop_aggregator.h | 26 ++-- .../metrics/aggregator/histogram_aggregator.h | 84 +++++++++--- .../metrics/aggregator/lastvalue_aggregator.h | 95 +++++++++++--- .../sdk/metrics/aggregator/sum_aggregator.h | 81 ++++++++++-- .../sdk/metrics/data/metric_data.h | 41 ++++-- .../sdk/metrics/data/point_data.h | 39 ++++-- sdk/include/opentelemetry/sdk/metrics/meter.h | 2 +- .../metrics/state/writable_metric_storage.h | 9 +- .../sdk/metrics/view/aggregation.h | 2 - .../sdk/metrics/view/default_aggregation.h | 12 +- .../sdk/metrics/view/histogram_aggregation.h | 2 +- .../sdk/metrics/view/sum_aggregation.h | 2 +- .../opentelemetry/sdk/metrics/view/view.h | 2 +- sdk/src/metrics/CMakeLists.txt | 10 +- sdk/src/metrics/meter.cc | 18 +-- sdk/src/metrics/meter_context.cc | 2 +- sdk/src/metrics/view/default_aggregation.cc | 33 +++-- sdk/src/metrics/view/drop_aggregation.cc | 6 +- sdk/src/metrics/view/histogram_aggregation.cc | 24 ++-- ...aggregtion.cc => lastvalue_aggregation.cc} | 12 +- sdk/src/metrics/view/sum_aggregation.cc | 11 +- 23 files changed, 462 insertions(+), 181 deletions(-) rename sdk/src/metrics/view/{lastvalue_aggregtion.cc => lastvalue_aggregation.cc} (66%) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h index f082600ccc..83ccd2fd92 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/accumulation.h @@ -15,59 +15,99 @@ namespace sdk namespace metrics { -template class Accumulation { public: - virtual void Record(T value) noexcept = 0; + virtual void Record(long value) noexcept = 0; + virtual void Record(double value) noexcept = 0; }; -template -class SumAccumulation : public Accumulation +class LongSumAccumulation : public Accumulation { public: - SumAccumulation(T value = 0) : sum_(value) {} + LongSumAccumulation(long value = 0) : sum_(value) {} - void Record(T value) noexcept override { sum_ += value; } + void Record(long value) noexcept override { sum_ += value; } + void Record(double value) noexcept override {} - SingularPointData ToPointData() { return SingularPointData(sum_); } + LongSingularPointData ToPointData() { return LongSingularPointData(sum_); } private: - T sum_; + long sum_; }; -template -class LastValueAccumulation : public Accumulation +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: - LastValueAccumulation( - T value = 0, + LongLastValueAccumulation( + long value = 0, opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()) : last_value_(value), ts_{ts} {} - void Record(T value) noexcept override + 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_; } - SingularPointData ToPointData() { return SingularPointData(last_value_); } + LongSingularPointData ToPointData() { return LongSingularPointData(last_value_); } private: - T last_value_; + long last_value_; opentelemetry::common::SystemTimestamp ts_; }; -template -class HistogramAccumulation : public Accumulation +class DoubleLastValueAccumulation : public Accumulation { public: - HistogramAccumulation(const std::vector &boundaries) : histogram_(boundaries) {} + DoubleLastValueAccumulation( + long value = 0, + opentelemetry::common::SystemTimestamp ts = std::chrono::system_clock::now()) + : last_value_(value), ts_{ts} + {} - void Record(T value) noexcept override + 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; @@ -81,24 +121,50 @@ class HistogramAccumulation : public Accumulation } } - HistogramPointData &ToPointData() { return histogram_; } + void Record(double value) noexcept override {} + + LongHistogramPointData &ToPointData() { return histogram_; } private: - HistogramPointData histogram_; + LongHistogramPointData histogram_; }; -template -class DropAccumulation : public Accumulation +class DoubleHistogramAccumulation : public Accumulation { public: - DropAccumulation() {} + DoubleHistogramAccumulation(std::vector &boundaries) : histogram_(boundaries) {} - void Record(T value) noexcept override {} + 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 {} - DropPointData ToPointData() { return DropPointData(); } + DoubleHistogramPointData &ToPointData() { return histogram_; } private: - T sum_; + 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 diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h index 85c4780d31..ba83f4e1a9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/aggregator.h @@ -3,6 +3,7 @@ #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 @@ -11,15 +12,16 @@ namespace sdk namespace metrics { -template class Aggregator { public: - virtual T CreateAccumulation() noexcept = 0; + virtual std::unique_ptr CreateAccumulation() noexcept = 0; - virtual T Merge(T &prev, T ¤t) noexcept = 0; + virtual std::unique_ptr Merge(Accumulation &prev, + Accumulation ¤t) noexcept = 0; - virtual T diff(T &prev, T ¤t) noexcept = 0; + virtual std::unique_ptr diff(Accumulation &prev, + Accumulation ¤t) noexcept = 0; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h index f628b15eee..d33961dcdb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/drop_aggregator.h @@ -13,36 +13,38 @@ namespace sdk namespace metrics { /** Aggregators to drop the instruments. */ -template -class DropAggregator : public Aggregator> +class DropAggregator : public Aggregator { public: - DropAccumulation CreateAccumulation() noexcept override { return DropAccumulation(); } + std::unique_ptr CreateAccumulation() noexcept override + { + return std::move(std::unique_ptr(new DropAccumulation())); + } /** Returns the result of the merge of the given accumulations.*/ - DropAccumulation Merge(DropAccumulation &prev, - DropAccumulation ¤t) noexcept override + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override { - return DropAccumulation(); + return std::move( + std::unique_ptr(std::unique_ptr(new DropAccumulation()))); } /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - DropAccumulation diff(DropAccumulation &prev, - DropAccumulation ¤t) noexcept override + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override { - return DropAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + return std::move( + std::unique_ptr(std::unique_ptr(new DropAccumulation()))); } - DropMetricData ToMetricData( + DropMetricData ToMetricData( opentelemetry::sdk::resource::Resource *resource, opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - std::vector>> + std::vector> &accumulation_by_attributes, opentelemetry::common::SystemTimestamp &start_epoch_ns, opentelemetry::common::SystemTimestamp &end_epoch_ns) { - DropMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; + DropMetricData metrics_data; return metrics_data; } }; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h index c78a4293d6..14b318c3ec 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -16,50 +16,92 @@ namespace metrics * Basic aggregator which observes events and counts them in pre-defined buckets * and provides the total sum and count of all observations. */ -template -class HistogramAggregator : public Aggregator> +class LongHistogramAggregator : public Aggregator { // TBD - This clas is placeholder, and needs to be implemented public: - HistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} - HistogramAccumulation CreateAccumulation() noexcept override + LongHistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} + std::unique_ptr CreateAccumulation() noexcept override { - return HistogramAccumulation(boundaries_); + // TBD + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); } /** Returns the result of the merge of the given accumulations.*/ - HistogramAccumulation Merge(HistogramAccumulation &prev, - HistogramAccumulation ¤t) noexcept override + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override { - return HistogramAccumulation(boundaries_); + // TBD + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); } /** Returns a new delta aggregation by comparing two cumulative measurements.*/ - HistogramAccumulation diff(HistogramAccumulation &prev, - HistogramAccumulation ¤t) noexcept override{ - return HistogramAccumulation(boundaries_)} + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override + { + return std::move(std::unique_ptr(new LongHistogramAccumulation(boundaries_))); + } - HistogramMetricsData ToMetricData( + LongHistogramMetricData ToMetricData( opentelemetry::sdk::resource::Resource *resource, opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - std::vector>> + std::vector> &accumulation_by_attributes, opentelemetry::common::SystemTimestamp &start_epoch_ns, opentelemetry::common::SystemTimestamp &end_epoch_ns) { - HistogramMetricsData metrics_data = {resource, instrumentation_library, + LongHistogramMetricData 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)); - } + // TBD - Populate me + return metrics_data; + } + +private: + std::vector boundaries_; +}; + +class DoubleHistogramAggregator : public Aggregator +{ + // TBD - This clas 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 diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h index 7b5884629d..2d9ede4ba9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/lastvalue_aggregator.h @@ -3,9 +3,11 @@ #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 @@ -13,46 +15,48 @@ namespace sdk namespace metrics { /** Basic aggregator which calculates a last value from individual measurements. */ -template -class LastValueAggregator : public Aggregator> +class LongLastValueAggregator : public Aggregator { public: - LastValueAccumulation CreateAccumulation() noexcept override + std::unique_ptr CreateAccumulation() noexcept override { - return new LastValueAccumulation(); + return std::move(std::unique_ptr(new LongLastValueAccumulation())); } /** Returns the result of the merge of the given accumulations.*/ - LastValueAccumulation Merge(LastValueAccumulation &previous, - LastValueAccumulation ¤t) noexcept override + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override { - auto latest_accumulation = (current.GetLastValueTimeStamp().time_since_epoch() >= - previous.GetLastValueTimeStamp().time_since_epoch()) - ? current - : previous; + 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 new LastValueAccumulation(latest_accumulation.toPointData().value_, - latest_accumulation.GetLastValueTimeStamp()); + 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.*/ - LastValueAccumulation diff(LastValueAccumulation &prev, - LastValueAccumulation ¤t) noexcept override + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override { - return LastValueAccumulation(prev.ToPoint() - current.ToPoint()); + 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_))); } - SingularMetricsData ToMetricData( + LongSingularMetricData ToMetricData( opentelemetry::sdk::resource::Resource *resource, opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - std::vector>> + std::vector> &accumulation_by_attributes, opentelemetry::common::SystemTimestamp &start_epoch_ns, opentelemetry::common::SystemTimestamp &end_epoch_ns) { - SingularMetricsData metrics_data = {resource, instrumentation_library, + LongSingularMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; for (auto &acc_by_attr : accumulation_by_attributes) { @@ -64,6 +68,61 @@ class LastValueAggregator : public Aggregator> 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 diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h index b20e152846..97ad05e379 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/sum_aggregator.h @@ -3,46 +3,56 @@ #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. */ -template -class SumAggregator : public Aggregator> +class LongSumAggregator : public Aggregator { public: - SumAggregator(InstrumentDescriptor &instrument_descriptor) {} - SumAccumulation CreateAccumulation() noexcept override { return SumAccumulation(); } + 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.*/ - SumAccumulation Merge(SumAccumulation &prev, SumAccumulation ¤t) noexcept override + std::unique_ptr Merge(Accumulation &prev, Accumulation ¤t) noexcept override { - return SumAccumulation(prev.ToPointData().value_ + current.ToPointData().value_); + 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.*/ - SumAccumulation diff(SumAccumulation &prev, SumAccumulation ¤t) noexcept override + std::unique_ptr diff(Accumulation &prev, Accumulation ¤t) noexcept override { - return SumAccumulation(prev.ToPointData().value_ - current.ToPointData().value_); + return std::move(std::unique_ptr(new LongSumAccumulation( + static_cast(&prev)->ToPointData().value_ - + static_cast(¤t)->ToPointData().value_))); } - SingularMetricsData ToMetricData( + LongSingularMetricData ToMetricData( opentelemetry::sdk::resource::Resource *resource, opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - std::vector>> + std::vector> &accumulation_by_attributes, opentelemetry::common::SystemTimestamp &start_epoch_ns, opentelemetry::common::SystemTimestamp &end_epoch_ns) { - SingularMetricsData metrics_data = {resource, instrumentation_library, + LongSingularMetricData metrics_data = {resource, instrumentation_library, instrument_descriptor}; for (auto &acc_by_attr : accumulation_by_attributes) { @@ -54,6 +64,55 @@ class SumAggregator : public Aggregator> 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 diff --git a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h index 4455911785..5026c33116 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/metric_data.h @@ -15,31 +15,52 @@ namespace sdk { namespace metrics { -template -struct MetricData +/*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; }; -template -struct SingularMetricsData : public MetricData +class LongHistogramMetricData { public: - std::vector>> point_data_list; + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_datas_list; }; -template -class HistogramMetricData : public MetricData +class DoubleHistogramMetricData { public: - std::vector>> point_datas_list; + opentelemetry::sdk::resource::Resource *resource_; + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + InstrumentDescriptor instrument_descriptor; + std::vector> point_datas_list; }; -template -class DropMetricData : public MetricData +class DropMetricData {}; + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h index aa0e5ebaf1..acd3b8ee6b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/data/point_data.h +++ b/sdk/include/opentelemetry/sdk/metrics/data/point_data.h @@ -26,31 +26,44 @@ struct BasePointData PointAttributes attributes_; }; -template -class AbstractPointData +class PointData {}; -template -struct SingularPointData : public AbstractPointData +struct LongSingularPointData : public PointData { - SingularPointData(T value) : value_(value) {} - T value_; + LongSingularPointData(long value) : value_(value) {} + long value_; }; -template -struct HistogramPointData : public AbstractPointData +struct DoubleSingularPointData : public PointData { - HistogramPointData(std::vector &boundaries) + 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 boundaries_; std::vector counts_; - T sum_; + long sum_; uint64_t count_; }; -template -struct DropPointData : public AbstractPointData +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 diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index ec7877abd8..0f7fe43026 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -102,7 +102,7 @@ class Meter final : public opentelemetry::metrics::Meter private: std::shared_ptr instrumentation_library_; std::shared_ptr context_; - WritableMatricStorage &RegisterMetricStorage(InstrumentDescriptor &); + // WritableMetricStorage &RegisterMetricStorage(InstrumentDescriptor &); }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h index 533e4637cf..dc4b213dac 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/writable_metric_storage.h @@ -3,6 +3,7 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/key_value_iterable.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -14,8 +15,9 @@ template class WritableMetricStorage { public: - virtual void Record(T value) noexcept = 0; - virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Record(T value) noexcept = 0; + virtual void Record(T value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; }; template @@ -23,7 +25,8 @@ class NoopWritableMetricsStorage : public WritableMetricStorage { public: void Record(T value) noexcept override {} - void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Record(T value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override + {} }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h index 0bc1b39693..50ee020dac 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/aggregation.h @@ -6,8 +6,6 @@ # include # include "opentelemetry/sdk/metrics/aggregator/aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h" -# include "opentelemetry/sdk/metrics/aggregator/noop_aggregator.h" # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h index 61c7bdb2a9..e375d601bb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/default_aggregation.h @@ -8,7 +8,11 @@ # include "opentelemetry/sdk/metrics/view/aggregation.h" # include - +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ class DefaultAggregation : public Aggregation { public: @@ -16,10 +20,10 @@ class DefaultAggregation : public Aggregation opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; private: - static opentelemetry::sdk::metrics::Aggregation &resolve( + 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/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h index 532c13283d..84e8ec305b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/histogram_aggregation.h @@ -23,7 +23,7 @@ class HistogramAggregation : public Aggregation static Aggregation &GetInstance() noexcept; private: - LastValueAggregation() = default; + HistogramAggregation() = default; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h index 8473f34433..5157172dda 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/sum_aggregation.h @@ -20,7 +20,7 @@ class SumAggregation : public Aggregation std::unique_ptr CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept override; - static Aggregation &GetInstance(); + static Aggregation &GetInstance() noexcept; private: SumAggregation() = default; 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 efdf6cbb70..e531980a78 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -3,11 +3,11 @@ add_library( meter_provider.cc meter.cc meter_context.cc - sum_aggregation.cc - lastvalue_aggregation.cc - histogram_aggregation.cc - drop_aggregation.cc - default_aggregation.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 bcc339e2fb..028ec42f30 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -29,9 +29,10 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::string_view description, nostd::string_view unit) noexcept { - InstrumentDescriptor instrument_descriptor = {name, description, unit, InstrumentType::kCounter, + InstrumentDescriptor instrument_descriptor = {name.data(), description.data(), unit.data(), + InstrumentType::kCounter, InstrumentValueType::kLong}; - auto storage = registerMetricStorage(instrument_descriptor); + // 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)}; @@ -161,12 +162,13 @@ 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); }); -} +// 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 diff --git a/sdk/src/metrics/meter_context.cc b/sdk/src/metrics/meter_context.cc index fff7b1b292..fcc647d35d 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -28,7 +28,7 @@ const resource::Resource &MeterContext::GetResource() const noexcept return resource_; } -const opentelemetry::sdk::metrics::ViewRegistry &GetViewRegistry() const noexcept +const opentelemetry::sdk::metrics::ViewRegistry &MeterContext::GetViewRegistry() const noexcept { return *views_.get(); } diff --git a/sdk/src/metrics/view/default_aggregation.cc b/sdk/src/metrics/view/default_aggregation.cc index d262c64264..376e8d9306 100644 --- a/sdk/src/metrics/view/default_aggregation.cc +++ b/sdk/src/metrics/view/default_aggregation.cc @@ -2,10 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/view/drop_aggregation.h" -# include "opentelemetry/sdk/view/histogram_aggregation.h" -# include "opentelemetry/sdk/view/lastvalue_aggregation.h" -# include "opentelemetry/sdk/view/sum_aggregation.h" +# 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" @@ -16,35 +17,33 @@ namespace sdk namespace metrics { -std::unique_ptr DefaultAggregator::CreateAggregator( +std::unique_ptr DefaultAggregation::CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept { return resolve(instrument_descriptor).CreateAggregator(instrument_descriptor); } -static opentelemetry::sdk::metrics::Aggregation &DefaultAggregator::resolve( - opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept; +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.kUpDownCounter: - case InstrumentType.kObservableUpDownCounter: + case InstrumentType::kCounter: + case InstrumentType::kUpDownCounter: + case InstrumentType::kObservableUpDownCounter: return SumAggregation::GetInstance(); break; - case InstrumentType.kHistogram: + case InstrumentType::kHistogram: return HistogramAggregation::GetInstance(); break; - case InstrumentType.kObservableGauge: + case InstrumentType::kObservableGauge: return LastValueAggregation::GetInstance(); break; default: // TBD - Return drop aggregation - OTEL_INTERNAL_LOG_WARNING("[METRICS] Aggregation: Instrument Type - " - << static_cast(instrument_descriptor.type_)) - << " - not supported. NoopAggregation will be used." return DropAggregation::GetInstance() - .CreateAggregator(instrument_descriptor); + OTEL_INTERNAL_LOG_WARN( + "[METRICS] Aggregation: Invalid Instrument Type - Using DropAggregation "); + return DropAggregation::GetInstance(); }; } } // namespace metrics diff --git a/sdk/src/metrics/view/drop_aggregation.cc b/sdk/src/metrics/view/drop_aggregation.cc index 39a6a5c5df..d2fe9575f7 100644 --- a/sdk/src/metrics/view/drop_aggregation.cc +++ b/sdk/src/metrics/view/drop_aggregation.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/view/drop_aggregation.h" +# 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" @@ -16,10 +16,10 @@ namespace metrics std::unique_ptr DropAggregation::CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept { - return std::move(std::unique_ptr(new DropAggregator(instrument_descriptor))); + return std::move(std::unique_ptr(new DropAggregator())); } -static Aggregation &DropAggregation::GetInstance() +Aggregation &DropAggregation::GetInstance() { static DropAggregation drop_aggregation; return drop_aggregation; diff --git a/sdk/src/metrics/view/histogram_aggregation.cc b/sdk/src/metrics/view/histogram_aggregation.cc index 80201eb608..285653adc3 100644 --- a/sdk/src/metrics/view/histogram_aggregation.cc +++ b/sdk/src/metrics/view/histogram_aggregation.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/view/histogram_aggregation.h" +# 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" @@ -18,17 +18,25 @@ std::unique_ptr HistogramAggregation::CreateAggregator( { // 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}; - auto aggregator = (instrument_descriptor.valueType_ == InstrumentValueType.kLong) - ? std::unique_ptr(new HitogramAggregation(boundaries)) - : std::unique_ptr(new HistogramAggregation(boundaries)); + 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); } -static Aggregation &HistogramAggregation::GetInstance() noexcept +Aggregation &HistogramAggregation::GetInstance() noexcept { - static LastValueAggregation lastvalue_aggregation; - return lastvalue_aggregation; + static HistogramAggregation histogram_aggregation; + return histogram_aggregation; } } // namespace metrics diff --git a/sdk/src/metrics/view/lastvalue_aggregtion.cc b/sdk/src/metrics/view/lastvalue_aggregation.cc similarity index 66% rename from sdk/src/metrics/view/lastvalue_aggregtion.cc rename to sdk/src/metrics/view/lastvalue_aggregation.cc index 6cd29f2d3a..4853a474cf 100644 --- a/sdk/src/metrics/view/lastvalue_aggregtion.cc +++ b/sdk/src/metrics/view/lastvalue_aggregation.cc @@ -2,8 +2,10 @@ // 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/view/lastvalue_aggregation.h" +# include "opentelemetry/sdk/metrics/instruments.h" + # include "opentelemetry/sdk_config.h" # include "opentelemetry/version.h" @@ -16,13 +18,13 @@ 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 LastValueAggregation()) - : std::unique_ptr(new LastValueAggregation()); + auto aggregator = (instrument_descriptor.valueType_ == InstrumentValueType::kLong) + ? std::unique_ptr(new LongLastValueAggregator()) + : std::unique_ptr(new DoubleLastValueAggregator()); return std::move(aggregator); } -static Aggregation &LastValueAggregation::GetInstance() noexcept +Aggregation &LastValueAggregation::GetInstance() noexcept { static LastValueAggregation lastvalue_aggregation; return lastvalue_aggregation; diff --git a/sdk/src/metrics/view/sum_aggregation.cc b/sdk/src/metrics/view/sum_aggregation.cc index 8cdd1cba35..25c27bca70 100644 --- a/sdk/src/metrics/view/sum_aggregation.cc +++ b/sdk/src/metrics/view/sum_aggregation.cc @@ -2,8 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/view/sum_aggregation.h" +# 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" @@ -17,13 +18,13 @@ std::unique_ptr SumAggregation::CreateAggregator( opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) noexcept { auto aggregator = - (instrument_descriptor.valueType_ == InstrumentValueType.kLong) - ? std::unique_ptr(new SumAggregator(instrument_descriptor)) - : std::unique_ptr(new SumAggregator(instrument_descriptor)); + (instrument_descriptor.valueType_ == InstrumentValueType::kLong) + ? std::unique_ptr(new LongSumAggregator(instrument_descriptor)) + : std::unique_ptr(new DoubleSumAggregator(instrument_descriptor)); return std::move(aggregator); } -static Aggregation &SumAggregation::GetInstance() noexecept +Aggregation &SumAggregation::GetInstance() noexcept { static SumAggregation sum_aggregation; return sum_aggregation; From 62ad88e2e07ac34a7c5f4fe7beb332637c7ac62b Mon Sep 17 00:00:00 2001 From: Lalit Date: Tue, 18 Jan 2022 22:40:49 -0800 Subject: [PATCH 5/5] fix spell --- .../sdk/metrics/aggregator/histogram_aggregator.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h index 14b318c3ec..5a1f1b7693 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h @@ -18,7 +18,7 @@ namespace metrics */ class LongHistogramAggregator : public Aggregator { - // TBD - This clas is placeholder, and needs to be implemented + // TBD - This class is placeholder, and needs to be implemented public: LongHistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} std::unique_ptr CreateAccumulation() noexcept override @@ -61,7 +61,7 @@ class LongHistogramAggregator : public Aggregator class DoubleHistogramAggregator : public Aggregator { - // TBD - This clas is placeholder, and needs to be implemented + // TBD - This class is placeholder, and needs to be implemented public: DoubleHistogramAggregator(const std::vector &boundaries) : boundaries_(boundaries) {} std::unique_ptr CreateAccumulation() noexcept override