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



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

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



34
35
36
# File 'lib/graphite-api/client.rb', line 34

def check!
  connectors.check!
end

#every(interval, &block) ⇒ Object



38
39
40
# File 'lib/graphite-api/client.rb', line 38

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

#increment(*keys) ⇒ Object



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

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



57
58
59
# File 'lib/graphite-api/client.rb', line 57

def join
  sleep while buffer.new_records?
end

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



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

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
30
31
# File 'lib/graphite-api/client.rb', line 27

def timers
  @timers ||= Timers::Group.new.tap { |t| Thread.new { loop {
    t.wait { |n| sleep(n.nil? ? 0.1 : [n, 0].max) }
  } } }
end