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

fix to raise error if injected hostname_key is also specified in buffer chunk key #1357

Merged
merged 1 commit into from
Dec 10, 2016
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
11 changes: 11 additions & 0 deletions lib/fluent/plugin_helper/inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def configure(conf)
if @inject_config
@_inject_hostname_key = @inject_config.hostname_key
if @_inject_hostname_key
if self.respond_to?(:buffer_config)
# Output plugin cannot use "hostname"(specified by @hostname_key),
# injected by this plugin helper, in chunk keys.
# This plugin helper works in `#format` (in many cases), but modified record
# don't have any side effect in chunking of output plugin.
if self.buffer_config.chunk_keys.include?(@_inject_hostname_key)
log.error "Use filters to inject hostname to use it in buffer chunking."
raise Fluent::ConfigError, "the key specified by 'hostname_key' in <inject> cannot be used in buffering chunk key."
end
end

@_inject_hostname = @inject_config.hostname
unless @_inject_hostname
@_inject_hostname = Socket.gethostname
Expand Down
21 changes: 21 additions & 0 deletions test/plugin_helper/test_inject.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../helper'
require 'fluent/plugin_helper/inject'
require 'fluent/plugin/output'
require 'fluent/event'
require 'time'

Expand All @@ -15,6 +16,13 @@ class Dummy2 < Fluent::Plugin::TestBase
end
end

class Dummy3 < Fluent::Plugin::Output
helpers :inject
def write(chunk)
# dummy
end
end

def config_inject_section(hash = {})
config_element('ROOT', '', {}, [config_element('inject', '', hash)])
end
Expand All @@ -27,7 +35,9 @@ def config_inject_section(hash = {})
teardown do
if @d
@d.stop unless @d.stopped?
@d.before_shutdown unless @d.before_shutdown?
@d.shutdown unless @d.shutdown?
@d.after_shutdown unless @d.after_shutdown?
@d.close unless @d.closed?
@d.terminate unless @d.terminated?
end
Expand Down Expand Up @@ -93,6 +103,17 @@ def config_inject_section(hash = {})
assert_not_nil @d.instance_eval{ @_inject_time_formatter }
end

test 'raise an error when injected hostname is used in buffer chunk key too' do
@d = Dummy3.new
conf = config_element('ROOT', '', {}, [
config_element('inject', '', {'hostname_key' => 'h'}),
config_element('buffer', 'tag,h'),
])
assert_raise Fluent::ConfigError.new("the key specified by 'hostname_key' in <inject> cannot be used in buffering chunk key.") do
@d.configure(conf)
end
end

sub_test_case 'using inject_values_to_record' do
test 'injects hostname automatically detected' do
detected_hostname = `hostname`.chomp
Expand Down