Skip to content

Commit

Permalink
Merge pull request #491 from airbrake/faster-dumping-for-performance-…
Browse files Browse the repository at this point in the history
…stats

stat: define custom #inspect
  • Loading branch information
kyrylo authored Jul 9, 2019
2 parents 5e7e93b + 62b0d83 commit 289fbf1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Airbrake Ruby Changelog

* Improved performance of `PerformanceNotifier` (sic!)
([#490](https://github.com/airbrake/airbrake-ruby/pull/490))
* Improved performance of `Airbrake::Stat` when logging (or inspecting)
([#491](https://github.com/airbrake/airbrake-ruby/pull/491))

### [v4.5.0][v4.5.0] (June 25, 2019)

Expand Down
11 changes: 11 additions & 0 deletions lib/airbrake-ruby/stat.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'base64'

# rubocop:disable Metrics/BlockLength
module Airbrake
# Stat is a data structure that allows accumulating performance data (route
# performance, SQL query performance and such). It's powered by TDigests.
Expand Down Expand Up @@ -58,5 +59,15 @@ def increment_ms(ms)

tdigest.push(ms)
end

# We define custom inspect so that we weed out uninformative TDigest, which
# is also very slow to dump when we log Airbrake::Stat.
#
# @return [String]
def inspect
"#<struct Airbrake::Stat count=#{count}, sum=#{sum}, sumsq=#{sumsq}>"
end
alias_method :pretty_print, :inspect
end
end
# rubocop:enable Metrics/BlockLength
14 changes: 14 additions & 0 deletions spec/stat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@
expect(subject.tdigest.size).to eq(1)
end
end

describe "#inspect" do
it "provides custom inspect output" do
expect(subject.inspect).to eq(
'#<struct Airbrake::Stat count=0, sum=0.0, sumsq=0.0>'
)
end
end

describe "#pretty_print" do
it "is an alias of #inspect" do
expect(subject.method(:pretty_print)).to eql(subject.method(:inspect))
end
end
end

0 comments on commit 289fbf1

Please sign in to comment.