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: define custom #inspect #491

Merged
merged 1 commit into from
Jul 9, 2019
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 @@ -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