diff --git a/api/include/opentelemetry/metrics/async_instruments.h b/api/include/opentelemetry/metrics/async_instruments.h index daf43a32f5..065a47649a 100644 --- a/api/include/opentelemetry/metrics/async_instruments.h +++ b/api/include/opentelemetry/metrics/async_instruments.h @@ -4,7 +4,7 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW -# include "observer_result.h" +# include "opentelemetry/metrics/observer_result.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 9c8a776672..35cb8621ab 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -8,6 +8,7 @@ # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/nostd/type_traits.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h new file mode 100644 index 0000000000..d66bb69890 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -0,0 +1,179 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/metrics/async_instruments.h" +# include "opentelemetry/metrics/observer_result.h" +# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/measurement_processor.h" + +# include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +template +class Asynchronous +{ +public: + Asynchronous(nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : name_(name), + instrumentation_library_{instrumentation_library}, + measurement_processor_{measurement_processor}, + callback_(callback), + description_(description), + unit_(unit) + {} + +protected: + std::string name_; + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library_; + const MeasurementProcessor *measurement_processor_; + void (*callback_)(opentelemetry::metrics::ObserverResult &); + std::string description_; + std::string unit_; +}; + +class LongObservableCounter : public opentelemetry::metrics::ObservableCounter, + public Asynchronous +{ +public: + LongObservableCounter(nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + + {} +}; + +class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter, + public Asynchronous +{ +public: + DoubleObservableCounter(nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + + {} +}; + +class LongObservableGauge : public opentelemetry::metrics::ObservableGauge, + public Asynchronous +{ +public: + LongObservableGauge(nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + + {} +}; + +class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge, + public Asynchronous +{ +public: + DoubleObservableGauge(nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + + {} +}; + +class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter, + public Asynchronous +{ +public: + LongObservableUpDownCounter( + nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + + {} +}; + +class DoubleObservableUpDownCounter + : public opentelemetry::metrics::ObservableUpDownCounter, + public Asynchronous +{ +public: + DoubleObservableUpDownCounter( + nostd::string_view name, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library, + MeasurementProcessor *measurement_processor, + void (*callback)(opentelemetry::metrics::ObserverResult &), + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) + {} +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h new file mode 100644 index 0000000000..c7e7a6ed6a --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h @@ -0,0 +1,108 @@ +// 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/metric_reader.h" +# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" + +# include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +static std::size_t MakeKey(const MetricReader &metric_reader) +{ + return reinterpret_cast(&metric_reader); +} +class MeasurementProcessor +{ +public: + virtual void RecordLong(long value) noexcept = 0; + + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + + virtual void RecordDouble(double value) noexcept = 0; + + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + + virtual bool Collect(MetricReader &reader, + AggregationTemporarily aggregation_temporarily, + nostd::function_ref callback) noexcept = 0; +}; + +class DefaultMeasurementProcessor : public MeasurementProcessor +{ + +public: + bool AddMetricStorage(const MetricReader &reader) + { + // TBD = check if already present. + metric_storages_[MakeKey(reader)] = std::unique_ptr(new SyncMetricStorage()); + return true; + } + + virtual void RecordLong(long value) noexcept override + { + for (const auto &kv : metric_storages_) + { + kv.second->RecordLong(value); + } + } + + virtual void RecordLong( + long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + for (const auto &kv : metric_storages_) + { + kv.second->RecordLong(value, attributes); + } + } + + virtual void RecordDouble(double value) noexcept override + { + for (const auto &kv : metric_storages_) + { + kv.second->RecordDouble(value); + } + } + + virtual void RecordDouble( + double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + for (const auto &kv : metric_storages_) + { + kv.second->RecordDouble(value, attributes); + } + } + + bool Collect(MetricReader &reader, + AggregationTemporarily aggregation_temporarily, + nostd::function_ref callback) noexcept override + { + auto i = metric_storages_.find(MakeKey(reader)); + if (i != metric_storages_.end()) + { + return i->second->Collect(aggregation_temporarily, callback); + } + return false; + } + +private: + std::map> metric_storages_; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index b1a4e304d7..51e8e200b9 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/measurement_processor.h" # include "opentelemetry/sdk/metrics/meter_context.h" # include "opentelemetry/sdk/resource/resource.h" # include "opentelemetry/version.h" @@ -94,13 +95,16 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view unit = "") noexcept override; /** Returns the associated instruementation library */ - const sdk::instrumentationlibrary::InstrumentationLibrary &GetInstrumentationLibrary() + const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() const noexcept; + /** Returns the associated measurement processor */ + MeasurementProcessor *GetMeasurementProcessor() const noexcept; + private: // order of declaration is important here - instrumentation library should destroy after // meter-context. - std::shared_ptr instrumentation_library_; + std::unique_ptr instrumentation_library_; std::shared_ptr context_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context.h b/sdk/include/opentelemetry/sdk/metrics/meter_context.h index 9b0331a150..2df7d4868f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -46,6 +46,12 @@ class MeterContext */ const opentelemetry::sdk::resource::Resource &GetResource() const noexcept; + /** + * Obtain the reference of measurement_processor. + * + */ + MeasurementProcessor *GetMeasurementProcessor() 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 @@ -95,6 +101,7 @@ class MeterContext std::vector> exporters_; std::vector> readers_; std::unique_ptr views_; + std::unique_ptr measurement_processor_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index 71cd3fdd02..7d7966147f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -8,6 +8,7 @@ # include # include "opentelemetry/metrics/meter_provider.h" # include "opentelemetry/nostd/shared_ptr.h" +# include "opentelemetry/sdk/metrics/measurement_processor.h" # include "opentelemetry/sdk/metrics/meter.h" # include "opentelemetry/sdk/metrics/meter_context.h" # include "opentelemetry/sdk/resource/resource.h" @@ -51,6 +52,12 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider */ const sdk::resource::Resource &GetResource() const noexcept; + /** + * Obtain the reference of measurement processor. + * + */ + MeasurementProcessor *GetMeasurementProcessor() const noexcept; + /** * Attaches a metric exporter to list of configured exporters for this Meter provider. * @param exporter The metric exporter for this meter provider. This diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h index 4dedd80b4c..a31879c63c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_exporter.h @@ -13,6 +13,7 @@ namespace sdk { namespace metrics { + /** * MetricExporter defines the interface to be used by metrics libraries to * push metrics data to the OpenTelemetry exporters. @@ -43,6 +44,9 @@ class MetricExporter * @return return the status of the operation. */ virtual bool Shutdown() noexcept = 0; + +private: + AggregationTemporarily aggregation_temporarily; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index 92b4f9f219..16a1880113 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -4,6 +4,9 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include +# include "opentelemetry/sdk/common/global_log_handler.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -11,19 +14,52 @@ namespace sdk { namespace metrics { + /** * MetricReader defines the interface to collect metrics from SDK */ class MetricReader { public: + MetricReader( + AggregationTemporarily aggregation_temporarily = AggregationTemporarily::kCummulative) + : aggregation_temporarily_(aggregation_temporarily), measurement_processor_callback_({}) + {} + virtual ~MetricReader() = default; /** * Collect the metrics from SDK. * @return return the status of the operation. */ - virtual bool Collect() noexcept = 0; + bool Collect() noexcept + { + if (!measurement_processor_callback_) + { + OTEL_INTERNAL_LOG_WARN( + "Cannot invoke Collect() for MetricReader. No collection callback registered!") + } + return measurement_processor_callback_( + *this, aggregation_temporarily_, + [&](MetricData metric_data) noexcept { return ProcessReceivedMetrics(metric_data); }); + } + + /** + * Register the callback to Measurement Processor + * This function is internal to SDK. + */ + void RegisterCollectorCallback( + std::function)> measurement_processor_callback) + { + measurement_processor_callback_ = measurement_processor_callback; + } + + /** + * Process the metrics received through Measurement Processor. + */ + virtual bool ProcessReceivedMetrics(MetricData &metric_data) noexcept = 0; /** * Shut down the metric reader. @@ -31,6 +67,11 @@ class MetricReader * @return return the status of the operation. */ virtual bool Shutdown() noexcept = 0; + +private: + std::function)> + measurement_processor_callback_; + AggregationTemporarily aggregation_temporarily_; }; } // namespace metrics } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h new file mode 100644 index 0000000000..e25e1f9ed3 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -0,0 +1,73 @@ +// 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/data/metric_data.h" +# include "opentelemetry/sdk/metrics/instruments.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +/* Represent the storage from which to collect the metrics */ + +class MetricStorage +{ +public: + /* collect the metrics from this storage */ + virtual bool Collect(AggregationTemporarily aggregation_temporarily, + nostd::function_ref callback) noexcept = 0; +}; + +class WritableMetricStorage +{ +public: + virtual void RecordLong(long value) noexcept = 0; + + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + + virtual void RecordDouble(double value) noexcept = 0; + + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; +}; + +class NoopMetricStorage : public MetricStorage +{ +public: + bool Collect(AggregationTemporarily aggregation_temporarily, + nostd::function_ref callback) noexcept override + { + MetricData metric_data; + if (callback(metric_data)) + { + return true; + } + return false; + } +}; + +class NoopWritableMetricStorage : public WritableMetricStorage +{ +public: + void RecordLong(long value) noexcept = 0; + + void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + {} + + void RecordDouble(double value) noexcept override {} + + void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + {} +}; + +} // 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 new file mode 100644 index 0000000000..e0fde38c15 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -0,0 +1,76 @@ +// 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/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/state/metric_storage.h" +# include "opentelemetry/sdk/resource/resource.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class SyncMetricStorage : public MetricStorage, public WritableMetricStorage +{ + +public: + SyncMetricStorage( + // TBD + // Aggregation& aggregation + // opentelemetry::sdk::resource::resource *resource, + // sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, + ) + {} + + void RecordLong(long value) noexcept override + { + // TBD + // aggregation->recordLong(value); + } + + void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + // TBD + // aggregation->recordLong(value, attributes); + } + + void RecordDouble(double value) noexcept override + { + // TBD + // aggregation->recordDouble(value, attributes); + } + + void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override + { + // TBD + // aggregation->recordDouble(value, attributes); + } + + bool Collect(AggregationTemporarily aggregation_temporarily, + nostd::function_ref callback) noexcept override + { + /** + * This is placeholder while implementation is complete. + * In a real scenario (once fully implemented), Collect may generate more than + * metric data + */ + // while(there is metric data) { + MetricData metric_data; // fetch next metric data + if (!callback(metric_data)) + { + return false; + } + // } //while + return true; + } +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h new file mode 100644 index 0000000000..c77d5d9a65 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -0,0 +1,141 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/key_value_iterable.h" +# include "opentelemetry/metrics/sync_instruments.h" +# include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/measurement_processor.h" + +# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +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) + {} + +protected: + std::string name_; + const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + MeasurementProcessor *measurement_processor_; + std::string description_; + std::string unit_; +}; + +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 = ""); + + void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(long value) noexcept override; +}; + +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 = ""); + + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(double value) noexcept override; +}; + +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 = ""); + + void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(long value) noexcept override; +}; + +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 = ""); + + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(double value) noexcept override; +}; + +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 = ""); + + void Record(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Record(long value) noexcept override; +}; + +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 = ""); + + void Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Record(double value) noexcept override; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 72f9031e2a..f9b6d546ed 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -1,8 +1,12 @@ add_library( opentelemetry_metrics - meter_provider.cc meter.cc meter_context.cc - aggregation/histogram_aggregation.cc aggregation/lastvalue_aggregation.cc - aggregation/sum_aggregation.cc) + meter_provider.cc + meter.cc + meter_context.cc + aggregation/histogram_aggregation.cc + aggregation/lastvalue_aggregation.cc + aggregation/sum_aggregation.cc + sync_instruments.cc) set_target_properties(opentelemetry_metrics PROPERTIES EXPORT_NAME metrics) diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 6c759158a4..2ab7f068f7 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -5,7 +5,9 @@ # include "opentelemetry/sdk/metrics/meter.h" # include "opentelemetry/metrics/noop.h" # include "opentelemetry/nostd/shared_ptr.h" -# include "opentelemetry/sdk/common/global_log_handler.h" +# include "opentelemetry/sdk/metrics/async_instruments.h" +# include "opentelemetry/sdk/metrics/sync_instruments.h" + # include "opentelemetry/version.h" # include @@ -29,9 +31,8 @@ nostd::shared_ptr> Meter::CreateLongCounter(nostd::string nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopCounter(name, description, unit)}; + return nostd::shared_ptr>(new LongCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)); } nostd::shared_ptr> Meter::CreateDoubleCounter( @@ -39,9 +40,8 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateDoubleCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopCounter(name, description, unit)}; + return nostd::shared_ptr>{new DoubleCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; } nostd::shared_ptr> Meter::CreateLongObservableCounter( @@ -50,9 +50,8 @@ nostd::shared_ptr> Meter::CreateLongObservableC nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongObservableCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopObservableCounter(name, callback, description, unit)}; + return nostd::shared_ptr>{new LongObservableCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), callback, description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleObservableCounter( @@ -61,9 +60,8 @@ nostd::shared_ptr> Meter::CreateDoubleObserva nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateDoubleObservableCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopObservableCounter(name, callback, description, unit)}; + return nostd::shared_ptr>{new DoubleObservableCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), callback, description, unit)}; } nostd::shared_ptr> Meter::CreateLongHistogram( @@ -71,9 +69,8 @@ nostd::shared_ptr> Meter::CreateLongHistogram( nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongHistogram] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopHistogram(name, description, unit)}; + return nostd::shared_ptr>{new LongHistogram( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleHistogram( @@ -81,9 +78,8 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateDoubleHistogram] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopHistogram(name, description, unit)}; + return nostd::shared_ptr>{new DoubleHistogram( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; } nostd::shared_ptr> Meter::CreateLongObservableGauge( @@ -92,9 +88,8 @@ nostd::shared_ptr> Meter::CreateLongObservableGau nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongObservableGauge] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopObservableGauge(name, callback, description, unit)}; + return nostd::shared_ptr>{new LongObservableGauge( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), callback, description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleObservableGauge( @@ -103,9 +98,8 @@ nostd::shared_ptr> Meter::CreateDoubleObservabl nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateDoubleObservableGauge] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopObservableGauge(name, callback, description, unit)}; + return nostd::shared_ptr>{new DoubleObservableGauge( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), callback, description, unit)}; } nostd::shared_ptr> Meter::CreateLongUpDownCounter( @@ -113,9 +107,8 @@ nostd::shared_ptr> Meter::CreateLongUpDownCounter( nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateLongUpDownCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopUpDownCounter(name, description, unit)}; + return nostd::shared_ptr>{new LongUpDownCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; } nostd::shared_ptr> Meter::CreateDoubleUpDownCounter( @@ -123,9 +116,8 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN("[Meter::CreateDoubleUpDownCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopUpDownCounter(name, description, unit)}; + return nostd::shared_ptr>{new DoubleUpDownCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), description, unit)}; } nostd::shared_ptr> Meter::CreateLongObservableUpDownCounter( @@ -134,10 +126,8 @@ nostd::shared_ptr> Meter::CreateLongObser nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN( - "[Meter::CreateLongObservableUpDownCounter] Not Implemented - Returns Noop."); - return nostd::shared_ptr>{ - new metrics::NoopObservableUpDownCounter(name, callback, description, unit)}; + return nostd::shared_ptr>{new LongObservableUpDownCounter( + name, GetInstrumentationLibrary(), GetMeasurementProcessor(), callback, description, unit)}; } nostd::shared_ptr> @@ -146,16 +136,20 @@ Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, nostd::string_view description, nostd::string_view unit) noexcept { - OTEL_INTERNAL_LOG_WARN( - "[Meter::CreateDoubleObservableUpDownCounter] Not Implemented - Returns Noop."); return nostd::shared_ptr>{ - new metrics::NoopObservableUpDownCounter(name, callback, description, unit)}; + new DoubleObservableUpDownCounter(name, GetInstrumentationLibrary(), + GetMeasurementProcessor(), callback, description, unit)}; } -const sdk::instrumentationlibrary::InstrumentationLibrary &Meter::GetInstrumentationLibrary() +const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() const noexcept { - return *instrumentation_library_; + return instrumentation_library_.get(); +} + +MeasurementProcessor *Meter::GetMeasurementProcessor() const noexcept +{ + return context_->GetMeasurementProcessor(); } } // namespace metrics diff --git a/sdk/src/metrics/meter_context.cc b/sdk/src/metrics/meter_context.cc index b3f4f5c6bf..d2487ebecc 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -28,6 +28,11 @@ const resource::Resource &MeterContext::GetResource() const noexcept return resource_; } +MeasurementProcessor *MeterContext::GetMeasurementProcessor() const noexcept +{ + return measurement_processor_.get(); +} + void MeterContext::AddMetricExporter(std::unique_ptr exporter) noexcept { exporters_.push_back(std::move(exporter)); diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 9956c89ac5..db1737ce94 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -44,8 +44,8 @@ nostd::shared_ptr MeterProvider::GetMeter( for (auto &meter : meters_) { - auto &meter_lib = meter->GetInstrumentationLibrary(); - if (meter_lib.equal(name, version, schema_url)) + auto meter_lib = meter->GetInstrumentationLibrary(); + if (meter_lib->equal(name, version, schema_url)) { return nostd::shared_ptr{meter}; } @@ -60,6 +60,11 @@ const resource::Resource &MeterProvider::GetResource() const noexcept return context_->GetResource(); } +MeasurementProcessor *MeterProvider::GetMeasurementProcessor() const noexcept +{ + return context_->GetMeasurementProcessor(); +} + void MeterProvider::AddMetricExporter(std::unique_ptr exporter) noexcept { return context_->AddMetricExporter(std::move(exporter)); diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc new file mode 100644 index 0000000000..d85066ce2c --- /dev/null +++ b/sdk/src/metrics/sync_instruments.cc @@ -0,0 +1,135 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/sync_instruments.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +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) +{} + +void LongCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordLong(value, attributes); +} + +void LongCounter::Add(long value) noexcept +{ + return measurement_processor_->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) +{} + +void DoubleCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordDouble(value, attributes); +} + +void DoubleCounter::Add(double value) noexcept +{ + return measurement_processor_->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) +{} + +void LongUpDownCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordLong(value, attributes); +} + +void LongUpDownCounter::Add(long value) noexcept +{ + return measurement_processor_->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) +{} + +void DoubleUpDownCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordDouble(value, attributes); +} + +void DoubleUpDownCounter::Add(double value) noexcept +{ + return measurement_processor_->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) +{} + +void LongHistogram::Record(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordLong(value, attributes); +} + +void LongHistogram::Record(long value) noexcept +{ + return measurement_processor_->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) +{} + +void DoubleHistogram::Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_->RecordDouble(value, attributes); +} + +void DoubleHistogram::Record(double value) noexcept +{ + return measurement_processor_->RecordDouble(value); +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 3fc065220f..3f118a0c8d 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,5 +1,12 @@ -foreach(testname meter_provider_sdk_test view_registry_test aggregation_test - attributes_processor_test) +foreach( + testname + meter_provider_sdk_test + view_registry_test + aggregation_test + attributes_processor_test + sync_metric_storage_test + sync_instruments_test + async_instruments_test) add_executable(${testname} "${testname}.cc") target_link_libraries( ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} diff --git a/sdk/test/metrics/async_instruments_test.cc b/sdk/test/metrics/async_instruments_test.cc new file mode 100644 index 0000000000..abaeb83028 --- /dev/null +++ b/sdk/test/metrics/async_instruments_test.cc @@ -0,0 +1,72 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/async_instruments.h" +# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/measurement_processor.h" + +# include + +using namespace opentelemetry; +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; + +void asyc_generate_measurements_long(opentelemetry::metrics::ObserverResult &observer) {} + +void asyc_generate_measurements_double(opentelemetry::metrics::ObserverResult &observer) {} + +TEST(AsyncInstruments, LongObservableCounter) +{ + auto asyc_generate_meas_long = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(LongObservableCounter counter("long_counter", instrumentation_library.get(), + &measurement_processor, asyc_generate_meas_long, + "description", "1")); +} + +TEST(AsyncInstruments, DoubleObservableCounter) +{ + auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(DoubleObservableCounter counter("long_counter", instrumentation_library.get(), + &measurement_processor, asyc_generate_meas_double, + "description", "1")); +} + +TEST(AsyncInstruments, LongObservableGauge) +{ + auto asyc_generate_meas_long = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(LongObservableGauge counter("long_counter", instrumentation_library.get(), + &measurement_processor, asyc_generate_meas_long, + "description", "1")); +} + +TEST(AsyncInstruments, DoubleObservableGauge) +{ + auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(DoubleObservableGauge counter("long_counter", instrumentation_library.get(), + &measurement_processor, asyc_generate_meas_double, + "description", "1")); +} + +TEST(AsyncInstruments, LongObservableUpDownCounter) +{ + auto asyc_generate_meas_long = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(LongObservableUpDownCounter counter("long_counter", instrumentation_library.get(), + &measurement_processor, + asyc_generate_meas_long, "description", "1")); +} + +TEST(AsyncInstruments, DoubleObservableUpDownCounter) +{ + auto asyc_generate_meas_double = [](opentelemetry::metrics::ObserverResult &observer) {}; + EXPECT_NO_THROW(DoubleObservableUpDownCounter counter( + "long_counter", instrumentation_library.get(), &measurement_processor, + asyc_generate_meas_double, "description", "1")); +} + +#endif diff --git a/sdk/test/metrics/meter_provider_sdk_test.cc b/sdk/test/metrics/meter_provider_sdk_test.cc index 21c3d10716..b8d4db5670 100644 --- a/sdk/test/metrics/meter_provider_sdk_test.cc +++ b/sdk/test/metrics/meter_provider_sdk_test.cc @@ -33,7 +33,7 @@ class MockMetricExporter : public MetricExporter class MockMetricReader : public MetricReader { public: - bool Collect() noexcept override { return true; } + bool ProcessReceivedMetrics(MetricData &metric_data) noexcept override { return true; } bool Shutdown() noexcept override { return true; } }; diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc new file mode 100644 index 0000000000..f99e22377e --- /dev/null +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -0,0 +1,92 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#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 + +using namespace opentelemetry; +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"); + EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l)); + + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); +} + +TEST(SyncInstruments, DoubleCounter) +{ + DoubleCounter counter("double_counter", instrumentation_library.get(), &measurement_processor, + "description", "1"); + EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10)); + + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); +} + +TEST(SyncInstruments, LongUpDownCounter) +{ + LongUpDownCounter counter("long_up_down_counter", instrumentation_library.get(), + &measurement_processor, "description", "1"); + EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l)); + + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); +} + +TEST(SyncInstruments, DoubleUpDownCounter) +{ + DoubleUpDownCounter counter("double_up_down_counter", instrumentation_library.get(), + &measurement_processor, "description", "1"); + EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10)); + + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); +} + +TEST(SyncInstruments, LongHistogram) +{ + LongHistogram counter("long_histogram", instrumentation_library.get(), &measurement_processor, + "description", "1"); + EXPECT_NO_THROW(counter.Record(10l)); + EXPECT_NO_THROW(counter.Record(10l)); + + EXPECT_NO_THROW(counter.Record( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::common::KeyValueIterableView({}))); +} + +TEST(SyncInstruments, DoubleHistogram) +{ + DoubleHistogram counter("double_histogram", instrumentation_library.get(), &measurement_processor, + "description", "1"); + EXPECT_NO_THROW(counter.Record(10.10)); + EXPECT_NO_THROW(counter.Record(10.10)); + + EXPECT_NO_THROW(counter.Record( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::common::KeyValueIterableView({}))); +} + +#endif \ No newline at end of file diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc new file mode 100644 index 0000000000..39239503b1 --- /dev/null +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" +# include "opentelemetry/common/key_value_iterable_view.h" + +# include +# include + +using M = std::map; +TEST(WritableMetricStorageTest, BasicTests) +{ + opentelemetry::sdk::metrics::SyncMetricStorage storage; + EXPECT_NO_THROW(storage.RecordLong(10l)); + EXPECT_NO_THROW(storage.RecordDouble(10.10)); + + EXPECT_NO_THROW(storage.RecordLong( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}))); +} +#endif