Class: WendelinClient

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

Overview

class representing a Wendelin client

Instance Method Summary collapse

Constructor Details

#initialize(streamtool_uri, credentials, log, ssl_timeout, open_timeout, read_timeout, keep_alive_timeout) ⇒ WendelinClient

‘streamtool_uri` - URI pointing to portal_input_data_stream “mountpoint” `credentials` # => _, ‘password’ => _ TODO change to certificate ‘log` - logger to use



30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/wendelin_client.rb', line 30

def initialize(streamtool_uri, credentials, log,
               ssl_timeout, open_timeout, read_timeout, keep_alive_timeout)
  @streamtool_uri     = streamtool_uri
  @credentials        = credentials
  @log                = log
  @ssl_timeout        = ssl_timeout
  @open_timeout       = open_timeout
  @read_timeout       = read_timeout
  @keep_alive_timeout = keep_alive_timeout
end

Instance Method Details

#ingest(reference, data_chunk) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fluent/plugin/wendelin_client.rb', line 123

def ingest(reference, data_chunk)
    uri = URI("#{@streamtool_uri}/ingest?reference=#{reference}")
    req = Net::HTTP::Post.new(uri)
    if @credentials.has_key?('user')
      req.basic_auth @credentials['user'], @credentials['password']
    end

    # When using 'application/x-www-form-urlencoded', Ruby encodes with regex
    # and it is far too slow. Such POST is legit:
    # https://stackoverflow.com/a/14710450
    req.body = data_chunk
    req.content_type = 'application/octet-stream'

    @log.on_trace do
        @log.trace '>>> REQUEST'
        @log.trace "method\t=> #{req.method}"
        @log.trace "path\t=> #{req.path}"
        @log.trace "uri\t=> #{req.uri}"
        @log.trace "body\t=> #{req.body}"
        @log.trace "body_stream\t=> #{req.body_stream}"
        req.each {|h| @log.trace "#{h}:\t#{req[h]}"}
        @log.trace
    end

    begin
        # TODO keep connection open (so that every new ingest does not do
        # full connect again)
        res = Net::HTTP.start(uri.hostname, uri.port,
                    :use_ssl      => (uri.scheme == 'https'),
                    # NOTE = "do not check server cert"
                    # TODO move this out to conf parameters
                    :verify_mode  => OpenSSL::SSL::VERIFY_NONE,

                    # Net::HTTP default open timeout is infinity, which results
                    # in thread hang forever if other side does not fully
                    # establish connection. Default read_timeout is 60 seconds.
                    # We go safe way and make sure all timeouts are defined.
                    :ssl_timeout  => @ssl_timeout,
                    :open_timeout => @open_timeout,
                    :read_timeout => @read_timeout,
              ) do |http|
            http.request(req)
        end

    rescue
        # some http/ssl/other connection error
        @log.warn "HTTP ERROR:"
        raise

    else
        @log.on_trace do
            @log.trace '>>> RESPONSE'
            res.each {|h| @log.trace "#{h}:\t#{res[h]}"}
            @log.trace "code\t=> #{res.code}"
            @log.trace "msg\t=> #{res.message}"
            @log.trace "class\t=> #{res.class}"
            @log.trace "body:", res.body
        end

        if res.kind_of?(Net::HTTPSuccess)       # res.code is 2XX
            #@log.info "ingested ok"
        else
            @log.warn "FAIL:"
            res.value
        end
    end
end

#ingest_with_keep_alive(reference, data_chunk) ⇒ Object

ingest ‘data_chunk` to a stream referenced as `reference`



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluent/plugin/wendelin_client.rb', line 62

def ingest_with_keep_alive(reference, data_chunk)
  uri = URI("#{@streamtool_uri}/ingest?reference=#{reference}")
  # call start_connection if http is undefined
  if ! defined? @http
    start_connection(uri)
  end

  # connect again if the connection is not started
  if ! @http.started?()
    start_connection(uri)
  end

  @request = Net::HTTP::Post.new(uri)

  # When using 'application/x-www-form-urlencoded', Ruby encodes with regex
  # and it is far too slow. Such POST is legit:
  # https://stackoverflow.com/a/14710450
  @request.body = data_chunk
  @request.content_type = 'application/octet-stream'

  if @credentials.has_key?('user')
    @request.basic_auth @credentials['user'], @credentials['password']
  end

  @log.on_trace do
    @log.trace '>>> REQUEST'
    @log.trace "method\t=> #{@request.method}"
    @log.trace "path\t=> #{@request.path}"
    @log.trace "uri\t=> #{@request.uri}"
    @log.trace "body\t=> #{@request.body}"
    @log.trace "body_stream\t=> #{@request.body_stream}"
    @request.each {|h| @log.trace "#{h}:\t#{@request[h]}"}
    @log.trace
  end

  begin
    res = @http.request(@request) # Net::HTTPResponse object
  end

  rescue
    # some http/ssl/other connection error
    @log.warn "HTTP ERROR:"
  raise
else
  @log.on_trace do
    @log.trace '>>> RESPONSE'
    res.each {|h| @log.trace "#{h}:\t#{res[h]}"}
    @log.trace "code\t=> #{res.code}"
    @log.trace "msg\t=> #{res.message}"
    @log.trace "class\t=> #{res.class}"
    @log.trace "body:", res.body
  end

  if res.kind_of?(Net::HTTPSuccess)       # res.code is 2XX
    #@log.info "ingested ok"
  else
    @log.warn "FAIL:"
    res.value
  end
end

#start_connection(uri) ⇒ Object

start request in an independent function to keep the connection open



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin/wendelin_client.rb', line 43

def start_connection(uri)

  @log.debug "start new connection"

  @http = Net::HTTP.start(uri.hostname, uri.port,
    :use_ssl => (uri.scheme == 'https'),
    :verify_mode  => OpenSSL::SSL::VERIFY_NONE,

    # Net::HTTP default open timeout is infinity, which results
    # in thread hang forever if other side does not fully
    # establish connection. Default read_timeout is 60 seconds.
    # We go safe way and make sure all timeouts are defined.
    :ssl_timeout  => @ssl_timeout,
    :open_timeout => @open_timeout,
    :read_timeout => @read_timeout,
    :keep_alive_timeout => @keep_alive_timeout,)
end