From 1dc6ce1bd05173bf64530db92948c3251d541edf Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 23 Jul 2022 18:05:56 +0000 Subject: [PATCH 1/7] Add configuration options for Aggregation creation --- .../metrics/aggregation_config.h | 27 ++++ api/include/opentelemetry/metrics/meter.h | 79 +++++++---- api/include/opentelemetry/metrics/noop.h | 51 +++++-- .../common/metrics_foo_library/foo_library.cc | 17 ++- .../metrics/aggregation/default_aggregation.h | 23 +++- .../aggregation/histogram_aggregation.h | 7 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 86 ++++++++---- .../sdk/metrics/state/async_metric_storage.h | 16 ++- .../sdk/metrics/state/sync_metric_storage.h | 6 +- .../metrics/state/temporal_metric_storage.h | 7 +- .../aggregation/histogram_aggregation.cc | 26 +++- sdk/src/metrics/meter.cc | 130 ++++++++++-------- sdk/src/metrics/state/sync_metric_storage.cc | 2 - .../metrics/state/temporal_metric_storage.cc | 47 ++++--- sdk/test/metrics/aggregation_test.cc | 28 ++++ sdk/test/metrics/async_metric_storage_test.cc | 11 +- sdk/test/metrics/sync_metric_storage_test.cc | 10 +- 17 files changed, 393 insertions(+), 180 deletions(-) create mode 100644 api/include/opentelemetry/metrics/aggregation_config.h diff --git a/api/include/opentelemetry/metrics/aggregation_config.h b/api/include/opentelemetry/metrics/aggregation_config.h new file mode 100644 index 0000000000..dbc3cfded7 --- /dev/null +++ b/api/include/opentelemetry/metrics/aggregation_config.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/version.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace metrics +{ + +class AggregationConfig +{ +public: + virtual ~AggregationConfig() = default; +}; + +template +class HistogramAggregationConfig : public AggregationConfig +{ +public: + std::list boundaries_; +}; +} // namespace metrics +OPENTELEMETRY_END_NAMESPACE + +#endif // ENABLE_METRICS_PREVIEW \ No newline at end of file diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index e0454e360b..c0f9589fa8 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -4,6 +4,7 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/metrics/async_instruments.h" # include "opentelemetry/metrics/sync_instruments.h" # include "opentelemetry/nostd/shared_ptr.h" @@ -39,12 +40,16 @@ class Meter virtual nostd::shared_ptr> CreateLongCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; virtual nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; /** * Creates a Asynchronous (Observable) counter with the passed characteristics and returns a @@ -60,13 +65,18 @@ class Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; - virtual void CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; + virtual void CreateDoubleObservableCounter( + nostd::string_view name, + void (*callback)(ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; /** * Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram. @@ -79,12 +89,16 @@ class Meter virtual nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; virtual nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; /** * Creates a Asynchronouse (Observable) Gauge with the passed characteristics and returns a @@ -100,13 +114,17 @@ class Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; virtual void CreateDoubleObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; /** * Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that @@ -120,12 +138,16 @@ class Meter virtual nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; virtual nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept = 0; /** * Creates a Asynchronouse (Observable) UpDownCounter with the passed characteristics and returns @@ -137,18 +159,23 @@ class Meter * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. * @param state to be passed back to callback */ - virtual void CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; - - virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &, - void *), - nostd::string_view description = "", - nostd::string_view unit = "", - void *state = nullptr) noexcept = 0; + virtual void CreateLongObservableUpDownCounter( + nostd::string_view name, + void (*callback)(ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; + + virtual void CreateDoubleObservableUpDownCounter( + nostd::string_view name, + void (*callback)(ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept = 0; }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index c47f7489f0..8c98155d9b 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -122,9 +122,12 @@ class NoopObservableUpDownCounter : public ObservableUpDownCounter class NoopMeter final : public Meter { public: - nostd::shared_ptr> CreateLongCounter(nostd::string_view name, - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::shared_ptr> CreateLongCounter( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{new NoopCounter(name, description, unit)}; } @@ -132,7 +135,9 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{new NoopCounter(name, description, unit)}; } @@ -141,20 +146,26 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override {} void CreateDoubleObservableCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{new NoopHistogram(name, description, unit)}; } @@ -162,7 +173,9 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{new NoopHistogram(name, description, unit)}; } @@ -171,20 +184,26 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override {} void CreateDoubleObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit)}; @@ -193,7 +212,9 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit)}; @@ -203,13 +224,17 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override {} void CreateDoubleObservableUpDownCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, void *state = nullptr) noexcept override {} }; diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 2fcbd660e0..da6049692a 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -1,13 +1,16 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW -# include "foo_library.h" # include # include # include # include +# include "foo_library.h" # include "opentelemetry/context/context.h" +# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/metrics/provider.h" namespace nostd = opentelemetry::nostd; @@ -69,11 +72,19 @@ void foo_library::observable_counter_example(const std::string &name) void foo_library::histogram_example(const std::string &name) { + nostd::shared_ptr aggregation_config{ + new opentelemetry::metrics::HistogramAggregationConfig}; + static_cast *>( + aggregation_config.get()) + ->boundaries_ = + std::list{0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; + ; std::string histogram_name = name + "_histogram"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto histogram_counter = meter->CreateDoubleHistogram(histogram_name); - auto context = opentelemetry::context::Context{}; + auto histogram_counter = + meter->CreateDoubleHistogram(histogram_name, "des", "unit", aggregation_config); + auto context = opentelemetry::context::Context{}; while (true) { double val = (rand() % 700) + 1.1; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index 887e1beb92..a17e418bdb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -2,8 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include +#include "opentelemetry/sdk/metrics/data/point_data.h" #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/spin_lock_mutex.h" +# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" @@ -18,11 +21,13 @@ namespace sdk { namespace metrics { + class DefaultAggregation { public: static std::unique_ptr CreateAggregation( - const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor) + const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, + const opentelemetry::metrics::AggregationConfig *aggregation_config) { switch (instrument_descriptor.type_) { @@ -34,11 +39,17 @@ class DefaultAggregation ? std::move(std::unique_ptr(new LongSumAggregation())) : std::move(std::unique_ptr(new DoubleSumAggregation())); break; - case InstrumentType::kHistogram: + case InstrumentType::kHistogram: { return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) - ? std::move(std::unique_ptr(new LongHistogramAggregation())) - : std::move(std::unique_ptr(new DoubleHistogramAggregation())); + ? std::move(std::unique_ptr(new LongHistogramAggregation( + static_cast + *>(aggregation_config)))) + : std::move(std::unique_ptr(new DoubleHistogramAggregation( + static_cast< + const opentelemetry::metrics::HistogramAggregationConfig *>( + aggregation_config)))); break; + } case InstrumentType::kObservableGauge: return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) ? std::move(std::unique_ptr(new LongLastValueAggregation())) @@ -88,7 +99,7 @@ class DefaultAggregation } break; default: - return DefaultAggregation::CreateAggregation(instrument_descriptor); + return DefaultAggregation::CreateAggregation(instrument_descriptor, nullptr); } } @@ -135,7 +146,7 @@ class DefaultAggregation new DoubleSumAggregation(nostd::get(point_data))); } default: - return DefaultAggregation::CreateAggregation(instrument_descriptor); + return DefaultAggregation::CreateAggregation(instrument_descriptor, nullptr); } } }; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h index e2a55fba58..6f7a009d11 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h @@ -4,6 +4,7 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/spin_lock_mutex.h" +# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" # include @@ -17,7 +18,8 @@ namespace metrics class LongHistogramAggregation : public Aggregation { public: - LongHistogramAggregation(); + LongHistogramAggregation( + const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config = nullptr); LongHistogramAggregation(HistogramPointData &&); LongHistogramAggregation(const HistogramPointData &); @@ -46,7 +48,8 @@ class LongHistogramAggregation : public Aggregation class DoubleHistogramAggregation : public Aggregation { public: - DoubleHistogramAggregation(); + DoubleHistogramAggregation(const opentelemetry::metrics::HistogramAggregationConfig + *aggregation_config = nullptr); DoubleHistogramAggregation(HistogramPointData &&); DoubleHistogramAggregation(const HistogramPointData &); diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 44e54adf18..53a9b7eaab 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW # include # include "opentelemetry/metrics/meter.h" @@ -11,6 +12,7 @@ # include "opentelemetry/sdk/metrics/state/async_metric_storage.h" # include "opentelemetry/sdk/resource/resource.h" +# include "opentelemetry/sdk_config.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -35,74 +37,98 @@ class Meter final : public opentelemetry::metrics::Meter nostd::shared_ptr> CreateLongCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; - void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &, - void *), - nostd::string_view description = "", - nostd::string_view unit = "", - void *state = nullptr) noexcept override; + void CreateLongObservableCounter( + nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; void CreateDoubleObservableCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; - void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &, - void *), - nostd::string_view description = "", - nostd::string_view unit = "", - void *state = nullptr) noexcept override; + void CreateLongObservableGauge( + nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; void CreateDoubleObservableGauge( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}) noexcept override; void CreateLongObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; void CreateDoubleObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - void *state = nullptr) noexcept override; + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, + void *state = nullptr) noexcept override; /** Returns the associated instruementation library */ const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() @@ -121,18 +147,20 @@ class Meter final : public opentelemetry::metrics::Meter std::unordered_map> storage_registry_; std::unique_ptr RegisterMetricStorage( - InstrumentDescriptor &instrument_descriptor); + InstrumentDescriptor &instrument_descriptor, + nostd::shared_ptr aggregation_config); template - void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, - void (*callback)(opentelemetry::metrics::ObserverResult &, - void *), - void *state = nullptr) + void RegisterAsyncMetricStorage( + InstrumentDescriptor &instrument_descriptor, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), + nostd::shared_ptr aggregation_config, + void *state = nullptr) { auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, - [this, &instrument_descriptor, callback, state](const View &view) { + [this, &instrument_descriptor, callback, aggregation_config, state](const View &view) { auto view_instr_desc = instrument_descriptor; if (!view.GetName().empty()) { @@ -144,7 +172,7 @@ class Meter final : public opentelemetry::metrics::Meter } auto storage = std::shared_ptr>( new AsyncMetricStorage(view_instr_desc, view.GetAggregationType(), callback, - &view.GetAttributesProcessor(), state)); + &view.GetAttributesProcessor(), aggregation_config, state)); storage_registry_[instrument_descriptor.name_] = storage; return true; }); diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index e5dcbc2738..1f77eebac9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" @@ -25,19 +26,20 @@ template class AsyncMetricStorage : public MetricStorage { public: - AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, - const AggregationType aggregation_type, - void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, - void *), - const AttributesProcessor *attributes_processor, - void *state = nullptr) + AsyncMetricStorage( + InstrumentDescriptor instrument_descriptor, + const AggregationType aggregation_type, + void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, void *), + const AttributesProcessor *attributes_processor, + nostd::shared_ptr aggregation_config, + void *state = nullptr) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, measurement_collection_callback_{measurement_callback}, attributes_processor_{attributes_processor}, state_{state}, cumulative_hash_map_(new AttributesHashMap()), - temporal_metric_storage_(instrument_descriptor) + temporal_metric_storage_(instrument_descriptor, aggregation_config) {} bool Collect(CollectorHandle *collector, 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 37f485997c..17e397a20b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/sdk/common/attributemap_hash.h" @@ -29,13 +30,14 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage SyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, const AttributesProcessor *attributes_processor, - nostd::shared_ptr &&exemplar_reservoir) + nostd::shared_ptr &&exemplar_reservoir, + nostd::shared_ptr aggregation_config) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), attributes_processor_{attributes_processor}, exemplar_reservoir_(exemplar_reservoir), - temporal_metric_storage_(instrument_descriptor) + temporal_metric_storage_(instrument_descriptor, aggregation_config) { create_default_aggregation_ = [&]() -> std::unique_ptr { diff --git a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h index 16659c14f5..d79b917988 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include "opentelemetry/sdk/metrics/state/metric_collector.h" @@ -23,7 +25,9 @@ struct LastReportedMetrics class TemporalMetricStorage { public: - TemporalMetricStorage(InstrumentDescriptor instrument_descriptor); + TemporalMetricStorage( + InstrumentDescriptor instrument_descriptor, + nostd::shared_ptr aggregation_config); bool buildMetrics(CollectorHandle *collector, nostd::span> collectors, @@ -43,6 +47,7 @@ class TemporalMetricStorage // Lock while building metrics mutable opentelemetry::common::SpinLockMutex lock_; + const nostd::shared_ptr aggregation_config_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/src/metrics/aggregation/histogram_aggregation.cc b/sdk/src/metrics/aggregation/histogram_aggregation.cc index aa2be74713..61e1310a67 100644 --- a/sdk/src/metrics/aggregation/histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/histogram_aggregation.cc @@ -12,9 +12,17 @@ namespace sdk namespace metrics { -LongHistogramAggregation::LongHistogramAggregation() +LongHistogramAggregation::LongHistogramAggregation( + const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config) { - point_data_.boundaries_ = std::list{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l}; + if (aggregation_config && aggregation_config->boundaries_.size()) + { + point_data_.boundaries_ = aggregation_config->boundaries_; + } + else + { + point_data_.boundaries_ = std::list{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l}; + } point_data_.counts_ = std::vector(nostd::get>(point_data_.boundaries_).size() + 1, 0); point_data_.sum_ = 0l; @@ -73,10 +81,18 @@ PointType LongHistogramAggregation::ToPoint() const noexcept return point_data_; } -DoubleHistogramAggregation::DoubleHistogramAggregation() +DoubleHistogramAggregation::DoubleHistogramAggregation( + const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config) { - point_data_.boundaries_ = - std::list{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0}; + if (aggregation_config && aggregation_config->boundaries_.size()) + { + point_data_.boundaries_ = aggregation_config->boundaries_; + } + else + { + point_data_.boundaries_ = + std::list{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0}; + } point_data_.counts_ = std::vector(nostd::get>(point_data_.boundaries_).size() + 1, 0); point_data_.sum_ = 0.0; diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 600bae9863..5a8d7459bf 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -10,7 +10,6 @@ # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/sdk/metrics/sync_instruments.h" -# include "opentelemetry/sdk_config.h" # include "opentelemetry/version.h" @@ -31,14 +30,16 @@ Meter::Meter(std::shared_ptr meter_context, : instrumentation_library_{std::move(instrumentation_library)}, meter_context_{meter_context} {} -nostd::shared_ptr> Meter::CreateLongCounter(nostd::string_view name, - nostd::string_view description, - nostd::string_view unit) noexcept +nostd::shared_ptr> Meter::CreateLongCounter( + nostd::string_view name, + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kCounter, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>( new LongCounter(instrument_descriptor, std::move(storage))); } @@ -46,54 +47,59 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::shared_ptr> Meter::CreateDoubleCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kCounter, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>{ new DoubleCounter(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateLongObservableCounter( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } -void Meter::CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, - void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateDoubleObservableCounter( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } nostd::shared_ptr> Meter::CreateLongHistogram( nostd::string_view name, nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kHistogram, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>{ new LongHistogram(instrument_descriptor, std::move(storage))}; } @@ -101,53 +107,59 @@ nostd::shared_ptr> Meter::CreateLongHistogram( nostd::shared_ptr> Meter::CreateDoubleHistogram( nostd::string_view name, nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kHistogram, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>{ new DoubleHistogram(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableGauge(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateLongObservableGauge( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } -void Meter::CreateDoubleObservableGauge(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateDoubleObservableGauge( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>{ new LongUpDownCounter(instrument_descriptor, std::move(storage))}; } @@ -155,43 +167,46 @@ nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::shared_ptr> Meter::CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + nostd::shared_ptr aggregation_config) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor); + auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); return nostd::shared_ptr>{ new DoubleUpDownCounter(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, - void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateLongObservableUpDownCounter( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } -void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &, - void *), - nostd::string_view description, - nostd::string_view unit, - void *state) noexcept +void Meter::CreateDoubleObservableUpDownCounter( + nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + nostd::shared_ptr aggregation_config, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); } const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() @@ -201,14 +216,15 @@ const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumenta } std::unique_ptr Meter::RegisterMetricStorage( - InstrumentDescriptor &instrument_descriptor) + InstrumentDescriptor &instrument_descriptor, + nostd::shared_ptr aggregation_config) { auto view_registry = meter_context_->GetViewRegistry(); std::unique_ptr storages(new MultiMetricStorage()); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, - [this, &instrument_descriptor, &storages](const View &view) { + [this, &instrument_descriptor, &storages, &aggregation_config](const View &view) { auto view_instr_desc = instrument_descriptor; if (!view.GetName().empty()) { @@ -220,7 +236,7 @@ std::unique_ptr Meter::RegisterMetricStorage( } auto storage = std::shared_ptr(new SyncMetricStorage( view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(), - NoExemplarReservoir::GetNoExemplarReservoir())); + NoExemplarReservoir::GetNoExemplarReservoir(), aggregation_config)); storage_registry_[instrument_descriptor.name_] = storage; auto multi_storage = static_cast(storages.get()); multi_storage->AddStorage(storage); diff --git a/sdk/src/metrics/state/sync_metric_storage.cc b/sdk/src/metrics/state/sync_metric_storage.cc index 844b187c03..eec68ed6d7 100644 --- a/sdk/src/metrics/state/sync_metric_storage.cc +++ b/sdk/src/metrics/state/sync_metric_storage.cc @@ -17,8 +17,6 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, opentelemetry::common::SystemTimestamp collection_ts, nostd::function_ref callback) noexcept { - opentelemetry::common::SystemTimestamp last_collection_ts = sdk_start_ts; - // Add the current delta metrics to `unreported metrics stash` for all the collectors, // this will also empty the delta metrics hashmap, and make it available for // recordings diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index b208695b8a..9e94f80558 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -1,10 +1,15 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/sdk/metrics/state/temporal_metric_storage.h" +# include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/state/temporal_metric_storage.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -12,8 +17,10 @@ namespace sdk namespace metrics { -TemporalMetricStorage::TemporalMetricStorage(InstrumentDescriptor instrument_descriptor) - : instrument_descriptor_(instrument_descriptor) +TemporalMetricStorage::TemporalMetricStorage( + InstrumentDescriptor instrument_descriptor, + nostd::shared_ptr aggregation_config) + : instrument_descriptor_(instrument_descriptor), aggregation_config_(aggregation_config) {} bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, @@ -54,9 +61,9 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, } else { - merged_metrics->Set( - attributes, - DefaultAggregation::CreateAggregation(instrument_descriptor_)->Merge(aggregation)); + merged_metrics->Set(attributes, DefaultAggregation::CreateAggregation( + instrument_descriptor_, aggregation_config_.get()) + ->Merge(aggregation)); merged_metrics->GetAllEnteries( [](const MetricAttributes &attr, Aggregation &aggr) { return true; }); } @@ -80,20 +87,20 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, if (aggregation_temporarily == AggregationTemporality::kCumulative) { // merge current delta to previous cumulative - last_aggr_hashmap->GetAllEnteries( - [&merged_metrics, this](const MetricAttributes &attributes, Aggregation &aggregation) { - auto agg = merged_metrics->Get(attributes); - if (agg) - { - merged_metrics->Set(attributes, agg->Merge(aggregation)); - } - else - { - merged_metrics->Set(attributes, - DefaultAggregation::CreateAggregation(instrument_descriptor_)); - } - return true; - }); + last_aggr_hashmap->GetAllEnteries([&merged_metrics, this](const MetricAttributes &attributes, + Aggregation &aggregation) { + auto agg = merged_metrics->Get(attributes); + if (agg) + { + merged_metrics->Set(attributes, agg->Merge(aggregation)); + } + else + { + merged_metrics->Set( + attributes, DefaultAggregation::CreateAggregation(instrument_descriptor_, nullptr)); + } + return true; + }); } last_reported_metrics_[collector] = LastReportedMetrics{std::move(merged_metrics), collection_ts}; diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index f8051776d1..62d58e8084 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -7,6 +7,7 @@ # include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h" +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/nostd/variant.h" using namespace opentelemetry::sdk::metrics; @@ -123,6 +124,33 @@ TEST(Aggregation, LongHistogramAggregation) EXPECT_EQ(histogram_data.counts_[7], 1); // aggr2(105) - aggr1(0) } +TEST(Aggregation, LongHistogramAggregationBoundaries) +{ + nostd::shared_ptr> aggregation_config; + std::list user_boundaries = {0, 50, 100, 250, 500, 750, 1000, 2500, 5000, 10000}; + aggregation_config->boundaries_ = user_boundaries; + DoubleHistogramAggregation aggr{}; + auto data = aggr.ToPoint(); + ASSERT_TRUE(nostd::holds_alternative(data)); + auto histogram_data = nostd::get(data); + ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); + EXPECT_EQ(nostd::get>(histogram_data.boundaries_), user_boundaries); +} + +TEST(Aggregation, DoubleHistogramAggregationBoundaries) +{ + nostd::shared_ptr> aggregation_config; + std::list user_boundaries = {0.0, 50.0, 100.0, 250.0, 500.0, + 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; + aggregation_config->boundaries_ = user_boundaries; + DoubleHistogramAggregation aggr{}; + auto data = aggr.ToPoint(); + ASSERT_TRUE(nostd::holds_alternative(data)); + auto histogram_data = nostd::get(data); + ASSERT_TRUE(nostd::holds_alternative>(histogram_data.boundaries_)); + EXPECT_EQ(nostd::get>(histogram_data.boundaries_), user_boundaries); +} + TEST(Aggregation, DoubleHistogramAggregation) { DoubleHistogramAggregation aggr; diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index 5a51b6eda1..ecb6aa339f 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/async_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/meter_context.h" # include "opentelemetry/sdk/metrics/metric_exporter.h" @@ -20,7 +21,8 @@ using namespace opentelemetry::sdk::resource; using namespace opentelemetry::sdk::metrics; using namespace opentelemetry::common; -using M = std::map; +using M = std::map; +namespace nostd = opentelemetry::nostd; class MockCollectorHandle : public CollectorHandle { @@ -94,9 +96,10 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation) collectors.push_back(collector); size_t count_attributes = 0; - opentelemetry::sdk::metrics::AsyncMetricStorage storage(instr_desc, AggregationType::kSum, - MeasurementFetcher::Fetcher, - new DefaultAttributesProcessor()); + opentelemetry::sdk::metrics::AsyncMetricStorage storage( + instr_desc, AggregationType::kSum, MeasurementFetcher::Fetcher, + new DefaultAttributesProcessor(), + nostd::shared_ptr{}); storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 7dfc4f9475..81c973f81c 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -13,7 +14,8 @@ using namespace opentelemetry::sdk::metrics; using namespace opentelemetry::common; -using M = std::map; +using M = std::map; +namespace nostd = opentelemetry::nostd; class MockCollectorHandle : public CollectorHandle { @@ -42,7 +44,8 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), - NoExemplarReservoir::GetNoExemplarReservoir()); + NoExemplarReservoir::GetNoExemplarReservoir(), + nostd::shared_ptr{}); storage.RecordLong(10l, KeyValueIterableView>(attributes_get), opentelemetry::context::Context{}); @@ -148,7 +151,8 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), - NoExemplarReservoir::GetNoExemplarReservoir()); + NoExemplarReservoir::GetNoExemplarReservoir(), + nostd::shared_ptr{}); storage.RecordDouble(10.0, KeyValueIterableView>(attributes_get), From 54d49dfe60773381322588734332907383465fc3 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 24 Jul 2022 07:47:02 +0000 Subject: [PATCH 2/7] fix CI --- sdk/test/metrics/aggregation_test.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index 62d58e8084..5b595d2544 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -126,10 +126,11 @@ TEST(Aggregation, LongHistogramAggregation) TEST(Aggregation, LongHistogramAggregationBoundaries) { - nostd::shared_ptr> aggregation_config; + nostd::shared_ptr> aggregation_config{ + new opentelemetry::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0, 50, 100, 250, 500, 750, 1000, 2500, 5000, 10000}; aggregation_config->boundaries_ = user_boundaries; - DoubleHistogramAggregation aggr{}; + LongHistogramAggregation aggr{aggregation_config.get()}; auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto histogram_data = nostd::get(data); @@ -139,11 +140,12 @@ TEST(Aggregation, LongHistogramAggregationBoundaries) TEST(Aggregation, DoubleHistogramAggregationBoundaries) { - nostd::shared_ptr> aggregation_config; + nostd::shared_ptr> aggregation_config{ + new opentelemetry::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; aggregation_config->boundaries_ = user_boundaries; - DoubleHistogramAggregation aggr{}; + DoubleHistogramAggregation aggr{aggregation_config.get()}; auto data = aggr.ToPoint(); ASSERT_TRUE(nostd::holds_alternative(data)); auto histogram_data = nostd::get(data); From a827bf6f753db52dee21d61c7ef699b3715a141a Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 25 Jul 2022 19:40:23 +0000 Subject: [PATCH 3/7] review comment --- api/include/opentelemetry/metrics/meter.h | 79 ++++------- api/include/opentelemetry/metrics/noop.h | 51 ++----- .../common/metrics_foo_library/foo_library.cc | 13 +- examples/metrics_simple/metrics_ostream.cc | 10 +- .../metrics/aggregation}/aggregation_config.h | 4 +- .../metrics/aggregation/default_aggregation.h | 14 +- .../aggregation/histogram_aggregation.h | 9 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 89 +++++------- .../sdk/metrics/state/async_metric_storage.h | 14 +- .../sdk/metrics/state/sync_metric_storage.h | 2 +- .../metrics/state/temporal_metric_storage.h | 7 +- .../opentelemetry/sdk/metrics/view/view.h | 11 ++ .../aggregation/histogram_aggregation.cc | 4 +- sdk/src/metrics/meter.cc | 130 ++++++++---------- .../metrics/state/temporal_metric_storage.cc | 2 +- sdk/test/metrics/aggregation_test.cc | 6 +- sdk/test/metrics/async_metric_storage_test.cc | 2 +- 17 files changed, 180 insertions(+), 267 deletions(-) rename {api/include/opentelemetry/metrics => sdk/include/opentelemetry/sdk/metrics/aggregation}/aggregation_config.h (93%) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index c0f9589fa8..e0454e360b 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -4,7 +4,6 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/metrics/async_instruments.h" # include "opentelemetry/metrics/sync_instruments.h" # include "opentelemetry/nostd/shared_ptr.h" @@ -40,16 +39,12 @@ class Meter virtual nostd::shared_ptr> CreateLongCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; virtual nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; /** * Creates a Asynchronous (Observable) counter with the passed characteristics and returns a @@ -65,18 +60,13 @@ class Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; + void *state = nullptr) noexcept = 0; - virtual void CreateDoubleObservableCounter( - nostd::string_view name, - void (*callback)(ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; + virtual void CreateDoubleObservableCounter(nostd::string_view name, + void (*callback)(ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; /** * Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram. @@ -89,16 +79,12 @@ class Meter virtual nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; virtual nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; /** * Creates a Asynchronouse (Observable) Gauge with the passed characteristics and returns a @@ -114,17 +100,13 @@ class Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; + void *state = nullptr) noexcept = 0; virtual void CreateDoubleObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; + void *state = nullptr) noexcept = 0; /** * Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that @@ -138,16 +120,12 @@ class Meter virtual nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; virtual nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept = 0; + nostd::string_view unit = "") noexcept = 0; /** * Creates a Asynchronouse (Observable) UpDownCounter with the passed characteristics and returns @@ -159,23 +137,18 @@ class Meter * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. * @param state to be passed back to callback */ - virtual void CreateLongObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; - - virtual void CreateDoubleObservableUpDownCounter( - nostd::string_view name, - void (*callback)(ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept = 0; + virtual void CreateLongObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &, void *), + nostd::string_view description = "", + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; + + virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(ObserverResult &, + void *), + nostd::string_view description = "", + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 8c98155d9b..c47f7489f0 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -122,12 +122,9 @@ class NoopObservableUpDownCounter : public ObservableUpDownCounter class NoopMeter final : public Meter { public: - nostd::shared_ptr> CreateLongCounter( - nostd::string_view name, - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::shared_ptr> CreateLongCounter(nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{new NoopCounter(name, description, unit)}; } @@ -135,9 +132,7 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{new NoopCounter(name, description, unit)}; } @@ -146,26 +141,20 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override + void *state = nullptr) noexcept override {} void CreateDoubleObservableCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{new NoopHistogram(name, description, unit)}; } @@ -173,9 +162,7 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{new NoopHistogram(name, description, unit)}; } @@ -184,26 +171,20 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override + void *state = nullptr) noexcept override {} void CreateDoubleObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit)}; @@ -212,9 +193,7 @@ class NoopMeter final : public Meter nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override + nostd::string_view unit = "") noexcept override { return nostd::shared_ptr>{ new NoopUpDownCounter(name, description, unit)}; @@ -224,17 +203,13 @@ class NoopMeter final : public Meter void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override + void *state = nullptr) noexcept override {} void CreateDoubleObservableUpDownCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, void *state = nullptr) noexcept override {} }; diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index da6049692a..e32b7fc1a5 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -10,7 +10,6 @@ # include # include "foo_library.h" # include "opentelemetry/context/context.h" -# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/metrics/provider.h" namespace nostd = opentelemetry::nostd; @@ -72,19 +71,11 @@ void foo_library::observable_counter_example(const std::string &name) void foo_library::histogram_example(const std::string &name) { - nostd::shared_ptr aggregation_config{ - new opentelemetry::metrics::HistogramAggregationConfig}; - static_cast *>( - aggregation_config.get()) - ->boundaries_ = - std::list{0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; - ; std::string histogram_name = name + "_histogram"; auto provider = metrics_api::Provider::GetMeterProvider(); nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); - auto histogram_counter = - meter->CreateDoubleHistogram(histogram_name, "des", "unit", aggregation_config); - auto context = opentelemetry::context::Context{}; + auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "unit"); + auto context = opentelemetry::context::Context{}; while (true) { double val = (rand() % 700) + 1.1; diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 585856f91f..7352d9299e 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -72,8 +72,14 @@ void initMetrics(const std::string &name) new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)}; std::unique_ptr histogram_meter_selector{ new metric_sdk::MeterSelector(name, version, schema)}; - std::unique_ptr histogram_view{ - new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}}; + nostd::shared_ptr aggregation_config{ + new opentelemetry::sdk::metrics::HistogramAggregationConfig}; + static_cast *>( + aggregation_config.get()) + ->boundaries_ = + std::list{0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; + std::unique_ptr histogram_view{new metric_sdk::View{ + name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}}; p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); metrics_api::Provider::SetMeterProvider(provider); diff --git a/api/include/opentelemetry/metrics/aggregation_config.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation_config.h similarity index 93% rename from api/include/opentelemetry/metrics/aggregation_config.h rename to sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation_config.h index dbc3cfded7..bd2fe55c5a 100644 --- a/api/include/opentelemetry/metrics/aggregation_config.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/aggregation_config.h @@ -6,9 +6,10 @@ # include # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ namespace metrics { - class AggregationConfig { public: @@ -22,6 +23,7 @@ class HistogramAggregationConfig : public AggregationConfig std::list boundaries_; }; } // namespace metrics +} // namespace sdk OPENTELEMETRY_END_NAMESPACE #endif // ENABLE_METRICS_PREVIEW \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index a17e418bdb..624ce90780 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -6,8 +6,8 @@ #include "opentelemetry/sdk/metrics/data/point_data.h" #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/spin_lock_mutex.h" -# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" +# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h" @@ -27,7 +27,7 @@ class DefaultAggregation public: static std::unique_ptr CreateAggregation( const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, - const opentelemetry::metrics::AggregationConfig *aggregation_config) + const opentelemetry::sdk::metrics::AggregationConfig *aggregation_config) { switch (instrument_descriptor.type_) { @@ -42,12 +42,12 @@ class DefaultAggregation case InstrumentType::kHistogram: { return (instrument_descriptor.value_type_ == InstrumentValueType::kLong) ? std::move(std::unique_ptr(new LongHistogramAggregation( - static_cast - *>(aggregation_config)))) - : std::move(std::unique_ptr(new DoubleHistogramAggregation( static_cast< - const opentelemetry::metrics::HistogramAggregationConfig *>( - aggregation_config)))); + const opentelemetry::sdk::metrics::HistogramAggregationConfig *>( + aggregation_config)))) + : std::move(std::unique_ptr(new DoubleHistogramAggregation( + static_cast *>(aggregation_config)))); break; } case InstrumentType::kObservableGauge: diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h index 6f7a009d11..e648a6a4bc 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h @@ -4,8 +4,8 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/spin_lock_mutex.h" -# include "opentelemetry/metrics/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" +# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" # include @@ -18,8 +18,7 @@ namespace metrics class LongHistogramAggregation : public Aggregation { public: - LongHistogramAggregation( - const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config = nullptr); + LongHistogramAggregation(const HistogramAggregationConfig *aggregation_config = nullptr); LongHistogramAggregation(HistogramPointData &&); LongHistogramAggregation(const HistogramPointData &); @@ -48,8 +47,8 @@ class LongHistogramAggregation : public Aggregation class DoubleHistogramAggregation : public Aggregation { public: - DoubleHistogramAggregation(const opentelemetry::metrics::HistogramAggregationConfig - *aggregation_config = nullptr); + DoubleHistogramAggregation( + const HistogramAggregationConfig *aggregation_config = nullptr); DoubleHistogramAggregation(HistogramPointData &&); DoubleHistogramAggregation(const HistogramPointData &); diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 54c57ff194..540860ae48 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW # include # include "opentelemetry/metrics/meter.h" @@ -37,98 +36,74 @@ class Meter final : public opentelemetry::metrics::Meter nostd::shared_ptr> CreateLongCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; nostd::shared_ptr> CreateDoubleCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; - void CreateLongObservableCounter( - nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void CreateLongObservableCounter(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), + nostd::string_view description = "", + nostd::string_view unit = "", + void *state = nullptr) noexcept override; void CreateDoubleObservableCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; nostd::shared_ptr> CreateDoubleHistogram( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; - void CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &, void *), - nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void CreateLongObservableGauge(nostd::string_view name, + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), + nostd::string_view description = "", + nostd::string_view unit = "", + void *state = nullptr) noexcept override; void CreateDoubleObservableGauge( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; nostd::shared_ptr> CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description = "", - nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}) noexcept override; + nostd::string_view unit = "") noexcept override; void CreateLongObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void *state = nullptr) noexcept override; void CreateDoubleObservableUpDownCounter( nostd::string_view name, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "", - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, - void *state = nullptr) noexcept override; + void *state = nullptr) noexcept override; /** Returns the associated instruementation library */ const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() @@ -147,20 +122,18 @@ class Meter final : public opentelemetry::metrics::Meter std::unordered_map> storage_registry_; std::unique_ptr RegisterMetricStorage( - InstrumentDescriptor &instrument_descriptor, - nostd::shared_ptr aggregation_config); + InstrumentDescriptor &instrument_descriptor); template - void RegisterAsyncMetricStorage( - InstrumentDescriptor &instrument_descriptor, - void (*callback)(opentelemetry::metrics::ObserverResult &, void *), - nostd::shared_ptr aggregation_config, - void *state = nullptr) + void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), + void *state = nullptr) { auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, - [this, &instrument_descriptor, callback, aggregation_config, state](const View &view) { + [this, &instrument_descriptor, callback, state](const View &view) { auto view_instr_desc = instrument_descriptor; if (!view.GetName().empty()) { @@ -170,9 +143,9 @@ class Meter final : public opentelemetry::metrics::Meter { view_instr_desc.description_ = view.GetDescription(); } - auto storage = std::shared_ptr>( - new AsyncMetricStorage(view_instr_desc, view.GetAggregationType(), callback, - &view.GetAttributesProcessor(), aggregation_config, state)); + auto storage = std::shared_ptr>(new AsyncMetricStorage( + view_instr_desc, view.GetAggregationType(), callback, &view.GetAttributesProcessor(), + view.GetAggregationConfig(), state)); storage_registry_[instrument_descriptor.name_] = storage; return true; }); diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 1f77eebac9..900e027586 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -26,13 +26,13 @@ template class AsyncMetricStorage : public MetricStorage { public: - AsyncMetricStorage( - InstrumentDescriptor instrument_descriptor, - const AggregationType aggregation_type, - void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, void *), - const AttributesProcessor *attributes_processor, - nostd::shared_ptr aggregation_config, - void *state = nullptr) + AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, + const AggregationType aggregation_type, + void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, + void *), + const AttributesProcessor *attributes_processor, + nostd::shared_ptr aggregation_config, + void *state = nullptr) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, measurement_collection_callback_{measurement_callback}, 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 17e397a20b..515f66bd71 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -31,7 +31,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage const AggregationType aggregation_type, const AttributesProcessor *attributes_processor, nostd::shared_ptr &&exemplar_reservoir, - nostd::shared_ptr aggregation_config) + nostd::shared_ptr aggregation_config) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), diff --git a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h index d79b917988..d5aed04930 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h @@ -25,9 +25,8 @@ struct LastReportedMetrics class TemporalMetricStorage { public: - TemporalMetricStorage( - InstrumentDescriptor instrument_descriptor, - nostd::shared_ptr aggregation_config); + TemporalMetricStorage(InstrumentDescriptor instrument_descriptor, + nostd::shared_ptr aggregation_config); bool buildMetrics(CollectorHandle *collector, nostd::span> collectors, @@ -47,7 +46,7 @@ class TemporalMetricStorage // Lock while building metrics mutable opentelemetry::common::SpinLockMutex lock_; - const nostd::shared_ptr aggregation_config_; + const nostd::shared_ptr aggregation_config_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view.h b/sdk/include/opentelemetry/sdk/metrics/view/view.h index 3cd9f850e1..9c0333f565 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view.h @@ -3,7 +3,9 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -24,12 +26,15 @@ class View View(const std::string &name, const std::string &description = "", AggregationType aggregation_type = AggregationType::kDefault, + nostd::shared_ptr aggregation_config = + nostd::shared_ptr{}, std::unique_ptr attributes_processor = std::unique_ptr( new opentelemetry::sdk::metrics::DefaultAttributesProcessor())) : name_(name), description_(description), aggregation_type_{aggregation_type}, + aggregation_config_{aggregation_config}, attributes_processor_{std::move(attributes_processor)} {} @@ -39,6 +44,11 @@ class View virtual AggregationType GetAggregationType() const noexcept { return aggregation_type_; } + virtual nostd::shared_ptr GetAggregationConfig() const noexcept + { + return aggregation_config_; + } + virtual const opentelemetry::sdk::metrics::AttributesProcessor &GetAttributesProcessor() const noexcept { @@ -49,6 +59,7 @@ class View std::string name_; std::string description_; AggregationType aggregation_type_; + nostd::shared_ptr aggregation_config_; std::unique_ptr attributes_processor_; }; } // namespace metrics diff --git a/sdk/src/metrics/aggregation/histogram_aggregation.cc b/sdk/src/metrics/aggregation/histogram_aggregation.cc index 61e1310a67..79f0714b54 100644 --- a/sdk/src/metrics/aggregation/histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/histogram_aggregation.cc @@ -13,7 +13,7 @@ namespace metrics { LongHistogramAggregation::LongHistogramAggregation( - const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config) + const HistogramAggregationConfig *aggregation_config) { if (aggregation_config && aggregation_config->boundaries_.size()) { @@ -82,7 +82,7 @@ PointType LongHistogramAggregation::ToPoint() const noexcept } DoubleHistogramAggregation::DoubleHistogramAggregation( - const opentelemetry::metrics::HistogramAggregationConfig *aggregation_config) + const HistogramAggregationConfig *aggregation_config) { if (aggregation_config && aggregation_config->boundaries_.size()) { diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 65ab6864f8..5453eeae3a 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -10,6 +10,7 @@ # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/sdk/metrics/sync_instruments.h" +# include "opentelemetry/sdk_config.h" # include @@ -28,16 +29,14 @@ Meter::Meter(std::shared_ptr meter_context, : instrumentation_library_{std::move(instrumentation_library)}, meter_context_{meter_context} {} -nostd::shared_ptr> Meter::CreateLongCounter( - nostd::string_view name, - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept +nostd::shared_ptr> Meter::CreateLongCounter(nostd::string_view name, + nostd::string_view description, + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kCounter, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>( new LongCounter(instrument_descriptor, std::move(storage))); } @@ -45,59 +44,54 @@ nostd::shared_ptr> Meter::CreateLongCounter( nostd::shared_ptr> Meter::CreateDoubleCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kCounter, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>{ new DoubleCounter(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateLongObservableCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } -void Meter::CreateDoubleObservableCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateDoubleObservableCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, + void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } nostd::shared_ptr> Meter::CreateLongHistogram( nostd::string_view name, nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kHistogram, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>{ new LongHistogram(instrument_descriptor, std::move(storage))}; } @@ -105,59 +99,53 @@ nostd::shared_ptr> Meter::CreateLongHistogram( nostd::shared_ptr> Meter::CreateDoubleHistogram( nostd::string_view name, nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kHistogram, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>{ new DoubleHistogram(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableGauge( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateLongObservableGauge(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } -void Meter::CreateDoubleObservableGauge( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateDoubleObservableGauge(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter, InstrumentValueType::kLong}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>{ new LongUpDownCounter(instrument_descriptor, std::move(storage))}; } @@ -165,46 +153,43 @@ nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::shared_ptr> Meter::CreateDoubleUpDownCounter( nostd::string_view name, nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config) noexcept + nostd::string_view unit) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter, InstrumentValueType::kDouble}; - auto storage = RegisterMetricStorage(instrument_descriptor, aggregation_config); + auto storage = RegisterMetricStorage(instrument_descriptor); return nostd::shared_ptr>{ new DoubleUpDownCounter(instrument_descriptor, std::move(storage))}; } -void Meter::CreateLongObservableUpDownCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, + void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } -void Meter::CreateDoubleObservableUpDownCounter( - nostd::string_view name, - void (*callback)(metrics::ObserverResult &, void *), - nostd::string_view description, - nostd::string_view unit, - nostd::shared_ptr aggregation_config, - void *state) noexcept +void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, + void (*callback)(metrics::ObserverResult &, + void *), + nostd::string_view description, + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback, aggregation_config, state); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() @@ -214,15 +199,14 @@ const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumenta } std::unique_ptr Meter::RegisterMetricStorage( - InstrumentDescriptor &instrument_descriptor, - nostd::shared_ptr aggregation_config) + InstrumentDescriptor &instrument_descriptor) { auto view_registry = meter_context_->GetViewRegistry(); std::unique_ptr storages(new MultiMetricStorage()); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, - [this, &instrument_descriptor, &storages, &aggregation_config](const View &view) { + [this, &instrument_descriptor, &storages](const View &view) { auto view_instr_desc = instrument_descriptor; if (!view.GetName().empty()) { @@ -234,7 +218,7 @@ std::unique_ptr Meter::RegisterMetricStorage( } auto storage = std::shared_ptr(new SyncMetricStorage( view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(), - NoExemplarReservoir::GetNoExemplarReservoir(), aggregation_config)); + NoExemplarReservoir::GetNoExemplarReservoir(), view.GetAggregationConfig())); storage_registry_[instrument_descriptor.name_] = storage; auto multi_storage = static_cast(storages.get()); multi_storage->AddStorage(storage); diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index 9e94f80558..9a6ad8ebe9 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -19,7 +19,7 @@ namespace metrics TemporalMetricStorage::TemporalMetricStorage( InstrumentDescriptor instrument_descriptor, - nostd::shared_ptr aggregation_config) + nostd::shared_ptr aggregation_config) : instrument_descriptor_(instrument_descriptor), aggregation_config_(aggregation_config) {} diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index 5b595d2544..3f113661f6 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -127,7 +127,7 @@ TEST(Aggregation, LongHistogramAggregation) TEST(Aggregation, LongHistogramAggregationBoundaries) { nostd::shared_ptr> aggregation_config{ - new opentelemetry::metrics::HistogramAggregationConfig}; + new opentelemetry::sdk::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0, 50, 100, 250, 500, 750, 1000, 2500, 5000, 10000}; aggregation_config->boundaries_ = user_boundaries; LongHistogramAggregation aggr{aggregation_config.get()}; @@ -140,8 +140,8 @@ TEST(Aggregation, LongHistogramAggregationBoundaries) TEST(Aggregation, DoubleHistogramAggregationBoundaries) { - nostd::shared_ptr> aggregation_config{ - new opentelemetry::metrics::HistogramAggregationConfig}; + nostd::shared_ptr> + aggregation_config{new opentelemetry::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; aggregation_config->boundaries_ = user_boundaries; diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index ecb6aa339f..36e0f2ff2c 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -99,7 +99,7 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation) opentelemetry::sdk::metrics::AsyncMetricStorage storage( instr_desc, AggregationType::kSum, MeasurementFetcher::Fetcher, new DefaultAttributesProcessor(), - nostd::shared_ptr{}); + nostd::shared_ptr{}); storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { From 70962305ae82c6dbf37fd5bd6b1a2383f8cf23a2 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 25 Jul 2022 19:47:14 +0000 Subject: [PATCH 4/7] fix headers format --- examples/common/metrics_foo_library/foo_library.cc | 6 +++--- .../sdk/metrics/aggregation/default_aggregation.h | 4 ++-- .../sdk/metrics/state/async_metric_storage.h | 2 +- .../opentelemetry/sdk/metrics/state/sync_metric_storage.h | 2 +- .../sdk/metrics/state/temporal_metric_storage.h | 2 +- sdk/src/metrics/state/temporal_metric_storage.cc | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index e32b7fc1a5..c46276833c 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -1,16 +1,16 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include -#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW +# include "foo_library.h" # include # include +# include # include # include -# include "foo_library.h" # include "opentelemetry/context/context.h" # include "opentelemetry/metrics/provider.h" +# include "opentelemetry/nostd/shared_ptr.h" namespace nostd = opentelemetry::nostd; namespace metrics_api = opentelemetry::metrics; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index 624ce90780..98221d9b46 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -2,9 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include -#include "opentelemetry/sdk/metrics/data/point_data.h" #ifndef ENABLE_METRICS_PREVIEW +# include # include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" @@ -12,6 +11,7 @@ # include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h" # include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h" +# include "opentelemetry/sdk/metrics/data/point_data.h" # include "opentelemetry/sdk/metrics/instruments.h" # include diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 900e027586..11e7d83c14 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/instruments.h" 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 515f66bd71..d547f3699f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include #ifndef ENABLE_METRICS_PREVIEW +# include # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" diff --git a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h index d5aed04930..f8a0c36b0f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include "opentelemetry/sdk/metrics/state/metric_collector.h" diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index 9a6ad8ebe9..bee2986f09 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -1,11 +1,11 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include -#include -#include -#include "opentelemetry/nostd/shared_ptr.h" #ifndef ENABLE_METRICS_PREVIEW +# include +# include +# include +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" From 3dd8053e6174dbafb430b9b4d68443ad73be2871 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 25 Jul 2022 20:06:09 +0000 Subject: [PATCH 5/7] fix CI --- sdk/test/metrics/aggregation_test.cc | 6 +++--- sdk/test/metrics/sync_metric_storage_test.cc | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/test/metrics/aggregation_test.cc b/sdk/test/metrics/aggregation_test.cc index 3f113661f6..3fb9d55126 100644 --- a/sdk/test/metrics/aggregation_test.cc +++ b/sdk/test/metrics/aggregation_test.cc @@ -126,8 +126,8 @@ TEST(Aggregation, LongHistogramAggregation) TEST(Aggregation, LongHistogramAggregationBoundaries) { - nostd::shared_ptr> aggregation_config{ - new opentelemetry::sdk::metrics::HistogramAggregationConfig}; + nostd::shared_ptr> + aggregation_config{new opentelemetry::sdk::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0, 50, 100, 250, 500, 750, 1000, 2500, 5000, 10000}; aggregation_config->boundaries_ = user_boundaries; LongHistogramAggregation aggr{aggregation_config.get()}; @@ -141,7 +141,7 @@ TEST(Aggregation, LongHistogramAggregationBoundaries) TEST(Aggregation, DoubleHistogramAggregationBoundaries) { nostd::shared_ptr> - aggregation_config{new opentelemetry::metrics::HistogramAggregationConfig}; + aggregation_config{new opentelemetry::sdk::metrics::HistogramAggregationConfig}; std::list user_boundaries = {0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0}; aggregation_config->boundaries_ = user_boundaries; diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 81c973f81c..89086099b1 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -45,7 +45,7 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), NoExemplarReservoir::GetNoExemplarReservoir(), - nostd::shared_ptr{}); + nostd::shared_ptr{}); storage.RecordLong(10l, KeyValueIterableView>(attributes_get), opentelemetry::context::Context{}); @@ -152,7 +152,7 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), NoExemplarReservoir::GetNoExemplarReservoir(), - nostd::shared_ptr{}); + nostd::shared_ptr{}); storage.RecordDouble(10.0, KeyValueIterableView>(attributes_get), From 7462b50c773e770edc304107c75c1968b74da9b1 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 1 Aug 2022 19:29:41 +0000 Subject: [PATCH 6/7] review comment --- sdk/include/opentelemetry/sdk/metrics/view/view.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view.h b/sdk/include/opentelemetry/sdk/metrics/view/view.h index 9c0333f565..a7f8edb8fc 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view.h @@ -24,10 +24,9 @@ class View { public: View(const std::string &name, - const std::string &description = "", - AggregationType aggregation_type = AggregationType::kDefault, - nostd::shared_ptr aggregation_config = - nostd::shared_ptr{}, + const std::string &description = "", + AggregationType aggregation_type = AggregationType::kDefault, + std::shared_ptr aggregation_config = std::shared_ptr{}, std::unique_ptr attributes_processor = std::unique_ptr( new opentelemetry::sdk::metrics::DefaultAttributesProcessor())) From dcbad53e00f1395776ef05ddc4f3970b5b8444fc Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 1 Aug 2022 20:01:57 +0000 Subject: [PATCH 7/7] fic CI --- examples/metrics_simple/metrics_ostream.cc | 2 +- sdk/test/metrics/async_metric_storage_test.cc | 2 +- sdk/test/metrics/sync_metric_storage_test.cc | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 7352d9299e..669362830b 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -72,7 +72,7 @@ void initMetrics(const std::string &name) new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)}; std::unique_ptr histogram_meter_selector{ new metric_sdk::MeterSelector(name, version, schema)}; - nostd::shared_ptr aggregation_config{ + std::shared_ptr aggregation_config{ new opentelemetry::sdk::metrics::HistogramAggregationConfig}; static_cast *>( aggregation_config.get()) diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index 40505286c6..6e2e399e44 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -101,7 +101,7 @@ TEST_P(WritableMetricStorageTestFixture, TestAggregation) opentelemetry::sdk::metrics::AsyncMetricStorage storage( instr_desc, AggregationType::kSum, MeasurementFetcher::Fetcher, default_attributes_rocessor.get(), - nostd::shared_ptr{}); + std::shared_ptr{}); storage.Collect(collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData data) { diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 23db91a3d8..cb24ae5178 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -48,7 +48,7 @@ TEST_P(WritableMetricStorageTestFixture, LongSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, default_attributes_processor.get(), NoExemplarReservoir::GetNoExemplarReservoir(), - nostd::shared_ptr{}); + std::shared_ptr{}); storage.RecordLong(10l, KeyValueIterableView>(attributes_get), opentelemetry::context::Context{}); @@ -157,7 +157,7 @@ TEST_P(WritableMetricStorageTestFixture, DoubleSumAggregation) opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kSum, default_attributes_processor.get(), NoExemplarReservoir::GetNoExemplarReservoir(), - nostd::shared_ptr{}); + std::shared_ptr{}); storage.RecordDouble(10.0, KeyValueIterableView>(attributes_get),