diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index 6a19911390..e0454e360b 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -47,24 +47,26 @@ 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. + * @param callback the function to be observed by the instrument. * @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. + * @param state to be passed back to callback */ virtual void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), 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 (*callback)(ObserverResult &, void *), 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. @@ -89,20 +91,22 @@ 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 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. + * @param state to be passed back to callback */ virtual void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), 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 (*callback)(ObserverResult &, void *), 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 @@ -128,20 +132,23 @@ 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 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. + * @param state to be passed back to callback */ virtual void CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + void (*callback)(ObserverResult &, void *), 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 (*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 0fbda060b9..c47f7489f0 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -138,15 +138,17 @@ class NoopMeter final : public Meter } void CreateLongObservableCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + 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 (*callback)(ObserverResult &), + 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( @@ -166,15 +168,17 @@ class NoopMeter final : public Meter } void CreateLongObservableGauge(nostd::string_view name, - void (*callback)(ObserverResult &), + 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 (*callback)(ObserverResult &), + 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( @@ -196,15 +200,17 @@ class NoopMeter final : public Meter } void CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(ObserverResult &), + 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 (*callback)(ObserverResult &), + 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 9d9595f660..44e54adf18 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -43,15 +43,18 @@ 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 (*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 (*callback)(opentelemetry::metrics::ObserverResult &), + 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, @@ -64,15 +67,18 @@ 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 (*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 (*callback)(opentelemetry::metrics::ObserverResult &), + 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, @@ -86,15 +92,17 @@ class Meter final : public opentelemetry::metrics::Meter void CreateLongObservableUpDownCounter( nostd::string_view name, - void (*callback)(opentelemetry::metrics::ObserverResult &), + 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 (*callback)(opentelemetry::metrics::ObserverResult &), + 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() @@ -117,18 +125,26 @@ 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) { - auto view_instr_desc = instrument_descriptor; - view_instr_desc.name_ = view.GetName(); - view_instr_desc.description_ = view.GetDescription(); - auto storage = std::shared_ptr>( + [this, &instrument_descriptor, callback, state](const View &view) { + 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())); + &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..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,12 +27,15 @@ class AsyncMetricStorage : public MetricStorage public: AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, - void (*measurement_callback)(opentelemetry::metrics::ObserverResult &), - const AttributesProcessor *attributes_processor) + void (*measurement_callback)(opentelemetry::metrics::ObserverResult &, + void *), + 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 +49,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()) @@ -79,8 +82,9 @@ 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_; TemporalMetricStorage temporal_metric_storage_; }; diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 30725bdccf..600bae9863 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -58,27 +58,30 @@ nostd::shared_ptr> Meter::CreateDoubleCounter( } void Meter::CreateLongObservableCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + 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()}, 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 (*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()}, 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,27 +113,29 @@ nostd::shared_ptr> Meter::CreateDoubleHistogram( } void Meter::CreateLongObservableGauge(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + 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()}, 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 (*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()}, 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,27 +167,31 @@ nostd::shared_ptr> Meter::CreateDoubleUpDownCount } void Meter::CreateLongObservableUpDownCounter(nostd::string_view name, - void (*callback)(metrics::ObserverResult &), + 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()}, 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 (*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()}, 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..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) + static void Fetcher(opentelemetry::metrics::ObserverResult &observer_result, + void * /*state*/) { fetch_count++; if (fetch_count == 1)