Class: Vmpooler::Metrics::Graphite

Inherits:
Vmpooler::Metrics show all
Defined in:
lib/vmpooler/metrics/graphite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Vmpooler::Metrics

init

Constructor Details

#initialize(logger, params = {}) ⇒ Graphite

rubocop:disable Lint/MissingSuper

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/vmpooler/metrics/graphite.rb', line 11

def initialize(logger, params = {})
  raise ArgumentError, "Graphite server is required. Config: #{params.inspect}" if params['server'].nil? || params['server'].empty?

  @server = params['server']
  @port   = params['port'] || 2003
  @prefix = params['prefix'] || 'vmpooler'
  @logger = logger
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/vmpooler/metrics/graphite.rb', line 8

def port
  @port
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/vmpooler/metrics/graphite.rb', line 8

def prefix
  @prefix
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/vmpooler/metrics/graphite.rb', line 8

def server
  @server
end

Instance Method Details

#gauge(label, value) ⇒ Object



25
26
27
# File 'lib/vmpooler/metrics/graphite.rb', line 25

def gauge(label, value)
  log label, value
end

#increment(label) ⇒ Object

rubocop:enable Lint/MissingSuper



21
22
23
# File 'lib/vmpooler/metrics/graphite.rb', line 21

def increment(label)
  log label, 1
end

#log(path, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vmpooler/metrics/graphite.rb', line 33

def log(path, value)
  Thread.new do
    socket = TCPSocket.new(server, port)
    begin
      socket.puts "#{prefix}.#{path} #{value} #{Time.now.to_i}"
    ensure
      socket.close
    end
  end
rescue Errno::EADDRNOTAVAIL => e
  warn "Could not assign address to graphite server #{server}: #{e}"
rescue StandardError => e
  @logger.log('s', "[!] Failure logging #{path} to graphite server [#{server}:#{port}]: #{e}")
end

#timing(label, duration) ⇒ Object



29
30
31
# File 'lib/vmpooler/metrics/graphite.rb', line 29

def timing(label, duration)
  log label, duration
end