Class: ElbPing::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/elbping/stats.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



10
11
12
13
14
15
16
17
# File 'lib/elbping/stats.rb', line 10

def initialize
  @total = {
    :requests   =>  0,
    :responses  =>  0,
    :latencies  => LatencyBucket.new,
  }
  @nodes = {}
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



8
9
10
# File 'lib/elbping/stats.rb', line 8

def nodes
  @nodes
end

#totalObject (readonly)

Returns the value of attribute total.



8
9
10
# File 'lib/elbping/stats.rb', line 8

def total
  @total
end

Instance Method Details

#add_node(node) ⇒ Object

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

Arguments

  • node: (string) IP of node



24
25
26
27
28
29
30
31
32
# 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) ⇒ Object

Calculates loss for a specific node

Arguments:

  • node: (string) IP of node

TODO: Handle non-existent nodes



73
74
75
# File 'lib/elbping/stats.rb', line 73

def node_loss(node)
  calc_loss @nodes[node][:responses], @nodes[node][:requests]
end

#register(status) ⇒ Object

Registers stats following a ping

Arguments:

  • node: (string) IP of node

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_lossObject

Calculates loss across all nodes



62
63
64
# File 'lib/elbping/stats.rb', line 62

def total_loss
  calc_loss @total[:responses], @total[:requests]
end