fluent-mixin-rewrite-tag-name 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -20,8 +20,7 @@ For example with `td.apache.access` tag, it will get `td` by `${tag_parts[0]}` a
20
20
 
21
21
  **Note**
22
22
 
23
- * To support upcase placeholder, set `enable_placeholder_upcase true` in configuration.
24
- * Currently, range expression ```${tag_parts[0..2]}``` is not supported.
23
+ * range expression ```${tag_parts[0..2]}``` is also supported. see [unit test](https://github.com/y-ken/fluent-mixin-rewrite-tag-name/blob/master/test/mixin/test_rewrite_tag_name.rb#L97).
25
24
 
26
25
  #### Placeholder Option
27
26
 
@@ -183,9 +182,6 @@ These cool plugins are using this mixin!
183
182
 
184
183
  ## TODO
185
184
 
186
- * switchable tag template variable like 'tag', 'tag_format'
187
- * support range tag_parts like [fluent-plugin-forest](https://github.com/tagomoris/fluent-plugin-forest/compare/v0.2.2...master)
188
- * support tag_prefix and tag_suffix placeholder like [fluent-plugin-record-reformer](https://github.com/sonots/fluent-plugin-record-reformer)
189
185
  * merge into [fluentd/lib/fluent/mixin.rb](https://github.com/fluent/fluentd/blob/master/lib/fluent/mixin.rb) as RewriteTagNameMixin module.
190
186
 
191
187
  Pull requests are very welcome!!
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-mixin-rewrite-tag-name"
7
- spec.version = "0.0.3"
7
+ spec.version = "0.1.0"
8
8
  spec.authors = ["Kentaro Yoshida"]
9
9
  spec.email = ["y.ken.studio@gmail.com"]
10
10
  spec.summary = %q{Fluentd mixin plugin to provides placeholder function for rewriting tag for your any plugins as like fluent-plugin-rewrite-tag-filter. It will let you get easy to implement tag placeholder for your own plugins.}
@@ -3,7 +3,6 @@ module Fluent
3
3
  module RewriteTagName
4
4
  include RecordFilterMixin
5
5
  attr_accessor :tag, :hostname_command
6
- attr_accessor :enable_placeholder_upcase
7
6
 
8
7
  DEFAULT_HOSTNAME_COMMAND = 'hostname'
9
8
 
@@ -11,14 +10,6 @@ module Fluent
11
10
  super
12
11
 
13
12
  @placeholder_expander = PlaceholderExpander.new
14
-
15
- if enable_upcase = conf['enable_placeholder_upcase']
16
- @enable_placeholder_upcase = enable_upcase
17
- end
18
- if @enable_placeholder_upcase
19
- @placeholder_expander.enable_placeholder_upcase
20
- end
21
-
22
13
  hostname_command = @hostname_command || DEFAULT_HOSTNAME_COMMAND
23
14
  hostname = `#{hostname_command}`.chomp
24
15
  @placeholder_expander.set_hostname(hostname)
@@ -32,57 +23,74 @@ module Fluent
32
23
  end
33
24
 
34
25
  def rewrite_tag!(tag)
35
-
36
26
  @placeholder_expander.set_tag(tag)
37
27
  emit_tag = @placeholder_expander.expand(@tag)
38
- tag.gsub!(tag, emit_tag)
28
+ tag.sub!(tag, emit_tag)
39
29
  end
40
30
 
41
31
  class PlaceholderExpander
42
32
  # referenced https://github.com/fluent/fluent-plugin-rewrite-tag-filter, thanks!
43
33
  # referenced https://github.com/sonots/fluent-plugin-record-reformer, thanks!
34
+ # referenced https://github.com/tagomoris/fluent-plugin-forest, thanks!
44
35
  attr_reader :placeholders
45
36
 
46
37
  def initialize
38
+ @tag = ''
47
39
  @placeholders = {}
48
- @enable_options = {
49
- :upcase => false,
50
- }
51
40
  end
52
41
 
53
42
  def expand(str)
54
- str.gsub(/(\${[a-z_]+(\[-?[0-9]+\])?}|__[A-Z_]+(\[-?[0-9]+\])?__)/) {
55
- $log.warn "RewriteTagNameMixin: unknown placeholder `#{$1}` found" unless @placeholders.include?($1)
56
- @placeholders[$1]
57
- }
43
+ str = str.gsub(/(\${(tag|hostname)}|__(TAG|HOSTNAME)__)/) do |name|
44
+ $log.warn "RewriteTagNameMixin: unknown placeholder `#{name}` found" unless @placeholders.include?(name)
45
+ @placeholders[name]
46
+ end
47
+ str = str.gsub(/__TAG_PARTS\[-?[0-9]+(?:\.\.\.?-?[0-9]+)?\]__|\$\{tag_parts\[-?[0-9]+(?:\.\.\.?-?[0-9]+)?\]\}/) do |tag_parts_offset|
48
+ expand_tag_parts(tag_parts_offset)
49
+ end
58
50
  end
59
51
 
60
- def enable_placeholder_upcase
61
- @enable_options[:upcase] = true
52
+ def expand_tag_parts(tag_parts_offset)
53
+ begin
54
+ position = /\[(?<first>-?[0-9]+)(?<range_part>(?<range_type>\.\.\.?)(?<last>-?[0-9]+))?\]/.match(tag_parts_offset)
55
+ raise "failed to parse offset even though matching tag_parts" unless position
56
+ tag_parts = @tag.split('.')
57
+ if position[:range_part]
58
+ extract_tag_part_range(tag_parts, position)
59
+ else
60
+ extract_tag_part_index(tag_parts, position)
61
+ end
62
+ rescue StandardError => e
63
+ $log.warn "RewriteTagNameMixin: failed to expand tag_parts. :message=>#{e.message} tag:#{@tag} placeholder:#{tag_parts_matched}"
64
+ nil
65
+ end
66
+ end
67
+
68
+ def extract_tag_part_index(tag_parts, position)
69
+ index = position[:first].to_i
70
+ raise "missing placeholder." unless tag_parts[index]
71
+ tag_parts[index]
72
+ end
73
+
74
+ def extract_tag_part_range(tag_parts, position)
75
+ exclude_end = (position[:range_type] == '...')
76
+ range = Range.new(position[:first].to_i, position[:last].to_i, exclude_end)
77
+ raise "missing placeholder." unless tag_parts[range]
78
+ tag_parts[range].join('.')
62
79
  end
63
80
 
64
81
  def set_tag(value)
82
+ @tag = value
65
83
  set_placeholder('tag', value)
66
- set_tag_parts(value)
67
84
  end
68
85
 
69
86
  def set_hostname(value)
70
87
  set_placeholder('hostname', value)
71
88
  end
72
89
 
73
- def set_tag_parts(tag)
74
- tag_parts = tag.split('.')
75
- size = tag_parts.size
76
- tag_parts.each_with_index { |t, idx|
77
- set_placeholder("tag_parts[#{idx}]", t)
78
- set_placeholder("tag_parts[#{idx-size}]", t) # support tag_parts[-1]
79
- }
80
- end
81
-
82
90
  private
83
91
  def set_placeholder(key, value)
84
92
  @placeholders.store("${#{key.downcase}}", value)
85
- @placeholders.store("__#{key.upcase}__", value) if @enable_options[:upcase]
93
+ @placeholders.store("__#{key.upcase}__", value)
86
94
  end
87
95
  end
88
96
  end
@@ -32,7 +32,6 @@ class RewriteTagNameMixinTest < Test::Unit::TestCase
32
32
  end
33
33
  emits = d1.emits
34
34
  assert_equal 1, emits.length
35
- p emits[0]
36
35
  assert_equal 'rewrited.input.access', emits[0][0] # tag
37
36
  assert_equal 'foo', emits[0][2]['message']
38
37
  end
@@ -41,14 +40,12 @@ class RewriteTagNameMixinTest < Test::Unit::TestCase
41
40
  d1 = create_driver(%[
42
41
  tag rewrited.__TAG__
43
42
  remove_tag_prefix input.
44
- enable_placeholder_upcase true
45
43
  ], 'input.access')
46
44
  d1.run do
47
45
  d1.emit({'message' => 'foo'})
48
46
  end
49
47
  emits = d1.emits
50
48
  assert_equal 1, emits.length
51
- p emits[0]
52
49
  assert_equal 'rewrited.access', emits[0][0] # tag
53
50
  assert_equal 'foo', emits[0][2]['message']
54
51
  end
@@ -63,52 +60,10 @@ class RewriteTagNameMixinTest < Test::Unit::TestCase
63
60
  end
64
61
  emits = d1.emits
65
62
  assert_equal 1, emits.length
66
- p emits[0]
67
63
  assert_equal 'rewrited.access', emits[0][0] # tag
68
64
  assert_equal 'foo', emits[0][2]['message']
69
65
  end
70
66
 
71
- def test_emit_tag_parts
72
- d1 = create_driver(%[
73
- tag rewrited.${tag_parts[1]}
74
- ], 'input.access.foo.bar')
75
- d1.run do
76
- d1.emit({'message' => 'foo'})
77
- end
78
- emits = d1.emits
79
- assert_equal 1, emits.length
80
- p emits[0]
81
- assert_equal 'rewrited.access', emits[0][0] # tag
82
- end
83
-
84
- def test_emit_tag_parts_negative
85
- d1 = create_driver(%[
86
- tag rewrited.${tag_parts[-1]}
87
- ], 'input.access.foo.bar')
88
- d1.run do
89
- d1.emit({'message' => 'foo'})
90
- end
91
- emits = d1.emits
92
- assert_equal 1, emits.length
93
- p emits[0]
94
- assert_equal 'rewrited.bar', emits[0][0] # tag
95
- assert_equal 'foo', emits[0][2]['message']
96
- end
97
-
98
- def test_emit_tag_parts_negative2
99
- d1 = create_driver(%[
100
- tag rewrited.${tag_parts[-2]}
101
- ], 'input.access.foo.bar')
102
- d1.run do
103
- d1.emit({'message' => 'foo'})
104
- end
105
- emits = d1.emits
106
- assert_equal 1, emits.length
107
- p emits[0]
108
- assert_equal 'rewrited.foo', emits[0][0] # tag
109
- assert_equal 'foo', emits[0][2]['message']
110
- end
111
-
112
67
  def test_emit_hostname
113
68
  d1 = create_driver(%[
114
69
  tag rewrited.${hostname}
@@ -118,7 +73,6 @@ class RewriteTagNameMixinTest < Test::Unit::TestCase
118
73
  end
119
74
  emits = d1.emits
120
75
  assert_equal 1, emits.length
121
- p emits[0]
122
76
  hostname = `hostname`.chomp
123
77
  assert_equal "rewrited.#{hostname}", emits[0][0] # tag
124
78
  assert_equal 'foo', emits[0][2]['message']
@@ -134,10 +88,34 @@ class RewriteTagNameMixinTest < Test::Unit::TestCase
134
88
  end
135
89
  emits = d1.emits
136
90
  assert_equal 1, emits.length
137
- p emits[0]
138
91
  hostname_command = d1.instance.config['hostname_command']
139
92
  hostname = `#{hostname_command}`.chomp
140
93
  assert_equal "rewrited.#{hostname}", emits[0][0] # tag
141
94
  assert_equal 'foo', emits[0][2]['message']
142
95
  end
96
+
97
+ def test_emit_tag_parts
98
+ tag = '0.1.2.3.4'
99
+ rules = [
100
+ {:conf=>'tag rewrited.${tag_parts[0]}', :expect_tag => 'rewrited.0'},
101
+ {:conf=>'tag rewrited.${tag_parts[1]}', :expect_tag => 'rewrited.1'},
102
+ {:conf=>'tag rewrited.${tag_parts[-1]}', :expect_tag => 'rewrited.4'},
103
+ {:conf=>'tag rewrited.${tag_parts[-2]}', :expect_tag => 'rewrited.3'},
104
+ {:conf=>'tag rewrited.${tag_parts[1..1000]}', :expect_tag => 'rewrited.1.2.3.4'},
105
+ {:conf=>'tag rewrited.${tag_parts[0..2]}', :expect_tag => 'rewrited.0.1.2'},
106
+ {:conf=>'tag rewrited.${tag_parts[0..-3]}', :expect_tag => 'rewrited.0.1.2'},
107
+ {:conf=>'tag rewrited.${tag_parts[1..-2]}', :expect_tag => 'rewrited.1.2.3'},
108
+ {:conf=>'tag rewrited.${tag_parts[-2..-1]}', :expect_tag => 'rewrited.3.4'},
109
+ ]
110
+ rules.each do |rule|
111
+ d = create_driver(rule[:conf], tag)
112
+ d.run do
113
+ d.emit({'message' => 'foo'})
114
+ end
115
+ emits = d.emits
116
+ assert_equal 1, emits.length
117
+ assert_equal rule[:expect_tag], emits[0][0] # tag
118
+ assert_equal 'foo', emits[0][2]['message']
119
+ end
120
+ end
143
121
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-mixin-rewrite-tag-name
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-22 00:00:00.000000000 Z
12
+ date: 2014-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler