diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 51e8e200b9..24c60b454a 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -6,6 +6,7 @@ # include # include "opentelemetry/metrics/meter.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/measurement_processor.h" # include "opentelemetry/sdk/metrics/meter_context.h" # include "opentelemetry/sdk/resource/resource.h" @@ -20,7 +21,7 @@ class Meter final : public opentelemetry::metrics::Meter { public: /** Construct a new Meter with the given pipeline. */ - explicit Meter(std::shared_ptr context, + explicit Meter(std::shared_ptr meter_context, std::unique_ptr instrumentation_library = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create( @@ -105,7 +106,12 @@ class Meter final : public opentelemetry::metrics::Meter // order of declaration is important here - instrumentation library should destroy after // meter-context. std::unique_ptr instrumentation_library_; - std::shared_ptr context_; + std::shared_ptr meter_context_; + // Mapping between instrument-name and Aggregation Storage. + std::unordered_map> storage_registry_; + + std::unique_ptr RegisterMetricStorage( + InstrumentDescriptor &instrument_descriptor); }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context.h b/sdk/include/opentelemetry/sdk/metrics/meter_context.h index 2df7d4868f..f0fdc6d946 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -52,6 +52,12 @@ class MeterContext */ MeasurementProcessor *GetMeasurementProcessor() const noexcept; + /** + * Obtain the View Registry configured + * @return The reference to view registry + */ + ViewRegistry *GetViewRegistry() const noexcept; + /** * Attaches a metric exporter to list of configured exporters for this Meter context. * @param exporter The metric exporter for this meter context. This diff --git a/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h new file mode 100644 index 0000000000..d9a66f732a --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h @@ -0,0 +1,66 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/state/metric_storage.h" + +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class MultiMetricStorage : public WritableMetricStorage +{ +public: + void AddStorage(std::shared_ptr storage) { storages_.push_back(storage); } + + virtual void RecordLong(long value) noexcept override + { + for (auto &s : storages_) + { + s->RecordLong(value); + } + } + + virtual void RecordLong( + long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + for (auto &s : storages_) + { + s->RecordLong(value, attributes); + } + } + + virtual void RecordDouble(double value) noexcept override + { + for (auto &s : storages_) + { + s->RecordDouble(value); + } + } + + virtual void RecordDouble( + double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + for (auto &s : storages_) + { + s->RecordDouble(value, attributes); + } + } + +private: + std::vector> storages_; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif 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 063cf7f6bb..ee64c5fba4 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -25,9 +25,10 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage { public: - SyncMetricStorage(InstrumentDescriptor instrument_descriptor, - const AggregationType aggregation_type, - AttributesProcessor *attributes_processor = new DefaultAttributesProcessor()) + SyncMetricStorage( + InstrumentDescriptor instrument_descriptor, + const AggregationType aggregation_type, + const AttributesProcessor *attributes_processor = new DefaultAttributesProcessor()) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), @@ -104,7 +105,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage InstrumentDescriptor instrument_descriptor_; AggregationType aggregation_type_; std::unique_ptr attributes_hashmap_; - AttributesProcessor *attributes_processor_; + const AttributesProcessor *attributes_processor_; std::function()> create_default_aggregation_; std::unique_ptr create_aggregation() diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index c77d5d9a65..aa0348f0b0 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -19,36 +19,21 @@ namespace metrics class Synchronous { public: - Synchronous(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = "") - : name_(name), - instrumentation_library_(instrumentation_library), - measurement_processor_(measurement_processor), - description_(description), - unit_(unit) + Synchronous(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : instrument_descriptor_(instrument_descriptor), storage_(std::move(storage)) {} protected: - std::string name_; - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; - MeasurementProcessor *measurement_processor_; - std::string description_; - std::string unit_; + InstrumentDescriptor instrument_descriptor_; + std::unique_ptr storage_; }; class LongCounter : public Synchronous, public opentelemetry::metrics::Counter { public: - LongCounter(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + LongCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; @@ -59,12 +44,8 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter { public: - DoubleCounter(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + DoubleCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; @@ -75,12 +56,8 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter { public: - LongUpDownCounter(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + LongUpDownCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; @@ -90,12 +67,8 @@ class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpD class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter { public: - DoubleUpDownCounter(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; @@ -106,12 +79,8 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram { public: - LongHistogram(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + LongHistogram(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Record(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; @@ -122,12 +91,8 @@ class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogr class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram { public: - DoubleHistogram(nostd::string_view name, - const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description = "", - nostd::string_view unit = ""); + DoubleHistogram(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage); void Record(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index 6611abe97f..fdc4e35c53 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -22,7 +22,7 @@ class AttributesProcessor // Process the metric instrument attributes. // @returns The processed attributes virtual MetricAttributes process( - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0; }; /** @@ -33,7 +33,7 @@ class AttributesProcessor class DefaultAttributesProcessor : public AttributesProcessor { MetricAttributes process( - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes) const noexcept override { MetricAttributes result(attributes); return result; @@ -54,7 +54,7 @@ class FilteringAttributesProcessor : public AttributesProcessor {} MetricAttributes process( - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes) const noexcept override { MetricAttributes result; attributes.ForEachKeyValue( diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view.h b/sdk/include/opentelemetry/sdk/metrics/view/view.h index 4d1119d894..e88e7126c6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view.h @@ -5,6 +5,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/nostd/string_view.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -21,15 +22,14 @@ class View { public: View(const std::string &name, - const std::string &description = "", - std::unique_ptr aggregation = - std::unique_ptr(new DropAggregation()), + const std::string &description = "", + AggregationType aggregation_type = AggregationType::kDrop, std::unique_ptr attributes_processor = std::unique_ptr( new opentelemetry::sdk::metrics::DefaultAttributesProcessor())) : name_(name), description_(description), - aggregation_{std::move(aggregation)}, + aggregation_type_{aggregation_type}, attributes_processor_{std::move(attributes_processor)} {} @@ -37,10 +37,7 @@ class View virtual std::string GetDescription() const noexcept { return description_; } - virtual const opentelemetry::sdk::metrics::Aggregation &GetAggregation() const noexcept - { - return *aggregation_.get(); - } + virtual AggregationType GetAggregationType() const noexcept { return aggregation_type_; } virtual const opentelemetry::sdk::metrics::AttributesProcessor &GetAttributesProcessor() const noexcept @@ -51,7 +48,7 @@ class View private: std::string name_; std::string description_; - std::unique_ptr aggregation_; + AggregationType aggregation_type_; std::unique_ptr attributes_processor_; }; } // namespace metrics diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 2ab7f068f7..52ecfc9e4e 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -6,7 +6,9 @@ # include "opentelemetry/metrics/noop.h" # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/async_instruments.h" +# include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/sdk/metrics/sync_instruments.h" +# include "opentelemetry/sdk_config.h" # include "opentelemetry/version.h" @@ -21,18 +23,22 @@ namespace metrics namespace metrics = opentelemetry::metrics; namespace nostd = opentelemetry::nostd; -Meter::Meter(std::shared_ptr context, +Meter::Meter(std::shared_ptr meter_context, std::unique_ptr instrumentation_library) noexcept - : context_{context}, instrumentation_library_{std::move(instrumentation_library)} + : meter_context_{meter_context}, instrumentation_library_{std::move(instrumentation_library)} {} nostd::shared_ptr> Meter::CreateLongCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>(new LongCounter( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)); + 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); + return nostd::shared_ptr>( + new LongCounter(instrument_descriptor, std::move(storage))); } nostd::shared_ptr> Meter::CreateDoubleCounter( @@ -40,8 +46,13 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new DoubleCounter( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; + 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); + return nostd::shared_ptr>{ + new DoubleCounter(instrument_descriptor, std::move(storage))}; } nostd::shared_ptr> Meter::CreateLongObservableCounter( @@ -69,8 +80,13 @@ nostd::shared_ptr> Meter::CreateLongHistogram( nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new LongHistogram( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; + 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); + return nostd::shared_ptr>{ + new LongHistogram(instrument_descriptor, std::move(storage))}; } nostd::shared_ptr> Meter::CreateDoubleHistogram( @@ -78,8 +94,13 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new DoubleHistogram( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; + 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); + return nostd::shared_ptr>{ + new DoubleHistogram(instrument_descriptor, std::move(storage))}; } nostd::shared_ptr> Meter::CreateLongObservableGauge( @@ -107,8 +128,13 @@ nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new LongUpDownCounter( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; + 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); + return nostd::shared_ptr>{ + new LongUpDownCounter(instrument_descriptor, std::move(storage))}; } nostd::shared_ptr> Meter::CreateDoubleUpDownCounter( @@ -116,8 +142,13 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount nostd::string_view description, nostd::string_view unit) noexcept { - return nostd::shared_ptr>{new DoubleUpDownCounter( - name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; + 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); + return nostd::shared_ptr>{ + new DoubleUpDownCounter(instrument_descriptor, std::move(storage))}; } nostd::shared_ptr> Meter::CreateLongObservableUpDownCounter( @@ -149,7 +180,36 @@ const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumenta MeasurementProcessor *Meter::GetMeasurementProcessor() const noexcept { - return context_->GetMeasurementProcessor(); + return meter_context_->GetMeasurementProcessor(); +} + +std::unique_ptr Meter::RegisterMetricStorage( + 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](const View &view) { + auto view_instr_desc = instrument_descriptor; + view_instr_desc.name_ = view.GetName(); + view_instr_desc.description_ = view.GetDescription(); + auto storage = std::shared_ptr(new SyncMetricStorage( + view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor())); + storage_registry_[instrument_descriptor.name_] = storage; + auto multi_storage = static_cast(storages.get()); + multi_storage->AddStorage(storage); + return true; + }); + + if (!success) + { + OTEL_INTERNAL_LOG_ERROR( + "[Meter::RegisterMetricStorage] - Error during finding matching views." + << "Some of the matching view configurations mayn't be used for metric collection"); + } + return std::move(storages); } } // namespace metrics diff --git a/sdk/src/metrics/meter_context.cc b/sdk/src/metrics/meter_context.cc index d2487ebecc..bc2f75de2a 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -33,6 +33,11 @@ MeasurementProcessor *MeterContext::GetMeasurementProcessor() const noexcept return measurement_processor_.get(); } +ViewRegistry *MeterContext::GetViewRegistry() const noexcept +{ + return views_.get(); +} + void MeterContext::AddMetricExporter(std::unique_ptr exporter) noexcept { exporters_.push_back(std::move(exporter)); diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index d85066ce2c..85e770a715 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -9,124 +9,100 @@ namespace sdk { namespace metrics { -LongCounter::LongCounter( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +LongCounter::LongCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void LongCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordLong(value, attributes); + return storage_->RecordLong(value, attributes); } void LongCounter::Add(long value) noexcept { - return measurement_processor_->RecordLong(value); + return storage_->RecordLong(value); } -DoubleCounter::DoubleCounter( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +DoubleCounter::DoubleCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void DoubleCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordDouble(value, attributes); + return storage_->RecordDouble(value, attributes); } void DoubleCounter::Add(double value) noexcept { - return measurement_processor_->RecordDouble(value); + return storage_->RecordDouble(value); } -LongUpDownCounter::LongUpDownCounter( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +LongUpDownCounter::LongUpDownCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void LongUpDownCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordLong(value, attributes); + return storage_->RecordLong(value, attributes); } void LongUpDownCounter::Add(long value) noexcept { - return measurement_processor_->RecordLong(value); + return storage_->RecordLong(value); } -DoubleUpDownCounter::DoubleUpDownCounter( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +DoubleUpDownCounter::DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void DoubleUpDownCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordDouble(value, attributes); + return storage_->RecordDouble(value, attributes); } void DoubleUpDownCounter::Add(double value) noexcept { - return measurement_processor_->RecordDouble(value); + return storage_->RecordDouble(value); } -LongHistogram::LongHistogram( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +LongHistogram::LongHistogram(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void LongHistogram::Record(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordLong(value, attributes); + return storage_->RecordLong(value, attributes); } void LongHistogram::Record(long value) noexcept { - return measurement_processor_->RecordLong(value); + return storage_->RecordLong(value); } -DoubleHistogram::DoubleHistogram( - nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, - MeasurementProcessor *measurement_processor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, instrumentation_library, measurement_processor, description, unit) +DoubleHistogram::DoubleHistogram(InstrumentDescriptor instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) {} void DoubleHistogram::Record(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_->RecordDouble(value, attributes); + return storage_->RecordDouble(value, attributes); } void DoubleHistogram::Record(double value) noexcept { - return measurement_processor_->RecordDouble(value); + return storage_->RecordDouble(value); } } // namespace metrics diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD index f1350469a7..d49d855d8f 100644 --- a/sdk/test/metrics/BUILD +++ b/sdk/test/metrics/BUILD @@ -64,6 +64,22 @@ cc_test( ], ) +cc_test( + name = "multi_metric_storage_test", + srcs = [ + "multi_metric_storage_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//sdk/src/metrics", + "//sdk/src/resource", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "attributes_processor_test", srcs = [ diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 279ff541fd..fb823feaca 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -6,6 +6,7 @@ foreach( attributes_processor_test attributes_hashmap_test sync_metric_storage_test + multi_metric_storage_test sync_instruments_test async_instruments_test) add_executable(${testname} "${testname}.cc") diff --git a/sdk/test/metrics/multi_metric_storage_test.cc b/sdk/test/metrics/multi_metric_storage_test.cc new file mode 100644 index 0000000000..6696c7bb0a --- /dev/null +++ b/sdk/test/metrics/multi_metric_storage_test.cc @@ -0,0 +1,53 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" +# include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" + +# include + +using namespace opentelemetry; +using namespace opentelemetry::sdk::instrumentationlibrary; +using namespace opentelemetry::sdk::metrics; + +class TestMetricStorage : public WritableMetricStorage +{ +public: + void RecordLong(long value) noexcept override { num_calls_long++; } + + void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + num_calls_long++; + } + + void RecordDouble(double value) noexcept override { num_calls_double++; } + + void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + num_calls_double++; + } + + size_t num_calls_long; + size_t num_calls_double; +}; + +TEST(MultiMetricStorageTest, BasicTests) +{ + std::shared_ptr storage( + new TestMetricStorage()); + MultiMetricStorage storages; + storages.AddStorage(storage); + EXPECT_NO_THROW(storages.RecordLong(10l)); + EXPECT_NO_THROW(storages.RecordLong(20l)); + + EXPECT_NO_THROW(storages.RecordDouble(10.0)); + EXPECT_NO_THROW(storages.RecordLong(30l)); + + EXPECT_EQ(static_cast(storage.get())->num_calls_long, 3); + EXPECT_EQ(static_cast(storage.get())->num_calls_double, 1); +} +#endif diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index f99e22377e..27af8711d1 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -4,7 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/sync_instruments.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" -# include "opentelemetry/sdk/metrics/measurement_processor.h" +# include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include @@ -13,14 +13,15 @@ using namespace opentelemetry::sdk::instrumentationlibrary; using namespace opentelemetry::sdk::metrics; auto instrumentation_library = InstrumentationLibrary::Create("opentelemetry-cpp", "0.1.0"); -DefaultMeasurementProcessor measurement_processor; using M = std::map; TEST(SyncInstruments, LongCounter) { - LongCounter counter("long_counter", instrumentation_library.get(), &measurement_processor, - "description", "1"); + InstrumentDescriptor instrument_descriptor = { + "long_counter", "description", "1", InstrumentType::kCounter, InstrumentValueType::kLong}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + LongCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); EXPECT_NO_THROW(counter.Add(10l)); @@ -31,8 +32,10 @@ TEST(SyncInstruments, LongCounter) TEST(SyncInstruments, DoubleCounter) { - DoubleCounter counter("double_counter", instrumentation_library.get(), &measurement_processor, - "description", "1"); + InstrumentDescriptor instrument_descriptor = { + "double_counter", "description", "1", InstrumentType::kCounter, InstrumentValueType::kDouble}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + DoubleCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); EXPECT_NO_THROW(counter.Add(10.10)); @@ -43,8 +46,11 @@ TEST(SyncInstruments, DoubleCounter) TEST(SyncInstruments, LongUpDownCounter) { - LongUpDownCounter counter("long_up_down_counter", instrumentation_library.get(), - &measurement_processor, "description", "1"); + InstrumentDescriptor instrument_descriptor = {"long_updowncounter", "description", "1", + InstrumentType::kUpDownCounter, + InstrumentValueType::kLong}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + LongUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); EXPECT_NO_THROW(counter.Add(10l)); @@ -55,8 +61,11 @@ TEST(SyncInstruments, LongUpDownCounter) TEST(SyncInstruments, DoubleUpDownCounter) { - DoubleUpDownCounter counter("double_up_down_counter", instrumentation_library.get(), - &measurement_processor, "description", "1"); + InstrumentDescriptor instrument_descriptor = {"double_updowncounter", "description", "1", + InstrumentType::kUpDownCounter, + InstrumentValueType::kDouble}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + DoubleUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); EXPECT_NO_THROW(counter.Add(10.10)); @@ -67,8 +76,10 @@ TEST(SyncInstruments, DoubleUpDownCounter) TEST(SyncInstruments, LongHistogram) { - LongHistogram counter("long_histogram", instrumentation_library.get(), &measurement_processor, - "description", "1"); + InstrumentDescriptor instrument_descriptor = { + "long_histogram", "description", "1", InstrumentType::kHistogram, InstrumentValueType::kLong}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + LongHistogram counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Record(10l)); EXPECT_NO_THROW(counter.Record(10l)); @@ -79,8 +90,11 @@ TEST(SyncInstruments, LongHistogram) TEST(SyncInstruments, DoubleHistogram) { - DoubleHistogram counter("double_histogram", instrumentation_library.get(), &measurement_processor, - "description", "1"); + InstrumentDescriptor instrument_descriptor = {"double_histogram", "description", "1", + InstrumentType::kHistogram, + InstrumentValueType::kDouble}; + std::unique_ptr metric_storage(new MultiMetricStorage()); + DoubleHistogram counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Record(10.10)); EXPECT_NO_THROW(counter.Record(10.10)); diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 98690a3bb2..916425dfa1 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -3,11 +3,12 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" -# include -# include # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/sdk/metrics/instruments.h" +# include +# include + using namespace opentelemetry::sdk::metrics; using M = std::map;