Skip to content

Commit

Permalink
Add custom headers (#267)
Browse files Browse the repository at this point in the history
* [feat] Add support for @custom_headers
  • Loading branch information
cjgibson authored Jan 10, 2023
1 parent fdf8ef3 commit 3544e74
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ Splunk app name using this plugin (default to `hec_plugin_gem`)

The version of Splunk app using this this plugin (default to plugin version)

### custom_headers (Hash) (Optional)

Hash of custom headers to be added to the HTTP request. Used to populate [`override_headers`](https://docs.seattlerb.org/net-http-persistent/Net/HTTP/Persistent.html#attribute-i-override_headers) attribute of the underlying `Net::HTTP::Persistent` connection.

#### When `data_type` is `event`

In this case, parameters inside `<fields>` are used as indexed fields and removed from the original input events. Please see the "Add a "fields" property at the top JSON level" [here](http://dev.splunk.com/view/event-collector/SP-CAAAFB6) for details. Given we have configuration like
Expand Down
7 changes: 6 additions & 1 deletion lib/fluent/plugin/out_splunk_hec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class SplunkHecOutput < SplunkOutput
DESC
config_param :non_utf8_replacement_string, :string, :default => ' '

desc 'Any custom headers to include alongside requests made to Splunk'
config_param :custom_headers, :hash, :default => {}

def initialize
super
@default_host = Socket.gethostname
Expand Down Expand Up @@ -168,7 +171,9 @@ def start
c.override_headers['Authorization'] = "Splunk #{@hec_token}"
c.override_headers['__splunk_app_name'] = "#{@app_name}"
c.override_headers['__splunk_app_version'] = "#{@app_version}"

@custom_headers.each do |header, value|
c.override_headers[header] = value
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/fluent/plugin/out_splunk_hec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
it 'should support enabling gzip' do
expect(create_hec_output_driver('hec_host hec_token', 'gzip_compression true').instance.gzip_compression).must_equal true
end
it 'should define custom_headers as {} (hash) initially' do
assert_empty(create_hec_output_driver('hec_host hec_token').instance.custom_headers)
expect(create_hec_output_driver('hec_host hec_token').instance.custom_headers).is_a? Hash
end
it 'should allow setting custom_headers' do
assert_equal(create_hec_output_driver('hec_host hec_token', 'custom_headers {"custom":"header"}').instance.custom_headers, {"custom" => "header"})
end
end

describe 'hec_host validation' do
Expand Down

0 comments on commit 3544e74

Please sign in to comment.