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

include http status code in metadata #42

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.5.0
- Feat: add http response status code to metadata

## 1.4.1
- Fix: don't process response body for HEAD requests [#40](https://github.com/logstash-plugins/logstash-filter-http/pull/40)

Expand Down
9 changes: 9 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
| <<plugins-{type}s-{plugin}-query>> |<<hash,hash>>|No
| <<plugins-{type}s-{plugin}-target_body>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-target_headers>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-target_status>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-url>> |<<string,string>>|Yes
| <<plugins-{type}s-{plugin}-verb>> |<<string,string>>|No
|=======================================================================
Expand Down Expand Up @@ -155,6 +156,14 @@ Define the target field for placing the body of the HTTP response.

Define the target field for placing the headers of the HTTP response.

[id="plugins-{type}s-{plugin}-target_status"]
===== `target_status`

* Value type is <<string,string>>
* Default value `"[@metadata][filter][http][response][status_code]"`

Define the target field for placing the status code of the HTTP response.

[id="plugins-{type}s-{plugin}-url"]
===== `url`

Expand Down
24 changes: 15 additions & 9 deletions lib/logstash/filters/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class LogStash::Filters::Http < LogStash::Filters::Base
config :target_body, :validate => :field_reference
# default [headers] (legacy) or [@metadata][filter][http][response][headers] in ECS mode
config :target_headers, :validate => :field_reference
# default [@metadata][filter][http][response][status_code] in all modes
config :target_status, :validate => :field_reference, default: '[@metadata][filter][http][response][status_code]'

# Append values to the `tags` field when there has been no
# successful match or json parsing error
Expand Down Expand Up @@ -85,16 +87,20 @@ 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)
@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',
:code => code, :body => response_body)
process_response(response_body, response_headers, event)
filter_matched(event)
event.set(@target_status, code)
if !code.between?(200, 299)
@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',
:code => code, :body => response_body)
process_response(response_body, response_headers, event)
filter_matched(event)
end
end
end # def filter

Expand Down
2 changes: 1 addition & 1 deletion logstash-filter-http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-filter-http'
s.version = '1.4.1'
s.version = '1.5.0'
s.licenses = ['Apache License (2.0)']
s.summary = 'This filter requests data from a RESTful Web Service.'
s.description = 'This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install logstash-filter-http. This gem is not a stand-alone program'
Expand Down
18 changes: 18 additions & 0 deletions spec/filters/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
expect(event).to_not include('rest')
expect(event.get('tags')).to include('_httprequestfailure')
end

it 'stores the HTTP status in the associated target' do
expect(event.get('[@metadata][filter][http][response][status_code]')).to eq(404)
end
end

describe "headers", :ecs_compatibility_support do
Expand Down Expand Up @@ -141,6 +145,8 @@
expect(event.get('[@metadata][filter][http][response][headers]')).to include "Server" => "Apache"
expect(event.get('[@metadata][filter][http][response][headers]')).to include "X-Backend-Server" => "logstash.elastic.co"
end

expect(event.get('[@metadata][filter][http][response][status_code]')).to eq(200)
end

context 'with a headers target' do
Expand All @@ -158,6 +164,18 @@

end

context 'with a status target' do
let(:config) { super().merge("target_status" => '[res][status]') }

it 'sets the the targeted field on the event' do
expect(subject).to receive(:request_http).with(anything, config['url'], anything).and_return(response)

subject.filter(event)

expect(event.get('[res][status]')).to eq(200)
end
end

end

context "(extra) request headers" do
Expand Down