Class: Metrix::Graphite

Inherits:
Object
  • Object
show all
Defined in:
lib/metrix/graphite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 2003) ⇒ Graphite

Returns a new instance of Graphite.



8
9
10
11
# File 'lib/metrix/graphite.rb', line 8

def initialize(host, port = 2003)
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/metrix/graphite.rb', line 6

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/metrix/graphite.rb', line 6

def port
  @port
end

Instance Method Details

#<<(metric) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/metrix/graphite.rb', line 13

def <<(metric)
  metric.metrics.each do |m|
    logger.debug "adding #{m.to_graphite}"
    buffers << m.to_graphite
    flush if buffers.count > 90
  end
end

#buffersObject



21
22
23
# File 'lib/metrix/graphite.rb', line 21

def buffers
  @buffers ||= []
end

#flushObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/metrix/graphite.rb', line 25

def flush
  if buffers.empty?
    logger.info "nothing to send"
    return
  end
  started = Time.now
  Socket.tcp(@host, @port) do |socket|
    socket.puts(buffers.join("\n"))
  end
  logger.info "sent #{buffers.count} in %.06fs" % [Time.now - started]
  buffers.clear
end

#loggerObject



38
39
40
# File 'lib/metrix/graphite.rb', line 38

def logger
  Metrix.logger
end