From 2feef1b75da5a59fb11e50e1f48aaaf2c21a9ddf Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 18 May 2022 17:26:24 -0700 Subject: [PATCH 1/4] initial commit --- api/include/opentelemetry/metrics/meter.h | 30 +++++++++++-------- api/include/opentelemetry/metrics/noop.h | 18 +++++++---- sdk/include/opentelemetry/sdk/metrics/meter.h | 23 +++++++++----- .../sdk/metrics/state/async_metric_storage.h | 7 +++-- sdk/src/metrics/meter.cc | 30 +++++++++++-------- sdk/test/metrics/async_metric_storage_test.cc | 2 +- 6 files changed, 69 insertions(+), 41 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index 6a19911390..dcc605bc16 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -51,18 +51,20 @@ class Meter * shared_ptr to that Observable Counter * * @param name the name of the new Observable Counter. + * @param callback the function to be observed by the instrument. + * @param state to be passed back to callback * @param description a brief description of what the Observable Counter is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. - * @param callback the function to be observed by the instrument. - * @return a shared pointer to the created Observable Counter. */ virtual void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; virtual void CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; @@ -89,18 +91,20 @@ class Meter * shared_ptr to that Observable Counter * * @param name the name of the new Observable Gauge. + * @param callback the function to be observed by the instrument. + * @param state to be passed back to callback * @param description a brief description of what the Observable Gauge is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. - * @param callback the function to be observed by the instrument. - * @return a shared pointer to the created Observable Gauge. */ virtual void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; virtual void CreateDoubleObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; @@ -128,18 +132,20 @@ class Meter * a shared_ptr to that Observable UpDownCounter * * @param name the name of the new Observable UpDownCounter. + * @param callback the function to be observed by the instrument. + * @param state to be passed back to callback * @param description a brief description of what the Observable UpDownCounter is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. - * @param callback the function to be observed by the instrument. - * @return a shared pointer to the created Observable UpDownCounter. */ virtual void CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), + void *state = nullptr, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; }; diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 0fbda060b9..bb3c366481 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -138,13 +138,15 @@ class NoopMeter final : public Meter } void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} void CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} @@ -166,13 +168,15 @@ class NoopMeter final : public Meter } void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} void CreateDoubleObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} @@ -196,13 +200,15 @@ class NoopMeter final : public Meter } void CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} void CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void *state, + void (*callback)(ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override {} diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 9d9595f660..6c72f1141f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -43,13 +43,15 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view unit = "") noexcept override; void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; void CreateDoubleObservableCounter( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; @@ -64,13 +66,15 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view unit = "") noexcept override; void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; void CreateDoubleObservableGauge( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; @@ -86,13 +90,15 @@ class Meter final : public opentelemetry::metrics::Meter void CreateLongObservableUpDownCounter( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; void CreateDoubleObservableUpDownCounter( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state, + void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", nostd::string_view unit = "") noexcept override; @@ -117,7 +123,8 @@ class Meter final : public opentelemetry::metrics::Meter template void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, - void (*callback)(opentelemetry::metrics::ObserverResult &)) + void (*callback)(opentelemetry::metrics::ObserverResult &), + void *state = nullptr) { auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( @@ -128,7 +135,7 @@ class Meter final : public opentelemetry::metrics::Meter view_instr_desc.description_ = view.GetDescription(); auto storage = std::shared_ptr>( new AsyncMetricStorage(view_instr_desc, view.GetAggregationType(), callback, - &view.GetAttributesProcessor())); + &view.GetAttributesProcessor(), state)); storage_registry_[instrument_descriptor.name_] = storage; return true; }); diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index e4c20e4010..87bae179c6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -28,11 +28,13 @@ class AsyncMetricStorage : public MetricStorage AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, void (*measurement_callback)(opentelemetry::metrics::ObserverResult &), - const AttributesProcessor *attributes_processor) + const AttributesProcessor *attributes_processor, + void *state = nullptr) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, measurement_collection_callback_{measurement_callback}, attributes_processor_{attributes_processor}, + state_{state}, cumulative_hash_map_(new AttributesHashMap()), temporal_metric_storage_(instrument_descriptor) {} @@ -46,7 +48,7 @@ class AsyncMetricStorage : public MetricStorage opentelemetry::sdk::metrics::ObserverResult ob_res(attributes_processor_); // read the measurement using configured callback - measurement_collection_callback_(ob_res); + measurement_collection_callback_(ob_res, state_); std::shared_ptr delta_hash_map(new AttributesHashMap()); // process the read measurements - aggregate and store in hashmap for (auto &measurement : ob_res.GetMeasurements()) @@ -81,6 +83,7 @@ class AsyncMetricStorage : public MetricStorage AggregationType aggregation_type_; void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult &); const AttributesProcessor *attributes_processor_; + void *state_; std::unique_ptr cumulative_hash_map_; TemporalMetricStorage temporal_metric_storage_; }; diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 30725bdccf..7bc45dc378 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -58,7 +58,8 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( } void Meter::CreateLongObservableCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -66,11 +67,12 @@ void Meter::CreateLongObservableCounter(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } void Meter::CreateDoubleObservableCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -78,7 +80,7 @@ void Meter::CreateDoubleObservableCounter(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } nostd::shared_ptr> Meter::CreateLongHistogram( @@ -110,7 +112,8 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( } void Meter::CreateLongObservableGauge(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -118,11 +121,12 @@ void Meter::CreateLongObservableGauge(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } void Meter::CreateDoubleObservableGauge(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -130,7 +134,7 @@ void Meter::CreateDoubleObservableGauge(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableGauge, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } nostd::shared_ptr> Meter::CreateLongUpDownCounter( @@ -162,7 +166,8 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount } void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -170,11 +175,12 @@ void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kLong}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + void *state, + void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, nostd::string_view unit) noexcept { @@ -182,7 +188,7 @@ void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, std::string{unit.data(), unit.size()}, InstrumentType::kObservableUpDownCounter, InstrumentValueType::kDouble}; - RegisterAsyncMetricStorage(instrument_descriptor, callback); + RegisterAsyncMetricStorage(instrument_descriptor, callback, state); } const sdk::instrumentationlibrary::InstrumentationLibrary *Meter::GetInstrumentationLibrary() diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index a4decaaa79..fd3730c888 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -39,7 +39,7 @@ class WritableMetricStorageTestFixture : public ::testing::TestWithParam &observer_result) + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result, void *state) { fetch_count++; if (fetch_count == 1) From eaca516f44f9a08f0ac34a6952c1c4ce2e942697 Mon Sep 17 00:00:00 2001 From: Lalit Date: Wed, 18 May 2022 21:37:22 -0700 Subject: [PATCH 2/4] more changes --- api/include/opentelemetry/metrics/meter.h | 33 ++++++++--------- api/include/opentelemetry/metrics/noop.h | 24 ++++++------- .../common/metrics_foo_library/foo_library.cc | 2 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 35 ++++++++++--------- .../sdk/metrics/state/async_metric_storage.h | 5 +-- sdk/src/metrics/meter.cc | 33 +++++++++-------- 6 files changed, 70 insertions(+), 62 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index dcc605bc16..aa2a001719 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -52,21 +52,21 @@ class Meter * * @param name the name of the new Observable Counter. * @param callback the function to be observed by the instrument. - * @param state to be passed back to callback * @param description a brief description of what the Observable Counter is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. + * @param state to be passed back to callback */ virtual void CreateLongObservableCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), - void *state = nullptr, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; virtual void CreateDoubleObservableCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), - void *state = nullptr, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; /** * Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram. @@ -92,21 +92,21 @@ class Meter * * @param name the name of the new Observable Gauge. * @param callback the function to be observed by the instrument. - * @param state to be passed back to callback * @param description a brief description of what the Observable Gauge is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. + * @param state to be passed back to callback */ virtual void CreateLongObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), - void *state = nullptr, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; virtual void CreateDoubleObservableGauge(nostd::string_view name, void (*callback)(ObserverResult &, void *), - void *state = nullptr, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; /** * Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that @@ -133,21 +133,22 @@ class Meter * * @param name the name of the new Observable UpDownCounter. * @param callback the function to be observed by the instrument. - * @param state to be passed back to callback * @param description a brief description of what the Observable UpDownCounter is used for. * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. + * @param state to be passed back to callback */ virtual void CreateLongObservableUpDownCounter(nostd::string_view name, void (*callback)(ObserverResult &, void *), - void *state = nullptr, nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &, void *), - void *state = nullptr, + void (*callback)(ObserverResult &, + void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept = 0; + nostd::string_view unit = "", + void *state = nullptr) noexcept = 0; }; } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index bb3c366481..c47f7489f0 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -138,17 +138,17 @@ class NoopMeter final : public Meter } void CreateLongObservableCounter(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} void CreateDoubleObservableCounter(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongHistogram( @@ -168,17 +168,17 @@ class NoopMeter final : public Meter } void CreateLongObservableGauge(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} void CreateDoubleObservableGauge(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} nostd::shared_ptr> CreateLongUpDownCounter( @@ -200,17 +200,17 @@ class NoopMeter final : public Meter } void CreateLongObservableUpDownCounter(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} void CreateDoubleObservableUpDownCounter(nostd::string_view name, - void *state, void (*callback)(ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::string_view unit = "", + void *state = nullptr) noexcept override {} }; diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 79e0cbc5ab..2fcbd660e0 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -30,7 +30,7 @@ std::map get_random_attr() class MeasurementFetcher { public: - static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result) + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result, void *state) { double val = (rand() % 700) + 1.1; std::map labels = get_random_attr(); diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 6c72f1141f..0254ace561 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -43,17 +43,18 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view unit = "") noexcept override; void CreateLongObservableCounter(nostd::string_view name, - void *state, - void (*callback)(opentelemetry::metrics::ObserverResult &, void *), + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; void CreateDoubleObservableCounter( nostd::string_view name, - void *state, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongHistogram( nostd::string_view name, @@ -66,17 +67,18 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view unit = "") noexcept override; void CreateLongObservableGauge(nostd::string_view name, - void *state, - void (*callback)(opentelemetry::metrics::ObserverResult &, void *), + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; void CreateDoubleObservableGauge( nostd::string_view name, - void *state, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; nostd::shared_ptr> CreateLongUpDownCounter( nostd::string_view name, @@ -90,17 +92,17 @@ class Meter final : public opentelemetry::metrics::Meter void CreateLongObservableUpDownCounter( nostd::string_view name, - void *state, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; void CreateDoubleObservableUpDownCounter( nostd::string_view name, - void *state, void (*callback)(opentelemetry::metrics::ObserverResult &, void *), nostd::string_view description = "", - nostd::string_view unit = "") noexcept override; + nostd::string_view unit = "", + void *state = nullptr) noexcept override; /** Returns the associated instruementation library */ const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary() @@ -123,13 +125,14 @@ class Meter final : public opentelemetry::metrics::Meter template void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor, - void (*callback)(opentelemetry::metrics::ObserverResult &), + void (*callback)(opentelemetry::metrics::ObserverResult &, + void *), void *state = nullptr) { auto view_registry = meter_context_->GetViewRegistry(); auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, - [this, &instrument_descriptor, callback](const View &view) { + [this, &instrument_descriptor, callback, state](const View &view) { auto view_instr_desc = instrument_descriptor; view_instr_desc.name_ = view.GetName(); view_instr_desc.description_ = view.GetDescription(); diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 87bae179c6..e5dcbc2738 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -27,7 +27,8 @@ class AsyncMetricStorage : public MetricStorage public: AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, - void (*measurement_callback)(opentelemetry::metrics::ObserverResult &), + void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, + void *), const AttributesProcessor *attributes_processor, void *state = nullptr) : instrument_descriptor_(instrument_descriptor), @@ -81,7 +82,7 @@ class AsyncMetricStorage : public MetricStorage private: InstrumentDescriptor instrument_descriptor_; AggregationType aggregation_type_; - void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult &); + void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult &, void *); const AttributesProcessor *attributes_processor_; void *state_; std::unique_ptr cumulative_hash_map_; diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 7bc45dc378..600bae9863 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -58,10 +58,10 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( } void Meter::CreateLongObservableCounter(nostd::string_view name, - void *state, void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, @@ -71,10 +71,11 @@ void Meter::CreateLongObservableCounter(nostd::string_view name, } void Meter::CreateDoubleObservableCounter(nostd::string_view name, - void *state, - void (*callback)(metrics::ObserverResult &, void *), + void (*callback)(metrics::ObserverResult &, + void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, @@ -112,10 +113,10 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( } void Meter::CreateLongObservableGauge(nostd::string_view name, - void *state, void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, @@ -125,10 +126,10 @@ void Meter::CreateLongObservableGauge(nostd::string_view name, } void Meter::CreateDoubleObservableGauge(nostd::string_view name, - void *state, void (*callback)(metrics::ObserverResult &, void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, @@ -166,10 +167,11 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount } void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, - void *state, - void (*callback)(metrics::ObserverResult &, void *), + void (*callback)(metrics::ObserverResult &, + void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, @@ -179,10 +181,11 @@ void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, } void Meter::CreateDoubleObservableUpDownCounter(nostd::string_view name, - void *state, - void (*callback)(metrics::ObserverResult &, void *), + void (*callback)(metrics::ObserverResult &, + void *), nostd::string_view description, - nostd::string_view unit) noexcept + nostd::string_view unit, + void *state) noexcept { InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, From d9ec874457f8b2eb80b8de9ad1869fbb8dcef3a4 Mon Sep 17 00:00:00 2001 From: Lalit Date: Wed, 18 May 2022 22:17:22 -0700 Subject: [PATCH 3/4] comment out argument not used --- sdk/test/metrics/async_metric_storage_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/test/metrics/async_metric_storage_test.cc b/sdk/test/metrics/async_metric_storage_test.cc index fd3730c888..2be5332a8d 100644 --- a/sdk/test/metrics/async_metric_storage_test.cc +++ b/sdk/test/metrics/async_metric_storage_test.cc @@ -39,7 +39,8 @@ class WritableMetricStorageTestFixture : public ::testing::TestWithParam &observer_result, void *state) + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result, + void * /*state*/) { fetch_count++; if (fetch_count == 1) From 824ebc068e4b091466266125545762ff6e2afd43 Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 19 May 2022 19:18:26 -0700 Subject: [PATCH 4/4] fix review comments --- api/include/opentelemetry/metrics/meter.h | 2 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index aa2a001719..e0454e360b 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -47,7 +47,7 @@ class Meter nostd::string_view unit = "") noexcept = 0; /** - * Creates a Asynchronouse (Observable) counter with the passed characteristics and returns a + * Creates a Asynchronous (Observable) counter with the passed characteristics and returns a * shared_ptr to that Observable Counter * * @param name the name of the new Observable Counter. diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 0254ace561..44e54adf18 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -133,10 +133,16 @@ class Meter final : public opentelemetry::metrics::Meter auto success = view_registry->FindViews( instrument_descriptor, *instrumentation_library_, [this, &instrument_descriptor, callback, state](const View &view) { - auto view_instr_desc = instrument_descriptor; - view_instr_desc.name_ = view.GetName(); - view_instr_desc.description_ = view.GetDescription(); - auto storage = std::shared_ptr>( + auto view_instr_desc = instrument_descriptor; + if (!view.GetName().empty()) + { + view_instr_desc.name_ = view.GetName(); + } + if (!view.GetDescription().empty()) + { + view_instr_desc.description_ = view.GetDescription(); + } + auto storage = std::shared_ptr>( new AsyncMetricStorage(view_instr_desc, view.GetAggregationType(), callback, &view.GetAttributesProcessor(), state)); storage_registry_[instrument_descriptor.name_] = storage;