From 6a681081cd5b91d6a351aba099b79b45e0b9fd84 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 21 Jan 2022 22:49:32 -0800 Subject: [PATCH 01/11] Sync instruments sdk --- .../opentelemetry/metrics/sync_instruments.h | 1 + .../sdk/metrics/async_instruments.h | 122 ++++++++++++++++++ .../sdk/metrics/sync_instruments.h | 108 ++++++++++++++++ sdk/src/metrics/CMakeLists.txt | 3 +- sdk/src/metrics/sync_instruments.cc | 88 +++++++++++++ 5 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/async_instruments.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/sync_instruments.h create mode 100644 sdk/src/metrics/sync_instruments.cc 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..e6bb083014 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -0,0 +1,122 @@ +// 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/sdk/instruments.h" +# include "opentelemetry/nostd/string_view.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +typedef void (*AsyncCallback)(ObserverResult &); +class Asynchronous +{ +public: + Asynchrounous(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : name_(name), + instrument_descriptor_(descriptor), + callback_(callback), + description_(description), + unit_(unit) + {} + + nostd::string_view GetName() { return name_; } + + nostd::string_view GetDescription() { return description_; } + + nostd::string_unit GetUnit() { return unit_; } + + AsyncCallback GetCallback() { return callback_; } + +private: + std::string name_; + std::string description_; + std::string unit_; + InstrumentDescriptor instrument_descriptor_; + AsyncCallback callback_; +}; + +class LongObservableCounter : public opentelemetry::metrics::ObservableCounter +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, callback, description, unit); + + {} +}; + +class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, callback, description, unit); + + {} +}; + +class LongObservableGauge : public opentelemetry::metrics::ObservableGauge +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, callback, description, unit); + + {} +}; + +class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, callback, description, unit); + + {} +}; + +class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, callback, description, unit); + + {} +}; + +class DoubleObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter +{ + LongObservableCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + AsyncCallback callback, + nostd::string_view description = "", + nostd::string_view unit = "") + : Asynchronous(name, descriptor, 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/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h new file mode 100644 index 0000000000..793694b862 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.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.h" +# include "opentelemetry/metrics/sync_instruments.h" +# include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class Synchronous +{ +public: + Synchronous(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = "") + : name_(name), description_(description), unit_(unit) + {} + + nostd::string_view GetName() { return name_; } + + nostd::string_view GetDescription() { return description_; } + + nostd::string_view GetUnit() { return unit_; } + +private: + std::string name_; + std::string description_; + std::string unit_; + InstrumentDescriptor instrument_descriptor_; +}; + +class LongCounter : public Synchronous, opentelemetry::metrics::Counter +{ +public: + LongCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Add(long value, const common::KeyValueIterable &attributes) noexcept override; +}; + +class DoubleCounter : public Synchronous, opentelemetry::metrics::Counter +{ + +public: + DoubleCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Add(double value, const common::KeyValueIterable &attributes) noexcept override; +}; + +class LongUpDownCounter : public Synchronous, opentelemetry::metrics::UpDownCounter +{ +public: + LongUpDownCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Add(long value, const common::KeyValueIterable &attributes) noexcept override; +}; + +class DoubleUpDownCounter : public Synchronous, opentelemetry::metrics::Counter +{ +public: + DoubleUpDownCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Add(double value, const common::KeyValueIterable &attributes) noexcept override; +}; + +class LongHistogram : public Synchronous, opentelemetry::metrics::Histogram +{ +public: + LongHistogram(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Record(long value, const common::KeyValueIterable &attributes) noexcept override; +}; + +class DoubleHistogram : public Synchronous, opentelemetry::metrics::Histogram +{ +public: + DoubleHistogram(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description = "", + nostd::string_view unit = ""); + + void Record(double value, const common::KeyValueIterable &attributes) noexcept override; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index d1d7ae97bc..c35c5aae57 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -1,4 +1,5 @@ -add_library(opentelemetry_metrics meter_provider.cc meter.cc meter_context.cc) +add_library(opentelemetry_metrics meter_provider.cc meter.cc meter_context.cc + sync_instruments.cc) set_target_properties(opentelemetry_metrics PROPERTIES EXPORT_NAME metrics) diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc new file mode 100644 index 0000000000..047af7da23 --- /dev/null +++ b/sdk/src/metrics/sync_instruments.cc @@ -0,0 +1,88 @@ +// 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, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void LongCounter::Add(long value, const common::KeyValueIterable &attributes) noexcept +{ + // TBD - Push to consumer / writable storage for aggregation +} + +DoubleCounter::DoubleCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void DoubleCounter::Add(double value, const common::KeyValueIterable &attributes) noexcept +{ + // TBD - Push to consumer / writable storage for aggregation +} + +LongUpDownCounter::LongUpDownCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void LongUpDownCounter::Add(long value, const common::KeyValueIterable &attributes) noexcept +{ + // TBD - Push to consumer / writable storage for aggregation +} + +DoubleUpDownCounter::DoubleUpDownCounter(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void DoubleUpDownCounter::Add(double value, const common::KeyValueIterable &attributes) noexcept +{ + // TBD - Push to consumer / writable storage for aggregation +} + +LongHistogram::LongHistogram(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void LongHistogram::Record(long value, const common::KeyValueIterable &attributes) noexcept +{ + + // TBD - Push to consumer / writable storage for aggregation +} + +DoubleHistogram::DoubleHistogram(nostd::string_view name, + InstrumentDescriptor &descriptor, + nostd::string_view description, + nostd::string_view unit) + : Synchronous(name, descriptor, description, unit) +{} + +void DoubleHistogram::Record(double value, const common::KeyValueIterable &attributes) noexcept +{ + + // TBD - Push to consumer / writable storage for aggregation +} +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif From 6a9b07c28b1c875a95fc9831563c8554316b43ad Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 19:24:21 -0800 Subject: [PATCH 02/11] Sync Intruments --- .../opentelemetry/metrics/async_instruments.h | 2 +- .../sdk/metrics/async_instruments.h | 177 ++++++++++++------ .../sdk/metrics/measurement_processor.h | 107 +++++++++++ sdk/include/opentelemetry/sdk/metrics/meter.h | 6 +- .../opentelemetry/sdk/metrics/meter_context.h | 7 + .../sdk/metrics/meter_provider.h | 7 + .../sdk/metrics/state/metric_storage.h | 83 ++++++++ .../sdk/metrics/state/sync_metric_storage.h | 68 +++++++ .../sdk/metrics/sync_instruments.h | 89 ++++++--- sdk/src/metrics/meter.cc | 73 ++++---- sdk/src/metrics/meter_context.cc | 5 + sdk/src/metrics/meter_provider.cc | 5 + sdk/src/metrics/sync_instruments.cc | 131 ++++++++----- sdk/test/metrics/async_instruments_test.cc | 0 sdk/test/metrics/sync_instruments_test.cc | 14 ++ 15 files changed, 610 insertions(+), 164 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/metrics/measurement_processor.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h create mode 100644 sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h create mode 100644 sdk/test/metrics/async_instruments_test.cc create mode 100644 sdk/test/metrics/sync_instruments_test.cc 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/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index e6bb083014..574a326544 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -4,119 +4,190 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/metrics/async_instruments.h" -# include "opentelemetry/metrics/sdk/instruments.h" +# include "opentelemetry/metrics/observer_result.h" # include "opentelemetry/nostd/string_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { -typedef void (*AsyncCallback)(ObserverResult &); +// using AsyncCallback = void (*)(opentelemetry::metrics::ObserverResult &); +// using AsyncCallbackDouble = void (*)(opentelemetry::metrics::ObserverResult &); + +template class Asynchronous { public: - Asynchrounous(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, - nostd::string_view description = "", - nostd::string_view unit = "") + 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), - instrument_descriptor_(descriptor), + instrumentation_library_{instrumentation_library}, + measurement_processor_{measurement_processor}, callback_(callback), description_(description), unit_(unit) {} - nostd::string_view GetName() { return name_; } + /*nostd::string_view GetName() { return name_; } nostd::string_view GetDescription() { return description_; } - nostd::string_unit GetUnit() { return unit_; } + nostd::string_view GetUnit() { return unit_; } + + const sdk::instrumentationlibrary::InstrumentationLibrary& GetInstrumentationLibrary() + { + return instrumentation_library_; + } + + MeasurementProcessor& GetMeasurementProcessor() { return measurement_processor_; } - AsyncCallback GetCallback() { return callback_; } + AsyncCallback GetCallback() { return callback_; }*/ -private: +protected: std::string name_; + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library_; + MeasurementProcessor &measurement_processor_; + void (*callback_)(opentelemetry::metrics::ObserverResult &); std::string description_; std::string unit_; - InstrumentDescriptor instrument_descriptor_; - AsyncCallback callback_; }; -class LongObservableCounter : public opentelemetry::metrics::ObservableCounter +class LongObservableCounter : public opentelemetry::metrics::ObservableCounter, + public Asynchronous { +public: LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, + 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, descriptor, callback, description, unit); + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) {} }; -class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter +class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter, + public Asynchronous { - LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, descriptor, callback, description, unit); +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 +class LongObservableGauge : public opentelemetry::metrics::ObservableGauge, + public Asynchronous { - LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, descriptor, callback, description, unit); +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 +class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge, + public Asynchronous { - LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, +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, descriptor, callback, description, unit); + : Asynchronous(name, + instrumentation_library, + measurement_processor, + callback, + description, + unit) {} }; -class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter +class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter, + public Asynchronous { - LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, descriptor, callback, description, unit); +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 +class DoubleObservableUpDownCounter + : public opentelemetry::metrics::ObservableUpDownCounter, + public Asynchronous { - LongObservableCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - AsyncCallback callback, - nostd::string_view description = "", - nostd::string_view unit = "") - : Asynchronous(name, descriptor, callback, description, unit); - +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..91baccf2e2 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h @@ -0,0 +1,107 @@ +// 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/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 SyncMeasurementProcessor : 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..0e5d6b9de0 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" @@ -97,10 +98,13 @@ class Meter final : public opentelemetry::metrics::Meter const sdk::instrumentationlibrary::InstrumentationLibrary &GetInstrumentationLibrary() const noexcept; + /** Returns the associated instruementation library */ + 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..2dd5af8773 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..27cf66d4ed 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/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h new file mode 100644 index 0000000000..b0db7084ab --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -0,0 +1,83 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/key_value_iterable_view.h" +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class MetricData +{ + // TBD - This clas to be removed once #1178 is merged +}; + +enum class AggregationTemporarily +{ + // TBD - This enum to be removed once #1178 is merged. + kUnspecified, + kDelta, + kCummulative +}; + +/* 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..b67c4f2cef --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -0,0 +1,68 @@ +// 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 + { + if (callback(MetricData())) + { + return true; + } + return false; + } +}; + +} // 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 index 793694b862..ce66c4cc3e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -7,6 +7,10 @@ # 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 { @@ -16,90 +20,119 @@ class Synchronous { public: Synchronous(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = "") - : name_(name), description_(description), unit_(unit) + : name_(name), + instrumentation_library_(instrumentation_library), + measurement_processor_(measurement_processor), + description_(description), + unit_(unit) {} - nostd::string_view GetName() { return name_; } - - nostd::string_view GetDescription() { return description_; } - - nostd::string_view GetUnit() { return unit_; } - -private: +protected: std::string name_; + const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library_; + MeasurementProcessor &measurement_processor_; std::string description_; std::string unit_; - InstrumentDescriptor instrument_descriptor_; }; -class LongCounter : public Synchronous, opentelemetry::metrics::Counter +class LongCounter : public Synchronous, public opentelemetry::metrics::Counter { public: LongCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Add(long value, const common::KeyValueIterable &attributes) noexcept override; + void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(long value) noexcept override; }; -class DoubleCounter : public Synchronous, opentelemetry::metrics::Counter +class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter { public: DoubleCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Add(double value, const common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(double value) noexcept override; }; -class LongUpDownCounter : public Synchronous, opentelemetry::metrics::UpDownCounter +class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter { public: LongUpDownCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Add(long value, const common::KeyValueIterable &attributes) noexcept override; + void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(long value) noexcept override; }; -class DoubleUpDownCounter : public Synchronous, opentelemetry::metrics::Counter +class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter { public: DoubleUpDownCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Add(double value, const common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Add(double value) noexcept override; }; -class LongHistogram : public Synchronous, opentelemetry::metrics::Histogram +class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram { public: LongHistogram(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Record(long value, const common::KeyValueIterable &attributes) noexcept override; + void Record(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Record(long value) noexcept override; }; -class DoubleHistogram : public Synchronous, opentelemetry::metrics::Histogram +class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram { public: DoubleHistogram(nostd::string_view name, - InstrumentDescriptor &descriptor, + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library, + MeasurementProcessor &measurement_processor, nostd::string_view description = "", nostd::string_view unit = ""); - void Record(double value, const common::KeyValueIterable &attributes) noexcept override; + void Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + + void Record(double value) noexcept override; }; } // namespace metrics diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 6c759158a4..6f689242a6 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 @@ -23,15 +25,15 @@ Meter::Meter(std::shared_ptr context, std::unique_ptr instrumentation_library) noexcept : context_{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 { - 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 +41,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 +51,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 +61,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 +70,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 +79,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 +89,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 +99,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 +108,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 +117,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 +127,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 +137,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 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..cae61405a8 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..7b99025f98 100644 --- a/sdk/src/metrics/meter_provider.cc +++ b/sdk/src/metrics/meter_provider.cc @@ -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 index 047af7da23..4eba84b7ce 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -9,79 +9,126 @@ namespace sdk { namespace metrics { -LongCounter::LongCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void LongCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept { - // TBD - Push to consumer / writable storage for aggregation + return measurement_processor_.RecordLong(value, attributes); } -DoubleCounter::DoubleCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void DoubleCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept { - // TBD - Push to consumer / writable storage for aggregation + return measurement_processor_.RecordDouble(value, attributes); } -LongUpDownCounter::LongUpDownCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void LongUpDownCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + return measurement_processor_.RecordLong(value, attributes); +} + +void LongUpDownCounter::Add(long value) noexcept { - // TBD - Push to consumer / writable storage for aggregation + return measurement_processor_.RecordLong(value); } -DoubleUpDownCounter::DoubleUpDownCounter(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void DoubleUpDownCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept { - // TBD - Push to consumer / writable storage for aggregation + return measurement_processor_.RecordDouble(value, attributes); } -LongHistogram::LongHistogram(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void LongHistogram::Record(long value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept { + return measurement_processor_.RecordLong(value, attributes); +} - // TBD - Push to consumer / writable storage for aggregation +void LongHistogram::Record(long value) noexcept +{ + return measurement_processor_.RecordLong(value); } -DoubleHistogram::DoubleHistogram(nostd::string_view name, - InstrumentDescriptor &descriptor, - nostd::string_view description, - nostd::string_view unit) - : Synchronous(name, descriptor, description, unit) +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 common::KeyValueIterable &attributes) noexcept +void DoubleHistogram::Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept { + return measurement_processor_.RecordDouble(value, attributes); +} - // TBD - Push to consumer / writable storage for aggregation +void DoubleHistogram::Record(double value) noexcept +{ + return measurement_processor_.RecordDouble(value); } + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/metrics/async_instruments_test.cc b/sdk/test/metrics/async_instruments_test.cc new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc new file mode 100644 index 0000000000..2b70dff170 --- /dev/null +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/sync_instruments.h" +# include +# include "opentelemetry/sdk/metrics/instruments.h" + +TEST(SyncInstruments, LongCounter) +{ + InstrumentDescriptor instr_desc = {}; +} + +#endif \ No newline at end of file From d4aa275f554177acbe7883a33e508c1c05289b3f Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 19:37:37 -0800 Subject: [PATCH 03/11] fix for gcc4.8 --- sdk/include/opentelemetry/sdk/metrics/async_instruments.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index 574a326544..6ecd60fe9c 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -53,7 +53,7 @@ class Asynchronous std::string name_; const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library_; - MeasurementProcessor &measurement_processor_; + const MeasurementProcessor &measurement_processor_; void (*callback_)(opentelemetry::metrics::ObserverResult &); std::string description_; std::string unit_; From 739c563f3fef54ffac64dd226561e13e420575ed Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 19:42:33 -0800 Subject: [PATCH 04/11] more changes --- sdk/include/opentelemetry/sdk/metrics/async_instruments.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index 6ecd60fe9c..8b1781df85 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -22,7 +22,7 @@ class Asynchronous Asynchronous(nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + const MeasurementProcessor &measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") From 29a817c7fc850d3231f50cedac961ef7c359c13a Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 20:01:47 -0800 Subject: [PATCH 05/11] change reference to pointer --- .../sdk/metrics/async_instruments.h | 47 +++++++----------- sdk/include/opentelemetry/sdk/metrics/meter.h | 4 +- .../opentelemetry/sdk/metrics/meter_context.h | 2 +- .../sdk/metrics/meter_provider.h | 2 +- .../sdk/metrics/sync_instruments.h | 32 ++++++------- sdk/src/metrics/meter.cc | 6 +-- sdk/src/metrics/meter_context.cc | 4 +- sdk/src/metrics/meter_provider.cc | 6 +-- sdk/src/metrics/sync_instruments.cc | 48 +++++++++---------- 9 files changed, 68 insertions(+), 83 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index 8b1781df85..f7cffecefe 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -21,8 +21,8 @@ class Asynchronous public: Asynchronous(nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - const MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") @@ -34,26 +34,11 @@ class Asynchronous unit_(unit) {} - /*nostd::string_view GetName() { return name_; } - - nostd::string_view GetDescription() { return description_; } - - nostd::string_view GetUnit() { return unit_; } - - const sdk::instrumentationlibrary::InstrumentationLibrary& GetInstrumentationLibrary() - { - return instrumentation_library_; - } - - MeasurementProcessor& GetMeasurementProcessor() { return measurement_processor_; } - - AsyncCallback GetCallback() { return callback_; }*/ - protected: std::string name_; const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library_; - const MeasurementProcessor &measurement_processor_; + *instrumentation_library_; + const MeasurementProcessor *measurement_processor_; void (*callback_)(opentelemetry::metrics::ObserverResult &); std::string description_; std::string unit_; @@ -65,8 +50,8 @@ class LongObservableCounter : public opentelemetry::metrics::ObservableCounter &), nostd::string_view description = "", nostd::string_view unit = "") @@ -86,8 +71,8 @@ class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter public: DoubleObservableCounter(nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") @@ -107,8 +92,8 @@ class LongObservableGauge : public opentelemetry::metrics::ObservableGauge public: LongObservableGauge(nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") @@ -128,8 +113,8 @@ class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge &), nostd::string_view description = "", nostd::string_view unit = "") @@ -150,8 +135,8 @@ class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpD LongObservableUpDownCounter( nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") @@ -173,8 +158,8 @@ class DoubleObservableUpDownCounter DoubleObservableUpDownCounter( nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, void (*callback)(opentelemetry::metrics::ObserverResult &), nostd::string_view description = "", nostd::string_view unit = "") diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 0e5d6b9de0..c5fd563b6f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -95,11 +95,11 @@ 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 instruementation library */ - MeasurementProcessor &GetMeasurementProcessor() const noexcept; + MeasurementProcessor *GetMeasurementProcessor() const noexcept; private: // order of declaration is important here - instrumentation library should destroy after diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_context.h b/sdk/include/opentelemetry/sdk/metrics/meter_context.h index 2dd5af8773..2df7d4868f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_context.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_context.h @@ -50,7 +50,7 @@ class MeterContext * Obtain the reference of measurement_processor. * */ - MeasurementProcessor &GetMeasurementProcessor() const noexcept; + MeasurementProcessor *GetMeasurementProcessor() const noexcept; /** * Attaches a metric exporter to list of configured exporters for this Meter context. diff --git a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h index 27cf66d4ed..7d7966147f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter_provider.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter_provider.h @@ -56,7 +56,7 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider * Obtain the reference of measurement processor. * */ - MeasurementProcessor &GetMeasurementProcessor() const noexcept; + MeasurementProcessor *GetMeasurementProcessor() const noexcept; /** * Attaches a metric exporter to list of configured exporters for this Meter provider. diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index ce66c4cc3e..c77d5d9a65 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -21,8 +21,8 @@ class Synchronous public: Synchronous(nostd::string_view name, const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary - &instrumentation_library, - MeasurementProcessor &measurement_processor, + *instrumentation_library, + MeasurementProcessor *measurement_processor, nostd::string_view description = "", nostd::string_view unit = "") : name_(name), @@ -34,8 +34,8 @@ class Synchronous protected: std::string name_; - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library_; - MeasurementProcessor &measurement_processor_; + const sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library_; + MeasurementProcessor *measurement_processor_; std::string description_; std::string unit_; }; @@ -45,8 +45,8 @@ class LongCounter : public Synchronous, public opentelemetry::metrics::CounterGetMeasurementProcessor(); } diff --git a/sdk/src/metrics/meter_context.cc b/sdk/src/metrics/meter_context.cc index cae61405a8..d2487ebecc 100644 --- a/sdk/src/metrics/meter_context.cc +++ b/sdk/src/metrics/meter_context.cc @@ -28,9 +28,9 @@ const resource::Resource &MeterContext::GetResource() const noexcept return resource_; } -MeasurementProcessor &MeterContext::GetMeasurementProcessor() const noexcept +MeasurementProcessor *MeterContext::GetMeasurementProcessor() const noexcept { - return *measurement_processor_.get(); + return measurement_processor_.get(); } void MeterContext::AddMetricExporter(std::unique_ptr exporter) noexcept diff --git a/sdk/src/metrics/meter_provider.cc b/sdk/src/metrics/meter_provider.cc index 7b99025f98..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,7 +60,7 @@ const resource::Resource &MeterProvider::GetResource() const noexcept return context_->GetResource(); } -MeasurementProcessor &MeterProvider::GetMeasurementProcessor() const noexcept +MeasurementProcessor *MeterProvider::GetMeasurementProcessor() const noexcept { return context_->GetMeasurementProcessor(); } diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 4eba84b7ce..d85066ce2c 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -11,8 +11,8 @@ namespace metrics { LongCounter::LongCounter( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -21,18 +21,18 @@ LongCounter::LongCounter( void LongCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordLong(value, attributes); + return measurement_processor_->RecordLong(value, attributes); } void LongCounter::Add(long value) noexcept { - return measurement_processor_.RecordLong(value); + return measurement_processor_->RecordLong(value); } DoubleCounter::DoubleCounter( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -41,18 +41,18 @@ DoubleCounter::DoubleCounter( void DoubleCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordDouble(value, attributes); + return measurement_processor_->RecordDouble(value, attributes); } void DoubleCounter::Add(double value) noexcept { - return measurement_processor_.RecordDouble(value); + return measurement_processor_->RecordDouble(value); } LongUpDownCounter::LongUpDownCounter( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -61,18 +61,18 @@ LongUpDownCounter::LongUpDownCounter( void LongUpDownCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordLong(value, attributes); + return measurement_processor_->RecordLong(value, attributes); } void LongUpDownCounter::Add(long value) noexcept { - return measurement_processor_.RecordLong(value); + return measurement_processor_->RecordLong(value); } DoubleUpDownCounter::DoubleUpDownCounter( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -81,18 +81,18 @@ DoubleUpDownCounter::DoubleUpDownCounter( void DoubleUpDownCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordDouble(value, attributes); + return measurement_processor_->RecordDouble(value, attributes); } void DoubleUpDownCounter::Add(double value) noexcept { - return measurement_processor_.RecordDouble(value); + return measurement_processor_->RecordDouble(value); } LongHistogram::LongHistogram( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -101,18 +101,18 @@ LongHistogram::LongHistogram( void LongHistogram::Record(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordLong(value, attributes); + return measurement_processor_->RecordLong(value, attributes); } void LongHistogram::Record(long value) noexcept { - return measurement_processor_.RecordLong(value); + return measurement_processor_->RecordLong(value); } DoubleHistogram::DoubleHistogram( nostd::string_view name, - const sdk::instrumentationlibrary::InstrumentationLibrary &instrumentation_library, - MeasurementProcessor &measurement_processor, + 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) @@ -121,12 +121,12 @@ DoubleHistogram::DoubleHistogram( void DoubleHistogram::Record(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return measurement_processor_.RecordDouble(value, attributes); + return measurement_processor_->RecordDouble(value, attributes); } void DoubleHistogram::Record(double value) noexcept { - return measurement_processor_.RecordDouble(value); + return measurement_processor_->RecordDouble(value); } } // namespace metrics From aab9f34e3aa4f758a134997a4d8e56bcf9097eb2 Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 21:45:55 -0800 Subject: [PATCH 06/11] add tests --- .../sdk/metrics/state/metric_storage.h | 2 +- sdk/test/metrics/CMakeLists.txt | 3 +- sdk/test/metrics/sync_instruments_test.cc | 84 ++++++++++++++++++- sdk/test/metrics/sync_metric_storage_test.cc | 22 +++++ 4 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 sdk/test/metrics/sync_metric_storage_test.cc diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index b0db7084ab..0349a456bb 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -11,7 +11,7 @@ namespace metrics { class MetricData { - // TBD - This clas to be removed once #1178 is merged + // TBD - This class to be removed once #1178 is merged }; enum class AggregationTemporarily diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index f65f3b8bc8..c2d33e0deb 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,4 +1,5 @@ -foreach(testname meter_provider_sdk_test view_registry_test) +foreach(testname meter_provider_sdk_test view_registry_test + sync_metric_storage_test sync_instruments_test) add_executable(${testname} "${testname}.cc") target_link_libraries( ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 2b70dff170..159311252c 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -3,12 +3,92 @@ #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 -# include "opentelemetry/sdk/metrics/instruments.h" + +using namespace opentelemetry; +using namespace opentelemetry::sdk::instrumentationlibrary; +using namespace opentelemetry::sdk::metrics; + +std::string library_name = "opentelemetry-cpp"; +std::string library_version = "0.1.0"; +auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); +SyncMeasurementProcessor measurement_processor; + +using M = std::map; TEST(SyncInstruments, LongCounter) { - InstrumentDescriptor instr_desc = {}; + 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 From ac394a9af2e9f221ae9b2e66f1c4ad116b4c3b69 Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 27 Jan 2022 21:49:37 -0800 Subject: [PATCH 07/11] remove commented code --- sdk/include/opentelemetry/sdk/metrics/async_instruments.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index f7cffecefe..e2f3a1728e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -12,8 +12,6 @@ namespace sdk { namespace metrics { -// using AsyncCallback = void (*)(opentelemetry::metrics::ObserverResult &); -// using AsyncCallbackDouble = void (*)(opentelemetry::metrics::ObserverResult &); template class Asynchronous From d27cd2f24de8b6ad20b37352daec9e29488ac4de Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 31 Jan 2022 23:45:08 -0800 Subject: [PATCH 08/11] more changes for metric reader and measurement processor interface --- .../opentelemetry/sdk/metrics/instruments.h | 13 ++++++ .../sdk/metrics/measurement_processor.h | 3 +- .../sdk/metrics/metric_exporter.h | 4 ++ .../opentelemetry/sdk/metrics/metric_reader.h | 44 ++++++++++++++++++- .../sdk/metrics/state/metric_storage.h | 13 +----- sdk/test/metrics/async_instruments_test.cc | 22 ++++++++++ sdk/test/metrics/meter_provider_sdk_test.cc | 2 +- sdk/test/metrics/sync_instruments_test.cc | 2 +- 8 files changed, 86 insertions(+), 17 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index 8d101090c7..c5599a44ad 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -36,6 +36,19 @@ struct InstrumentDescriptor InstrumentValueType valueType_; }; +enum class AggregationTemporarily +{ + // TBD - This enum to be removed once #1178 is merged. + kUnspecified, + kDelta, + kCummulative +}; + +class MetricData +{ + // TBD - This class to be removed once #1178 is merged +}; + /*class InstrumentSelector { public: InstrumentSelector(opentelemetry::nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h index 91baccf2e2..c7e7a6ed6a 100644 --- a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h @@ -5,6 +5,7 @@ #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" @@ -38,7 +39,7 @@ class MeasurementProcessor nostd::function_ref callback) noexcept = 0; }; -class SyncMeasurementProcessor : public MeasurementProcessor +class DefaultMeasurementProcessor : public MeasurementProcessor { public: 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..c752e659c9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -4,6 +4,8 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include +# include "opentelemetry/sdk/common/global_log_handler.h" +# include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -11,19 +13,52 @@ namespace sdk { namespace metrics { + /** * MetricReader defines the interface to collect metrics from SDK */ class MetricReader { public: - virtual ~MetricReader() = default; + MetricReader( + AggregationTemporarily aggregation_temporarily = AggregationTemporarily::kCummulative) + : aggregation_temporarily_(aggregation_temporarily), measurement_processor_callback_({}) + {} + + virtual ~MetricReader() { measurement_processor_callback_ = {}; } /** * 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 +66,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 index 0349a456bb..3fcf68b662 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -4,23 +4,12 @@ #pragma once #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics { -class MetricData -{ - // TBD - This class to be removed once #1178 is merged -}; - -enum class AggregationTemporarily -{ - // TBD - This enum to be removed once #1178 is merged. - kUnspecified, - kDelta, - kCummulative -}; /* Represent the storage from which to collect the metrics */ diff --git a/sdk/test/metrics/async_instruments_test.cc b/sdk/test/metrics/async_instruments_test.cc index e69de29bb2..ea907e0add 100644 --- a/sdk/test/metrics/async_instruments_test.cc +++ b/sdk/test/metrics/async_instruments_test.cc @@ -0,0 +1,22 @@ +// 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; + +std::string library_name = "opentelemetry-cpp"; +std::string library_version = "0.1.0"; +auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); +SyncMeasurementProcessor measurement_processor; + +using M = std::map; + +TEST(AsyncInstruments, LongCounter) {} 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 index 159311252c..11124dcc1e 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -15,7 +15,7 @@ using namespace opentelemetry::sdk::metrics; std::string library_name = "opentelemetry-cpp"; std::string library_version = "0.1.0"; auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); -SyncMeasurementProcessor measurement_processor; +DefaultMeasurementProcessor measurement_processor; using M = std::map; From 0f05485b140142b8e4ba660e3d8970d378cb84fc Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 7 Feb 2022 19:08:20 -0800 Subject: [PATCH 09/11] Review comments --- .../opentelemetry/sdk/metrics/instruments.h | 13 ------------- sdk/include/opentelemetry/sdk/metrics/meter.h | 2 +- .../opentelemetry/sdk/metrics/metric_reader.h | 3 ++- .../sdk/metrics/state/metric_storage.h | 1 + .../sdk/metrics/state/sync_metric_storage.h | 14 +++++++++++--- sdk/src/metrics/CMakeLists.txt | 10 +++++++--- sdk/src/metrics/meter.cc | 1 - sdk/test/metrics/CMakeLists.txt | 6 ++++-- sdk/test/metrics/async_instruments_test.cc | 2 -- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index 6dfddd3578..88d432a136 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -36,19 +36,6 @@ struct InstrumentDescriptor InstrumentValueType value_type_; }; -enum class AggregationTemporarily -{ - // TBD - This enum to be removed once #1178 is merged. - kUnspecified, - kDelta, - kCummulative -}; - -class MetricData -{ - // TBD - This class to be removed once #1178 is merged -}; - /*class InstrumentSelector { public: InstrumentSelector(opentelemetry::nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index c5fd563b6f..51e8e200b9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -98,7 +98,7 @@ class Meter final : public opentelemetry::metrics::Meter const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() const noexcept; - /** Returns the associated instruementation library */ + /** Returns the associated measurement processor */ MeasurementProcessor *GetMeasurementProcessor() const noexcept; private: diff --git a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h index c752e659c9..16a1880113 100644 --- a/sdk/include/opentelemetry/sdk/metrics/metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/metric_reader.h @@ -5,6 +5,7 @@ #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" @@ -25,7 +26,7 @@ class MetricReader : aggregation_temporarily_(aggregation_temporarily), measurement_processor_callback_({}) {} - virtual ~MetricReader() { measurement_processor_callback_ = {}; } + virtual ~MetricReader() = default; /** * Collect the metrics from SDK. diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index 3fcf68b662..e25e1f9ed3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -4,6 +4,7 @@ #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 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 b67c4f2cef..e0fde38c15 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -54,11 +54,19 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage bool Collect(AggregationTemporarily aggregation_temporarily, nostd::function_ref callback) noexcept override { - if (callback(MetricData())) + /** + * 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 true; + return false; } - return false; + // } //while + return true; } }; diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 32e18a3b4a..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 sync_instruments.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 28579a8cbe..2ab7f068f7 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -25,7 +25,6 @@ Meter::Meter(std::shared_ptr context, std::unique_ptr instrumentation_library) noexcept : context_{context}, instrumentation_library_{std::move(instrumentation_library)} - {} nostd::shared_ptr> Meter::CreateLongCounter(nostd::string_view name, diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 2fa8672c35..6e4a40759f 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,5 +1,7 @@ -foreach(testname meter_provider_sdk_test view_registry_test aggregation_test - attributes_processor_test sync_metric_storage_test sync_instruments_test) +foreach( + testname + meter_provider_sdk_test view_registry_test aggregation_test + attributes_processor_test sync_metric_storage_test sync_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 index ea907e0add..981905e4ab 100644 --- a/sdk/test/metrics/async_instruments_test.cc +++ b/sdk/test/metrics/async_instruments_test.cc @@ -4,7 +4,6 @@ #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 @@ -15,7 +14,6 @@ using namespace opentelemetry::sdk::metrics; std::string library_name = "opentelemetry-cpp"; std::string library_version = "0.1.0"; auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); -SyncMeasurementProcessor measurement_processor; using M = std::map; From da76ead5f5abe688aacf93ba6cdf3957339d79eb Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 7 Feb 2022 19:22:47 -0800 Subject: [PATCH 10/11] review comment --- sdk/test/metrics/async_instruments_test.cc | 4 +++- sdk/test/metrics/sync_instruments_test.cc | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/test/metrics/async_instruments_test.cc b/sdk/test/metrics/async_instruments_test.cc index 981905e4ab..a0ffbba46f 100644 --- a/sdk/test/metrics/async_instruments_test.cc +++ b/sdk/test/metrics/async_instruments_test.cc @@ -4,6 +4,7 @@ #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 @@ -13,7 +14,8 @@ using namespace opentelemetry::sdk::metrics; std::string library_name = "opentelemetry-cpp"; std::string library_version = "0.1.0"; -auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); +auto instrumentation_library = InstrumentationLibrary::Create("opentelemetry-cpp", "0.1.0"); +DefaultMeasurementProcessor measurement_processor; using M = std::map; diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 11124dcc1e..f99e22377e 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -12,9 +12,7 @@ using namespace opentelemetry; using namespace opentelemetry::sdk::instrumentationlibrary; using namespace opentelemetry::sdk::metrics; -std::string library_name = "opentelemetry-cpp"; -std::string library_version = "0.1.0"; -auto instrumentation_library = InstrumentationLibrary::Create(library_name, library_version); +auto instrumentation_library = InstrumentationLibrary::Create("opentelemetry-cpp", "0.1.0"); DefaultMeasurementProcessor measurement_processor; using M = std::map; From 9f04cbfe0abf662ed2e14671587bcbe0d487251f Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 7 Feb 2022 22:50:22 -0800 Subject: [PATCH 11/11] add tests for async instrument --- .../sdk/metrics/async_instruments.h | 3 + sdk/test/metrics/CMakeLists.txt | 9 ++- sdk/test/metrics/async_instruments_test.cc | 56 ++++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h index e2f3a1728e..d66bb69890 100644 --- a/sdk/include/opentelemetry/sdk/metrics/async_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/async_instruments.h @@ -5,6 +5,9 @@ #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 diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 6e4a40759f..3f118a0c8d 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -1,7 +1,12 @@ foreach( testname - meter_provider_sdk_test view_registry_test aggregation_test - attributes_processor_test sync_metric_storage_test sync_instruments_test) + 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 index a0ffbba46f..abaeb83028 100644 --- a/sdk/test/metrics/async_instruments_test.cc +++ b/sdk/test/metrics/async_instruments_test.cc @@ -12,11 +12,61 @@ using namespace opentelemetry; using namespace opentelemetry::sdk::instrumentationlibrary; using namespace opentelemetry::sdk::metrics; -std::string library_name = "opentelemetry-cpp"; -std::string library_version = "0.1.0"; auto instrumentation_library = InstrumentationLibrary::Create("opentelemetry-cpp", "0.1.0"); DefaultMeasurementProcessor measurement_processor; using M = std::map; -TEST(AsyncInstruments, LongCounter) {} +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