Skip to content
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
19 changes: 15 additions & 4 deletions lib/fluent/plugin/out_splunk_hec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ class SplunkHecOutput < SplunkOutput
config_param :protocol, :enum, list: %i[http https], default: :https

desc 'The hostname/IP to HEC, or HEC load balancer.'
config_param :hec_host, :string
config_param :hec_host, :string, default: ''

desc 'The port number to HEC, or HEC load balancer.'
config_param :hec_port, :integer, default: 8088

desc 'Full url to connect tosplunk. Example: https://mydomain.com:8088/apps/splunk'
config_param :full_url, :string, default: ''

desc 'The HEC token.'
config_param :hec_token, :string

Expand Down Expand Up @@ -132,7 +135,7 @@ def initialize

def configure(conf)
super

raise Fluent::ConfigError, 'One of `hec_host` or `full_url` is required.' if @hec_host.empty? && @full_url.empty?
check_metric_configs
pick_custom_format_method
end
Expand Down Expand Up @@ -279,9 +282,17 @@ def format_metric(tag, time, record)
end

def construct_api
URI("#{@protocol}://#{@hec_host}:#{@hec_port}/services/collector")
if @full_url.empty?
URI("#{@protocol}://#{@hec_host}:#{@hec_port}/services/collector")
else
URI("#{@full_url.delete_suffix("/")}/services/collector")
end
rescue StandardError
raise Fluent::ConfigError, "hec_host (#{@hec_host}) and/or hec_port (#{@hec_port}) are invalid."
if @full_url.empty?
raise Fluent::ConfigError, "hec_host (#{@hec_host}) and/or hec_port (#{@hec_port}) are invalid."
else
raise Fluent::ConfigError, "full_url (#{@full_url}) is invalid."
end
end

def new_connection
Expand Down
13 changes: 12 additions & 1 deletion test/fluent/plugin/out_splunk_hec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

describe 'hec_host validation' do
describe 'invalid host' do
it 'should require hec_host' do
it 'should require hec_host or full_url' do
expect { create_hec_output_driver }.must_raise Fluent::ConfigError
end

Expand All @@ -78,6 +78,17 @@
end
end

describe 'full_url validation' do
describe 'invalid full_url' do
it { expect { create_hec_output_driver(full_url: '%bad-host%.com') }.must_raise Fluent::ConfigError }
end
describe 'good full_url' do
it {
expect(create_hec_output_driver('full_url https://splunk.com').instance.full_url).must_equal 'https://splunk.com'
}
end
end

it 'should send request to Splunk' do
req = verify_sent_events do |batch|
expect(batch.size).must_equal 2
Expand Down