Class: GraphiteAPI::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphite-api/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
# File 'lib/graphite-api/client.rb', line 18

def initialize opt
  @options = build_options validate opt.clone
  @buffer  = GraphiteAPI::Buffer.new options, timers
  @connectors = GraphiteAPI::Connector::Group.new options
  @mu = Mutex.new

  timers.every(options[:interval], true, &method(:send_metrics!)) unless options[:direct]
end

Class Method Details

.default_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphite-api/client.rb', line 59

def self.default_options
  {
    :backends => [],
    :cleaner_interval => 43200,
    :port => 2003,
    :log_level => :info,
    :cache => nil,
    :host => "localhost",
    :prefix => [],
    :interval => 0,
    :slice => 60,
    :pid => "/tmp/graphite-middleware.pid",
    :default_aggregation_method => :sum
  }
end

Instance Method Details

#check!Object

throw exception on Socket error



32
33
34
# File 'lib/graphite-api/client.rb', line 32

def check!
  connectors.check!
end

#every(interval, &block) ⇒ Object



36
37
38
# File 'lib/graphite-api/client.rb', line 36

def every interval, &block
  @timers.every(interval) { block.arity == 1 ? block.call(self) : block.call }
end

#increment(*keys) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/graphite-api/client.rb', line 46

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.merge k => by }
  metrics(metric, time)
end

#joinObject



55
56
57
# File 'lib/graphite-api/client.rb', line 55

def join
  sleep while buffer.new_records?
end

#metrics(metric, time = nil, aggregation_method = nil) ⇒ Object



40
41
42
43
44
# File 'lib/graphite-api/client.rb', line 40

def metrics metric, time = nil, aggregation_method = nil
  return if metric.empty?
  buffer.push :metric => metric, :time => (time || Time.now), :aggregation_method => aggregation_method
  send_metrics! if options[:direct]
end

#timersObject



27
28
29
# File 'lib/graphite-api/client.rb', line 27

def timers
  @timers ||= Timers::Group.new.tap {|t| Thread.new { loop { t.wait } } }
end