class ElbPing::Stats

Tracks the statistics of requests sent, responses received (hence loss) and latency

Attributes

nodes[R]
total[R]

Public Class Methods

new() click to toggle source
# File lib/elbping/stats.rb, line 10
def initialize
  @total = {
    :requests   =>  0,
    :responses  =>  0,
    :latencies  => LatencyBucket.new,
  }
  @nodes = {}
end

Public Instance Methods

add_node(node) click to toggle source

Initializes stats buckets for a node if it doesn’t already exist

Arguments

  • node: (string) IP of node

# File lib/elbping/stats.rb, line 24
def add_node(node)
  unless @nodes.keys.include? node
    @nodes[node] = {
      :requests   =>  0,
      :responses  =>  0,
      :latencies  => LatencyBucket.new,
    }
  end
end
node_loss(node) click to toggle source

Calculates loss for a specific node

Arguments:

  • node: (string) IP of node

TODO: Handle non-existent nodes

# File lib/elbping/stats.rb, line 73
def node_loss(node)
  calc_loss @nodes[node][:responses], @nodes[node][:requests]
end
register(status) click to toggle source

Registers stats following a ping

Arguments:

  • node: (string) IP of node

  • status: (hash) Status object as returned from Pinger::ping_node

# File lib/elbping/stats.rb, line 40
def register(status)
  node = status[:node]
  # Register the node if it hasn't been already
  add_node node

  # Update requests sent regardless of errors
  @total[:requests] += 1
  @nodes[node][:requests] += 1

  # Don't update response counters or latencies if we encountered an error
  unless [:timeout, :econnrefused, :exception].include? status[:code]
    # Increment counters
    @total[:responses] += 1
    @nodes[node][:responses] += 1

    # Track latencies
    @total[:latencies] << status[:duration]
    @nodes[node][:latencies] << status[:duration]
  end
end
total_loss() click to toggle source

Calculates loss across all nodes

# File lib/elbping/stats.rb, line 62
def total_loss
  calc_loss @total[:responses], @total[:requests]
end

Private Instance Methods

calc_loss(responses, requests) click to toggle source

Generic function to calculate loss as a per-1 float

Arguments:

  • responses: (number) How many responses were received (numerator)

  • requests: (number) How many requests were sent (denominator)

# File lib/elbping/stats.rb, line 85
def calc_loss(responses, requests)
  1 - (responses.to_f/requests)
end