Skip to content

Commit e29465d

Browse files
authored
Merge pull request #56 from sumo-drosiek/drosiek-custom-fields
Allow to add custom fields via config
2 parents 9d1832b + 12c3f85 commit e29465d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lib/fluent/plugin/out_sumologic.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Fluent::Plugin::Sumologic < Fluent::Plugin::Output
8686
config_param :timestamp_key, :string, :default => 'timestamp'
8787
config_param :proxy_uri, :string, :default => nil
8888
config_param :disable_cookies, :bool, :default => false
89+
# https://help.sumologic.com/Manage/Fields
90+
desc 'Fields string (eg "cluster=payment, service=credit_card") which is going to be added to every record.'
91+
config_param :custom_fields, :string, :default => nil
8992

9093
config_section :buffer do
9194
config_set_default :@type, DEFAULT_BUFFER_TYPE
@@ -129,6 +132,10 @@ def configure(conf)
129132
end
130133
end
131134

135+
if conf['custom_fields'].nil? || conf['custom_fields'].strip.length == 0
136+
conf['custom_fields'] = nil
137+
end
138+
132139
@sumo_conn = SumologicConnection.new(conf['endpoint'], conf['verify_ssl'], conf['open_timeout'].to_i, conf['proxy_uri'], conf['disable_cookies'])
133140
super
134141
end
@@ -268,6 +275,14 @@ def write(chunk)
268275
messages_list.each do |key, messages|
269276
source_name, source_category, source_host, fields = key[:source_name], key[:source_category],
270277
key[:source_host], key[:fields]
278+
279+
# Merge custom and record fields
280+
if fields.nil? || fields.strip.length == 0
281+
fields = @custom_fields
282+
else
283+
fields = [fields,@custom_fields].compact.join(",")
284+
end
285+
271286
@sumo_conn.publish(
272287
messages.join("\n"),
273288
source_host =source_host,

test/plugin/test_out_sumologic.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,78 @@ def test_emit_with_sumo_metadata_with_fields_fields_format
267267
times:1
268268
end
269269

270+
def test_emit_with_sumo_metadata_with_fields_and_custom_fields_fields_format
271+
config = %{
272+
endpoint https://collectors.sumologic.com/v1/receivers/http/1234
273+
log_format fields
274+
custom_fields "lorem=ipsum,dolor=amet"
275+
}
276+
driver = create_driver(config)
277+
time = event_time
278+
stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234')
279+
ENV['HOST'] = "foo"
280+
driver.run do
281+
driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => {
282+
"host": "#{ENV['HOST']}",
283+
"source": "${tag}",
284+
"category": "test",
285+
"fields": "foo=bar, sumo = logic"
286+
}})
287+
end
288+
assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234",
289+
headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'foo=bar, sumo = logic,lorem=ipsum,dolor=amet'},
290+
body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/,
291+
times:1
292+
end
293+
294+
def test_emit_with_sumo_metadata_with_fields_and_empty_custom_fields_fields_format
295+
config = %{
296+
endpoint https://collectors.sumologic.com/v1/receivers/http/1234
297+
log_format fields
298+
custom_fields ""
299+
}
300+
driver = create_driver(config)
301+
time = event_time
302+
stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234')
303+
ENV['HOST'] = "foo"
304+
driver.run do
305+
driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => {
306+
"host": "#{ENV['HOST']}",
307+
"source": "${tag}",
308+
"category": "test",
309+
"fields": "foo=bar, sumo = logic"
310+
}})
311+
end
312+
assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234",
313+
headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'foo=bar, sumo = logic'},
314+
body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/,
315+
times:1
316+
end
317+
318+
def test_emit_with_sumo_metadata_with_empty_fields_and_custom_fields_fields_format
319+
config = %{
320+
endpoint https://collectors.sumologic.com/v1/receivers/http/1234
321+
log_format fields
322+
custom_fields "lorem=ipsum"
323+
}
324+
driver = create_driver(config)
325+
time = event_time
326+
stub_request(:post, 'https://collectors.sumologic.com/v1/receivers/http/1234')
327+
ENV['HOST'] = "foo"
328+
driver.run do
329+
driver.feed("output.test", time, {'foo' => 'shark', 'message' => 'test', '_sumo_metadata' => {
330+
"host": "#{ENV['HOST']}",
331+
"source": "${tag}",
332+
"category": "test",
333+
"fields": ""
334+
}})
335+
end
336+
assert_requested :post, "https://collectors.sumologic.com/v1/receivers/http/1234",
337+
headers: {'X-Sumo-Category'=>'test', 'X-Sumo-Client'=>'fluentd-output', 'X-Sumo-Host'=>'foo', 'X-Sumo-Name'=>'output.test', 'X-Sumo-Fields' => 'lorem=ipsum'},
338+
body: /\A{"timestamp":\d+.,"foo":"shark","message":"test"}\z/,
339+
times:1
340+
end
341+
270342
def test_emit_with_sumo_metadata
271343
config = %{
272344
endpoint https://collectors.sumologic.com/v1/receivers/http/1234

0 commit comments

Comments
 (0)