fluent-plugin-input-jsonstream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +16 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +25 -0
- data/Rakefile +12 -0
- data/fluent-plugin-input-jsonstream.gemspec +25 -0
- data/lib/fluent/plugin/in_jsonstream.rb +53 -0
- data/test/helper.rb +57 -0
- data/test/plugin/test_in_jsonstream.rb +108 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc06d04f6fe9640b488591223d11a19e60f45fb6
|
4
|
+
data.tar.gz: 482a366b30a2733483820c944994b6812e72f498
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87bf0305b59d703578b6fb931d1b0299c57b8135c7f216600a96c959f467d7ed31b16fdbb28b9edaeccc47d7a55835620d38833566673bf804933835e0604b44
|
7
|
+
data.tar.gz: fdd242be75c8b5ee16de7eb827ed5d97a813ab10115914a002e4c640b428226fb189c9d0ede4b173ad8f97a776e51d6bfd0dec62d2c36fbd04e613cd595f0065
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Daniel Malon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# fluent-plugin-input-jsonstream
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/MerlinDMC/fluent-plugin-input-jsonstream.svg?branch=master)](https://travis-ci.org/MerlinDMC/fluent-plugin-input-jsonstream)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/fluent-plugin-input-jsonstream.svg)](http://badge.fury.io/rb/fluent-plugin-input-jsonstream)
|
5
|
+
|
6
|
+
## Overview
|
7
|
+
|
8
|
+
A streaming JSON input for [Fluentd](http://www.fluentd.org/).
|
9
|
+
|
10
|
+
### Configuration
|
11
|
+
|
12
|
+
Accept chunked JSON objects via TCP
|
13
|
+
|
14
|
+
```
|
15
|
+
<source>
|
16
|
+
@type jsonstream
|
17
|
+
tag example.jsonstream
|
18
|
+
bind 127.0.0.1
|
19
|
+
port 12345
|
20
|
+
</source>
|
21
|
+
|
22
|
+
<match example.jsonstream>
|
23
|
+
type stdout
|
24
|
+
</match>
|
25
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.test_files = FileList['test/plugin/test_*.rb']
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => [:build]
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "fluent-plugin-input-jsonstream"
|
6
|
+
gem.description = "A streaming JSON input plugin for fluentd"
|
7
|
+
gem.license = "MIT"
|
8
|
+
gem.homepage = "https://github.com/MerlinDMC/fluent-plugin-input-jsonstream"
|
9
|
+
gem.summary = gem.description
|
10
|
+
gem.version = "0.1.0"
|
11
|
+
gem.authors = ["Daniel Malon"]
|
12
|
+
gem.email = "daniel.malon@me.com"
|
13
|
+
gem.has_rdoc = false
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.require_paths = ['lib']
|
18
|
+
|
19
|
+
gem.add_runtime_dependency "fluentd"
|
20
|
+
gem.add_runtime_dependency 'yajl-ruby', '~> 1.0'
|
21
|
+
|
22
|
+
gem.add_development_dependency "bundler"
|
23
|
+
gem.add_development_dependency "rake"
|
24
|
+
gem.add_development_dependency "test-unit"
|
25
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'cool.io'
|
4
|
+
require 'yajl'
|
5
|
+
|
6
|
+
require 'fluent/input'
|
7
|
+
|
8
|
+
module Fluent::Plugin
|
9
|
+
class JSONStreamInput < Input
|
10
|
+
Fluent::Plugin.register_input('jsonstream', self)
|
11
|
+
|
12
|
+
helpers :server, :extract
|
13
|
+
|
14
|
+
desc 'Tag of output events.'
|
15
|
+
config_param :tag, :string
|
16
|
+
desc 'The port to listen to.'
|
17
|
+
config_param :port, :integer, default: 5170
|
18
|
+
desc 'The bind address to listen to.'
|
19
|
+
config_param :bind, :string, default: '0.0.0.0'
|
20
|
+
|
21
|
+
config_param :blocking_timeout, :time, default: 0.5
|
22
|
+
|
23
|
+
def configure(conf)
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def multi_workers_ready?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def start
|
32
|
+
super
|
33
|
+
|
34
|
+
server_create_connection(:in_jsonstream_server, @port, bind: @bind) do |conn|
|
35
|
+
parser = Yajl::Parser.new(:symbolize_keys => false)
|
36
|
+
parser.on_parse_complete = lambda { |record|
|
37
|
+
tag = extract_tag_from_record(record)
|
38
|
+
tag ||= @tag
|
39
|
+
time ||= extract_time_from_record(record) || Fluent::EventTime.now
|
40
|
+
|
41
|
+
# Use the recorded event time if available
|
42
|
+
time = record.delete('timestamp').to_i if record.key?('timestamp')
|
43
|
+
|
44
|
+
router.emit(tag, time, record)
|
45
|
+
}
|
46
|
+
|
47
|
+
conn.data do |data|
|
48
|
+
parser << data
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "fluent/test"
|
3
|
+
require "fluent/test/helpers"
|
4
|
+
require "fluent/test/driver/input"
|
5
|
+
|
6
|
+
include Fluent::Test::Helpers
|
7
|
+
|
8
|
+
def unused_port(num = 1, protocol: :tcp, bind: "0.0.0.0")
|
9
|
+
case protocol
|
10
|
+
when :tcp
|
11
|
+
unused_port_tcp(num)
|
12
|
+
when :udp
|
13
|
+
unused_port_udp(num, bind: bind)
|
14
|
+
else
|
15
|
+
raise ArgumentError, "unknown protocol: #{protocol}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def unused_port_tcp(num = 1)
|
20
|
+
ports = []
|
21
|
+
sockets = []
|
22
|
+
num.times do
|
23
|
+
s = TCPServer.open(0)
|
24
|
+
sockets << s
|
25
|
+
ports << s.addr[1]
|
26
|
+
end
|
27
|
+
sockets.each{|s| s.close }
|
28
|
+
if num == 1
|
29
|
+
return ports.first
|
30
|
+
else
|
31
|
+
return *ports
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
PORT_RANGE_AVAILABLE = (1024...65535)
|
36
|
+
|
37
|
+
def unused_port_udp(num = 1, bind: "0.0.0.0")
|
38
|
+
family = IPAddr.new(IPSocket.getaddress(bind)).ipv4? ? ::Socket::AF_INET : ::Socket::AF_INET6
|
39
|
+
ports = []
|
40
|
+
sockets = []
|
41
|
+
while ports.size < num
|
42
|
+
port = rand(PORT_RANGE_AVAILABLE)
|
43
|
+
u = UDPSocket.new(family)
|
44
|
+
if (u.bind(bind, port) rescue nil)
|
45
|
+
ports << port
|
46
|
+
sockets << u
|
47
|
+
else
|
48
|
+
u.close
|
49
|
+
end
|
50
|
+
end
|
51
|
+
sockets.each{|s| s.close }
|
52
|
+
if num == 1
|
53
|
+
return ports.first
|
54
|
+
else
|
55
|
+
return *ports
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require_relative '../helper'
|
2
|
+
require 'fluent/test/driver/input'
|
3
|
+
require "fluent/plugin/in_jsonstream"
|
4
|
+
|
5
|
+
class JSONStreamInputTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
Fluent::Test.setup
|
8
|
+
end
|
9
|
+
|
10
|
+
PORT = unused_port
|
11
|
+
BASE_CONFIG = %[
|
12
|
+
port #{PORT}
|
13
|
+
tag test
|
14
|
+
]
|
15
|
+
CONFIG = BASE_CONFIG + %[
|
16
|
+
bind 127.0.0.1
|
17
|
+
]
|
18
|
+
|
19
|
+
attr_reader :base_config
|
20
|
+
|
21
|
+
def create_driver(conf)
|
22
|
+
Fluent::Test::Driver::Input.new(Fluent::Plugin::JSONStreamInput).configure(conf)
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_tcp_socket(host, port, &block)
|
26
|
+
if block_given?
|
27
|
+
TCPSocket.open(host, port, &block)
|
28
|
+
else
|
29
|
+
TCPSocket.open(host, port)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_configure_requires_tag
|
34
|
+
assert_raise Fluent::ConfigError do
|
35
|
+
create_driver("")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_configuring_tag
|
40
|
+
d = create_driver(BASE_CONFIG)
|
41
|
+
assert_equal d.instance.tag, "test"
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_simple_parse
|
45
|
+
payload = '{"foo":"bar"}'
|
46
|
+
expected = { 'foo' => 'bar' }
|
47
|
+
|
48
|
+
d = create_driver(CONFIG)
|
49
|
+
d.run(expect_records: 1) do
|
50
|
+
create_tcp_socket('127.0.0.1', PORT) do |sock|
|
51
|
+
sock.send(payload, 0)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
assert_equal 1, d.events.size
|
56
|
+
assert_equal "test", d.events[0][0]
|
57
|
+
assert d.events[0][1].is_a?(Fluent::EventTime)
|
58
|
+
assert_equal expected, d.events[0][2]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_chunked_parse
|
62
|
+
payloads = [
|
63
|
+
'{"fo', 'o": "b', 'ar"}'
|
64
|
+
]
|
65
|
+
expected = { 'foo' => 'bar' }
|
66
|
+
|
67
|
+
d = create_driver(CONFIG)
|
68
|
+
d.run(expect_records: 1) do
|
69
|
+
create_tcp_socket('127.0.0.1', PORT) do |sock|
|
70
|
+
payloads.each do |payload|
|
71
|
+
sock.send(payload, 0)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_equal 1, d.events.size
|
77
|
+
assert_equal "test", d.events[0][0]
|
78
|
+
assert d.events[0][1].is_a?(Fluent::EventTime)
|
79
|
+
assert_equal expected, d.events[0][2]
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_chunked_multiple_parse
|
83
|
+
payloads = [
|
84
|
+
'{"fo', 'o": "b', 'ar"}',
|
85
|
+
'{"fo', 'o": "b', 'az"}'
|
86
|
+
]
|
87
|
+
expecteds = [
|
88
|
+
{ 'foo' => 'bar' },
|
89
|
+
{ 'foo' => 'baz' }
|
90
|
+
]
|
91
|
+
|
92
|
+
d = create_driver(CONFIG)
|
93
|
+
d.run(expect_records: 2) do
|
94
|
+
create_tcp_socket('127.0.0.1', PORT) do |sock|
|
95
|
+
payloads.each do |payload|
|
96
|
+
sock.send(payload, 0)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_equal 2, d.events.size
|
102
|
+
expecteds.each_with_index do |expected, i|
|
103
|
+
assert_equal "test", d.events[i][0]
|
104
|
+
assert d.events[i][1].is_a?(Fluent::EventTime)
|
105
|
+
assert_equal expecteds[i], d.events[i][2]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-input-jsonstream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Malon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-12 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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yajl-ruby
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A streaming JSON input plugin for fluentd
|
84
|
+
email: daniel.malon@me.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitignore"
|
90
|
+
- ".travis.yml"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- fluent-plugin-input-jsonstream.gemspec
|
96
|
+
- lib/fluent/plugin/in_jsonstream.rb
|
97
|
+
- test/helper.rb
|
98
|
+
- test/plugin/test_in_jsonstream.rb
|
99
|
+
homepage: https://github.com/MerlinDMC/fluent-plugin-input-jsonstream
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.8
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: A streaming JSON input plugin for fluentd
|
123
|
+
test_files:
|
124
|
+
- test/helper.rb
|
125
|
+
- test/plugin/test_in_jsonstream.rb
|