From f58fdfcc585f4a8cda2ed3a4182fe08e7bcc1d65 Mon Sep 17 00:00:00 2001 From: Robert Paschedag Date: Mon, 22 Feb 2021 08:29:27 +0100 Subject: [PATCH] added option 'ignore_errors' to also return failed REST calls --- docs/index.asciidoc | 10 ++++++++++ lib/logstash/filters/http.rb | 7 ++++--- spec/filters/http_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index c97020e..a055835 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -52,6 +52,7 @@ There are also multiple configuration options related to the HTTP connectivity: | <> |<>|No | <> |<>|No | <> |<>|No +| <> |<>|No | <> |<>|No | <> |a valid filesystem path|No | <> |<>|No @@ -202,6 +203,15 @@ across requests as a normal web browser would. Enabled by default Should redirects be followed? Defaults to `true` +[id="plugins-{type}s-{plugin}-ignore_errors"] +===== `ignore_errors` + + * Value type is <> + * Default value is `false` + +Set to `true` if you want to also process failed requests. Keep in mind that +the filter will always succeed, unless there is an exception. + [id="plugins-{type}s-{plugin}-keepalive"] ===== `keepalive` diff --git a/lib/logstash/filters/http.rb b/lib/logstash/filters/http.rb index d22f02d..c4f537b 100644 --- a/lib/logstash/filters/http.rb +++ b/lib/logstash/filters/http.rb @@ -12,7 +12,7 @@ class LogStash::Filters::Http < LogStash::Filters::Base config_name 'http' - VALID_VERBS = ['GET', 'HEAD', 'PATCH', 'DELETE', 'POST'] + VALID_VERBS = ['GET', 'HEAD', 'PATCH', 'DELETE', 'POST', 'PUT'] config :url, :validate => :string, :required => true config :verb, :validate => VALID_VERBS, :required => false, :default => 'GET' @@ -20,6 +20,7 @@ class LogStash::Filters::Http < LogStash::Filters::Base config :query, :validate => :hash, :required => false, :default => {} config :body, :required => false config :body_format, :validate => ['text', 'json'], :default => "text" + config :ignore_errors, :validate => :boolean, :default => false config :target_body, :validate => :string, :default => "body" config :target_headers, :validate => :string, :default => "headers" @@ -65,13 +66,13 @@ def filter(event) :url => url_for_event, :body => body_sprintfed, :client_error => client_error.message) @tag_on_request_failure.each { |tag| event.tag(tag) } - elsif !code.between?(200, 299) + elsif !code.between?(200, 299) and !ignore_errors @logger.error('error during HTTP request', :url => url_for_event, :code => code, :response => response_body) @tag_on_request_failure.each { |tag| event.tag(tag) } else - @logger.debug? && @logger.debug('success received', + @logger.debug? && @logger.debug('received response', :code => code, :body => response_body) process_response(response_body, response_headers, event) filter_matched(event) diff --git a/spec/filters/http_spec.rb b/spec/filters/http_spec.rb index c45cce6..0300baf 100644 --- a/spec/filters/http_spec.rb +++ b/spec/filters/http_spec.rb @@ -68,6 +68,26 @@ expect(event.get('tags')).to include('_httprequestfailure') end end + context 'when request returns 404 but ignores errors' do + before(:each) { subject.register } + let(:config) do + { + 'url' => 'http://httpstat.us/404', + 'target_body' => 'rest', + 'ignore_errors' => true + } + end + let(:response) { [404, {}, "request failed"] } + + before(:each) do + allow(subject).to receive(:request_http).and_return(response) + subject.filter(event) + end + + it "fetches event and returns body" do + expect(event.get('rest')).to eq("request failed") + end + end describe "headers" do before(:each) { subject.register } let(:response) { [200, {}, "Bom dia"] }