Skip to content

Commit 029bc50

Browse files
[METRICS SDK] Use std::mutex for SyncMetricStorage record-path lock (#4245)
On Windows the SpinLockMutex final back-off sleep_for(1ms) is rounded up to the ~15.6 ms timer quantum, so a waiter contending on a hot instrument stalls a full quantum and inflates tail latency. Switch only SyncMetricStorage::attribute_hashmap_lock_ to std::mutex; the API SpinLockMutex and all other SDK locks are unchanged.
1 parent bd94db8 commit 029bc50

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [METRICS SDK] Fix Windows metrics tail latency on the synchronous record path:
19+
`SyncMetricStorage::attribute_hashmap_lock_` now uses `std::mutex` instead of
20+
`common::SpinLockMutex`. The spin lock's final `sleep_for(1ms)` back-off is
21+
rounded up to the Windows timer quantum (~15.6 ms), so a waiter contending on a
22+
hot instrument could stall for a full quantum, inflating tail latency.
23+
`std::mutex` blocks the waiter instead. Only this one contended lock changes;
24+
the API `SpinLockMutex` and all other SDK locks are unchanged.
25+
[#4245](https://github.com/open-telemetry/opentelemetry-cpp/pull/4245)
26+
1827
* [CODE HEALTH] Fix more clang tidy warnings (member initialization)
1928
[#4270](https://github.com/open-telemetry/opentelemetry-cpp/pull/4270)
2029

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
103103
}
104104
#endif
105105
static MetricAttributes attr = MetricAttributes{};
106-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
106+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
107107
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
108108
MetricAttributes resolved = ResolveCardinality(attr);
109109
attributes_hashmap_->GetOrSetDefault(std::move(resolved), create_default_aggregation_)
@@ -131,7 +131,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
131131
#endif
132132

133133
MetricAttributes attr{attributes, attributes_processor_.get()};
134-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
134+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
135135
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
136136
// Resolve via the unified cardinality policy so unbound and bound paths
137137
// share one combined limit (see ResolveCardinality()).
@@ -161,7 +161,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
161161
}
162162
#endif
163163
static MetricAttributes attr = MetricAttributes{};
164-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
164+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
165165
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
166166
MetricAttributes resolved = ResolveCardinality(attr);
167167
attributes_hashmap_->GetOrSetDefault(std::move(resolved), create_default_aggregation_)
@@ -188,7 +188,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
188188
}
189189
#endif
190190
MetricAttributes attr{attributes, attributes_processor_.get()};
191-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
191+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
192192
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
193193
MetricAttributes resolved = ResolveCardinality(std::move(attr));
194194
// cppcheck-suppress accessMoved
@@ -300,7 +300,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage
300300
nostd::shared_ptr<ExemplarReservoir> exemplar_reservoir_;
301301
#endif
302302
TemporalMetricStorage temporal_metric_storage_;
303-
opentelemetry::common::SpinLockMutex attribute_hashmap_lock_;
303+
std::mutex attribute_hashmap_lock_;
304304
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
305305
// NOTE: ENABLE_METRICS_BOUND_INSTRUMENTS_PREVIEW changes the layout and
306306
// vtable of SyncMetricStorage (these conditional members and the virtual

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
5151
std::vector<std::shared_ptr<BoundEntry>> entry_snapshot;
5252
#endif
5353
{
54-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
54+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
5555
delta_metrics = std::move(attributes_hashmap_);
5656
attributes_hashmap_.reset(new AttributesHashMap(aggregation_config_->cardinality_limit_));
5757
#ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW
@@ -142,7 +142,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector,
142142
// Drop snapshot refs so use_count reflects only storage + user holders.
143143
entry_snapshot.clear();
144144
{
145-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
145+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
146146
for (const auto &k : snapshot_keys)
147147
{
148148
auto it = bound_entries_.find(k);
@@ -189,7 +189,7 @@ std::shared_ptr<BoundSyncWritableMetricStorage> SyncMetricStorage::Bind(
189189
// Filter attributes once, at bind time.
190190
MetricAttributes filtered{attributes, attributes_processor_.get()};
191191

192-
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
192+
std::lock_guard<std::mutex> guard(attribute_hashmap_lock_);
193193

194194
// Dedupe: same post-filter attribute set returns the same bound entry.
195195
auto it = bound_entries_.find(filtered);

0 commit comments

Comments
 (0)