fluent-plugin-flatten 0.0.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 +18 -0
- data/Gemfile +2 -0
- data/LICENSE +2 -0
- data/README.md +81 -0
- data/Rakefile +9 -0
- data/fluent-plugin-flatten.gemspec +18 -0
- data/lib/fluent/plugin/out_flatten.rb +46 -0
- data/test/plugin/test_out_flatten.rb +39 -0
- data/test/test_helper.rb +32 -0
- metadata +104 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# fluent-plugin-flatten
|
|
2
|
+
|
|
3
|
+
## Component
|
|
4
|
+
|
|
5
|
+
### FlattenOutput
|
|
6
|
+
|
|
7
|
+
Fluentd output plugin to flatten JSON-formatted string values in records to top level key/value-s.
|
|
8
|
+
|
|
9
|
+
## Synopsis
|
|
10
|
+
|
|
11
|
+
When you have a config as below:
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
<match test.**>
|
|
15
|
+
type flatten
|
|
16
|
+
key foo
|
|
17
|
+
</match>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
And you feed such a value into fluentd:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
{
|
|
24
|
+
"foo" => '{"bar" : {"qux" : "quux", "hoe" : "poe" }, "baz" : "bazz" }',
|
|
25
|
+
"hoge" => "fuga"
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Then you'll get:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
{
|
|
33
|
+
"foo" => '{"bar" : {"qux" : "quux", "hoe" : "poe" }, "baz" : "bazz" }',
|
|
34
|
+
"hoge" => "fuga",
|
|
35
|
+
|
|
36
|
+
"foo.bar.qux" => "quux",
|
|
37
|
+
"foo.bar.hoe" => "poe",
|
|
38
|
+
"foo.baz" => "bazz"
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
That is, JSON-formatted string in the value of the key `foo` is flattened and now put into the top level of the hash.
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
### key
|
|
47
|
+
|
|
48
|
+
The `key` is used to point a key whose value contains JSON-formatted
|
|
49
|
+
string.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
Add this line to your application's Gemfile:
|
|
54
|
+
|
|
55
|
+
gem 'fluent-plugin-flatten'
|
|
56
|
+
|
|
57
|
+
And then execute:
|
|
58
|
+
|
|
59
|
+
$ bundle
|
|
60
|
+
|
|
61
|
+
Or install it yourself as:
|
|
62
|
+
|
|
63
|
+
$ gem install fluent-plugin-flatten
|
|
64
|
+
|
|
65
|
+
## Contributing
|
|
66
|
+
|
|
67
|
+
1. Fork it
|
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
69
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
71
|
+
5. Create new Pull Request
|
|
72
|
+
|
|
73
|
+
## Copyright
|
|
74
|
+
|
|
75
|
+
### Copyright
|
|
76
|
+
|
|
77
|
+
Copyright (c) 2012- Kentaro Kuribayashi (@kentaro)
|
|
78
|
+
|
|
79
|
+
### License
|
|
80
|
+
|
|
81
|
+
Apache License, Version 2.0
|
data/Rakefile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Gem::Specification.new do |gem|
|
|
2
|
+
gem.name = 'fluent-plugin-flatten'
|
|
3
|
+
gem.version = '0.0.1'
|
|
4
|
+
gem.authors = ['Kentaro Kuribayashi']
|
|
5
|
+
gem.email = ['kentarok@gmail.com']
|
|
6
|
+
gem.homepage = 'http://github.com/kentaro/fluent-plugin-flatten'
|
|
7
|
+
gem.description = %q{Fluentd plugin to flatten JSON-formatted string values to top level key/value-s.}
|
|
8
|
+
gem.summary = %q{Fluentd plugin to flatten JSON-formatted string values to top level key/value-s.}
|
|
9
|
+
|
|
10
|
+
gem.files = `git ls-files`.split($\)
|
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
13
|
+
gem.require_paths = ['lib']
|
|
14
|
+
|
|
15
|
+
gem.add_development_dependency 'rake'
|
|
16
|
+
gem.add_development_dependency 'fluentd'
|
|
17
|
+
gem.add_runtime_dependency 'fluentd'
|
|
18
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module Fluent
|
|
4
|
+
class FlattenOutput < Output
|
|
5
|
+
class Error < StandardError; end
|
|
6
|
+
|
|
7
|
+
Fluent::Plugin.register_output('flatten', self)
|
|
8
|
+
config_param :key, :string
|
|
9
|
+
|
|
10
|
+
def emit(tag, es, chain)
|
|
11
|
+
es.each do |time, record|
|
|
12
|
+
Engine.emit(tag, time, flatten(record))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
chain.next
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def flatten(record)
|
|
19
|
+
if record.has_key?(@key)
|
|
20
|
+
hash = JSON.parse(record[@key])
|
|
21
|
+
record = record.merge(_flatten(@key, hash))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
record
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def _flatten(root, hash)
|
|
28
|
+
unless hash.is_a?(Hash)
|
|
29
|
+
raise Error.new('The value to be flattened must be a Hash: #{hash}')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
flattened = {}
|
|
33
|
+
hash.each do |path, value|
|
|
34
|
+
key = [root, path].join('.')
|
|
35
|
+
|
|
36
|
+
if value.is_a?(String)
|
|
37
|
+
flattened[key] = value
|
|
38
|
+
else
|
|
39
|
+
flattened = flattened.merge(_flatten(key, value))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
flattened
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class FlattenOutputTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
Fluent::Test.setup
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def create_driver(conf, tag = 'test')
|
|
9
|
+
Fluent::Test::OutputTestDriver.new(Fluent::FlattenOutput, tag).configure(conf)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_configure
|
|
13
|
+
d = create_driver(%[
|
|
14
|
+
key foo
|
|
15
|
+
])
|
|
16
|
+
|
|
17
|
+
assert_equal 'foo', d.instance.key
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_emit
|
|
21
|
+
d = create_driver(%[
|
|
22
|
+
key foo
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
d.run do
|
|
26
|
+
d.emit( 'foo' => '{"bar" : "baz"}', 'hoge' => 'fuga' )
|
|
27
|
+
d.emit( 'foo' => '{"bar" : {"qux" : "quux", "hoe" : "poe" }, "baz" : "bazz" }', 'hoge' => 'fuga' )
|
|
28
|
+
end
|
|
29
|
+
emits = d.emits
|
|
30
|
+
|
|
31
|
+
assert_equal 3, emits[0][2].count
|
|
32
|
+
assert_equal 'baz', emits[0][2]['foo.bar']
|
|
33
|
+
|
|
34
|
+
assert_equal 5, emits[1][2].count
|
|
35
|
+
assert_equal 'quux', emits[1][2]['foo.bar.qux']
|
|
36
|
+
assert_equal 'poe', emits[1][2]['foo.bar.hoe']
|
|
37
|
+
assert_equal 'bazz', emits[1][2]['foo.baz']
|
|
38
|
+
end
|
|
39
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'bundler'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
Bundler.setup(:default, :development)
|
|
6
|
+
rescue Bundler::BundlerError => e
|
|
7
|
+
$stderr.puts e.message
|
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
|
9
|
+
exit e.status_code
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require 'test/unit'
|
|
13
|
+
|
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
16
|
+
|
|
17
|
+
require 'fluent/test'
|
|
18
|
+
|
|
19
|
+
unless ENV.has_key?('VERBOSE')
|
|
20
|
+
nulllogger = Object.new
|
|
21
|
+
nulllogger.instance_eval {|obj|
|
|
22
|
+
def method_missing(method, *args)
|
|
23
|
+
# pass
|
|
24
|
+
end
|
|
25
|
+
}
|
|
26
|
+
$log = nulllogger
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
require 'fluent/plugin/out_flatten'
|
|
30
|
+
|
|
31
|
+
class Test::Unit::TestCase
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fluent-plugin-flatten
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Kentaro Kuribayashi
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-11-27 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rake
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: fluentd
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: fluentd
|
|
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'
|
|
62
|
+
description: Fluentd plugin to flatten JSON-formatted string values to top level key/value-s.
|
|
63
|
+
email:
|
|
64
|
+
- kentarok@gmail.com
|
|
65
|
+
executables: []
|
|
66
|
+
extensions: []
|
|
67
|
+
extra_rdoc_files: []
|
|
68
|
+
files:
|
|
69
|
+
- .gitignore
|
|
70
|
+
- Gemfile
|
|
71
|
+
- LICENSE
|
|
72
|
+
- README.md
|
|
73
|
+
- Rakefile
|
|
74
|
+
- fluent-plugin-flatten.gemspec
|
|
75
|
+
- lib/fluent/plugin/out_flatten.rb
|
|
76
|
+
- test/plugin/test_out_flatten.rb
|
|
77
|
+
- test/test_helper.rb
|
|
78
|
+
homepage: http://github.com/kentaro/fluent-plugin-flatten
|
|
79
|
+
licenses: []
|
|
80
|
+
post_install_message:
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
none: false
|
|
86
|
+
requirements:
|
|
87
|
+
- - ! '>='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
none: false
|
|
92
|
+
requirements:
|
|
93
|
+
- - ! '>='
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project:
|
|
98
|
+
rubygems_version: 1.8.23
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 3
|
|
101
|
+
summary: Fluentd plugin to flatten JSON-formatted string values to top level key/value-s.
|
|
102
|
+
test_files:
|
|
103
|
+
- test/plugin/test_out_flatten.rb
|
|
104
|
+
- test/test_helper.rb
|