Class: Naplug::Helpers::ENGraphite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/naplug/helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/naplug/helpers.rb', line 39

def initialize(options)
  raise ArgumentError, 'missing graphite server address' if options[:graphite].nil?
  @graphite = options[:graphite]
  @port = options[:port].nil? ? 2003 : options[:port].to_i
  @prefix = options[:prefix].nil? ? '' : options[:prefix]
  @metrics = []
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



37
38
39
# File 'lib/naplug/helpers.rb', line 37

def metrics
  @metrics
end

Instance Method Details

#flush!(options = { :timeout => 3}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/naplug/helpers.rb', line 58

def flush!(options = { :timeout => 3})
  begin
    Timeout.timeout(options[:timeout]) do
      s = TCPSocket.open(@graphite,@port)
      @metrics.each do |metric|
        metric = "#{@prefix}.#{metric.to_s}\n"
        s.write metric
      end
      s.close
    end
  rescue Timeout::Error => e
    raise Naplug::Error, "graphite timeout (#{options[:timeout]}s)"
  rescue Errno::ECONNREFUSED, SocketError => e
    raise Naplug::Error, 'graphite socket error'
  end
end

#metric(path, value, time = Time.now) ⇒ Object



47
48
49
# File 'lib/naplug/helpers.rb', line 47

def metric path, value, time = Time.now
  @metrics.push(Metric.new(path,value,time))
end

#metrics!Object



51
52
53
54
55
56
# File 'lib/naplug/helpers.rb', line 51

def metrics!
  @metrics.each do |metric|
    metric = "#{@prefix}.#{metric.to_s}\n"
    print metric
  end
end