fluent-plugin-websocket-ruby-1.9 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee71b2e44f040b928c65908be3d8bd33161081ab
4
+ data.tar.gz: 45940c7f09ebbd0af2b627c19a1a05813ac0c3cd
5
+ SHA512:
6
+ metadata.gz: f9d5573bf874c5ddc920f416845e2337066f6ff3f6fe745f4c231d7cd0834283195f0609ba5aa5ae8e9b899f9bf35b03fa14e4fa804c3752e792fc7400787966
7
+ data.tar.gz: e0df6e5a827c45f2142cd80db5fba8a9328828b3dde217e8b17206c1bce21fa94bae8060256726a0b8071ca3f7676d7c21d26aac2aed757ca68a16e3605991bb
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-websocket.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2013 IZAWA Tetsu (@moccos)
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.
data/README.rdoc ADDED
@@ -0,0 +1,98 @@
1
+ = Fluent::Plugin::Websocket
2
+
3
+ Fluentd websocket output plugin.
4
+
5
+ This plugin works as websocket server which can output JSON string or {MessagePack}[http://msgpack.org/] binary.
6
+
7
+ In the current version, emitted data will be broadcasted to the all connected clients.
8
+
9
+ This fork is from {moccos/fluent-plugin-websocket}[https://github.com/moccos/fluent-plugin-websocket], but locks in Ruby 1.9 as a minimum Ruby version. The newest fluentd gem dependency "serverengine" requires Ruby 2.1.
10
+
11
+ == Installation
12
+
13
+ gem install fluent-plugin-websocket
14
+
15
+ This plugin depends on {*em-websocket*}[https://github.com/igrigorik/em-websocket].
16
+
17
+ == Configuration
18
+ <match foo.**>
19
+ type websocket
20
+ host 192.168.1.1 # default: 0.0.0.0 (ANY)
21
+ port 8080 # default: 8080
22
+ use_msgpack false # default: false
23
+ add_time false # default: false
24
+ add_tag true # default: true
25
+ buffered_messages 100 # default: 0
26
+ </match>
27
+
28
+ * *host*: WebSocket server IP address.
29
+ * *port*: WebSocket server port.
30
+ * <b>use_msgpack</b>: Send {MessagePack}[http://msgpack.org/] format binary. Otherwise, you send JSON format text.
31
+ * <b>add_time</b>: Add timestamp to the data.
32
+ * <b>add_tag</b>: Add fluentd tag to the data.
33
+ * <b>buffered_messages</b>: The number of messages to be buffered. The new connection receives them.
34
+
35
+ If there are no websocket connections, this plugin silently discards data. You may use <em>out_copy</em> plugin like this:
36
+
37
+ <match foo.**>
38
+ type copy
39
+ <store>
40
+ type file
41
+ path /var/log/foo/bar.log
42
+ </store>
43
+ <store>
44
+ type websocket
45
+ port 8080
46
+ </store>
47
+ </match>
48
+
49
+ If <em>buffered_messages</em> is greater than <em>0</em>, the last stored data is sent to the client upon new connection.
50
+
51
+ == Data format
52
+ [tag, timestamp, data_object]
53
+
54
+ * tag is appended when <em>add_tag</em> option is true.
55
+ * timestamp is appended when <em>add_time</em> option is true.
56
+
57
+ === Example
58
+ curl -X POST -d 'json={"action":"login","user":6}' http://localhost:8888/foo/bar
59
+
60
+ ["foo.bar",1364699026,{"action":"login","user":6}]
61
+
62
+ == Client sample
63
+ === JSON format (use_msgpack: false)
64
+ function onMessage(evt) {
65
+ data = JSON.parse(evt.data);
66
+ ...
67
+ }
68
+
69
+ === Msgpack format binary (use_msgpack: true)
70
+ Extract data by {msgpack.js}[https://github.com/msgpack/msgpack-javascript].
71
+
72
+ websocket.binaryType = "arraybuffer"
73
+ ...
74
+ function onMessage(evt) {
75
+ data = msgpack.unpack(new Uint8Array(evt.data))
76
+ ...
77
+ }
78
+
79
+ == Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create new Pull Request
86
+
87
+ == Recent changes
88
+ * 0.1.7 Add message buffering. (Thanks to {SebastianOsuna}[https://github.com/SebastianOsuna])
89
+ * 0.1.6 Added license to gemspec.
90
+ * 0.1.5 Fixed dependencies. (Thanks to {ca-gacky}[https://github.com/ca-gacky])
91
+ * 0.1.4 Changed json parser to yajl. (Thanks to {Kogarasi}[https://github.com/Kogarasi])
92
+ * 0.1.3 Bug fix.
93
+ * 0.1.2 Released gem.
94
+
95
+ == Copyright
96
+
97
+ Copyright:: Copyright (c) 2013 IZAWA Tetsu (@moccos)
98
+ License:: Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/plugin/test*.rb']
8
+ t.verbose = true
9
+ end
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fluent-plugin-websocket-ruby-1.9"
6
+ gem.version = "0.1.8"
7
+ gem.authors = ["IZAWA Tetsu (@moccos)"]
8
+ gem.email = ["tt.izawa@gmail.com"]
9
+ gem.homepage = "https://github.com/mikeatlas/fluent-plugin-websocket-ruby-1.9"
10
+ gem.summary = %q{Fluentd websocket output plugin}
11
+ gem.description = %q{Fluentd websocket output plugin which can output JSON string or MessagePack binary to the clients.}
12
+ gem.required_ruby_version = ">= 1.9.2"
13
+ gem.licenses = ["Apache License, Version 2.0"]
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "websocket-eventmachine-client"
22
+ gem.add_runtime_dependency "msgpack"
23
+ gem.add_runtime_dependency "yajl-ruby"
24
+ gem.add_runtime_dependency "fluentd", '0.12.29'
25
+ gem.add_runtime_dependency "em-websocket"
26
+ end
@@ -0,0 +1,96 @@
1
+ # Copyright (C) 2013 IZAWA Tetsu (@moccos)
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.
14
+
15
+ require 'em-websocket'
16
+ require 'yajl'
17
+ require 'thread'
18
+
19
+ module Fluent
20
+ $lock = Mutex::new
21
+ $channel = EM::Channel.new
22
+
23
+ class WebSocketOutput < Fluent::Output
24
+ Fluent::Plugin.register_output('websocket', self)
25
+ config_param :use_msgpack, :bool, :default => false
26
+ config_param :host, :string, :default => "0.0.0.0"
27
+ config_param :port, :integer, :default => 8080
28
+ config_param :add_time, :bool, :default => false
29
+ config_param :add_tag, :bool, :default => true
30
+ config_param :buffered_messages, :integer, :default => 0
31
+
32
+ def configure(conf)
33
+ super
34
+ @thread = Thread.new do
35
+ $log.trace "Started em-websocket thread."
36
+ $log.info "WebSocket server #{@host}:#{@port} [msgpack: #{@use_msgpack}]"
37
+ EM.run {
38
+ EM::WebSocket.run(:host => @host, :port => @port) do |ws|
39
+ ws.onopen { |handshake|
40
+ callback = @use_msgpack ? proc{|msg| ws.send_binary(msg)} : proc{|msg| ws.send(msg)}
41
+ $lock.synchronize do
42
+ sid = $channel.subscribe callback
43
+ $log.trace "WebSocket connection: ID " + sid.to_s
44
+ ws.onclose {
45
+ $log.trace "Connection closed: ID " + sid.to_s
46
+ $lock.synchronize do
47
+ $channel.unsubscribe(sid)
48
+ end
49
+ }
50
+ @buffer.each do |msg|
51
+ ws.send(msg)
52
+ end
53
+ end
54
+
55
+ #ws.onmessage { |msg|
56
+ #}
57
+ }
58
+ end
59
+ }
60
+ end
61
+ end
62
+
63
+ def start
64
+ @buffer = []
65
+ super
66
+ end
67
+
68
+ def shutdown
69
+ super
70
+ EM.stop
71
+ Thread::kill(@thread)
72
+ $log.trace "Killed em-websocket thread."
73
+ end
74
+
75
+ def emit(tag, es, chain)
76
+ chain.next
77
+ es.each {|time,record|
78
+ data = [record]
79
+ if (@add_time) then data.unshift(time) end
80
+ if (@add_tag) then data.unshift(tag) end
81
+ output = @use_msgpack ? data.to_msgpack : Yajl::Encoder.encode( data )
82
+ buffer(output)
83
+ $lock.synchronize do
84
+ $channel.push output
85
+ end
86
+ }
87
+ end
88
+
89
+ def buffer(data)
90
+ return unless @buffered_messages > 0
91
+ @buffer << data
92
+ # Buffer only new @buffered_messages messages
93
+ @buffer = @buffer[-@buffered_messages, @buffered_messages] if @buffer.length > @buffered_messages
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,76 @@
1
+ require 'json'
2
+ require 'msgpack'
3
+ require 'fluent/test'
4
+ require 'fluent/plugin/out_websocket'
5
+ require 'websocket-eventmachine-client'
6
+
7
+ class WebSocketForTest
8
+ def initialize(key, n)
9
+ @n_received = 0
10
+ @v_received = []
11
+
12
+ @ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://localhost:8080')
13
+ @ws.onmessage do |msg, type|
14
+ #puts "Received message: #{msg.to_s} / #{type.to_s}"
15
+ data = type == :text ? JSON.parse(msg): MessagePack.unpack(msg)
16
+ @v_received.push(data[0][key])
17
+ @n_received += 1
18
+ if (n == @n_received)
19
+ @ws.close()
20
+ end
21
+ end
22
+ end
23
+
24
+ attr_reader :n_received, :v_received
25
+ end
26
+
27
+ class WebSocketOutputTest < Test::Unit::TestCase
28
+ def setup
29
+ Fluent::Test.setup
30
+ v_str = "sekaiichi kawaiiyo"
31
+ v_int = -1234567
32
+ v_double = -123.4567
33
+ @key = "key"
34
+ @values = [v_str, v_int, v_double]
35
+ end
36
+
37
+ CONFIG = %[
38
+ ]
39
+
40
+ def create_driver(conf = CONFIG)
41
+ Fluent::Test::OutputTestDriver.new(Fluent::WebSocketOutput).configure(conf)
42
+ end
43
+
44
+ def impl_test(d)
45
+ sleep(0.2)
46
+ ws = WebSocketForTest.new(@key, @values.length)
47
+ sleep(0.2)
48
+ for v in @values
49
+ d.emit({@key => v}, 0)
50
+ end
51
+ sleep(0.5)
52
+ assert_equal(@values.length, ws.n_received)
53
+ result = ws.v_received
54
+ for i in 0 .. @values.length
55
+ assert_equal(@values[i], result[i])
56
+ end
57
+ end
58
+
59
+ # TODO: How to test another configuration?
60
+ #def test_json_out
61
+ #d = create_driver %[
62
+ #use_msgpack false
63
+ #add_tag false
64
+ #]
65
+ #impl_test(d)
66
+ #end
67
+
68
+ def test_msgpack_out
69
+ d = create_driver %[
70
+ use_msgpack true
71
+ add_tag false
72
+ ]
73
+ impl_test(d)
74
+ end
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-websocket-ruby-1.9
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - IZAWA Tetsu (@moccos)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: websocket-eventmachine-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: msgpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: yajl-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: fluentd
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.12.29
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.12.29
83
+ - !ruby/object:Gem::Dependency
84
+ name: em-websocket
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Fluentd websocket output plugin which can output JSON string or MessagePack
98
+ binary to the clients.
99
+ email:
100
+ - tt.izawa@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - fluent-plugin-websocket.gemspec
111
+ - lib/fluent/plugin/out_websocket.rb
112
+ - test/plugin/test_out_websocket.rb
113
+ homepage: https://github.com/mikeatlas/fluent-plugin-websocket-ruby-1.9
114
+ licenses:
115
+ - Apache License, Version 2.0
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 1.9.2
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.5.1
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Fluentd websocket output plugin
137
+ test_files:
138
+ - test/plugin/test_out_websocket.rb