Class: Airbrake::Stat

Inherits:
Object
  • Object
show all
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.

Examples:

stat = Airbrake::Stat.new
stat.increment_ms(2000)
stat.to_h # Pack and serialize data so it can be transmitted.

Since:

  • v3.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sum: 0.0, sumsq: 0.0, tdigest: TDigest.new(0.05)) ⇒ Stat

Returns a new instance of Stat.

Parameters:

  • sum (Float) (defaults to: 0.0)

    The sum of duration in milliseconds

  • sumsq (Float) (defaults to: 0.0)

    The squared sum of duration in milliseconds

  • tdigest (TDigest::TDigest) (defaults to: TDigest.new(0.05))

    Packed durations. By default, compression is 20

Since:

  • v3.2.0



23
24
25
26
27
28
# File 'lib/airbrake-ruby/stat.rb', line 23

def initialize(sum: 0.0, sumsq: 0.0, tdigest: TDigest.new(0.05))
  @sum = sum
  @sumsq = sumsq
  @tdigest = tdigest
  @mutex = Mutex.new
end

Instance Attribute Details

#sumObject

Since:

  • v3.2.0



17
18
19
# File 'lib/airbrake-ruby/stat.rb', line 17

def sum
  @sum
end

#sumsqObject

Since:

  • v3.2.0



17
18
19
# File 'lib/airbrake-ruby/stat.rb', line 17

def sumsq
  @sumsq
end

#tdigestObject

Since:

  • v3.2.0



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.

Parameters:

  • ms (Float)

Since:

  • v3.2.0



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

#inspectString 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.

Returns:

  • (String)

Since:

  • v3.2.0



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_hHash{String=>Object}

Returns stats as a hash with compressed TDigest (serialized as base64).

Returns:

  • (Hash{String=>Object})

    stats as a hash with compressed TDigest (serialized as base64)

Since:

  • v3.2.0



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