Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sonots committed Apr 27, 2015
0 parents commit 5b5942a
Show file tree
Hide file tree
Showing 12 changed files with 295 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/*
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rvm:
- 1.9.3
- 2.0.0
- 2.1
- 2.2
gemfile:
- Gemfile
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

First version
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015 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.
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# fluent-plugin-filter_typecast

[![Build Status](https://secure.travis-ci.org/sonots/fluent-plugin-filter_typecast.png?branch=master)](http:https://travis-ci.org/sonots/fluent-plugin-filter_typecast)

A Fluentd filter plugin to cast record types

## Requirements

Fluentd >= v0.12

## Install

Use RubyGems:

```
gem install fluent-plugin-filter_typecast
```

## Configuration Example

```apache
<source>
type dummy
tag dummy
dummy {"field1":"1","field2":"2","field3":"2013-02-12 22:04:14 UTC","field4":"true","field5":"a,b,c"}
</source>
<filter **>
type typecast
types field1:integer,field2:string,field3:time,field4:bool,field5:array
</filter>
<match **>
type stdout
</match>
```

You should see casted records:

```
dummy {"field1":1,"field2":"2","field3":1418380657,"field4":true,"field5":["a","b","c"]}
```

## Parameters

* types
* KEY:TYPE pairs separated by comma(,)
* support types:
* integer
* float
* string
* time
* bool
* array

### time_format

Time format can be specified like `KEY:time:TIME_FORMAT` as:

```apache
<filter **>
type typecast
types field3:time:%d/%b/%Y:%H:%M:%S %z
</filter>
```

### array_delimiter

Array delimiter can be specified like `KEY:array:DELIMITER` as:

```apache
<filter **>
type typecast
types field5:array:.
</filter>
```

## 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) 2015 Naotoshi Seo. See [LICENSE](LICENSE) for details.
18 changes: 18 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: utf-8
require "bundler/gem_tasks"

require 'rake/testtask'
desc 'Run test_unit based test'
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.test_files = Dir["test/**/test_*.rb"].sort
t.verbose = true
#t.warning = true
end
task :default => :test

desc 'Open an irb session preloaded with the gem library'
task :console do
sh 'irb -rubygems -I lib'
end
task :c => :console
14 changes: 14 additions & 0 deletions example.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<source>
type dummy
tag dummy
dummy {"field1":"1","field2":"2","field3":"2013-02-12 22:04:14 UTC","field4":"true","field5":"a,b,c","other":"other"}
</source>

<filter **>
type typecast
types field1:integer,field2:string,field3:time,field4:bool,field5:array
</filter>

<match **>
type stdout
</match>
24 changes: 24 additions & 0 deletions fluent-plugin-filter_typecast.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "fluent-plugin-filter_typecast"
s.version = "0.0.1"
s.authors = ["Naotoshi Seo"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/sonots/fluent-plugin-filter_typecast"
s.summary = "A Fluentd filter plugin to cast record types"
s.description = s.summary
s.licenses = ["MIT"]

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_runtime_dependency "fluentd", ">= 0.12"
s.add_development_dependency "rake"
s.add_development_dependency "test-unit"
s.add_development_dependency "pry"
s.add_development_dependency "pry-nav"
end
13 changes: 13 additions & 0 deletions lib/fluent/plugin/filter_typecast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Fluent
class TypecastFilter < Filter
Fluent::Plugin.register_filter('typecast', self)

include ::Fluent::TextParser::TypeConverter

def filter(tag, time, record)
record.map do |key, val|
[key, convert_type(key, val)]
end.to_h
end
end
end
8 changes: 8 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test/unit'
require 'fluent/log'
require 'fluent/test'

unless defined?(Test::Unit::AssertionFailedError)
class Test::Unit::AssertionFailedError < StandardError
end
end
77 changes: 77 additions & 0 deletions test/test_filter_typecast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require_relative 'helper'
require 'fluent/plugin/filter_typecast'

class DupFilterTest < Test::Unit::TestCase
include Fluent

setup do
Fluent::Test.setup
@time = Fluent::Engine.now
end

def create_driver(conf = '')
Test::FilterTestDriver.new(TypecastFilter).configure(conf, true)
end

def filter(d, msgs)
d.run {
msgs.each {|msg|
d.filter(msg, @time)
}
}
d.filtered_as_array
end

sub_test_case 'configure' do
def test_types
d = create_driver(%[
types string:string,integer:integer,float:float,bool:bool,time:time,array:array
])
types = d.instance.instance_variable_get(:@type_converters)
converters = ::Fluent::TextParser::TypeConverter::Converters
assert_equal converters['string'], types['string']
assert_equal converters['integer'], types['integer']
assert_equal converters['float'], types['float']
assert_equal converters['bool'], types['bool']
# assert_equal converters['time'], types['time']
# assert_equal converters['array'], types['array']
end
end

sub_test_case 'test_type_cast' do
def test_types
d = create_driver(%[
types string:string,integer:integer,float:float,bool:bool,time:time,array:array
])
msg = {
'integer' => '1',
'string' => 'foo',
'time' => '2013-02-12 22:01:15 UTC',
'bool' => 'true',
'array' => 'a,b,c',
'other' => 'other',
}
filtered = filter(d, [msg]).first[2]
assert_equal 1, filtered['integer']
assert_equal 'foo', filtered['string']
assert_equal 1360706475, filtered['time']
assert_equal true, filtered['bool']
assert_equal ['a', 'b', 'c'], filtered['array']
assert_equal 'other', filtered['other']
end

def test_time_format
d = create_driver(%[types time:time:%d/%b/%Y:%H:%M:%S %z])
msg = { 'time' => '12/Dec/2014:19:37:37 +0900' }
filtered = filter(d, [msg]).first[2]
assert_equal 1418380657, filtered['time']
end

def test_array_delimiter
d = create_driver(%[types array:array:.])
msg = { 'array' => 'a.b.c' }
filtered = filter(d, [msg]).first[2]
assert_equal ['a','b','c'], filtered['array']
end
end
end

0 comments on commit 5b5942a

Please sign in to comment.