Skip to content

Commit

Permalink
capture response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sskim committed Sep 8, 2017
1 parent e67b21a commit a82194f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fluent-plugin-http-pull.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "fluent-plugin-http-pull"
spec.version = "0.4.0"
spec.version = "0.5.0"
spec.authors = ["filepang"]
spec.email = ["filepang@gmail.com"]

Expand Down
14 changes: 14 additions & 0 deletions lib/fluent/plugin/in_http_pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def initialize
desc 'password of basic auth'
config_param :password, :string, default: nil

config_section :response_header, param_name: :response_headers, multi: true do
desc 'The name of header to cature from response'
config_param :header, :string
end

def configure(conf)
compat_parameters_convert(conf, :parser)
super
Expand All @@ -69,8 +74,17 @@ def on_timer
request_options[:password] = @password if @password

res = RestClient::Request.execute request_options

record["status"] = res.code
record["body"] = res.body

record["header"] = {} unless @response_headers.empty?
@response_headers.each do |section|
name = section["header"]
symbolize_name = name.downcase.gsub(/-/, '_').to_sym

record["header"][name] = res.headers[symbolize_name]
end
rescue StandardError => err
if err.respond_to? :http_code
record["status"] = err.http_code || 0
Expand Down
32 changes: 32 additions & 0 deletions test/plugin/test_in_http_pull.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,38 @@ class HttpPullInputTest < Test::Unit::TestCase
end
end

sub_test_case "capture response header" do
TEST_INTERVAL_3_RES_HEADER_CONFIG = %[
tag test
url http://127.0.0.1:3939
interval 3s
format json
<response_header>
header Content-Type
</response_header>
]

test 'interval 3' do
d = create_driver TEST_INTERVAL_3_RES_HEADER_CONFIG
assert_equal("test", d.instance.tag)
assert_equal(3, d.instance.interval)

d.run(timeout: 8) do
sleep 7
end
assert_equal(2, d.events.size)

d.events.each do |tag, time, record|
assert_equal("test", tag)

assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200,"message"=>{"status"=>"OK"},"header"=>{"Content-Type"=>"application/json"}}, record)
assert(time.is_a?(Fluent::EventTime))
end
end
end

private

def create_driver(conf)
Expand Down

0 comments on commit a82194f

Please sign in to comment.