Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix observable attributes drop #2557

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class MeasurementFetcher
{
double random_incr = (rand() % 5) + 1.1;
value_ += random_incr;
std::map<std::string, std::string> labels = get_random_attr();
nostd::get<nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>>>(
observer_result)
->Observe(value_ /*, labelkv */);
->Observe(value_, labels);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the changes by the modified metrics example. More exhaustive tests need to be added for observable instruments, will add them along with the filtering issue fix.

}
}
static double value_;
Expand Down
8 changes: 6 additions & 2 deletions sdk/include/opentelemetry/sdk/metrics/observer_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class ObserverResultT final : public opentelemetry::metrics::ObserverResultT<T>

~ObserverResultT() override = default;

void Observe(T value) noexcept override { data_.insert({{}, value}); }
void Observe(T value) noexcept override
{
data_[MetricAttributes{{}, attributes_processor_}] = value;
Copy link
Member Author

@lalitb lalitb Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to the bug. Ideally, observing a value with an identical set of attributes should lead to it being overwritten. However, at present, using std::unordered_map::insert() leads to the value being disregarded rather than replaced. The observable callback shouldn't be reporting multiple values for the same attribute-set in single invoke, but fixing it just in case they do so.

}

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<MetricAttributes, T, AttributeHashGenerator> &GetMeasurements()
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/metrics/state/observable_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void ObservableRegistry::CleanupCallback(opentelemetry::metrics::ObservableInstr

void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collection_ts)
{
static DefaultAttributesProcessor default_attribute_processor;
ThomsonTan marked this conversation as resolved.
Show resolved Hide resolved
std::lock_guard<std::mutex> lock_guard{callbacks_m_};
for (auto &callback_wrap : callbacks_)
{
Expand All @@ -69,7 +70,7 @@ void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collecti
if (value_type == InstrumentValueType::kDouble)
{
nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>> ob_res(
new opentelemetry::sdk::metrics::ObserverResultT<double>());
new opentelemetry::sdk::metrics::ObserverResultT<double>(&default_attribute_processor));
callback_wrap->callback(ob_res, callback_wrap->state);
storage->RecordDouble(
static_cast<opentelemetry::sdk::metrics::ObserverResultT<double> *>(ob_res.get())
Expand All @@ -79,7 +80,7 @@ void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collecti
else
{
nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<int64_t>> ob_res(
new opentelemetry::sdk::metrics::ObserverResultT<int64_t>());
new opentelemetry::sdk::metrics::ObserverResultT<int64_t>(&default_attribute_processor));
callback_wrap->callback(ob_res, callback_wrap->state);
storage->RecordLong(
static_cast<opentelemetry::sdk::metrics::ObserverResultT<int64_t> *>(ob_res.get())
Expand Down