Skip to content

Commit

Permalink
[SDK] Metrics ObservableRegistry Cleanup (#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
perhapsmaple authored Oct 18, 2023
1 parent e918960 commit 231ca4a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/include/opentelemetry/sdk/metrics/async_instruments.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ObservableInstrument : public opentelemetry::metrics::ObservableInstrument
ObservableInstrument(InstrumentDescriptor instrument_descriptor,
std::unique_ptr<AsyncWritableMetricStorage> storage,
std::shared_ptr<ObservableRegistry> observable_registry);
~ObservableInstrument() override;

void AddCallback(opentelemetry::metrics::ObservableCallbackPtr callback,
void *state) noexcept override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ObservableRegistry
void *state,
opentelemetry::metrics::ObservableInstrument *instrument);

void CleanupCallback(opentelemetry::metrics::ObservableInstrument *instrument);

void Observe(opentelemetry::common::SystemTimestamp collection_ts);

private:
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/metrics/async_instruments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ ObservableInstrument::ObservableInstrument(InstrumentDescriptor instrument_descr

{}

ObservableInstrument::~ObservableInstrument()
{
observable_registry_->CleanupCallback(this);
}

void ObservableInstrument::AddCallback(opentelemetry::metrics::ObservableCallbackPtr callback,
void *state) noexcept
{
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/metrics/state/observable_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ void ObservableRegistry::RemoveCallback(opentelemetry::metrics::ObservableCallba
callbacks_.erase(new_end, callbacks_.end());
}

void ObservableRegistry::CleanupCallback(opentelemetry::metrics::ObservableInstrument *instrument)
{
std::lock_guard<std::mutex> lock_guard{callbacks_m_};
auto iter = std::remove_if(callbacks_.begin(), callbacks_.end(),
[instrument](const std::unique_ptr<ObservableCallbackRecord> &record) {
return record->instrument == instrument;
});
callbacks_.erase(iter, callbacks_.end());
}

void ObservableRegistry::Observe(opentelemetry::common::SystemTimestamp collection_ts)
{
std::lock_guard<std::mutex> lock_guard{callbacks_m_};
Expand Down

1 comment on commit 231ca4a

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 231ca4a Previous: e918960 Ratio
BM_LockFreeBuffer/2 2686788.977646246 ns/iter 998230.2188873291 ns/iter 2.69
BM_LockFreeBuffer/4 8263249.397277832 ns/iter 3778745.007041274 ns/iter 2.19

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.