Skip to content

Commit 5595b4f

Browse files
Merge pull request #10 from HatsuneMiku3939/feature/header-support
request/response header support
2 parents e67b21a + 1a3104a commit 5595b4f

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

fluent-plugin-http-pull.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
33

44
Gem::Specification.new do |spec|
55
spec.name = "fluent-plugin-http-pull"
6-
spec.version = "0.4.0"
6+
spec.version = "0.5.0"
77
spec.authors = ["filepang"]
88
spec.email = ["[email protected]"]
99

lib/fluent/plugin/in_http_pull.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,30 @@ def initialize
4545
desc 'password of basic auth'
4646
config_param :password, :string, default: nil
4747

48+
config_section :response_header, param_name: :response_headers, multi: true do
49+
desc 'The name of header to cature from response'
50+
config_param :header, :string
51+
end
52+
53+
config_section :request_header, param_name: :request_headers, multi: true do
54+
desc 'The name of request header'
55+
config_param :header, :string
56+
57+
desc 'The value of request header'
58+
config_param :value, :string
59+
end
60+
4861
def configure(conf)
4962
compat_parameters_convert(conf, :parser)
5063
super
5164

5265
@parser = parser_create unless @status_only
66+
@_request_headers = @request_headers.map do |section|
67+
header = section["header"]
68+
value = section["value"]
69+
70+
[header.to_sym, value]
71+
end.to_h
5372
end
5473

5574
def start
@@ -67,10 +86,20 @@ def on_timer
6786
request_options[:proxy] = @proxy if @proxy
6887
request_options[:user] = @user if @user
6988
request_options[:password] = @password if @password
89+
request_options[:headers] = @_request_headers unless @request_headers.empty?
7090

7191
res = RestClient::Request.execute request_options
92+
7293
record["status"] = res.code
7394
record["body"] = res.body
95+
96+
record["header"] = {} unless @response_headers.empty?
97+
@response_headers.each do |section|
98+
name = section["header"]
99+
symbolize_name = name.downcase.gsub(/-/, '_').to_sym
100+
101+
record["header"][name] = res.headers[symbolize_name]
102+
end
74103
rescue StandardError => err
75104
if err.respond_to? :http_code
76105
record["status"] = err.http_code || 0

test/helper/stub_server.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def initialize
1111
@server.mount_proc '/internal_error', &method(:internal_error)
1212
@server.mount_proc '/redirect', &method(:redirect)
1313
@server.mount_proc '/protected', &method(:protected)
14+
@server.mount_proc '/custom_header', &method(:custom_header)
1415
end
1516

1617
def start
@@ -81,4 +82,12 @@ def protected(req, res)
8182
res['Content-Type'] = 'application/json'
8283
res.body = '{ "status": "OK" }'
8384
end
85+
86+
def custom_header(req, res)
87+
res.header["HATSUNE-MIKU"] = req["HATSUNE-MIKU"] if req["HATSUNE-MIKU"]
88+
89+
res.status = 200
90+
res['Content-Type'] = 'application/json'
91+
res.body = '{ "status": "OK" }'
92+
end
8493
end

test/plugin/test_in_http_pull.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,109 @@ class HttpPullInputTest < Test::Unit::TestCase
453453
end
454454
end
455455

456+
sub_test_case "capture response header" do
457+
TEST_INTERVAL_3_RES_HEADER_CONFIG = %[
458+
tag test
459+
url http://127.0.0.1:3939
460+
461+
interval 3s
462+
format json
463+
464+
<response_header>
465+
header Content-Type
466+
</response_header>
467+
]
468+
469+
TEST_INTERVAL_3_RES_MULTI_HEADER_CONFIG = %[
470+
tag test
471+
url http://127.0.0.1:3939
472+
473+
interval 3s
474+
format json
475+
476+
<response_header>
477+
header Content-Type
478+
</response_header>
479+
480+
<response_header>
481+
header Content-Length
482+
</response_header>
483+
]
484+
485+
test 'interval 3 with single header' do
486+
d = create_driver TEST_INTERVAL_3_RES_HEADER_CONFIG
487+
assert_equal("test", d.instance.tag)
488+
assert_equal(3, d.instance.interval)
489+
490+
d.run(timeout: 8) do
491+
sleep 7
492+
end
493+
assert_equal(2, d.events.size)
494+
495+
d.events.each do |tag, time, record|
496+
assert_equal("test", tag)
497+
498+
assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200,"message"=>{"status"=>"OK"},"header"=>{"Content-Type"=>"application/json"}}, record)
499+
assert(time.is_a?(Fluent::EventTime))
500+
end
501+
end
502+
503+
test 'interval 3 with multiple header' do
504+
d = create_driver TEST_INTERVAL_3_RES_MULTI_HEADER_CONFIG
505+
assert_equal("test", d.instance.tag)
506+
assert_equal(3, d.instance.interval)
507+
508+
d.run(timeout: 8) do
509+
sleep 7
510+
end
511+
assert_equal(2, d.events.size)
512+
513+
d.events.each do |tag, time, record|
514+
assert_equal("test", tag)
515+
516+
assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200,"message"=>{"status"=>"OK"},"header"=>{"Content-Type"=>"application/json","Content-Length"=>"18"}}, record)
517+
assert(time.is_a?(Fluent::EventTime))
518+
end
519+
end
520+
end
521+
522+
sub_test_case "custom request header" do
523+
TEST_INTERVAL_3_CUSTOM_HEADER_CONFIG = %[
524+
tag test
525+
url http://127.0.0.1:3939/custom_header
526+
527+
interval 3s
528+
format json
529+
530+
<request_header>
531+
header HATSUNE-MIKU
532+
value 3939
533+
</request_header>
534+
535+
<response_header>
536+
header HATSUNE-MIKU
537+
</response_header>
538+
]
539+
540+
test 'interval 3 with single header' do
541+
d = create_driver TEST_INTERVAL_3_CUSTOM_HEADER_CONFIG
542+
assert_equal("test", d.instance.tag)
543+
assert_equal(3, d.instance.interval)
544+
545+
d.run(timeout: 8) do
546+
sleep 7
547+
end
548+
assert_equal(2, d.events.size)
549+
550+
d.events.each do |tag, time, record|
551+
assert_equal("test", tag)
552+
553+
assert_equal({"url"=>"http://127.0.0.1:3939/custom_header","status"=>200,"message"=>{"status"=>"OK"},"header"=>{"HATSUNE-MIKU"=>"3939"}}, record)
554+
assert(time.is_a?(Fluent::EventTime))
555+
end
556+
end
557+
end
558+
456559
private
457560

458561
def create_driver(conf)

0 commit comments

Comments
 (0)