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

Add custom headers #267

Merged
merged 3 commits into from
Jan 10, 2023
Merged
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
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