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)


60
61
62
63
64
65
66
# File 'lib/naplug/helpers.rb', line 60

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.



58
59
60
# File 'lib/naplug/helpers.rb', line 58

def metrics
  @metrics
end

Instance Method Details

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/naplug/helpers.rb', line 79

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



68
69
70
# File 'lib/naplug/helpers.rb', line 68

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

#metrics!Object



72
73
74
75
76
77
# File 'lib/naplug/helpers.rb', line 72

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