Skip to content

Commit 162c04e

Browse files
authored
[SDK] Replace SpinLockMutex with std::mutex in the metrics library (#4416)
1 parent d3b1670 commit 162c04e

6 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Increment the:
6868
(programmatic and from yaml)
6969
([#4366](https://github.com/open-telemetry/opentelemetry-cpp/pull/4366))
7070

71+
* [SDK] Replace SpinLockMutex with std::mutex in the metrics storage clases
72+
[#4416](https://github.com/open-telemetry/opentelemetry-cpp/pull/4416)
73+
7174
* [CONFIGURATION/BUILD] Add resource detector targets and README
7275
[#4430](https://github.com/open-telemetry/opentelemetry-cpp/pull/4430)
7376

‎sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
6363
// Async counter always record monotonically increasing values, and the
6464
// exporter/reader can request either for delta or cumulative value.
6565
// So we convert the async counter value to delta before passing it to temporal storage.
66-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
66+
std::lock_guard<std::mutex> guard(hashmap_lock_);
6767
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
6868
const bool offer_exemplars =
6969
ExemplarFilterEnabled(exemplar_filter_type_, opentelemetry::context::Context{});
@@ -130,7 +130,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
130130

131131
std::shared_ptr<AttributesHashMap> delta_metrics = nullptr;
132132
{
133-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
133+
std::lock_guard<std::mutex> guard(hashmap_lock_);
134134
delta_metrics = std::move(delta_hash_map_);
135135
delta_hash_map_ =
136136
std::make_unique<AttributesHashMap>(aggregation_config_->cardinality_limit_);
@@ -148,7 +148,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
148148
const AggregationConfig *aggregation_config_;
149149
std::unique_ptr<AttributesHashMap> cumulative_hash_map_;
150150
std::unique_ptr<AttributesHashMap> delta_hash_map_;
151-
opentelemetry::common::SpinLockMutex hashmap_lock_;
151+
std::mutex hashmap_lock_;
152152
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
153153
ExemplarFilterType exemplar_filter_type_;
154154
nostd::shared_ptr<ExemplarReservoir> exemplar_reservoir_;

‎sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <unordered_map>
1212

1313
#include "opentelemetry/common/key_value_iterable.h"
14-
#include "opentelemetry/common/spin_lock_mutex.h"
1514
#include "opentelemetry/common/timestamp.h"
1615
#include "opentelemetry/context/context.h"
1716
#include "opentelemetry/nostd/function_ref.h"
@@ -197,7 +196,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
197196
std::shared_ptr<BoundSyncWritableMetricStorage> Bind(
198197
const opentelemetry::common::KeyValueIterable &attributes) noexcept override;
199198

200-
// Internal: stable bound entry. Self-contained: owns its own spinlock and
199+
// Internal: stable bound entry. Self-contained: owns its own mutex and
201200
// aggregation so the user-held handle stays safe to call even if the parent
202201
// SyncMetricStorage is destroyed first (writes simply have no observer).
203202
// Collect() rotates current_ when dirty so bound + unbound writes for the
@@ -226,7 +225,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
226225
InstrumentValueType value_type_;
227226
MetricAttributes attributes_;
228227
// Protected by lock_.
229-
opentelemetry::common::SpinLockMutex lock_;
228+
std::mutex lock_;
230229
std::unique_ptr<Aggregation> current_;
231230
bool dirty_ = false;
232231
};

‎sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
#include <list>
77
#include <memory>
8+
#include <mutex>
89
#include <unordered_map>
910

10-
#include "opentelemetry/common/spin_lock_mutex.h"
1111
#include "opentelemetry/common/timestamp.h"
1212
#include "opentelemetry/nostd/function_ref.h"
1313
#include "opentelemetry/nostd/span.h"
@@ -54,7 +54,7 @@ class TemporalMetricStorage
5454
std::unordered_map<CollectorHandle *, LastReportedMetrics> last_reported_metrics_;
5555

5656
// Lock while building metrics
57-
mutable opentelemetry::common::SpinLockMutex lock_;
57+
mutable std::mutex lock_;
5858
const AggregationConfig *aggregation_config_;
5959
opentelemetry::common::SystemTimestamp last_delta_collection_ts_;
6060
bool has_last_delta_collection_ts_ = false;

‎sdk/src/metrics/state/sync_metric_storage.cc‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
# include <unordered_set>
2323
# include <vector>
2424

25-
# include "opentelemetry/common/spin_lock_mutex.h"
2625
# include "opentelemetry/sdk/common/global_log_handler.h"
2726
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
2827
# include "opentelemetry/sdk/metrics/data/exemplar_data.h"
@@ -47,7 +46,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
4746
std::shared_ptr<AttributesHashMap> delta_metrics = nullptr;
4847
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
4948
// Snapshot of bound entries (under map lock) that we will rotate without
50-
// holding the map lock. Each entry has its own spinlock for the swap.
49+
// holding the map lock. Each entry has its own mutex for the swap.
5150
std::vector<std::shared_ptr<BoundEntry>> entry_snapshot;
5251
#endif
5352
{
@@ -66,7 +65,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
6665
bool can_erase = false;
6766
if (it->second.use_count() == 1)
6867
{
69-
std::lock_guard<opentelemetry::common::SpinLockMutex> g(it->second->lock_);
68+
std::lock_guard<std::mutex> g(it->second->lock_);
7069
can_erase = !it->second->dirty_;
7170
}
7271
if (can_erase)
@@ -95,15 +94,15 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
9594
}
9695

9796
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
98-
// Rotate dirty bound entries: under each entry's own spinlock, swap out the
97+
// Rotate dirty bound entries: under each entry's own mutex, swap out the
9998
// current aggregation and merge it into delta_metrics so bound + unbound
10099
// writes for the same post-filter attribute set produce one datapoint.
101100
for (auto &entry : entry_snapshot)
102101
{
103102
std::unique_ptr<Aggregation> rotated;
104103
MetricAttributes attrs_copy;
105104
{
106-
std::lock_guard<opentelemetry::common::SpinLockMutex> g(entry->lock_);
105+
std::lock_guard<std::mutex> g(entry->lock_);
107106
if (!entry->dirty_)
108107
{
109108
continue;
@@ -160,7 +159,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
160159
// satisfy the documented invariant on dirty_.
161160
bool entry_dirty = false;
162161
{
163-
std::lock_guard<opentelemetry::common::SpinLockMutex> g(it->second->lock_);
162+
std::lock_guard<std::mutex> g(it->second->lock_);
164163
entry_dirty = it->second->dirty_;
165164
}
166165
if (entry_dirty)
@@ -228,7 +227,7 @@ void SyncMetricStorage::BoundEntry::RecordLong(int64_t value) noexcept
228227
"is not long");
229228
return;
230229
}
231-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(lock_);
230+
std::lock_guard<std::mutex> guard(lock_);
232231
current_->Aggregate(value);
233232
dirty_ = true;
234233
}
@@ -242,7 +241,7 @@ void SyncMetricStorage::BoundEntry::RecordDouble(double value) noexcept
242241
"is not double");
243242
return;
244243
}
245-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(lock_);
244+
std::lock_guard<std::mutex> guard(lock_);
246245
current_->Aggregate(value);
247246
dirty_ = true;
248247
}

‎sdk/src/metrics/state/temporal_metric_storage.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <utility>
1010
#include <vector>
1111

12-
#include "opentelemetry/common/spin_lock_mutex.h"
1312
#include "opentelemetry/common/timestamp.h"
1413
#include "opentelemetry/nostd/function_ref.h"
1514
#include "opentelemetry/nostd/span.h"
@@ -46,7 +45,7 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector,
4645
const std::shared_ptr<AttributesHashMap> &delta_metrics,
4746
nostd::function_ref<bool(MetricData)> callback) noexcept
4847
{
49-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(lock_);
48+
std::lock_guard<std::mutex> guard(lock_);
5049
AggregationTemporality aggregation_temporarily =
5150
collector->GetAggregationTemporality(instrument_descriptor_.type_);
5251
// Per OTel spec (issue #4062): the start_ts for the first delta collection

0 commit comments

Comments
 (0)