Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat: synchronize to_h with increment_ms #560

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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