diff --git a/fluent-plugin-http-pull.gemspec b/fluent-plugin-http-pull.gemspec index e35a387..0242107 100644 --- a/fluent-plugin-http-pull.gemspec +++ b/fluent-plugin-http-pull.gemspec @@ -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"] diff --git a/lib/fluent/plugin/in_http_pull.rb b/lib/fluent/plugin/in_http_pull.rb index 43ec0b5..b1a032f 100644 --- a/lib/fluent/plugin/in_http_pull.rb +++ b/lib/fluent/plugin/in_http_pull.rb @@ -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 @@ -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 + + @response_headers.each do |section| + name = section["header"] + symbolize_name = name.downcase.gsub(/-/, '_').to_sym + + record["header"] = {} unless record["header"] + record["header"][name] = res.headers[symbolize_name] + end rescue StandardError => err if err.respond_to? :http_code record["status"] = err.http_code || 0 diff --git a/test/plugin/test_in_http_pull.rb b/test/plugin/test_in_http_pull.rb index 6e68a71..e37c853 100644 --- a/test/plugin/test_in_http_pull.rb +++ b/test/plugin/test_in_http_pull.rb @@ -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 + + + header Content-Type + + ] + + 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)