fluent-plugin-string-scrub 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73d0da5e8e95386a46ed8d332e3ad698448b2c5f
4
- data.tar.gz: 64bd7f44ed8164404d4793c6c947f8dff9cd86ab
3
+ metadata.gz: 22a7e6ce42d946054cdfb80294dbc339215242e5
4
+ data.tar.gz: 282dccc73abfd4f2c14209a3478f9d3bbe733ccd
5
5
  SHA512:
6
- metadata.gz: c960490269222cdc16e9ce8d5c1addd1747436b73ed0cc9ef099c0d33989ec148cf9f133620b03f25cb9d070a996ec9dfff110b61f3e71891ae870be56d23aeb
7
- data.tar.gz: 0b1443b6c15cd8ae04915f73230ca9287a2b0b033c266a446ad97eaa958ac488c07095d3336e8b43f6b99ce89e434ef85ab004e8fe908bf5f4d8ebf420560d0f
6
+ metadata.gz: 6f812ab0fbe3bc87f5e8a46e1a3d715a2da4950e0334631ca456606dbd77b8acd027517a334bea0afcb9e3a75180a5a746c4b0bd1dd82290620f62cb8fc93136
7
+ data.tar.gz: 8646f2982fe4ca357512f739f67d246c3a6808584798b6f64f16167316a8fab8e95725a9212109b5023fc2d9e0caeba6825def2d490e3e6f388c43f6db695147
data/.travis.yml CHANGED
@@ -2,4 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
- - 2.1.0
5
+ - 2.1.*
6
+ gemfile:
7
+ - Gemfile
8
+ - Gemfile.fluentd.lt.0.12
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'fluentd', '~> 0.10.43'
4
+ gemspec
data/README.md CHANGED
@@ -51,6 +51,26 @@ Or install it yourself as:
51
51
  </match>
52
52
  ```
53
53
 
54
+ ## Filter plugin
55
+
56
+ Fluentd >= v0.12 can use filter plugin.
57
+
58
+ ```
59
+ <source>
60
+ type forward
61
+ </source>
62
+
63
+ <filter **>
64
+ type string_scrub
65
+ replace_char ?
66
+ </filter>
67
+
68
+ <match **>
69
+ type stdout
70
+ </match>
71
+ ```
72
+
73
+
54
74
  ## Contributing
55
75
 
56
76
  1. Fork it ( https://github.com/[my-github-username]/fluent-plugin-string-scrub/fork )
@@ -3,7 +3,7 @@ lib = File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-string-scrub"
6
- spec.version = "0.0.4"
6
+ spec.version = "0.1.0"
7
7
  spec.authors = ["Noriaki Katayama"]
8
8
  spec.email = ["kataring@gmail.com"]
9
9
  spec.summary = %q{Fluentd Output filter plugin.}
@@ -0,0 +1,60 @@
1
+ class Fluent::StringScrubFilter < Fluent::Filter
2
+ Fluent::Plugin.register_filter('string_scrub', self)
3
+
4
+ config_param :replace_char, :string, :default => ''
5
+
6
+ def initialize
7
+ super
8
+ require 'string/scrub' if RUBY_VERSION.to_f < 2.1
9
+ end
10
+
11
+ def configure(conf)
12
+ super
13
+
14
+ if @replace_char =~ /\\u\{*[A-F0-9]{4}\}*/
15
+ @replace_char = eval("\"#{@replace_char}\"")
16
+ end
17
+ end
18
+
19
+ def filter_stream(tag, es)
20
+ new_es = Fluent::MultiEventStream.new
21
+ es.each do |time,record|
22
+ begin
23
+ scrubbed = recv_record(record)
24
+ next if scrubbed.nil?
25
+ new_es.add(time, record)
26
+ rescue => e
27
+ router.emit_error_event(tag, time, record, e)
28
+ end
29
+ end
30
+
31
+ new_es
32
+ end
33
+
34
+ def recv_record(record)
35
+ scrubbed = {}
36
+ record.each do |k,v|
37
+ if v.instance_of? Hash
38
+ scrubbed[with_scrub(k)] = recv_record(v)
39
+ else
40
+ scrubbed[with_scrub(k)] = with_scrub(v)
41
+ end
42
+ end
43
+ scrubbed
44
+ end
45
+
46
+ def with_scrub(string)
47
+ begin
48
+ string =~ //
49
+ return string
50
+ rescue ArgumentError => e
51
+ raise e unless e.message.index("invalid byte sequence in") == 0
52
+ if string.frozen?
53
+ string = string.dup.scrub!(@replace_char)
54
+ else
55
+ string.scrub!(@replace_char)
56
+ end
57
+ retry
58
+ end
59
+ end
60
+ end if defined?(Fluent::Filter) # Support only >= v0.12
@@ -6,6 +6,11 @@ class Fluent::StringScrubOutput < Fluent::Output
6
6
  config_param :add_prefix, :string, :default => nil
7
7
  config_param :replace_char, :string, :default => ''
8
8
 
9
+ # Define `router` method of v0.12 to support v0.10 or earlier
10
+ unless method_defined?(:router)
11
+ define_method("router") { Fluent::Engine }
12
+ end
13
+
9
14
  def initialize
10
15
  super
11
16
  require 'string/scrub' if RUBY_VERSION.to_f < 2.1
@@ -14,19 +19,22 @@ class Fluent::StringScrubOutput < Fluent::Output
14
19
  def configure(conf)
15
20
  super
16
21
 
17
- if not @tag and not @remove_prefix and not @add_prefix
18
- raise Fluent::ConfigError, "missing both of remove_prefix and add_prefix"
19
- end
20
- if @tag and (@remove_prefix or @add_prefix)
21
- raise Fluent::ConfigError, "both of tag and remove_prefix/add_prefix must not be specified"
22
- end
23
- if @remove_prefix
24
- @removed_prefix_string = @remove_prefix + '.'
25
- @removed_length = @removed_prefix_string.length
26
- end
27
- if @add_prefix
28
- @added_prefix_string = @add_prefix + '.'
22
+ if conf['@label'].nil?
23
+ if not @tag and not @remove_prefix and not @add_prefix
24
+ raise Fluent::ConfigError, "missing both of remove_prefix and add_prefix"
25
+ end
26
+ if @tag and (@remove_prefix or @add_prefix)
27
+ raise Fluent::ConfigError, "both of tag and remove_prefix/add_prefix must not be specified"
28
+ end
29
+ if @remove_prefix
30
+ @removed_prefix_string = @remove_prefix + '.'
31
+ @removed_length = @removed_prefix_string.length
32
+ end
33
+ if @add_prefix
34
+ @added_prefix_string = @add_prefix + '.'
35
+ end
29
36
  end
37
+
30
38
  if @replace_char =~ /\\u\{*[A-F0-9]{4}\}*/
31
39
  @replace_char = eval("\"#{@replace_char}\"")
32
40
  end
@@ -53,7 +61,7 @@ class Fluent::StringScrubOutput < Fluent::Output
53
61
  es.each do |time,record|
54
62
  scrubbed = recv_record(record)
55
63
  next if scrubbed.nil?
56
- Fluent::Engine.emit(tag, time, scrubbed)
64
+ router.emit(tag, time, scrubbed)
57
65
  end
58
66
 
59
67
  chain.next
data/test/helper.rb CHANGED
@@ -23,6 +23,7 @@ unless ENV.has_key?('VERBOSE')
23
23
  end
24
24
 
25
25
  require 'fluent/plugin/out_string_scrub'
26
+ require 'fluent/plugin/filter_string_scrub'
26
27
 
27
28
  class Test::Unit::TestCase
28
29
  end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ class StringScrubFilterTest < Test::Unit::TestCase
4
+ include Fluent
5
+
6
+ def setup
7
+ Fluent::Test.setup
8
+ @time = Fluent::Engine.now
9
+ end
10
+
11
+ CONFIG = %[
12
+ replace_char ?
13
+ ]
14
+
15
+ CONFIG_UNICODE_1 = %[
16
+ replace_char \uFFFD
17
+ ]
18
+
19
+ CONFIG_UNICODE_2 = %[
20
+ replace_char \u{FFFD}
21
+ ]
22
+
23
+ def create_driver(conf=CONFIG, tag='test.filter')
24
+ Fluent::Test::FilterTestDriver.new(Fluent::StringScrubFilter).configure(conf, tag)
25
+ end
26
+
27
+ def filter(config, msgs)
28
+ d = create_driver(config)
29
+ d.run {
30
+ msgs.each {|msg|
31
+ d.filter(msg, @time)
32
+ }
33
+ }
34
+ filtered = d.filtered_as_array
35
+ filtered.map {|m| m[2] }
36
+ end
37
+
38
+ def test_filter1
39
+ return unless defined? Fluent::Filter
40
+
41
+ orig_message = 'testtesttest'
42
+ invalid_utf8 = "\xff".force_encoding('UTF-8')
43
+ msg = {"message" => orig_message + invalid_utf8}
44
+ filtered = filter(CONFIG, [msg])
45
+ assert_equal([{"message" => orig_message + '?'}], filtered)
46
+ end
47
+
48
+ end
@@ -28,8 +28,8 @@ class StringScrubOutputTest < Test::Unit::TestCase
28
28
  replace_char \u{FFFD}
29
29
  ]
30
30
 
31
- def create_driver(conf=CONFIG,tag='test')
32
- Fluent::Test::OutputTestDriver.new(Fluent::StringScrubOutput, tag).configure(conf)
31
+ def create_driver(conf = CONFIG, tag = 'test', use_v1_config = true)
32
+ Fluent::Test::OutputTestDriver.new(Fluent::StringScrubOutput, tag).configure(conf, use_v1_config)
33
33
  end
34
34
 
35
35
  def test_configure
@@ -78,6 +78,26 @@ class StringScrubOutputTest < Test::Unit::TestCase
78
78
  }
79
79
  end
80
80
 
81
+ def test_emit_fluentd_0_12
82
+ if Fluent::VERSION >= "0.12"
83
+ in_tag = 'input.log'
84
+ orig_message = 'testtesttest'
85
+ Fluent::Engine.root_agent.add_label('@foo')
86
+ d = create_driver(%[
87
+ @label @foo
88
+ ], in_tag)
89
+ label = Fluent::Engine.root_agent.find_label('@foo')
90
+ assert_equal(label.event_router, d.instance.router)
91
+
92
+ d.run do
93
+ d.emit({'message' => orig_message})
94
+ end
95
+ emits = d.emits
96
+ tag, time, record = emits.first
97
+ assert_equal(in_tag, tag)
98
+ end
99
+ end
100
+
81
101
  def test_emit1_invalid_string
82
102
  orig_message = 'testtesttest'
83
103
  invalid_utf8 = "\xff".force_encoding('UTF-8')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-string-scrub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noriaki Katayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-24 00:00:00.000000000 Z
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -62,12 +62,15 @@ files:
62
62
  - ".gitignore"
63
63
  - ".travis.yml"
64
64
  - Gemfile
65
+ - Gemfile.fluentd.lt.0.12
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
68
69
  - fluent-plugin-string-scrub.gemspec
70
+ - lib/fluent/plugin/filter_string_scrub.rb
69
71
  - lib/fluent/plugin/out_string_scrub.rb
70
72
  - test/helper.rb
73
+ - test/plugin/test_filter_string-scrub.rb
71
74
  - test/plugin/test_out_string-scrub.rb
72
75
  homepage: https://github.com/kataring/fluent-plugin-string-scrub
73
76
  licenses:
@@ -95,4 +98,5 @@ specification_version: 4
95
98
  summary: Fluentd Output filter plugin.
96
99
  test_files:
97
100
  - test/helper.rb
101
+ - test/plugin/test_filter_string-scrub.rb
98
102
  - test/plugin/test_out_string-scrub.rb