From 32196a51f0e98d46cb4e1fa8928affabb5edc36b Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 27 Feb 2024 08:56:57 -0800 Subject: [PATCH] Fix observable attributes drop (#2557) --- examples/common/metrics_foo_library/foo_library.cc | 3 ++- sdk/include/opentelemetry/sdk/metrics/observer_result.h | 8 ++++++-- sdk/src/metrics/state/observable_registry.cc | 5 +++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index fea86e7a41..79427d195b 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -40,9 +40,10 @@ class MeasurementFetcher { double random_incr = (rand() % 5) + 1.1; value_ += random_incr; + std::map labels = get_random_attr(); nostd::get>>( observer_result) - ->Observe(value_ /*, labelkv */); + ->Observe(value_, labels); } } static double value_; diff --git a/sdk/include/opentelemetry/sdk/metrics/observer_result.h b/sdk/include/opentelemetry/sdk/metrics/observer_result.h index 5be40329c1..4ed3cd168d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/observer_result.h +++ b/sdk/include/opentelemetry/sdk/metrics/observer_result.h @@ -26,11 +26,15 @@ class ObserverResultT final : public opentelemetry::metrics::ObserverResultT ~ObserverResultT() override = default; - void Observe(T value) noexcept override { data_.insert({{}, value}); } + void Observe(T value) noexcept override + { + data_[MetricAttributes{{}, attributes_processor_}] = value; + } void Observe(T value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override { - data_.insert({MetricAttributes{attributes, attributes_processor_}, value}); + data_[MetricAttributes{attributes, attributes_processor_}] = + value; // overwrites the previous value if present } const std::unordered_map &GetMeasurements() diff --git a/sdk/src/metrics/state/observable_registry.cc b/sdk/src/metrics/state/observable_registry.cc index 41b8e86067..86a6d17abd 100644 --- a/sdk/src/metrics/state/observable_registry.cc +++ b/sdk/src/metrics/state/observable_registry.cc @@ -50,6 +50,7 @@ void ObservableRegistry::CleanupCallback(opentelemetry::metrics::ObservableInstr void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collection_ts) { + static DefaultAttributesProcessor default_attribute_processor; std::lock_guard lock_guard{callbacks_m_}; for (auto &callback_wrap : callbacks_) { @@ -69,7 +70,7 @@ void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collecti if (value_type == InstrumentValueType::kDouble) { nostd::shared_ptr> ob_res( - new opentelemetry::sdk::metrics::ObserverResultT()); + new opentelemetry::sdk::metrics::ObserverResultT(&default_attribute_processor)); callback_wrap->callback(ob_res, callback_wrap->state); storage->RecordDouble( static_cast *>(ob_res.get()) @@ -79,7 +80,7 @@ void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collecti else { nostd::shared_ptr> ob_res( - new opentelemetry::sdk::metrics::ObserverResultT()); + new opentelemetry::sdk::metrics::ObserverResultT(&default_attribute_processor)); callback_wrap->callback(ob_res, callback_wrap->state); storage->RecordLong( static_cast *>(ob_res.get())