Skip to content

Commit

Permalink
fix to raise error if injected hostname_key is also specified in buff…
Browse files Browse the repository at this point in the history
…er chunk key

Inject plugin helper does record.dup, so that Output plugin code cannot find any
side effect of inject plugin helper. It means output cannnot do chunking with injected values.
On the other hand, there is just few good use cases about chunking with injected hostname.
* chunking hostname (just after injected) is to route events with hostname... and hostname is unique on a host
* just one good use-case, to extract hostname into ${hostname} in configured values
  * but it can be done by ruby code eval
  • Loading branch information
tagomoris committed Dec 8, 2016
1 parent 2cdd515 commit 41452d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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 in 'hostname_key' 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 in 'hostname_key' 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

0 comments on commit 41452d4

Please sign in to comment.