From 233582cd90a25a1f5cae0ae229e9827e1ffd2628 Mon Sep 17 00:00:00 2001 From: Robert Paschedag Date: Mon, 12 Apr 2021 17:32:53 +0200 Subject: [PATCH] rename to 'ignorable_codes' as in http output plugin... Also use the same logic. Only difference is that http codes between 200 and 299 are - as before - considered success. Other http codes are only considered success, if they are listed within 'ignorable_codes'. --- docs/index.asciidoc | 14 +++++++------- lib/logstash/filters/http.rb | 5 +++-- spec/filters/http_spec.rb | 25 +++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index a055835..c13f60a 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -52,7 +52,7 @@ There are also multiple configuration options related to the HTTP connectivity: | <> |<>|No | <> |<>|No | <> |<>|No -| <> |<>|No +| <> |<>|No | <> |<>|No | <> |a valid filesystem path|No | <> |<>|No @@ -203,14 +203,14 @@ 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` +[id="plugins-{type}s-{plugin}-ignorable_codes"] +===== `ignorable_codes` - * Value type is <> - * Default value is `false` + * Value type is <> + * There is no default for this setting. -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. +If you would like to consider some non-2xx codes to be successes +enumerate them here. Responses returning these codes will be considered successes [id="plugins-{type}s-{plugin}-keepalive"] ===== `keepalive` diff --git a/lib/logstash/filters/http.rb b/lib/logstash/filters/http.rb index c4f537b..4209c03 100644 --- a/lib/logstash/filters/http.rb +++ b/lib/logstash/filters/http.rb @@ -20,7 +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 :ignorable_codes, :validate => :number, :list => true config :target_body, :validate => :string, :default => "body" config :target_headers, :validate => :string, :default => "headers" @@ -66,7 +66,8 @@ 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) and !ignore_errors + elsif !code.between?(200, 299) and (!@ignorable_codes || !@ignorable_codes.include?(code)) + # fail, if code is not 2xx AND either ignorable_codes not set at all OR listed codeds do NOT match @logger.error('error during HTTP request', :url => url_for_event, :code => code, :response => response_body) diff --git a/spec/filters/http_spec.rb b/spec/filters/http_spec.rb index d457ad7..92bb542 100644 --- a/spec/filters/http_spec.rb +++ b/spec/filters/http_spec.rb @@ -68,13 +68,13 @@ expect(event.get('tags')).to include('_httprequestfailure') end end - context 'when request returns 404 but ignores errors' do + context 'failing request with ignorable code' do before(:each) { subject.register } let(:config) do { 'url' => 'http://httpstat.us/404', 'target_body' => 'rest', - 'ignore_errors' => true + 'ignorable_codes' => [404, 400] } end let(:response) { [404, {}, "request failed"] } @@ -88,6 +88,27 @@ expect(event.get('rest')).to eq("request failed") end end + context 'failing request with non matching ignorable code' do + before(:each) { subject.register } + let(:config) do + { + 'url' => 'http://httpstat.us/404', + 'target_body' => 'rest', + 'ignorable_codes' => [500, 400] + } + end + let(:response) { [404, {}, ""] } + + before(:each) do + allow(subject).to receive(:request_http).and_return(response) + subject.filter(event) + end + + it "tags the event with _httprequestfailure" do + expect(event).to_not include('rest') + expect(event.get('tags')).to include('_httprequestfailure') + end + end describe "headers" do before(:each) { subject.register } let(:response) { [200, {}, "Bom dia"] }