diff --git a/CHANGELOG.md b/CHANGELOG.md index 367d35a406..db9189baf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ Increment the: * [CONFIGURATION] Add support for the composite sampler configuration (programmatic and from yaml) ([#4366](https://github.com/open-telemetry/opentelemetry-cpp/pull/4366)) + +* [SDK] Replace SpinLockMutex with std::mutex in the metrics library + [#4416](https://github.com/open-telemetry/opentelemetry-cpp/pull/4416) + * [SDK] `OTELResourceDetector` now percent-decodes values parsed from the `OTEL_RESOURCE_ATTRIBUTES` environment variable, per the W3C Baggage value grammar the resource spec defers to. A malformed escape sequence is left diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h index 5cebfe1c0d..f6799d9d92 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_aggregation.h @@ -5,8 +5,8 @@ #include #include +#include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" #include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h" @@ -49,7 +49,7 @@ class Base2ExponentialHistogramAggregation : public Aggregation double value) noexcept; void Downscale(uint32_t by) noexcept; - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; Base2ExponentialHistogramPointData point_data_; Base2ExponentialHistogramIndexer indexer_; bool record_min_max_ = true; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h index dfef7f844c..8935eea293 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h @@ -7,9 +7,9 @@ #include #include #include +#include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" @@ -49,7 +49,7 @@ class LongHistogramAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; HistogramPointData point_data_; bool record_min_max_ = true; }; @@ -79,7 +79,7 @@ class DoubleHistogramAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; mutable HistogramPointData point_data_; bool record_min_max_ = true; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h index 9bf277c6e3..849a82a1d9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h @@ -5,8 +5,8 @@ #include #include +#include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/data/point_data.h" @@ -34,7 +34,7 @@ class LongLastValueAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; LastValuePointData point_data_; }; @@ -55,7 +55,7 @@ class DoubleLastValueAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; mutable LastValuePointData point_data_; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h index 9b71790288..e317be7fa7 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/sum_aggregation.h @@ -5,8 +5,8 @@ #include #include +#include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/data/point_data.h" @@ -35,7 +35,7 @@ class LongSumAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; SumPointData point_data_; }; @@ -56,7 +56,7 @@ class DoubleSumAggregation : public Aggregation PointType ToPoint() const noexcept override; private: - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; SumPointData point_data_; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h index 674863428b..b1c31cf324 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h @@ -63,7 +63,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora // Async counter always record monotonically increasing values, and the // exporter/reader can request either for delta or cumulative value. // So we convert the async counter value to delta before passing it to temporal storage. - std::lock_guard guard(hashmap_lock_); + std::lock_guard guard(hashmap_lock_); #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW const bool offer_exemplars = ExemplarFilterEnabled(exemplar_filter_type_, opentelemetry::context::Context{}); @@ -130,7 +130,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora std::shared_ptr delta_metrics = nullptr; { - std::lock_guard guard(hashmap_lock_); + std::lock_guard guard(hashmap_lock_); delta_metrics = std::move(delta_hash_map_); delta_hash_map_ = std::make_unique(aggregation_config_->cardinality_limit_); @@ -148,7 +148,7 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora const AggregationConfig *aggregation_config_; std::unique_ptr cumulative_hash_map_; std::unique_ptr delta_hash_map_; - opentelemetry::common::SpinLockMutex hashmap_lock_; + std::mutex hashmap_lock_; #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW ExemplarFilterType exemplar_filter_type_; nostd::shared_ptr exemplar_reservoir_; 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 c8d58bb7b2..1406e0b9a4 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -11,7 +11,6 @@ #include #include "opentelemetry/common/key_value_iterable.h" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/common/timestamp.h" #include "opentelemetry/context/context.h" #include "opentelemetry/nostd/function_ref.h" @@ -226,7 +225,7 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage InstrumentValueType value_type_; MetricAttributes attributes_; // Protected by lock_. - opentelemetry::common::SpinLockMutex lock_; + std::mutex lock_; std::unique_ptr current_; bool dirty_ = false; }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h index d86093c376..50fa1a2240 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/temporal_metric_storage.h @@ -5,9 +5,9 @@ #include #include +#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" @@ -54,7 +54,7 @@ class TemporalMetricStorage std::unordered_map last_reported_metrics_; // Lock while building metrics - mutable opentelemetry::common::SpinLockMutex lock_; + mutable std::mutex lock_; const AggregationConfig *aggregation_config_; opentelemetry::common::SystemTimestamp last_delta_collection_ts_; bool has_last_delta_collection_ts_ = false; diff --git a/sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc b/sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc index 103f01f14e..100a6eaf40 100644 --- a/sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/base2_exponential_histogram_aggregation.cc @@ -11,7 +11,6 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" @@ -184,7 +183,7 @@ void Base2ExponentialHistogramAggregation::Aggregate( double value, const PointAttributes & /* attributes */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.sum_ += value; point_data_.count_++; @@ -456,7 +455,7 @@ std::unique_ptr Base2ExponentialHistogramAggregation::Diff( PointType Base2ExponentialHistogramAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); Base2ExponentialHistogramPointData copy; copy.sum_ = point_data_.sum_; diff --git a/sdk/src/metrics/aggregation/histogram_aggregation.cc b/sdk/src/metrics/aggregation/histogram_aggregation.cc index 2d76aba0f4..65ac5d504e 100644 --- a/sdk/src/metrics/aggregation/histogram_aggregation.cc +++ b/sdk/src/metrics/aggregation/histogram_aggregation.cc @@ -10,7 +10,6 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h" @@ -60,7 +59,7 @@ LongHistogramAggregation::LongHistogramAggregation(const HistogramPointData &dat void LongHistogramAggregation::Aggregate(int64_t value, const PointAttributes & /* attributes */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.count_ += 1; point_data_.sum_ = nostd::get(point_data_.sum_) + value; if (record_min_max_) @@ -101,7 +100,7 @@ std::unique_ptr LongHistogramAggregation::Diff(const Aggregation &n PointType LongHistogramAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } @@ -139,7 +138,7 @@ DoubleHistogramAggregation::DoubleHistogramAggregation(const HistogramPointData void DoubleHistogramAggregation::Aggregate(double value, const PointAttributes & /* attributes */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.count_ += 1; point_data_.sum_ = nostd::get(point_data_.sum_) + value; if (record_min_max_) @@ -181,7 +180,7 @@ std::unique_ptr DoubleHistogramAggregation::Diff( PointType DoubleHistogramAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } diff --git a/sdk/src/metrics/aggregation/lastvalue_aggregation.cc b/sdk/src/metrics/aggregation/lastvalue_aggregation.cc index daa907b272..f787c94863 100644 --- a/sdk/src/metrics/aggregation/lastvalue_aggregation.cc +++ b/sdk/src/metrics/aggregation/lastvalue_aggregation.cc @@ -6,7 +6,6 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/common/timestamp.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" @@ -34,7 +33,7 @@ LongLastValueAggregation::LongLastValueAggregation(const LastValuePointData &dat void LongLastValueAggregation::Aggregate(int64_t value, const PointAttributes & /* attributes */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.is_lastvalue_valid_ = true; point_data_.value_ = value; point_data_.sample_ts_ = std::chrono::system_clock::now(); @@ -73,7 +72,7 @@ std::unique_ptr LongLastValueAggregation::Diff(const Aggregation &n PointType LongLastValueAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } @@ -90,7 +89,7 @@ DoubleLastValueAggregation::DoubleLastValueAggregation(const LastValuePointData void DoubleLastValueAggregation::Aggregate(double value, const PointAttributes & /* attributes */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.is_lastvalue_valid_ = true; point_data_.value_ = value; point_data_.sample_ts_ = std::chrono::system_clock::now(); @@ -130,7 +129,7 @@ std::unique_ptr DoubleLastValueAggregation::Diff( PointType DoubleLastValueAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } } // namespace metrics diff --git a/sdk/src/metrics/aggregation/sum_aggregation.cc b/sdk/src/metrics/aggregation/sum_aggregation.cc index 906851b04e..f4f36f2828 100644 --- a/sdk/src/metrics/aggregation/sum_aggregation.cc +++ b/sdk/src/metrics/aggregation/sum_aggregation.cc @@ -6,7 +6,6 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/metrics/aggregation/aggregation.h" @@ -39,7 +38,7 @@ void LongSumAggregation::Aggregate(int64_t value, const PointAttributes & /* att << value); return; } - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.value_ = nostd::get(point_data_.value_) + value; } @@ -97,7 +96,7 @@ std::unique_ptr LongSumAggregation::Diff(const Aggregation &next) c PointType LongSumAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } @@ -120,7 +119,7 @@ void DoubleSumAggregation::Aggregate(double value, << value); return; } - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); point_data_.value_ = nostd::get(point_data_.value_) + value; } @@ -178,7 +177,7 @@ std::unique_ptr DoubleSumAggregation::Diff(const Aggregation &next) PointType DoubleSumAggregation::ToPoint() const noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard locked(lock_); return point_data_; } diff --git a/sdk/src/metrics/state/sync_metric_storage.cc b/sdk/src/metrics/state/sync_metric_storage.cc index 245ef77454..0c8a154e24 100644 --- a/sdk/src/metrics/state/sync_metric_storage.cc +++ b/sdk/src/metrics/state/sync_metric_storage.cc @@ -22,7 +22,6 @@ # 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" @@ -66,7 +65,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, bool can_erase = false; if (it->second.use_count() == 1) { - std::lock_guard g(it->second->lock_); + std::lock_guard g(it->second->lock_); can_erase = !it->second->dirty_; } if (can_erase) @@ -103,7 +102,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, std::unique_ptr rotated; MetricAttributes attrs_copy; { - std::lock_guard g(entry->lock_); + std::lock_guard g(entry->lock_); if (!entry->dirty_) { continue; @@ -160,7 +159,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, // satisfy the documented invariant on dirty_. bool entry_dirty = false; { - std::lock_guard g(it->second->lock_); + std::lock_guard g(it->second->lock_); entry_dirty = it->second->dirty_; } if (entry_dirty) @@ -228,7 +227,7 @@ void SyncMetricStorage::BoundEntry::RecordLong(int64_t value) noexcept "is not long"); return; } - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); current_->Aggregate(value); dirty_ = true; } @@ -242,7 +241,7 @@ void SyncMetricStorage::BoundEntry::RecordDouble(double value) noexcept "is not double"); return; } - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); current_->Aggregate(value); dirty_ = true; } diff --git a/sdk/src/metrics/state/temporal_metric_storage.cc b/sdk/src/metrics/state/temporal_metric_storage.cc index 85a0a4b3c0..71af45ab0b 100644 --- a/sdk/src/metrics/state/temporal_metric_storage.cc +++ b/sdk/src/metrics/state/temporal_metric_storage.cc @@ -9,7 +9,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" @@ -46,7 +45,7 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector, const std::shared_ptr &delta_metrics, nostd::function_ref callback) noexcept { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); AggregationTemporality aggregation_temporarily = collector->GetAggregationTemporality(instrument_descriptor_.type_); // Per OTel spec (issue #4062): the start_ts for the first delta collection