Class: Kurchatov::Riemann::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kurchatov/riemann/client.rb

Constant Summary collapse

CONNECT_TIMEOUT =
5
SEND_TIMEOUT =
5
RIEMANN_PORT =
5555

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client



15
16
17
18
19
# File 'lib/kurchatov/riemann/client.rb', line 15

def initialize(opts = {})
  @host = opts[:host]
  @port = opts[:port] || RIEMANN_PORT
  @mutex = Mutex.new
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/kurchatov/riemann/client.rb', line 9

def host
  @host
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/kurchatov/riemann/client.rb', line 9

def port
  @port
end

Instance Method Details

#<<(events) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kurchatov/riemann/client.rb', line 21

def <<(events)
  events = events.map { |e| Event.new(e) }
  message = Message.new(:events => events)
  with_connection do |socket|
    x = message.encode_with_length
    Timeout::timeout(SEND_TIMEOUT) {
      socket.write(x)
      socket.flush
    }
  end
end

#connectObject



39
40
41
42
43
# File 'lib/kurchatov/riemann/client.rb', line 39

def connect
  Timeout::timeout(CONNECT_TIMEOUT) {
    @socket ||= TCPSocket.new(@host, @port)
  }
end

#with_connectionObject



33
34
35
36
37
# File 'lib/kurchatov/riemann/client.rb', line 33

def with_connection
  @mutex.synchronize do
    yield(@socket || connect)
  end
end