Class: Metrix::TcpReporter

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

Direct Known Subclasses

Graphite, OpenTSDB

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 4242, window_size = nil) ⇒ TcpReporter

Returns a new instance of TcpReporter.



5
6
7
8
9
# File 'lib/metrix/tcp_reporter.rb', line 5

def initialize(host, port = 4242, window_size = nil)
  @host = host
  @port = port
  @window_size = window_size
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/metrix/tcp_reporter.rb', line 3

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/metrix/tcp_reporter.rb', line 3

def port
  @port
end

Instance Method Details

#<<(metric) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/metrix/tcp_reporter.rb', line 19

def <<(metric)
  metric.metrics.each do |m|
    line = serialize_metric(m)
    Metrix.logger.debug "buffering #{line}"
    buffers << line
    flush if buffers.count >= window_size
  end
rescue => err
  logger.error "#{err.message} #{err.inspect}"
end

#buffersObject



15
16
17
# File 'lib/metrix/tcp_reporter.rb', line 15

def buffers
  @buffers ||= []
end

#flushObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metrix/tcp_reporter.rb', line 30

def flush
  Timeout.timeout(1) do
    return if buffers.empty?
    cnt = buffers.count
    Metrix.logger.info "sending #{cnt} to #{@host}:#{@port}"
    ms = Benchmark.measure do
      Socket.tcp(@host, @port) do |socket|
        buffers.each do |line|
          socket.puts line
        end
        socket.flush
      end
    end
    logger.info "sent %d metrics in %.06f" % [cnt, ms.real]
  end
ensure
  buffers.clear
end

#loggerObject



49
50
51
# File 'lib/metrix/tcp_reporter.rb', line 49

def logger
  Metrix.logger
end

#window_sizeObject



11
12
13
# File 'lib/metrix/tcp_reporter.rb', line 11

def window_size
  @window_size || 1000
end