Skip to content

Commit

Permalink
added option 'ignore_errors' to also return failed REST calls
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpaschedag committed Feb 22, 2021
1 parent 760f0c4 commit 00b9d7b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +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}-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 @@ -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 <<boolean,boolean>>
* 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`

Expand Down
7 changes: 4 additions & 3 deletions lib/logstash/filters/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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'
config :headers, :validate => :hash, :required => false, :default => {}
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"
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions spec/filters/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down

0 comments on commit 00b9d7b

Please sign in to comment.