Skip to content

Commit

Permalink
Merge pull request #8 from open-telemetry/main
Browse files Browse the repository at this point in the history
Fix observable attributes drop (open-telemetry#2557)
  • Loading branch information
malkia authored Feb 27, 2024
2 parents f212b82 + 32196a5 commit 0ca7490
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
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);
}
}
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;
}

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;
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

0 comments on commit 0ca7490

Please sign in to comment.