Class: Metrix::TcpReporter
- Inherits:
-
Object
- Object
- Metrix::TcpReporter
- Defined in:
- lib/metrix/tcp_reporter.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #<<(metric) ⇒ Object
- #buffers ⇒ Object
- #flush ⇒ Object
-
#initialize(host, port = 4242, window_size = nil) ⇒ TcpReporter
constructor
A new instance of TcpReporter.
- #logger ⇒ Object
- #window_size ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'lib/metrix/tcp_reporter.rb', line 3 def host @host end |
#port ⇒ Object (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.} #{err.inspect}" end |
#buffers ⇒ Object
15 16 17 |
# File 'lib/metrix/tcp_reporter.rb', line 15 def buffers @buffers ||= [] end |
#flush ⇒ Object
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 |
#window_size ⇒ Object
11 12 13 |
# File 'lib/metrix/tcp_reporter.rb', line 11 def window_size @window_size || 1000 end |