Skip to content

Commit

Permalink
allow changing the default target field for host and headers data
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvd committed May 10, 2018
1 parent 1b76537 commit fe8b72a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.2.0
- adds `request_headers_target_field` and `remote_host_target_field` configuration options with defaults to `host` and `headers` respectively #68

## 3.1.0
- Replace Puma web server with Netty
- Support crt/key certificates
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.0
3.2.0
16 changes: 16 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ Persistent Queue for the logstash pipeline.

specify a custom set of response headers

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

* Value type is <<string,string>>
* Default value is `"host"`

specify a target field for the client host of the http request

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

* Value type is <<string,string>>
* Default value is `"headers"`

specify target field for the client host of the http request

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

Expand Down
10 changes: 8 additions & 2 deletions lib/logstash/inputs/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class LogStash::Inputs::Http < LogStash::Inputs::Base
# specify a custom set of response headers
config :response_headers, :validate => :hash, :default => { 'Content-Type' => 'text/plain' }

# target field for the client host of the http request
config :remote_host_target_field, :validate => :string, :default => "host"

# target field for the client host of the http request
config :request_headers_target_field, :validate => :string, :default => "headers"

config :threads, :validate => :number, :required => false, :default => ::LogStash::Config::CpuCoreStrategy.maximum

config :max_pending_requests, :validate => :number, :required => false, :default => 200
Expand Down Expand Up @@ -170,8 +176,8 @@ def decode_body(headers, remote_address, body, default_codec, additional_codecs)
end

def push_decoded_event(headers, remote_address, event)
event.set("headers", headers)
event.set("host", remote_address)
event.set(@request_headers_target_field, headers)
event.set(@remote_host_target_field, remote_address)
decorate(event)
@queue << event
end
Expand Down
52 changes: 52 additions & 0 deletions spec/inputs/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

describe "request handling" do
subject { LogStash::Inputs::Http.new("port" => port) }

before :each do
subject.register
t = Thread.new { subject.run(logstash_queue) }
Expand Down Expand Up @@ -81,6 +82,57 @@
end
end

describe "remote host" do
subject { LogStash::Inputs::Http.new(config.merge("port" => port)) }
context "by default" do
let(:config) { {} }
it "is written to the \"host\" field" do
client.post("http://localhost:#{port}/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello").call
event = logstash_queue.pop
expect(event.get("host")).to eq("127.0.0.1")
end
end

context "when using remote_host_target_field" do
let(:config) { { "remote_host_target_field" => "remote_host" } }
it "is written to the value of \"remote_host_target_field\" property" do
client.post("http://localhost:#{port}/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello").call
event = logstash_queue.pop
expect(event.get("remote_host")).to eq("127.0.0.1")
end
end
end

describe "request headers" do
subject { LogStash::Inputs::Http.new(config.merge("port" => port)) }
context "by default" do
let(:config) { {} }
it "are written to the \"headers\" field" do
client.post("http://localhost:#{port}/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello").call
event = logstash_queue.pop
expect(event.get("headers")).to be_a(Hash)
expect(event.get("headers")).to include("request_method" => "POST")
end
end
context "when using request_headers_target_field" do
let(:config) { { "request_headers_target_field" => "request_headers" } }
it "are written to the field set in \"request_headers_target_field\"" do
client.post("http://localhost:#{port}/meh.json",
:headers => { "content-type" => "text/plain" },
:body => "hello").call
event = logstash_queue.pop
expect(event.get("request_headers")).to be_a(Hash)
expect(event.get("request_headers")).to include("request_method" => "POST")
end
end
end

it "should include remote host in \"host\" property" do
client.post("http://127.0.0.1:#{port}/meh.json",
:headers => { "content-type" => "text/plain" },
Expand Down

0 comments on commit fe8b72a

Please sign in to comment.