Class: Airbrake::Stat
- Inherits:
-
Object
- Object
- Airbrake::Stat
- Defined in:
- lib/airbrake-ruby/stat.rb
Overview
Stat is a data structure that allows accumulating performance data (route performance, SQL query performance and such). It’s powered by TDigests.
Usually, one Stat corresponds to one metric (route or query, etc.). Incrementing a stat means pushing new performance statistics.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#increment_ms(ms) ⇒ void
Increments tdigest timings and updates tdigest with given
ms
value. -
#initialize(sum: 0.0, sumsq: 0.0, tdigest: TDigest.new(0.05)) ⇒ Stat
constructor
A new instance of Stat.
-
#inspect ⇒ String
(also: #pretty_print)
We define custom inspect so that we weed out uninformative TDigest, which is also very slow to dump when we log Airbrake::Stat.
-
#to_h ⇒ Hash{String=>Object}
Stats as a hash with compressed TDigest (serialized as base64).
Constructor Details
Instance Attribute Details
#sum ⇒ Object
17 18 19 |
# File 'lib/airbrake-ruby/stat.rb', line 17 def sum @sum end |
#sumsq ⇒ Object
17 18 19 |
# File 'lib/airbrake-ruby/stat.rb', line 17 def sumsq @sumsq end |
#tdigest ⇒ Object
17 18 19 |
# File 'lib/airbrake-ruby/stat.rb', line 17 def tdigest @tdigest end |
Instance Method Details
#increment_ms(ms) ⇒ void
This method returns an undefined value.
Increments tdigest timings and updates tdigest with given ms
value.
48 49 50 51 52 53 54 55 |
# File 'lib/airbrake-ruby/stat.rb', line 48 def increment_ms(ms) @mutex.synchronize do self.sum += ms self.sumsq += ms * ms tdigest.push(ms) end end |
#inspect ⇒ String Also known as: pretty_print
We define custom inspect so that we weed out uninformative TDigest, which is also very slow to dump when we log Airbrake::Stat.
61 62 63 |
# File 'lib/airbrake-ruby/stat.rb', line 61 def inspect "#<struct Airbrake::Stat count=#{tdigest.size}, sum=#{sum}, sumsq=#{sumsq}>" end |
#to_h ⇒ Hash{String=>Object}
Returns stats as a hash with compressed TDigest (serialized as base64).
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/airbrake-ruby/stat.rb', line 32 def to_h @mutex.synchronize do tdigest.compress! { 'count' => tdigest.size, 'sum' => sum, 'sumsq' => sumsq, 'tdigest' => Base64.strict_encode64(tdigest.as_small_bytes), } end end |