fluent-plugin-redis-counter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ script: bundle exec ruby -S -Itest test/plugin/out_redis_counter.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.0.1
2
+ * first version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-redis-counter.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Redis counter plugin for fluent [![Build Status](https://secure.travis-ci.org/kbinani/fluent-plugin-redis-counter.png)](http://travis-ci.org/kbinani/fluent-plugin-redis-counter)
2
+
3
+ fluent-plugin-redis-counter is a fluent plugin to count-up/down redis keys.
4
+
5
+ # Configuration
6
+
7
+ <match redis_counter.**>
8
+ type redis_counter
9
+
10
+ host localhost
11
+ port 6379
12
+
13
+ # database number is optional.
14
+ db_number 0 # 0 is default
15
+ </match>
16
+
17
+ # Example
18
+
19
+ prepare a conf file ("fluent.conf") in current directory like this:
20
+
21
+ <source>
22
+ type forward
23
+ </source>
24
+ <match debug.**>
25
+ type redis_counter
26
+ host localhost
27
+ port 6379
28
+ db_number 0
29
+ </match>
30
+
31
+ run commands for test:
32
+
33
+ $redis-server 2>&1 >/dev/null &
34
+ [1] 879
35
+ $redis-cli
36
+ redis 127.0.0.1:6379>del foo
37
+ (integer) 0
38
+ redis 127.0.0.1:6379>exit
39
+ $fluentd -c ./fluent.conf 2>&1 >/dev/null &
40
+ [2] 889
41
+ $echo {\"foo\":5} | fluent-cat debug
42
+ $echo {\"foo\":-2} | fluent-cat debug
43
+ $kill -s HUP 889
44
+ $redis-cli
45
+ redis 127.0.0.1:6379>get foo
46
+ "3"
47
+ redis 127.0.0.1:6379>
48
+
49
+ # Copyright
50
+ - Copyright(C) 2012 Buntaro Okada
51
+ - Copyright(C) 2011-2012 Yuki Nishijima
52
+
53
+ # License
54
+ - Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "fluent-plugin-redis-counter"
6
+ s.version = "0.0.1"
7
+ s.description = "fluent-plugin-redis-counter is a fluent plugin to count-up/down redis keys."
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Buntaro Okada"]
10
+ s.date = %q{2012-06-15}
11
+ s.email = "kbinani.bt@gmail.com"
12
+ s.homepage = "https://github.com/kbinani/fluent-plugin-redis-counter"
13
+ s.summary = "Redis counter plugin for fluent"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency %q<fluentd>, ["~> 0.10.0"]
21
+ s.add_dependency %q<redis>, ["~> 2.2.2"]
22
+ end
@@ -0,0 +1,63 @@
1
+ module Fluent
2
+ class RedisCounterOutput < BufferedOutput
3
+ Fluent::Plugin.register_output('redis_counter', self)
4
+ attr_reader :host, :port, :db_number, :redis
5
+
6
+ def initialize
7
+ super
8
+ require 'redis'
9
+ require 'msgpack'
10
+ end
11
+
12
+ def configure(conf)
13
+ super
14
+ @host = conf.has_key?('host') ? conf['host'] : 'localhost'
15
+ @port = conf.has_key?('port') ? conf['port'].to_i : 6379
16
+ @db_number = conf.has_key?('db_number') ? conf['db_number'].to_i : nil
17
+ end
18
+
19
+ def start
20
+ super
21
+ @redis = Redis.new(
22
+ :host => @host, :port => @port,
23
+ :thread_safe => true, :db => @db_number
24
+ )
25
+ end
26
+
27
+ def shutdown
28
+ @redis.quit
29
+ end
30
+
31
+ def format(tag, time, record)
32
+ record.to_msgpack
33
+ end
34
+
35
+ def write(chunk)
36
+ table = {}
37
+ table.default = 0
38
+ chunk.open { |io|
39
+ begin
40
+ MessagePack::Unpacker.new(io).each { |record|
41
+ record.each_key { |key|
42
+ begin
43
+ value = Integer(record[key])
44
+ if value != 0
45
+ table[key] += value
46
+ end
47
+ rescue ArgumentError
48
+ # convert to integer failed, do nothing.
49
+ rescue TypeError
50
+ # object can't convert to integer, do nothing.
51
+ end
52
+ }
53
+ }
54
+ rescue EOFError
55
+ # EOFError always occured when reached end of chunk.
56
+ end
57
+ }
58
+ table.each_key { |key|
59
+ @redis.incrby(key, table[key])
60
+ }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,65 @@
1
+ require 'fluent/test'
2
+
3
+ class RedisCounterTest < Test::Unit::TestCase
4
+ def setup
5
+ Fluent::Test.setup
6
+ require 'fluent/plugin/out_redis_counter'
7
+
8
+ @d = create_driver %[
9
+ host localhost
10
+ port 6379
11
+ db_number 1
12
+ ]
13
+ redis = Redis.new(
14
+ :host => "localhost", :port => 6379,
15
+ :thread_safe => true, :db => 1
16
+ )
17
+ redis.del("a")
18
+ redis.del("b")
19
+ redis.quit
20
+ end
21
+
22
+ def create_driver(conf = CONFIG)
23
+ Fluent::Test::BufferedOutputTestDriver.new(Fluent::RedisCounterOutput).configure(conf)
24
+ end
25
+
26
+ def test_configure
27
+ assert_equal 'localhost', @d.instance.host
28
+ assert_equal 6379, @d.instance.port
29
+ assert_equal 1, @d.instance.db_number
30
+ end
31
+
32
+ def test_format
33
+ @d.emit({"a" => 1})
34
+ @d.expect_format({"a" => 1}.to_msgpack)
35
+ @d.run
36
+ end
37
+
38
+ def test_write
39
+ @d.emit({"a" => 2})
40
+ @d.emit({"a" => 3})
41
+ @d.emit({"a" => "foo"})
42
+ @d.emit({"a" => -1, "b" => 1})
43
+ @d.run
44
+
45
+ assert_equal "4", @d.instance.redis.get("a")
46
+ assert_equal "1", @d.instance.redis.get("b")
47
+ end
48
+
49
+ def test_write_with_float
50
+ @d.emit({"a" => "1.1"})
51
+ @d.emit({"a" => "2"})
52
+ @d.run
53
+
54
+ assert_equal "2", @d.instance.redis.get("a")
55
+ end
56
+
57
+ def test_write_with_object
58
+ @d.emit({"a" => 1})
59
+ @d.emit({"a" => {"foo" => 1}})
60
+ @d.run
61
+
62
+ assert_equal "1", @d.instance.redis.get("a")
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-redis-counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Buntaro Okada
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.2.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.2.2
46
+ description: fluent-plugin-redis-counter is a fluent plugin to count-up/down redis
47
+ keys.
48
+ email: kbinani.bt@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .travis.yml
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - VERSION
60
+ - fluent-plugin-redis-counter.gemspec
61
+ - lib/fluent/plugin/out_redis_counter.rb
62
+ - test/plugin/out_redis_counter.rb
63
+ homepage: https://github.com/kbinani/fluent-plugin-redis-counter
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.24
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Redis counter plugin for fluent
87
+ test_files:
88
+ - test/plugin/out_redis_counter.rb