Class: Fluent::SocketLike

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/metrics_backends.rb

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ SocketLike

Returns a new instance of SocketLike.



58
59
60
61
62
63
64
65
66
# File 'lib/fluent/metrics_backends.rb', line 58

def initialize(parameters)
  @parameters = parameters
  # Is this actually needed here?
  @socket = nil
  if @parameters['proto'] =~ /^http/
    require 'net/http'
    require 'net/https' if @paramters['proto'] == 'https'
  end
end

Instance Method Details

#closeObject



50
51
52
53
54
55
56
# File 'lib/fluent/metrics_backends.rb', line 50

def close
  if @parameters['proto'] =~ /^http/
    @socket.finish
  else
    @socket.close
  end
end

#openObject

Just a little something to try to make any possible output bakced act like a socket so we don’t have to check the type and change method colls throughout the code.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/metrics_backends.rb', line 8

def open
  if @parameters.nil? or @parameters.empty?
    raise RuntimeError, "SocketLike open called with no parameters set"
  else
    if @parameters['proto'] == 'tcp'
      @socket = TCPSocket.new(
        @parameters['host'],
        @parameters['port']
      )
    elsif @parameters['proto'] == 'udp'
      @socket = UDPSocket.new(
        @parameters['host'],
        @parameters['port']
      )
    elsif @parameters['proto'] == 'unix'
      @socket = UNIXsocket.new(@parameters['path'])
    elsif @parameters['proto'] == 'file'
      @socket = File.new(@parameters['path'],'a')
    elsif @parameters['proto'] =~ /^http/
      @socket = Net::HTTP.new(@parameters['host'],@parameters['port'])
      @socket.use_ssl = @parameters['proto'] == 'https'
    else
      raise ArgumentError, 'SocketLike class does not support protocol' + @parameters['proto']
    end
  end
end

#write(string) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/metrics_backends.rb', line 35

def write(string)
  if @parameters['proto'] =~ /^http/
    req = Net::HTTP::Post.new(@parameters['path'])
    req.body = string
    @parameters['headers'].each do |h|
      req.add_field(h[0],h[1])
    end
    #@socket.request(req) or raise IOError "Error writing to backend"
    @socket.request(req)
  else
    #@socket.write(string) or raise IOError "Error writing to backend"
    @socket.write(string)
  end
end