Class: Naplug::Helpers::ENGraphite::Client
- Inherits:
-
Object
- Object
- Naplug::Helpers::ENGraphite::Client
- Defined in:
- lib/naplug/helpers.rb
Instance Attribute Summary collapse
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
Instance Method Summary collapse
- #flush!(options = { :timeout => 3}) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #metric(path, value, time = Time.now) ⇒ Object
- #metrics! ⇒ Object
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
39 40 41 42 43 44 45 |
# File 'lib/naplug/helpers.rb', line 39 def initialize() raise ArgumentError, 'missing graphite server address' if [:graphite].nil? @graphite = [:graphite] @port = [:port].nil? ? 2003 : [:port].to_i @prefix = [:prefix].nil? ? '' : [:prefix] @metrics = [] end |
Instance Attribute Details
#metrics ⇒ Object (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!( = { :timeout => 3}) begin Timeout.timeout([: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 |