diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e479d5ca..ffe609db7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,14 @@ Increment the: ## [Unreleased] +* [METRICS SDK] Fix Windows metrics tail latency on the synchronous record path: + `SyncMetricStorage::attribute_hashmap_lock_` now uses `std::mutex` instead of + `common::SpinLockMutex`. The spin lock's final `sleep_for(1ms)` back-off is + rounded up to the Windows timer quantum (~15.6 ms), so a waiter contending + on a hot instrument could stall for a full quantum, inflating tail latency. + `std::mutex` blocks the waiter instead. Only this one contended lock changes; + the API `SpinLockMutex` and all other SDK locks are unchanged. + [#4245](https://github.com/open-telemetry/opentelemetry-cpp/pull/4245) * [CONFIGURATION] Add the probability sampler to file configuration [#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334) diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h index e9aaebe6f5..e4df834eb1 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -103,7 +103,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage } #endif static MetricAttributes attr = MetricAttributes{}; - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW MetricAttributes resolved = ResolveCardinality(attr); attributes_hashmap_->GetOrSetDefault(std::move(resolved), create_default_aggregation_) @@ -131,7 +131,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage #endif MetricAttributes attr{attributes, attributes_processor_.get()}; - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW // Resolve via the unified cardinality policy so unbound and bound paths // share one combined limit (see ResolveCardinality()). @@ -161,7 +161,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage } #endif static MetricAttributes attr = MetricAttributes{}; - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW MetricAttributes resolved = ResolveCardinality(attr); attributes_hashmap_->GetOrSetDefault(std::move(resolved), create_default_aggregation_) @@ -188,7 +188,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage } #endif MetricAttributes attr{attributes, attributes_processor_.get()}; - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW MetricAttributes resolved = ResolveCardinality(std::move(attr)); // cppcheck-suppress accessMoved @@ -300,7 +300,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage nostd::shared_ptr exemplar_reservoir_; #endif TemporalMetricStorage temporal_metric_storage_; - opentelemetry::common::SpinLockMutex attribute_hashmap_lock_; + std::mutex attribute_hashmap_lock_; #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW // NOTE: ENABLE_METRICS_BOUND_INSTRUMENTS_PREVIEW changes the layout and // vtable of SyncMetricStorage (these conditional members and the virtual diff --git a/sdk/src/metrics/state/sync_metric_storage.cc b/sdk/src/metrics/state/sync_metric_storage.cc index 80ec279d10..67a5341656 100644 --- a/sdk/src/metrics/state/sync_metric_storage.cc +++ b/sdk/src/metrics/state/sync_metric_storage.cc @@ -5,7 +5,6 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/common/timestamp.h" #include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/span.h" @@ -23,6 +22,7 @@ # include # include +# include "opentelemetry/common/spin_lock_mutex.h" # include "opentelemetry/sdk/common/global_log_handler.h" # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" # include "opentelemetry/sdk/metrics/data/exemplar_data.h" @@ -51,7 +51,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, std::vector> entry_snapshot; #endif { - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); delta_metrics = std::move(attributes_hashmap_); attributes_hashmap_.reset(new AttributesHashMap(aggregation_config_->cardinality_limit_)); #ifdef OPENTELEMETRY_HAVE_METRICS_BOUND_INSTRUMENTS_PREVIEW @@ -142,7 +142,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, // Drop snapshot refs so use_count reflects only storage + user holders. entry_snapshot.clear(); { - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); for (const auto &k : snapshot_keys) { auto it = bound_entries_.find(k); @@ -189,7 +189,7 @@ std::shared_ptr SyncMetricStorage::Bind( // Filter attributes once, at bind time. MetricAttributes filtered{attributes, attributes_processor_.get()}; - std::lock_guard guard(attribute_hashmap_lock_); + std::lock_guard guard(attribute_hashmap_lock_); // Dedupe: same post-filter attribute set returns the same bound entry. auto it = bound_entries_.find(filtered);