Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
sonots committed Nov 20, 2013
0 parents commit 1e3f4b8
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*.gem
~*
#*
*~
.bundle
Gemfile.lock
.rbenv-version
vendor
doc/*
tmp/*
coverage
.yardoc
.ruby-version
pkg/*
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
gemfile:
- Gemfile
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http:https://rubygems.org"

gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 Naotoshi Seo

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# fluent-plugin-flowcounter-simple [![Build Status](https://secure.travis-ci.org/sonots/fluent-plugin-flowcounter-simple.png?branch=master)](http:https://travis-ci.org/sonots/fluent-plugin-flowcounter-simple)

Simple Fluentd Plugin to count number of messages and outputs to log

## Configuration

<match foo.bar.**>
type flowcounter_simple
unit second
</source>

This plugin does not emit, just writes into the log file as

out_flowcounter_simple: 30 / second

## Parameters

- unit

Either of second/minute/hour/day. Default: minute

## ChangeLog

See [CHANGELOG.md](CHANGELOG.md) for details.

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new [Pull Request](../../pull/new/master)

## Copyright

Copyright (c) 2013 Naotoshi Seo. See [LICENSE.txt](LICENSE.txt) for details.

11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end

task :default => :test
19 changes: 19 additions & 0 deletions fluent-plugin-flowcounter-simple.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |gem|
gem.name = "fluent-plugin-flowcounter-simple"
gem.version = "0.0.1"
gem.authors = ["Naotoshi Seo"]
gem.email = ["[email protected]"]
gem.summary = %q{Simple Fluentd Plugin to count number of messages and outputs to log}
gem.description = %q{Simple Fluentd Plugin to count number of messages and outputs to log}
gem.homepage = "https://github.com/sonots/fluent-plugin-flowcounter-simple"
gem.license = "MIT"

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_development_dependency "rake"
gem.add_runtime_dependency "fluentd"
end
84 changes: 84 additions & 0 deletions lib/fluent/plugin/out_flowcounter_simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class Fluent::FlowCounterSimpleOutput < Fluent::Output
Fluent::Plugin.register_output('flowcounter_simple', self)

config_param :unit, :string, :default => 'second'

attr_accessor :last_checked

def configure(conf)
super

@unit = case @unit
when 'second' then :second
when 'minute' then :minute
when 'hour' then :hour
when 'day' then :day
else
raise Fluent::ConfigError, "flowcounter-simple unit allows second/minute/hour/day"
end

@count = 0
@mutex = Mutex.new
end

def start
super
start_watch
end

def shutdown
super
@watcher.terminate
@watcher.join
end

def countup(count)
@mutex.synchronize {
@count = (@count || 0) + count
}
end

def flush_emit(step)
count, @count = @count, 0
if count > 0
$log.write "out_flowcounter_simple: #{count} / #{@unit}\n"
$log.flush
end
end

def start_watch
# for internal, or tests only
@watcher = Thread.new(&method(:watch))
end

def watch
# instance variable, and public accessable, for test
@last_checked = Fluent::Engine.now
tick = case @unit
when :second then 1
when :minute then 60
when :hour then 3600
when :day then 86400
else
raise RuntimeError, "@unit must be one of second/minute/hour/day"
end
while true
sleep 0.1
if Fluent::Engine.now - @last_checked >= tick
now = Fluent::Engine.now
flush_emit(now - @last_checked)
@last_checked = now
end
end
end

def emit(tag, es, chain)
count = 0
es.each {|time,record|
count += 1
}
countup(count)

chain.next
end
end
18 changes: 18 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'test/unit'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'fluent/test'
require 'fluent/plugin/out_flowcounter_simple'

class Test::Unit::TestCase
end
36 changes: 36 additions & 0 deletions test/plugin/test_out_flowcounter_simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'helper'

class FlowCounterSimpleOutputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
end

CONFIG = %[
unit second
]

def create_driver(conf=CONFIG,tag='test')
Fluent::Test::OutputTestDriver.new(Fluent::FlowCounterSimpleOutput, tag).configure(conf)
end

def test_configure
assert_nothing_raised {
d = create_driver('')
}
assert_nothing_raised {
d = create_driver(CONFIG)
}
end

def test_emit
d1 = create_driver(CONFIG, 'test.tag1')
d1.run do
10.times do
d1.emit({'message'=> 'a' * 100})
d1.emit({'message'=> 'b' * 100})
d1.emit({'message'=> 'c' * 100})
end
end
r1 = d1.instance.flush_emit(60)
end
end

0 comments on commit 1e3f4b8

Please sign in to comment.