class GraphiteAPI::Client
Public Class Methods
new(opt)
click to toggle source
# File lib/graphite-api/client.rb, line 9 def initialize opt @options = build_options validate opt.clone @buffer = GraphiteAPI::Buffer.new options @connectors = GraphiteAPI::Connector::Group.new options Zscheduler.every(options[:interval]){ send_metrics } unless options[:direct] end
Public Instance Methods
every(interval, &block)
click to toggle source
# File lib/graphite-api/client.rb, line 20 def every interval, &block Zscheduler.every( interval ) { block.arity == 1 ? block.call(self) : block.call } end
increment(*keys)
click to toggle source
increment keys
increment(“key1”,“key2”)
> metrics(“key1” => 1, “key2” => 1)¶ ↑
increment(“key1”,“key2”, {:by => 999})
> metrics(“key1” => 999, “key2” => 999)¶ ↑
increment(“key1”,“key2”, {:time => Time.at(123456)})
> metrics({“key1” => 1, “key2” => 1},Time.at(123456))¶ ↑
# File lib/graphite-api/client.rb, line 42 def increment(*keys) opt = {} opt.merge! keys.pop if keys.last.is_a? Hash by = opt.fetch(:by,1) time = opt.fetch(:time,Time.now) metric = keys.inject({}) {|h,k| h.tap { h[k] = by}} metrics(metric, time) end
join()
click to toggle source
# File lib/graphite-api/client.rb, line 51 def join sleep while buffer.new_records? end
method_missing(m, *args, &block)
click to toggle source
# File lib/graphite-api/client.rb, line 55 def method_missing m, *args, &block Proxy.new( self ).send(m,*args,&block) end
metrics(metric, time = Time.now)
click to toggle source
# File lib/graphite-api/client.rb, line 24 def metrics metric, time = Time.now return if metric.empty? buffer.push :metric => metric, :time => time send_metrics if options[:direct] end
Also aliased as: add_metrics
Protected Instance Methods
build_options(opt)
click to toggle source
# File lib/graphite-api/client.rb, line 85 def build_options opt default_options.tap do |options_hash| options_hash[:backends].push expand_host opt.delete :graphite options_hash.merge! opt options_hash[:direct] = options_hash[:interval] == 0 options_hash[:slice] = 1 if options_hash[:direct] end end
send_metrics()
click to toggle source
# File lib/graphite-api/client.rb, line 94 def send_metrics connectors.publish buffer.pull :string if buffer.new_records? end
validate(options)
click to toggle source
# File lib/graphite-api/client.rb, line 79 def validate options options.tap do |opt| raise ArgumentError.new ":graphite must be specified" if opt[:graphite].nil? end end