fluent-plugin-record-serializer 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.
@@ -0,0 +1,14 @@
1
+ .bundle
2
+ .yardoc
3
+ Gemfile.lock
4
+ _yardoc
5
+ coverage
6
+ doc
7
+ pkg
8
+ spec/reports
9
+ tmp
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-record-serializer.gemspec
4
+ gemspec
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2015- IKUTA Masahito
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,56 @@
1
+ # fluent-plugin-record-serializer
2
+
3
+ [fluentd](http://fluentd.org) filter plugin that serialize a record.
4
+
5
+ [![Build Status](https://travis-ci.org/cooldaemon/fluent-plugin-record-serializer.svg?branch=master)](https://travis-ci.org/cooldaemon/fluent-plugin-record-serializer)
6
+ [![Code Climate](https://codeclimate.com/github/cooldaemon/fluent-plugin-record-serializer/badges/gpa.svg)](https://codeclimate.com/github/cooldaemon/fluent-plugin-record-serializer)
7
+
8
+ If following record is passed:
9
+
10
+ ```
11
+ {"message": "hello world!"}
12
+ ```
13
+
14
+ then you got new record like below:
15
+
16
+ ```
17
+ {"tag": "pattern", "payload": "{\"message\": \"hello world!\"}"}
18
+ ```
19
+
20
+ ## Installation
21
+
22
+ Install with gem or fluent-gem command as:
23
+
24
+ ```
25
+ # for fluentd
26
+ $ gem install fluent-plugin-record-serializer
27
+
28
+ # for td-agent
29
+ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-record-serializer
30
+ ```
31
+
32
+ ## Configuration
33
+
34
+ In v0.12, you can use record_modifier filter.
35
+
36
+ ```
37
+ <filter pattern>
38
+ type record_serializer
39
+ </filter>
40
+ ```
41
+
42
+ In v0.10, you can use record_serializer output to emulate filter.
43
+
44
+ ```
45
+ <match pattern>
46
+ type record_serializer
47
+ tag serialized.pattern
48
+ </match>
49
+ ```
50
+
51
+ ## Copyright
52
+
53
+ - Copyright
54
+ - Copyright(C) 2015- IKUTA Masahito (cooldaemon)
55
+ - License
56
+ - Apache License, Version 2.0
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fluent/plugin/record/serializer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fluent-plugin-record-serializer"
8
+ spec.version = Fluent::Plugin::Record::Serializer::VERSION
9
+ spec.authors = ["IKUTA Masahito"]
10
+ spec.email = ["cooldaemon@gmail.com"]
11
+ spec.summary = %q{Fluentd output filter plugin for serialize record.}
12
+ spec.description = spec.summary
13
+ spec.homepage = "http://github.com/cooldaemon/fluent-plugin-record-serializer"
14
+ spec.license = "APLv2"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "test-unit", ["~> 3.0", "~> 3.1"]
23
+
24
+ spec.add_dependency "fluentd", ">= 0.10.53"
25
+ end
@@ -0,0 +1,24 @@
1
+ require 'fluent/plugin/record_serializer'
2
+
3
+ module Fluent
4
+ class RecordSerializerFilter < Filter
5
+ Fluent::Plugin.register_filter('record_serializer', self)
6
+
7
+ config_param :format, :string, :default => 'json'
8
+ config_param :field_name, :string, :default => 'payload'
9
+
10
+ include SetTagKeyMixin
11
+ include Fluent::RecordSerializer
12
+
13
+ def filter(tag, time, record)
14
+ begin
15
+ serialized_record = serialize_record(@format, record)
16
+ rescue => e
17
+ $log.warn "serialize error: #{e.message}"
18
+ return
19
+ end
20
+
21
+ {'tag' => tag, @field_name => serialized_record}
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ require 'fluent/plugin/record_serializer'
2
+
3
+ module Fluent
4
+ class RecordSerializerOutput < Output
5
+ Fluent::Plugin.register_output('record_serializer', self)
6
+
7
+ unless method_defined?(:router)
8
+ define_method("router") { Fluent::Engine }
9
+ end
10
+
11
+ config_param :tag, :string
12
+ config_param :format, :string, :default => 'json'
13
+ config_param :field_name, :string, :default => 'payload'
14
+
15
+ include SetTagKeyMixin
16
+ include Fluent::RecordSerializer
17
+
18
+ def emit(tag, es, chain)
19
+ es.each { |time, record|
20
+ begin
21
+ serialized_record = serialize_record(@format, record)
22
+ rescue => e
23
+ $log.warn "serialize error: #{e.message}"
24
+ next
25
+ end
26
+
27
+ router.emit(@tag, time, {
28
+ 'tag' => @tag,
29
+ @field_name => serialized_record
30
+ })
31
+ }
32
+
33
+ chain.next
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ module Fluent
2
+ module Plugin
3
+ module Record
4
+ module Serializer
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module Fluent
2
+ module RecordSerializer
3
+ def serialize_record(format, record)
4
+ if format == 'json'
5
+ json_dump(record)
6
+ elsif format == 'yaml'
7
+ record.to_yaml
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ begin
14
+ require 'oj'
15
+ def json_dump(record)
16
+ Oj.dump(record, :mode => :compat)
17
+ end
18
+ rescue LoadError
19
+ def json_dump(record)
20
+ record.to_json
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+
15
+ require 'fileutils'
16
+ require 'fluent/log'
17
+ require 'fluent/test'
18
+
19
+ $log = Fluent::Log.new(STDOUT, Fluent::Log::LEVEL_DEBUG)
20
+
21
+ unless defined?(Test::Unit::AssertionFailedError)
22
+ class Test::Unit::AssertionFailedError < StandardError
23
+ end
24
+ end
25
+
26
+ class Test::Unit::TestCase
27
+ end
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+ require 'fluent/plugin/filter_record_serializer'
3
+ require 'yaml'
4
+
5
+ exit unless defined?(Fluent::Filter)
6
+
7
+ class RecordSerializerFilterTest < Test::Unit::TestCase
8
+ def setup
9
+ Fluent::Test.setup
10
+ end
11
+
12
+ TAG = 'test.serialize'
13
+ FORMAT = 'json'
14
+ FIELD_NAME = 'payload'
15
+
16
+ CONFIG = %[
17
+ type record_modifier
18
+ ]
19
+
20
+ YAML_CONFIG = %[
21
+ type record_modifier
22
+ format yaml
23
+ ]
24
+
25
+ def create_driver(conf=CONFIG)
26
+ Fluent::Test::FilterTestDriver.new(Fluent::RecordSerializerFilter, TAG).configure(conf)
27
+ end
28
+
29
+ def test_configure
30
+ d = create_driver
31
+ assert_equal FORMAT, d.instance.format
32
+ assert_equal FIELD_NAME, d.instance.field_name
33
+ end
34
+
35
+ def test_json
36
+ record = {'spam' => 1, 'ham' => 'egg',}
37
+ d = create_driver
38
+
39
+ d.run do
40
+ d.emit(record)
41
+ end
42
+
43
+ assert_equal [{'tag' => TAG, FIELD_NAME => record.to_json}], d.filtered_as_array.map { |e| e.last }
44
+ end
45
+
46
+ def test_yaml
47
+ record = {'spam' => 1, 'ham' => 'egg',}
48
+ d = create_driver YAML_CONFIG
49
+
50
+ d.run do
51
+ d.emit(record)
52
+ end
53
+
54
+ assert_equal [{'tag' => TAG, FIELD_NAME => record.to_yaml}], d.filtered_as_array.map { |e| e.last }
55
+ end
56
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+ require 'fluent/plugin/out_record_serializer'
3
+ require 'yaml'
4
+
5
+ class RecordSerializerOutputTest < Test::Unit::TestCase
6
+ def setup
7
+ Fluent::Test.setup
8
+ end
9
+
10
+ TAG = 'test.serialize'
11
+ FORMAT = 'json'
12
+ FIELD_NAME = 'payload'
13
+
14
+ CONFIG = %[
15
+ tag #{TAG}
16
+ ]
17
+
18
+ YAML_CONFIG = %[
19
+ tag #{TAG}
20
+ format yaml
21
+ ]
22
+
23
+ def create_driver(conf=CONFIG)
24
+ Fluent::Test::OutputTestDriver.new(Fluent::RecordSerializerOutput).configure(conf)
25
+ end
26
+
27
+ def test_configure
28
+ d = create_driver
29
+ assert_equal FORMAT, d.instance.format
30
+ assert_equal FIELD_NAME, d.instance.field_name
31
+ end
32
+
33
+ def test_json
34
+ record = {'spam' => 1, 'ham' => 'egg',}
35
+ d = create_driver
36
+
37
+ d.run do
38
+ d.emit(record)
39
+ end
40
+
41
+ assert_equal [{'tag' => TAG, FIELD_NAME => record.to_json}], d.records
42
+ end
43
+
44
+ def test_yaml
45
+ record = {'spam' => 1, 'ham' => 'egg',}
46
+ d = create_driver YAML_CONFIG
47
+
48
+ d.run do
49
+ d.emit(record)
50
+ end
51
+
52
+ assert_equal [{'tag' => TAG, FIELD_NAME => record.to_yaml}], d.records
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-record-serializer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - IKUTA Masahito
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2015-11-29 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ type: :development
23
+ name: rake
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 10
30
+ - 0
31
+ version: "10.0"
32
+ requirement: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ prerelease: false
35
+ type: :development
36
+ name: test-unit
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 3
43
+ - 0
44
+ version: "3.0"
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 3
49
+ - 1
50
+ version: "3.1"
51
+ requirement: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ prerelease: false
54
+ type: :runtime
55
+ name: fluentd
56
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ - 10
63
+ - 53
64
+ version: 0.10.53
65
+ requirement: *id003
66
+ description: Fluentd output filter plugin for serialize record.
67
+ email:
68
+ - cooldaemon@gmail.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - fluent-plugin-record-serializer.gemspec
83
+ - lib/fluent/plugin/filter_record_serializer.rb
84
+ - lib/fluent/plugin/out_record_serializer.rb
85
+ - lib/fluent/plugin/record/serializer/version.rb
86
+ - lib/fluent/plugin/record_serializer.rb
87
+ - test/helper.rb
88
+ - test/plugin/test_filter_record_serializer.rb
89
+ - test/plugin/test_out_record_serializer.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/cooldaemon/fluent-plugin-record-serializer
92
+ licenses:
93
+ - APLv2
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.3.6
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Fluentd output filter plugin for serialize record.
120
+ test_files:
121
+ - test/helper.rb
122
+ - test/plugin/test_filter_record_serializer.rb
123
+ - test/plugin/test_out_record_serializer.rb