fluent-plugin-rewrite 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "fluent-plugin-rewrite"
3
- gem.version = '0.0.5'
3
+ gem.version = '0.0.6'
4
4
  gem.authors = ["Kentaro Kuribayashi"]
5
5
  gem.email = ["kentarok@gmail.com"]
6
6
  gem.homepage = "http://github.com/kentaro/fluent-plugin-rewrite"
@@ -41,23 +41,8 @@ module Fluent
41
41
  end
42
42
 
43
43
  def emit(tag, es, chain)
44
- es.each do |time, record|
45
- tag, record = rewrite(tag, record)
46
- Engine.emit(tag, time, record) if tag && record
47
- end
48
-
49
- chain.next
50
- end
51
-
52
- def length_will_be_removed
53
- return 0 unless @remove_prefix
54
- (@remove_prefix + '.').length
55
- end
56
-
57
- def rewrite(tag, record)
58
44
  if @remove_prefix and
59
- ((tag.start_with?(@removed_prefix_string) && tag.length > @removed_length) ||
60
- tag == @remove_prefix)
45
+ ((tag.start_with?(@removed_prefix_string) && tag.length > @removed_length) || tag == @remove_prefix)
61
46
  tag = tag[@removed_length..-1] || ''
62
47
  end
63
48
 
@@ -65,6 +50,15 @@ module Fluent
65
50
  tag = tag && tag.length > 0 ? @added_prefix_string + tag : @add_prefix
66
51
  end
67
52
 
53
+ es.each do |time, record|
54
+ filtered_tag, record = rewrite(tag, record)
55
+ Engine.emit(filtered_tag, time, record) if (filtered_tag && record) && (filtered_tag != tag)
56
+ end
57
+
58
+ chain.next
59
+ end
60
+
61
+ def rewrite(tag, record)
68
62
  rules.each do |rule|
69
63
  tag, record, last = apply_rule(rule, tag, record)
70
64
 
@@ -76,10 +70,10 @@ module Fluent
76
70
  end
77
71
 
78
72
  def apply_rule(rule, tag, record)
79
- tag = rule["append_to_tag"] ? tag.dup : tag
80
- key = rule["key"]
81
- pattern = rule["pattern"]
82
- last = nil
73
+ tag_prefix = tag && tag.length > 0 ? "." : ""
74
+ key = rule["key"]
75
+ pattern = rule["pattern"]
76
+ last = nil
83
77
 
84
78
  return [tag, record] if !key || !record.has_key?(key)
85
79
  return [tag, record] unless pattern
@@ -94,9 +88,11 @@ module Fluent
94
88
 
95
89
  if rule["append_to_tag"]
96
90
  if rule["tag"]
97
- tag << (tag == "" ? "" : ".") << "#{rule["tag"]}"
91
+ tag += (tag_prefix + rule["tag"])
98
92
  else
99
- matched.captures.each { |m| tag << (tag == "" ? "" : ".") << "#{m}" }
93
+ matched.captures.each do |m|
94
+ tag += (tag_prefix + "#{m}")
95
+ end
100
96
  end
101
97
  end
102
98
 
@@ -105,7 +101,7 @@ module Fluent
105
101
  end
106
102
  else
107
103
  if rule["append_to_tag"] && rule["fallback"]
108
- tag << ".#{rule["fallback"]}"
104
+ tag += (tag_prefix + rule["fallback"])
109
105
  end
110
106
  end
111
107
 
@@ -160,8 +160,6 @@ class RewriteOutputTest < Test::Unit::TestCase
160
160
  )
161
161
 
162
162
  d4 = create_driver(%[
163
- remove_prefix test
164
-
165
163
  <rule>
166
164
  key path
167
165
  pattern ^\/(users|entries)
@@ -170,13 +168,11 @@ class RewriteOutputTest < Test::Unit::TestCase
170
168
  ])
171
169
 
172
170
  assert_equal(
173
- [ "users", { "path" => "/users/antipop" } ],
171
+ [ "test.users", { "path" => "/users/antipop" } ],
174
172
  d4.instance.rewrite("test", { "path" => "/users/antipop" })
175
173
  )
176
174
 
177
175
  d5 = create_driver(%[
178
- remove_prefix test
179
-
180
176
  <rule>
181
177
  key is_logged_in
182
178
  pattern 1
@@ -186,7 +182,7 @@ class RewriteOutputTest < Test::Unit::TestCase
186
182
  ])
187
183
 
188
184
  assert_equal(
189
- [ "user", { "is_logged_in" => "1" } ],
185
+ [ "test.user", { "is_logged_in" => "1" } ],
190
186
  d5.instance.rewrite("test", { "is_logged_in" => "1" })
191
187
  )
192
188
  end
@@ -250,24 +246,6 @@ class RewriteOutputTest < Test::Unit::TestCase
250
246
  )
251
247
  end
252
248
 
253
- def test_tag_prefix
254
- d = create_driver(%[
255
- remove_prefix test
256
- add_prefix filtered
257
-
258
- <rule>
259
- key path
260
- pattern ^\/(users|entries)
261
- append_to_tag true
262
- </rule>
263
- ])
264
-
265
- assert_equal(
266
- [ "filtered.users", { "path" => "/users/antipop" } ],
267
- d.instance.rewrite("test", { "path" => "/users/antipop" })
268
- )
269
- end
270
-
271
249
  def test_emit
272
250
  d = create_driver(%[
273
251
  remove_prefix test
@@ -293,15 +271,21 @@ class RewriteOutputTest < Test::Unit::TestCase
293
271
 
294
272
  d.run do
295
273
  d.emit({ "path" => "/foo?bar=1" })
296
- d.emit({ "path" => "/users/antipop?hoge=1" })
297
274
  d.emit({ "path" => "/foo?bar=1", "status" => "500" })
275
+ d.emit({ "path" => "/users/antipop" })
276
+ d.emit({ "path" => "/users/kentaro" })
277
+ d.emit({ "path" => "/entries/1" })
298
278
  end
299
279
  emits = d.emits
300
280
 
301
- assert_equal 2, emits.size
281
+ assert_equal 4, emits.size
302
282
  assert_equal('filtered.others', emits[0][0])
303
283
  assert_equal({ "path" => "/foo" }, emits[0][2])
304
284
  assert_equal('filtered.users', emits[1][0])
305
285
  assert_equal({ "path" => "/users/antipop" }, emits[1][2])
286
+ assert_equal('filtered.users', emits[2][0])
287
+ assert_equal({ "path" => "/users/kentaro" }, emits[2][2])
288
+ assert_equal('filtered.entries', emits[3][0])
289
+ assert_equal({ "path" => "/entries/1" }, emits[3][2])
306
290
  end
307
291
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-rewrite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2012-07-02 00:00:00.000000000 Z
12
+ date: 2012-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake