Skip to content

Commit

Permalink
Merge pull request #560 from airbrake/545-hash-iteration-fix
Browse files Browse the repository at this point in the history
stat: synchronize `to_h` with `increment_ms`
  • Loading branch information
kyrylo authored Mar 2, 2020
2 parents 13dfd0c + d1d3e0d commit 9f47ff9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Airbrake Ruby Changelog
([#562](https://github.com/airbrake/airbrake-ruby/pull/562))
* Fixed addition of duplicate filters on multiple `Airbrake.configure` calls
([#563](https://github.com/airbrake/airbrake-ruby/pull/563))
* Improved thread-safety of `Airbrake::Stat`
([#560](https://github.com/airbrake/airbrake-ruby/pull/560))

### [v4.13.2][v4.13.2] (February 21, 2020)

Expand Down
25 changes: 15 additions & 10 deletions lib/airbrake-ruby/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ def initialize(sum: 0.0, sumsq: 0.0, tdigest: TDigest.new(0.05))
@sum = sum
@sumsq = sumsq
@tdigest = tdigest
@mutex = Mutex.new
end

# @return [Hash{String=>Object}] stats as a hash with compressed TDigest
# (serialized as base64)
def to_h
tdigest.compress!
{
'count' => tdigest.size,
'sum' => sum,
'sumsq' => sumsq,
'tdigest' => Base64.strict_encode64(tdigest.as_small_bytes),
}
@mutex.synchronize do
tdigest.compress!
{
'count' => tdigest.size,
'sum' => sum,
'sumsq' => sumsq,
'tdigest' => Base64.strict_encode64(tdigest.as_small_bytes),
}
end
end

# Increments tdigest timings and updates tdigest with the difference between
Expand All @@ -54,10 +57,12 @@ def increment(start_time, end_time = nil)
# @param [Float] ms
# @return [void]
def increment_ms(ms)
self.sum += ms
self.sumsq += ms * ms
@mutex.synchronize do
self.sum += ms
self.sumsq += ms * ms

tdigest.push(ms)
tdigest.push(ms)
end
end

# We define custom inspect so that we weed out uninformative TDigest, which
Expand Down

0 comments on commit 9f47ff9

Please sign in to comment.