fluent-plugin-out-http 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.travis.yml +25 -25
- data/CHANGELOG.md +75 -72
- data/Gemfile +4 -4
- data/ISSUE_TEMPLATE.md +21 -21
- data/LICENSE.txt +11 -11
- data/README.md +47 -45
- data/Rakefile +11 -11
- data/fluent-plugin-out-http.gemspec +25 -25
- data/lib/fluent/plugin/out_http.rb +314 -297
- data/lib/fluent/test/http_output_test.rb +42 -42
- data/test/plugin/script/plugin/formatter_test.rb +21 -21
- data/test/plugin/test_out_http.rb +872 -771
- metadata +3 -4
@@ -1,297 +1,314 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'uri'
|
3
|
-
require 'yajl'
|
4
|
-
require 'fluent/plugin/output'
|
5
|
-
require '
|
6
|
-
|
7
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
config_param :
|
62
|
-
config_param :
|
63
|
-
|
64
|
-
config_param :
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
config_set_default
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
if @
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
def
|
112
|
-
super
|
113
|
-
end
|
114
|
-
|
115
|
-
def
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
def
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
elsif @serializer == :
|
127
|
-
|
128
|
-
|
129
|
-
req
|
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
|
-
req.body
|
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
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
end
|
254
|
-
|
255
|
-
def
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
def
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'yajl'
|
4
|
+
require 'fluent/plugin/output'
|
5
|
+
require 'tempfile'
|
6
|
+
require 'openssl'
|
7
|
+
require 'zlib'
|
8
|
+
|
9
|
+
class Fluent::Plugin::HTTPOutput < Fluent::Plugin::Output
|
10
|
+
Fluent::Plugin.register_output('http', self)
|
11
|
+
|
12
|
+
class RecoverableResponse < StandardError; end
|
13
|
+
|
14
|
+
helpers :compat_parameters, :formatter
|
15
|
+
|
16
|
+
DEFAULT_BUFFER_TYPE = "memory"
|
17
|
+
DEFAULT_FORMATTER = "json"
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
# Endpoint URL ex. http://localhost.local/api/
|
24
|
+
config_param :endpoint_url, :string
|
25
|
+
|
26
|
+
# Set Net::HTTP.verify_mode to `OpenSSL::SSL::VERIFY_NONE`
|
27
|
+
config_param :ssl_no_verify, :bool, :default => false
|
28
|
+
|
29
|
+
# HTTP method
|
30
|
+
config_param :http_method, :enum, list: [:get, :put, :post, :delete], :default => :post
|
31
|
+
|
32
|
+
# form | json | text | raw
|
33
|
+
config_param :serializer, :enum, list: [:json, :form, :text, :raw], :default => :form
|
34
|
+
|
35
|
+
# Simple rate limiting: ignore any records within `rate_limit_msec`
|
36
|
+
# since the last one.
|
37
|
+
config_param :rate_limit_msec, :integer, :default => 0
|
38
|
+
|
39
|
+
# Raise errors that were rescued during HTTP requests?
|
40
|
+
config_param :raise_on_error, :bool, :default => true
|
41
|
+
|
42
|
+
# Specify recoverable error codes
|
43
|
+
config_param :recoverable_status_codes, :array, value_type: :integer, default: [503]
|
44
|
+
|
45
|
+
# ca file to use for https request
|
46
|
+
config_param :cacert_file, :string, :default => ''
|
47
|
+
|
48
|
+
# specify client sertificate
|
49
|
+
config_param :client_cert_path, :string, :default => ''
|
50
|
+
|
51
|
+
# specify private key path
|
52
|
+
config_param :private_key_path, :string, :default => ''
|
53
|
+
|
54
|
+
# specify private key passphrase
|
55
|
+
config_param :private_key_passphrase, :string, :default => '', :secret => true
|
56
|
+
|
57
|
+
# custom headers
|
58
|
+
config_param :custom_headers, :hash, :default => nil
|
59
|
+
|
60
|
+
# 'none' | 'basic' | 'jwt' | 'bearer'
|
61
|
+
config_param :authentication, :enum, list: [:none, :basic, :jwt, :bearer], :default => :none
|
62
|
+
config_param :username, :string, :default => ''
|
63
|
+
config_param :password, :string, :default => '', :secret => true
|
64
|
+
config_param :token, :string, :default => ''
|
65
|
+
# Switch non-buffered/buffered plugin
|
66
|
+
config_param :buffered, :bool, :default => false
|
67
|
+
config_param :bulk_request, :bool, :default => false
|
68
|
+
# Compress with gzip except for form serializer
|
69
|
+
config_param :compress_request, :bool, :default => false
|
70
|
+
|
71
|
+
config_section :buffer do
|
72
|
+
config_set_default :@type, DEFAULT_BUFFER_TYPE
|
73
|
+
config_set_default :chunk_keys, ['tag']
|
74
|
+
end
|
75
|
+
|
76
|
+
config_section :format do
|
77
|
+
config_set_default :@type, DEFAULT_FORMATTER
|
78
|
+
end
|
79
|
+
|
80
|
+
def configure(conf)
|
81
|
+
compat_parameters_convert(conf, :buffer, :formatter)
|
82
|
+
super
|
83
|
+
|
84
|
+
@ssl_verify_mode = if @ssl_no_verify
|
85
|
+
OpenSSL::SSL::VERIFY_NONE
|
86
|
+
else
|
87
|
+
OpenSSL::SSL::VERIFY_PEER
|
88
|
+
end
|
89
|
+
|
90
|
+
@ca_file = @cacert_file
|
91
|
+
@last_request_time = nil
|
92
|
+
raise Fluent::ConfigError, "'tag' in chunk_keys is required." if !@chunk_key_tag && @buffered
|
93
|
+
|
94
|
+
if @formatter_config = conf.elements('format').first
|
95
|
+
@formatter = formatter_create
|
96
|
+
end
|
97
|
+
|
98
|
+
if @bulk_request
|
99
|
+
class << self
|
100
|
+
alias_method :format, :bulk_request_format
|
101
|
+
end
|
102
|
+
@formatter = formatter_create(type: :json)
|
103
|
+
@serializer = :x_ndjson # secret settings for bulk_request
|
104
|
+
else
|
105
|
+
class << self
|
106
|
+
alias_method :format, :split_request_format
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def start
|
112
|
+
super
|
113
|
+
end
|
114
|
+
|
115
|
+
def shutdown
|
116
|
+
super
|
117
|
+
end
|
118
|
+
|
119
|
+
def format_url(tag, time, record)
|
120
|
+
@endpoint_url
|
121
|
+
end
|
122
|
+
|
123
|
+
def set_body(req, tag, time, record)
|
124
|
+
if @serializer == :json
|
125
|
+
set_json_body(req, record)
|
126
|
+
elsif @serializer == :text
|
127
|
+
set_text_body(req, record)
|
128
|
+
elsif @serializer == :raw
|
129
|
+
set_raw_body(req, record)
|
130
|
+
elsif @serializer == :x_ndjson
|
131
|
+
set_bulk_body(req, record)
|
132
|
+
else
|
133
|
+
req.set_form_data(record)
|
134
|
+
end
|
135
|
+
req
|
136
|
+
end
|
137
|
+
|
138
|
+
def set_header(req, tag, time, record)
|
139
|
+
if @custom_headers
|
140
|
+
@custom_headers.each do |k,v|
|
141
|
+
req[k] = v
|
142
|
+
end
|
143
|
+
req
|
144
|
+
else
|
145
|
+
req
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def compress_body(req, data)
|
150
|
+
return unless @compress_request
|
151
|
+
gz = Zlib::GzipWriter.new(StringIO.new)
|
152
|
+
gz << data
|
153
|
+
|
154
|
+
req['Content-Encoding'] = "gzip"
|
155
|
+
req.body = gz.close.string
|
156
|
+
end
|
157
|
+
|
158
|
+
def set_json_body(req, data)
|
159
|
+
req.body = Yajl.dump(data)
|
160
|
+
req['Content-Type'] = 'application/json'
|
161
|
+
compress_body(req, req.body)
|
162
|
+
end
|
163
|
+
|
164
|
+
def set_text_body(req, data)
|
165
|
+
req.body = data["message"]
|
166
|
+
req['Content-Type'] = 'text/plain'
|
167
|
+
compress_body(req, req.body)
|
168
|
+
end
|
169
|
+
|
170
|
+
def set_raw_body(req, data)
|
171
|
+
req.body = data.to_s
|
172
|
+
req['Content-Type'] = 'application/octet-stream'
|
173
|
+
compress_body(req, req.body)
|
174
|
+
end
|
175
|
+
|
176
|
+
def set_bulk_body(req, data)
|
177
|
+
req.body = data.to_s
|
178
|
+
req['Content-Type'] = 'application/x-ndjson'
|
179
|
+
compress_body(req, req.body)
|
180
|
+
end
|
181
|
+
|
182
|
+
def create_request(tag, time, record)
|
183
|
+
url = format_url(tag, time, record)
|
184
|
+
uri = URI.parse(url)
|
185
|
+
req = Net::HTTP.const_get(@http_method.to_s.capitalize).new(uri.request_uri)
|
186
|
+
set_body(req, tag, time, record)
|
187
|
+
set_header(req, tag, time, record)
|
188
|
+
return req, uri
|
189
|
+
end
|
190
|
+
|
191
|
+
def http_opts(uri)
|
192
|
+
opts = {
|
193
|
+
:use_ssl => uri.scheme == 'https'
|
194
|
+
}
|
195
|
+
opts[:verify_mode] = @ssl_verify_mode if opts[:use_ssl]
|
196
|
+
opts[:ca_file] = File.join(@ca_file) if File.file?(@ca_file)
|
197
|
+
opts[:cert] = OpenSSL::X509::Certificate.new(File.read(@client_cert_path)) if File.file?(@client_cert_path)
|
198
|
+
opts[:key] = OpenSSL::PKey.read(File.read(@private_key_path), @private_key_passphrase) if File.file?(@private_key_path)
|
199
|
+
opts
|
200
|
+
end
|
201
|
+
|
202
|
+
def proxies
|
203
|
+
ENV['HTTPS_PROXY'] || ENV['HTTP_PROXY'] || ENV['http_proxy'] || ENV['https_proxy']
|
204
|
+
end
|
205
|
+
|
206
|
+
def send_request(req, uri)
|
207
|
+
is_rate_limited = (@rate_limit_msec != 0 and not @last_request_time.nil?)
|
208
|
+
if is_rate_limited and ((Time.now.to_f - @last_request_time) * 1000.0 < @rate_limit_msec)
|
209
|
+
log.info('Dropped request due to rate limiting')
|
210
|
+
return
|
211
|
+
end
|
212
|
+
|
213
|
+
res = nil
|
214
|
+
|
215
|
+
begin
|
216
|
+
if @authentication == :basic
|
217
|
+
req.basic_auth(@username, @password)
|
218
|
+
elsif @authentication == :bearer
|
219
|
+
req['authorization'] = "bearer #{@token}"
|
220
|
+
elsif @authentication == :jwt
|
221
|
+
req['authorization'] = "jwt #{@token}"
|
222
|
+
end
|
223
|
+
@last_request_time = Time.now.to_f
|
224
|
+
|
225
|
+
if proxy = proxies
|
226
|
+
proxy_uri = URI.parse(proxy)
|
227
|
+
|
228
|
+
res = Net::HTTP.start(uri.host, uri.port,
|
229
|
+
proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password,
|
230
|
+
**http_opts(uri)) {|http| http.request(req) }
|
231
|
+
else
|
232
|
+
res = Net::HTTP.start(uri.host, uri.port, **http_opts(uri)) {|http| http.request(req) }
|
233
|
+
end
|
234
|
+
|
235
|
+
rescue => e # rescue all StandardErrors
|
236
|
+
# server didn't respond
|
237
|
+
log.warn "Net::HTTP.#{req.method.capitalize} raises exception: #{e.class}, '#{e.message}'"
|
238
|
+
raise e if @raise_on_error
|
239
|
+
else
|
240
|
+
unless res and res.is_a?(Net::HTTPSuccess)
|
241
|
+
res_summary = if res
|
242
|
+
"#{res.code} #{res.message} #{res.body}"
|
243
|
+
else
|
244
|
+
"res=nil"
|
245
|
+
end
|
246
|
+
if @recoverable_status_codes.include?(res.code.to_i)
|
247
|
+
raise RecoverableResponse, res_summary
|
248
|
+
else
|
249
|
+
log.warn "failed to #{req.method} #{uri} (#{res_summary})"
|
250
|
+
end
|
251
|
+
end #end unless
|
252
|
+
end # end begin
|
253
|
+
end # end send_request
|
254
|
+
|
255
|
+
def handle_record(tag, time, record)
|
256
|
+
if @formatter_config
|
257
|
+
record = @formatter.format(tag, time, record)
|
258
|
+
end
|
259
|
+
req, uri = create_request(tag, time, record)
|
260
|
+
send_request(req, uri)
|
261
|
+
end
|
262
|
+
|
263
|
+
def handle_records(tag, time, chunk)
|
264
|
+
req, uri = create_request(tag, time, chunk.read)
|
265
|
+
send_request(req, uri)
|
266
|
+
end
|
267
|
+
|
268
|
+
def prefer_buffered_processing
|
269
|
+
@buffered
|
270
|
+
end
|
271
|
+
|
272
|
+
def format(tag, time, record)
|
273
|
+
# For safety.
|
274
|
+
end
|
275
|
+
|
276
|
+
def split_request_format(tag, time, record)
|
277
|
+
[time, record].to_msgpack
|
278
|
+
end
|
279
|
+
|
280
|
+
def bulk_request_format(tag, time, record)
|
281
|
+
@formatter.format(tag, time, record)
|
282
|
+
end
|
283
|
+
|
284
|
+
def formatted_to_msgpack_binary?
|
285
|
+
if @bulk_request
|
286
|
+
false
|
287
|
+
else
|
288
|
+
true
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def multi_workers_ready?
|
293
|
+
true
|
294
|
+
end
|
295
|
+
|
296
|
+
def process(tag, es)
|
297
|
+
es.each do |time, record|
|
298
|
+
handle_record(tag, time, record)
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
def write(chunk)
|
303
|
+
tag = chunk.metadata.tag
|
304
|
+
@endpoint_url = extract_placeholders(@endpoint_url, chunk)
|
305
|
+
if @bulk_request
|
306
|
+
time = Fluent::Engine.now
|
307
|
+
handle_records(tag, time, chunk)
|
308
|
+
else
|
309
|
+
chunk.msgpack_each do |time, record|
|
310
|
+
handle_record(tag, time, record)
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|