Skip to content

Commit

Permalink
rename to 'ignorable_codes' as in http output plugin...
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
rpasche committed Apr 12, 2021
1 parent 95e4a27 commit d2921d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
14 changes: 7 additions & 7 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ There are also multiple configuration options related to the HTTP connectivity:
| <<plugins-{type}s-{plugin}-connect_timeout>> |<<number,number>>|No
| <<plugins-{type}s-{plugin}-cookies>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-follow_redirects>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-ignore_errors>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-ignorable_codes>> |<<number,number>>|No
| <<plugins-{type}s-{plugin}-keepalive>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-keystore>> |a valid filesystem path|No
| <<plugins-{type}s-{plugin}-keystore_password>> |<<password,password>>|No
Expand Down Expand Up @@ -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 <<boolean,boolean>>
* Default value is `false`
* Value type is <<number,number>>
* 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`
Expand Down
5 changes: 3 additions & 2 deletions lib/logstash/filters/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 23 additions & 2 deletions spec/filters/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"] }
Expand Down

0 comments on commit d2921d6

Please sign in to comment.