fluent-plugin-flatten-hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +13 -0
- data/fluent-plugin-flatten-hash.gemspec +21 -0
- data/lib/fluent/plugin/out_flatten_hash.rb +51 -0
- data/test/helper.rb +28 -0
- data/test/plugin/test_out_flatten_hash.rb +84 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 06106e3a3fba0cc454f3d2b638f79f2efe1194ec
|
4
|
+
data.tar.gz: 6ff631bc5f9b14ebc48d72477992701458328e04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 96bf0b454249f603ea4aaf6e78376685c77691d2a9a7158cca8d7b08a86e64f6deed59c4f8f9e8783ffa1ea14aafb5f12d2724a49e82b5c044a52397e6bebcaa
|
7
|
+
data.tar.gz: 4f3cbcabab41450deadb2683e926b27336cb0e942b2180163c2c01cedae197bbcbeb84101dd7328dab66ec32dc5805f5b930306cb25bbf2ee73be5b961815633
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Masahiro Sano
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# fluent-plugin-flatten-hash
|
2
|
+
|
3
|
+
A fluentd plugin to flatten nested hash structure as a flat record with unique keys generated by its path for each values.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fluent-plugin-flatten-hash'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fluent-plugin-flatten-hash
|
18
|
+
|
19
|
+
## Configuration
|
20
|
+
|
21
|
+
You can set a configuration like below:
|
22
|
+
|
23
|
+
```
|
24
|
+
<match message>
|
25
|
+
type flatten_hash
|
26
|
+
add_tag_prefix flattened.
|
27
|
+
separator _
|
28
|
+
</match>
|
29
|
+
```
|
30
|
+
|
31
|
+
In this configuration, if you get a following nested/complex message:
|
32
|
+
|
33
|
+
```js
|
34
|
+
{
|
35
|
+
"message":{
|
36
|
+
"today":"good day",
|
37
|
+
"tommorow":{
|
38
|
+
"is":{
|
39
|
+
"a":{
|
40
|
+
"bad":"day"
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
"days":[
|
46
|
+
"2013/08/24",
|
47
|
+
"2013/08/25"
|
48
|
+
]
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
The message is flattened like below:
|
53
|
+
|
54
|
+
```js
|
55
|
+
{
|
56
|
+
"message_today":"good day",
|
57
|
+
"message_tommorow_is_a_bad":"day"
|
58
|
+
"days_0" => "2013/08/24",
|
59
|
+
"days_1" => "2013/08/25"
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
70
|
+
|
71
|
+
### Mixins
|
72
|
+
|
73
|
+
* HandleTagNameMixin
|
74
|
+
|
75
|
+
## Copyright
|
76
|
+
|
77
|
+
<table>
|
78
|
+
<tr>
|
79
|
+
<td>Author</td><td>Masahiro Sano <sabottenda@gmail.com></td>
|
80
|
+
</tr>
|
81
|
+
<tr>
|
82
|
+
<td>Copyright</td><td>Copyright (c) 2013- Masahiro Sano</td>
|
83
|
+
</tr>
|
84
|
+
<tr>
|
85
|
+
<td>License</td><td>MIT License</td>
|
86
|
+
</tr>
|
87
|
+
</table>
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fluent-plugin-flatten-hash"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Masahiro Sano"]
|
9
|
+
spec.email = ["sabottenda@gmail.com"]
|
10
|
+
spec.description = %q{A fluentd plugin to flatten nested hash structure as a flat record}
|
11
|
+
spec.summary = %q{A fluentd plugin to flatten nested hash structure as a flat record}
|
12
|
+
spec.homepage = "https://github.com/sabottenda/fluent-plugin-flatten-hash"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.has_rdoc = false
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "fluentd"
|
21
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Fluent
|
2
|
+
class FlattenHashOutput < Output
|
3
|
+
include Fluent::HandleTagNameMixin
|
4
|
+
Fluent::Plugin.register_output('flatten_hash', self)
|
5
|
+
|
6
|
+
config_param :tag, :string, :default => nil
|
7
|
+
config_param :separator, :string, :default => '.'
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def configure(conf)
|
14
|
+
super
|
15
|
+
if (!@tag &&
|
16
|
+
!remove_tag_prefix &&
|
17
|
+
!remove_tag_suffix &&
|
18
|
+
!add_tag_prefix &&
|
19
|
+
!add_tag_suffix )
|
20
|
+
raise ConfigError, "out_flatten_hash: No tag parameters are set"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def emit(tag, es, chain)
|
25
|
+
@tag ||= tag
|
26
|
+
es.each do |time, record|
|
27
|
+
record = flatten_record(record, [])
|
28
|
+
filter_record(@tag, time, record)
|
29
|
+
Engine.emit(@tag, time, record)
|
30
|
+
end
|
31
|
+
chain.next
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def flatten_record(record, prefix)
|
36
|
+
ret = {}
|
37
|
+
if record.is_a? Hash
|
38
|
+
record.each { |key, value|
|
39
|
+
ret.merge! flatten_record(value, prefix + [key.to_s])
|
40
|
+
}
|
41
|
+
elsif record.is_a? Array
|
42
|
+
record.each_with_index { |elem, index|
|
43
|
+
ret.merge! flatten_record(elem, prefix + [index.to_s])
|
44
|
+
}
|
45
|
+
else
|
46
|
+
return {prefix.join(@separator) => record}
|
47
|
+
end
|
48
|
+
ret
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
require 'fluent/test'
|
15
|
+
unless ENV.has_key?('VERBOSE')
|
16
|
+
nulllogger = Object.new
|
17
|
+
nulllogger.instance_eval {|obj|
|
18
|
+
def method_missing(method, *args)
|
19
|
+
# pass
|
20
|
+
end
|
21
|
+
}
|
22
|
+
$log = nulllogger
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'fluent/plugin/out_flatten_hash'
|
26
|
+
|
27
|
+
class Test::Unit::TestCase
|
28
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FlattenHashOutputTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Fluent::Test.setup
|
6
|
+
end
|
7
|
+
|
8
|
+
BASE_CONFIG = %[
|
9
|
+
type flatten_hash
|
10
|
+
]
|
11
|
+
CONFIG = BASE_CONFIG + %[
|
12
|
+
add_tag_prefix flattened
|
13
|
+
]
|
14
|
+
|
15
|
+
def create_driver(conf = CONFIG, tag='test')
|
16
|
+
Fluent::Test::OutputTestDriver.new(Fluent::FlattenHashOutput, tag).configure(conf)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_configure
|
20
|
+
assert_raise(Fluent::ConfigError) {
|
21
|
+
create_driver(BASE_CONFIG)
|
22
|
+
}
|
23
|
+
assert_nothing_raised(Fluent::ConfigError) {
|
24
|
+
create_driver(BASE_CONFIG + %[
|
25
|
+
tag hoge
|
26
|
+
])
|
27
|
+
}
|
28
|
+
assert_nothing_raised(Fluent::ConfigError) {
|
29
|
+
create_driver(BASE_CONFIG + %[
|
30
|
+
add_tag_prefix hoge
|
31
|
+
])
|
32
|
+
}
|
33
|
+
assert_nothing_raised(Fluent::ConfigError) {
|
34
|
+
create_driver(BASE_CONFIG + %[
|
35
|
+
add_tag_suffix hoge
|
36
|
+
])
|
37
|
+
}
|
38
|
+
assert_nothing_raised(Fluent::ConfigError) {
|
39
|
+
create_driver(BASE_CONFIG + %[
|
40
|
+
remove_tag_prefix hoge
|
41
|
+
])
|
42
|
+
}
|
43
|
+
assert_nothing_raised(Fluent::ConfigError) {
|
44
|
+
create_driver(BASE_CONFIG + %[
|
45
|
+
remove_tag_suffix hoge
|
46
|
+
])
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_flatten_record
|
51
|
+
d = create_driver
|
52
|
+
|
53
|
+
d.run do
|
54
|
+
d.emit({'message' => {'foo' => 'bar'}})
|
55
|
+
d.emit({"message" => {'foo' => 'bar', 'hoge' => 'fuga'}})
|
56
|
+
d.emit({"message" => {'nest' => {'foo' => 'bar'}}})
|
57
|
+
d.emit({"message" => {'nest' => {'nest' => {'foo' => 'bar'}}}})
|
58
|
+
d.emit({"message" => {'array' => ['foo', 'bar']}})
|
59
|
+
d.emit({"message" => {'array' => [{'foo' => 'bar'}, {'hoge' => 'fuga'}]}})
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_equal [
|
63
|
+
{"message.foo" => "bar"},
|
64
|
+
{"message.foo" => "bar", "message.hoge" => "fuga"},
|
65
|
+
{"message.nest.foo" => "bar"},
|
66
|
+
{"message.nest.nest.foo" => "bar"},
|
67
|
+
{"message.array.0" => "foo", "message.array.1" => "bar"},
|
68
|
+
{"message.array.0.foo" => "bar", "message.array.1.hoge" => "fuga"},
|
69
|
+
], d.records
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_separator
|
73
|
+
d = create_driver CONFIG + %[separator /]
|
74
|
+
|
75
|
+
d.run do
|
76
|
+
d.emit({"message" => {'nest' => {'foo' => 'bar'}}})
|
77
|
+
end
|
78
|
+
|
79
|
+
assert_equal [
|
80
|
+
{"message/nest/foo" => "bar"},
|
81
|
+
], d.records
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-flatten-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masahiro Sano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: fluentd
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A fluentd plugin to flatten nested hash structure as a flat record
|
28
|
+
email:
|
29
|
+
- sabottenda@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- fluent-plugin-flatten-hash.gemspec
|
40
|
+
- lib/fluent/plugin/out_flatten_hash.rb
|
41
|
+
- test/helper.rb
|
42
|
+
- test/plugin/test_out_flatten_hash.rb
|
43
|
+
homepage: https://github.com/sabottenda/fluent-plugin-flatten-hash
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: A fluentd plugin to flatten nested hash structure as a flat record
|
67
|
+
test_files:
|
68
|
+
- test/helper.rb
|
69
|
+
- test/plugin/test_out_flatten_hash.rb
|