Module: Statsd::Forwarder

Defined in:
lib/statsd/forwarder.rb

Constant Summary collapse

@@sockets =
{}
@@destinations =
[]

Class Method Summary collapse

Class Method Details

.build_fresh_socketsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/statsd/forwarder.rb', line 30

def self.build_fresh_sockets
  # Reset destinations to those destinations for which we could
  # actually get a socket going.
  @@sockets.clear
  @@destinations = @@destinations.select do |destination|
    begin
      s = UDPSocket.new(Socket::AF_INET)
      s.connect destination['hostname'], destination['port']
      @@sockets[destination] = s
      true
    rescue SocketError => e
      puts "ERROR: Couldn't create a socket to #{destination['hostname']}/#{destination['port']}. Pruning destination from Forwarder. (#{e.inspect})"
      false
    end
  end
end

.destinationsObject



13
# File 'lib/statsd/forwarder.rb', line 13

def self.destinations; @@destinations; end

.destinations=(list) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/statsd/forwarder.rb', line 14

def self.destinations=(list)
  raise ArgumentError unless list.is_a?(Array)
  @@destinations = list
end

.receive_data(msg) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/statsd/forwarder.rb', line 19

def self.receive_data(msg)
  # Broadcast the incoming message to all the forwarding destinations.
  @@sockets.each do |destination, socket|
    begin
      socket.send(msg, 0)
    rescue SocketError, Errno::ECONNREFUSED => e
      puts "ERROR: Couldn't send message to #{destination}. Stopping this output.(#{e.inspect})"
      @@sockets.delete(destination)
    end
  end
end

.set_destinations(destinations) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/statsd/forwarder.rb', line 46

def self.set_destinations(destinations)
  raise ArgumentError unless destinations.is_a?(Array)
  raise ArgumentError unless destinations.map { |d| d.keys }.flatten.uniq.sort == ['hostname', 'port']
  @@destinations = destinations
end

.socketsObject



8
# File 'lib/statsd/forwarder.rb', line 8

def self.sockets; @@sockets; end

.sockets=(hash) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/statsd/forwarder.rb', line 9

def self.sockets=(hash)
  raise ArgumentError unless hash.is_a?(Hash)
  @@sockets = hash
end