fluent-plugin-rewrite-tag-filter 1.4.0 → 1.4.1

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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ vendor/*
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
+ - 2.1.0
4
5
  - 2.0.0
5
6
  - 1.9.3
6
7
 
data/README.md CHANGED
@@ -2,16 +2,14 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- ### RewriteTagFilterOutput
6
-
7
- Fluentd Output filter plugin. It has designed to rewrite tag like mod_rewrite.
8
- Re-emmit a record with rewrited tag when a value matches/unmatches with the regular expression.
9
- Also you can change a tag from apache log by domain, status-code(ex. 500 error),
5
+ Rewrite Tag Filter for [Fluentd](http://fluentd.org). It is designed to rewrite tags like mod_rewrite.
6
+ Re-emit the record with rewrited tag when a value matches/unmatches with a regular expression.
7
+ Also you can change a tag from Apache log by domain, status code (ex. 500 error),
10
8
  user-agent, request-uri, regex-backreference and so on with regular expression.
11
9
 
12
10
  ## Installation
13
11
 
14
- install with gem or fluent-gem command as:
12
+ Install with gem or fluent-gem command as:
15
13
 
16
14
  ```
17
15
  # for fluentd
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fluent-plugin-rewrite-tag-filter"
6
- s.version = "1.4.0"
6
+ s.version = "1.4.1"
7
7
  s.license = "Apache 2.0"
8
8
  s.authors = ["Kentaro Yoshida"]
9
9
  s.email = ["y.ken.studio@gmail.com"]
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.add_development_dependency "rake"
19
19
  s.add_runtime_dependency "fluentd"
20
+ s.add_runtime_dependency "string-scrub"
20
21
  end
@@ -7,6 +7,11 @@ class Fluent::RewriteTagFilterOutput < Fluent::Output
7
7
 
8
8
  MATCH_OPERATOR_EXCLUDE = '!'
9
9
 
10
+ def initialize
11
+ super
12
+ require 'string/scrub'
13
+ end
14
+
10
15
  def configure(conf)
11
16
  super
12
17
 
@@ -57,13 +62,13 @@ class Fluent::RewriteTagFilterOutput < Fluent::Output
57
62
  @rewriterules.each do |rewritekey, regexp, match_operator, rewritetag|
58
63
  rewritevalue = record[rewritekey].to_s
59
64
  next if rewritevalue.empty? && match_operator != MATCH_OPERATOR_EXCLUDE
60
- matched = regexp && regexp.match(rewritevalue)
65
+ last_match = regexp_last_match(regexp, rewritevalue)
61
66
  case match_operator
62
67
  when MATCH_OPERATOR_EXCLUDE
63
- next if matched
68
+ next if last_match
64
69
  else
65
- next if !matched
66
- backreference_table = get_backreference_table($~.captures)
70
+ next if !last_match
71
+ backreference_table = get_backreference_table(last_match.captures)
67
72
  rewritetag = rewritetag.gsub(/\$\d+/, backreference_table)
68
73
  end
69
74
  rewritetag = rewritetag.gsub(/(\${[a-z_]+(\[[0-9]+\])?}|__[A-Z_]+__)/) do
@@ -75,6 +80,18 @@ class Fluent::RewriteTagFilterOutput < Fluent::Output
75
80
  return nil
76
81
  end
77
82
 
83
+ def regexp_last_match(regexp, rewritevalue)
84
+ begin
85
+ return if regexp.nil?
86
+ regexp.match(rewritevalue)
87
+ return $~
88
+ rescue ArgumentError => e
89
+ raise e unless e.message.index('invalid byte sequence in') == 0
90
+ regexp.match(rewritevalue.scrub('?'))
91
+ return $~
92
+ end
93
+ end
94
+
78
95
  def parse_rewriterule(rule)
79
96
  if rule.match(/^([^\s]+)\s+(.+?)\s+([^\s]+)$/)
80
97
  return $~.captures
@@ -63,6 +63,11 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
63
63
  rewriterule4 world ^[a-z]+$ application.${tag_parts[1]}.future_server
64
64
  ]
65
65
 
66
+ # test for invalid byte sequence in UTF-8 error
67
+ CONFIG_INVALID_BYTE = %[
68
+ rewriterule1 client_name (.+) app.$1
69
+ ]
70
+
66
71
  def create_driver(conf=CONFIG,tag='test')
67
72
  Fluent::Test::OutputTestDriver.new(Fluent::RewriteTagFilterOutput, tag).configure(conf)
68
73
  end
@@ -87,7 +92,6 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
87
92
  rewriterule1 domain ^www.google.com$ site.Google
88
93
  rewriterule2 domain ^news.google.com$ site.GoogleNews
89
94
  ]
90
- puts d.instance.inspect
91
95
  assert_equal 'domain ^www.google.com$ site.Google', d.instance.config['rewriterule1']
92
96
  assert_equal 'domain ^news.google.com$ site.GoogleNews', d.instance.config['rewriterule2']
93
97
  end
@@ -104,16 +108,11 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
104
108
  end
105
109
  emits = d1.emits
106
110
  assert_equal 5, emits.length
107
- p emits[0]
108
111
  assert_equal 'site.Google', emits[0][0] # tag
109
- p emits[1]
110
112
  assert_equal 'site.GoogleNews', emits[1][0] # tag
111
113
  assert_equal 'news.google.com', emits[1][2]['domain']
112
- p emits[2]
113
114
  assert_equal 'agent.MacOSX', emits[2][0] #tag
114
- p emits[3]
115
115
  assert_equal 'agent.Googlebot-FooBar', emits[3][0] #tag
116
- p emits[4]
117
116
  assert_equal 'site.input.access.tagtest', emits[4][0] #tag
118
117
  end
119
118
 
@@ -127,14 +126,10 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
127
126
  end
128
127
  emits = d1.emits
129
128
  assert_equal 4, emits.length
130
- p emits[0]
131
129
  assert_equal 'site.Google', emits[0][0] # tag
132
- p emits[1]
133
130
  assert_equal 'site.GoogleNews', emits[1][0] # tag
134
131
  assert_equal 'news.google.com', emits[1][2]['domain']
135
- p emits[2]
136
132
  assert_equal 'agent.MacOSX', emits[2][0] #tag
137
- p emits[3]
138
133
  assert_equal 'agent.Googlebot-Foobar', emits[3][0] #tag
139
134
  end
140
135
 
@@ -145,7 +140,6 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
145
140
  end
146
141
  emits = d1.emits
147
142
  assert_equal 1, emits.length
148
- p emits[0]
149
143
  assert_equal 'access', emits[0][0] # tag
150
144
  end
151
145
 
@@ -156,7 +150,6 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
156
150
  end
157
151
  emits = d1.emits
158
152
  assert_equal 1, emits.length
159
- p emits[0]
160
153
  assert_equal 'access', emits[0][0] # tag
161
154
  end
162
155
 
@@ -167,7 +160,6 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
167
160
  end
168
161
  emits = d1.emits
169
162
  assert_equal 1, emits.length
170
- p emits[0]
171
163
  assert_equal `hostname -s`.chomp, emits[0][0] # tag
172
164
  end
173
165
 
@@ -180,11 +172,8 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
180
172
  end
181
173
  emits = d1.emits
182
174
  assert_equal 3, emits.length
183
- p emits[0]
184
175
  assert_equal 'start_with_www', emits[0][0] # tag
185
- p emits[1]
186
176
  assert_equal 'not_start_with_www', emits[1][0] # tag
187
- p emits[2]
188
177
  assert_equal 'not_start_with_www', emits[2][0] # tag
189
178
  end
190
179
 
@@ -196,9 +185,7 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
196
185
  end
197
186
  emits = d1.emits
198
187
  assert_equal 2, emits.length
199
- p emits[0]
200
188
  assert_equal 'site.Google', emits[0][0] # tag
201
- p emits[1]
202
189
  assert_equal 'site.GoogleNews', emits[1][0] # tag
203
190
  end
204
191
 
@@ -212,20 +199,33 @@ class RewriteTagFilterOutputTest < Test::Unit::TestCase
212
199
  d1.emit({'user_id' => '99999', 'world' => 'space', 'user_name' => 'Harlock'})
213
200
  end
214
201
  emits = d1.emits
215
- p emits
216
202
  assert_equal 5, emits.length
217
- p emits[0]
218
203
  assert_equal 'application.game.chaos_server', emits[0][0]
219
- p emits[1]
220
204
  assert_equal 'application.game.chaos_server', emits[1][0]
221
- p emits[2]
222
205
  assert_equal 'application.production.future_server', emits[2][0]
223
- p emits[3]
224
206
  assert_equal 'vip.production.remember_love', emits[3][0]
225
- p emits[4]
226
207
  assert_equal 'api.game.production', emits[4][0]
227
208
  end
228
209
 
210
+ def test_emit9_invalid_byte
211
+ invalid_utf8 = "\xff".force_encoding('UTF-8')
212
+ d1 = create_driver(CONFIG_INVALID_BYTE, 'input.activity')
213
+ d1.run do
214
+ d1.emit({'client_name' => invalid_utf8})
215
+ end
216
+ emits = d1.emits
217
+ assert_equal 1, emits.length
218
+ assert_equal "app.?", emits[0][0]
219
+ assert_equal invalid_utf8, emits[0][2]['client_name']
229
220
 
221
+ invalid_ascii = "\xff".force_encoding('US-ASCII')
222
+ d1 = create_driver(CONFIG_INVALID_BYTE, 'input.activity')
223
+ d1.run do
224
+ d1.emit({'client_name' => invalid_ascii})
225
+ end
226
+ emits = d1.emits
227
+ assert_equal 1, emits.length
228
+ assert_equal "app.?", emits[0][0]
229
+ assert_equal invalid_ascii, emits[0][2]['client_name']
230
+ end
230
231
  end
231
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-rewrite-tag-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
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: 2013-12-17 00:00:00.000000000 Z
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: string-scrub
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  description:
47
63
  email:
48
64
  - y.ken.studio@gmail.com