Skip to content

Commit

Permalink
fix metrics race condition (open-telemetry#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored and yxue committed Dec 5, 2022
1 parent 86ac23a commit 402a0bd
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LongHistogramAggregation : public Aggregation
PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
mutable opentelemetry::common::SpinLockMutex lock_;
HistogramPointData point_data_;
bool record_min_max_ = true;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LongLastValueAggregation : public Aggregation
PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
mutable opentelemetry::common::SpinLockMutex lock_;
LastValuePointData point_data_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LongSumAggregation : public Aggregation
PointType ToPoint() const noexcept override;

private:
opentelemetry::common::SpinLockMutex lock_;
mutable opentelemetry::common::SpinLockMutex lock_;
SumPointData point_data_;
};

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/metrics/aggregation/histogram_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ std::unique_ptr<Aggregation> LongHistogramAggregation::Diff(const Aggregation &n

PointType LongHistogramAggregation::ToPoint() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return point_data_;
}

Expand Down Expand Up @@ -176,6 +177,7 @@ std::unique_ptr<Aggregation> DoubleHistogramAggregation::Diff(

PointType DoubleHistogramAggregation::ToPoint() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return point_data_;
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/metrics/aggregation/lastvalue_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ std::unique_ptr<Aggregation> LongLastValueAggregation::Diff(const Aggregation &n

PointType LongLastValueAggregation::ToPoint() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return point_data_;
}

Expand Down Expand Up @@ -126,6 +127,7 @@ std::unique_ptr<Aggregation> DoubleLastValueAggregation::Diff(

PointType DoubleLastValueAggregation::ToPoint() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return point_data_;
}
} // namespace metrics
Expand Down
1 change: 1 addition & 0 deletions sdk/src/metrics/aggregation/sum_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ std::unique_ptr<Aggregation> LongSumAggregation::Diff(const Aggregation &next) c

PointType LongSumAggregation::ToPoint() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return point_data_;
}

Expand Down

0 comments on commit 402a0bd

Please sign in to comment.