Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option 'ignorable_codes' to also return failed REST calls #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}-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 @@ -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}-ignorable_codes"]
===== `ignorable_codes`

* Value type is <<number,number>>
* There is no default for this setting.

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
8 changes: 5 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 :ignorable_codes, :validate => :number, :list => true

config :target_body, :validate => :string, :default => "body"
config :target_headers, :validate => :string, :default => "headers"
Expand Down Expand Up @@ -65,13 +66,14 @@ 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 (!@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)
@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
43 changes: 42 additions & 1 deletion spec/filters/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,47 @@
expect(event.get('tags')).to include('_httprequestfailure')
end
end
context 'failing request with ignorable code' do
before(:each) { subject.register }
let(:config) do
{
'url' => 'http://httpstat.us/404',
'target_body' => 'rest',
'ignorable_codes' => [404, 400]
}
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
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 Expand Up @@ -197,7 +238,7 @@
"target_body" => "size"
}
end
["GET", "HEAD", "POST", "DELETE"].each do |verb_string|
["GET", "HEAD", "POST", "DELETE", "PUT"].each do |verb_string|
let(:verb) { verb_string }
context "when verb #{verb_string} is set" do
before(:each) { subject.register }
Expand Down