fluent-plugin-time_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Yjk2ZDdlYTEwNDRjM2EyNDY3MmU3N2RhYjc2YjI3MmI0NWE3Nzk3Mw==
5
+ data.tar.gz: !binary |-
6
+ YzE2YzcxM2IxYTg2NjVhYjgzYmE2MjY2OGJlNDZmYzlkYTQ3NDA3Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDIwMjU5NzdiZjFjZDczZWU5ODU4ZTc4ZTkwNjdhNDI1MDlmNGQwNzA0YTky
10
+ MWM2ODIyZDZkMjE4NDE5ZjRlNDc0NDNkOWU0YTgwZmNlOGYyODA4ZWFmMzY1
11
+ MmU4OGI4OGE0ZmE4ODI3NGVkODgyNWM4YzI5OGM0Y2ZjMWU5OWQ=
12
+ data.tar.gz: !binary |-
13
+ ZWE1NmFmYjFmMWE4MTU4MTE4YzE3YTlmMTQyMjYxZDc4OGYyMjEwM2YwMTY5
14
+ MmQ1ZDY2MjRkNGZiY2UwOTNjYmUzMzBjZGJkNWUyYTNjMjhjNTUxMjAwYzIz
15
+ ZmI5MTJmNDM4MzgyODgzZmE4NDFjMGEyMDRmZmZkNzdmZDI3NDg=
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,2 @@
1
+ Copyright (c) 2012 Kentaro Kuribayashi
2
+ Apache License, Version 2.0
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # fluent-plugin-time_parser
2
+
3
+ ## Component
4
+
5
+
6
+ ## Contributing
7
+
8
+ 1. Fork it
9
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
10
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
11
+ 4. Push to the branch (`git push origin my-new-feature`)
12
+ 5. Create new Pull Request
13
+
14
+ ## Copyright
15
+
16
+ ### Copyright
17
+
18
+ Copyright (c) 2013- Carlos Donderis (@CaDs)
19
+
20
+ ### License
21
+
22
+ Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'fluent-plugin-time_parser'
3
+ gem.version = '0.0.1'
4
+ gem.authors = ['Carlos Donderis']
5
+ gem.email = ['cdonderis@gmail.com']
6
+ gem.homepage = 'http://github.com/cads/fluent-plugin-time_parser'
7
+ gem.description = %q{Fluentd plugin to parse the time parameter.}
8
+ gem.summary = %q{Fluentd plugin to parse the time parameter.}
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.require_paths = ['lib']
14
+
15
+ gem.add_development_dependency 'rake'
16
+ gem.add_development_dependency 'fluentd'
17
+ gem.add_runtime_dependency 'fluentd'
18
+ end
Binary file
@@ -0,0 +1,62 @@
1
+ require 'time'
2
+ require 'tzinfo'
3
+
4
+ module Fluent
5
+ class TimeParserOutput < Output
6
+ include Fluent::HandleTagNameMixin
7
+ Fluent::Plugin.register_output('time_parser', self)
8
+
9
+ config_param :key, :string, :default => 'time'
10
+ config_param :time_zone, :string
11
+
12
+ def configure(conf)
13
+ super
14
+ if (
15
+ !remove_tag_prefix &&
16
+ !remove_tag_suffix &&
17
+ !add_tag_prefix &&
18
+ !add_tag_suffix
19
+ )
20
+ raise ConfigError, "out_extract_query_params: At least one of remove_tag_prefix/remove_tag_suffix/add_tag_prefix/add_tag_suffix is required to be set."
21
+ raise ConfigError, "You must specify a time zone" unless time_zone
22
+ end
23
+ end
24
+
25
+ def start
26
+ super
27
+ end
28
+
29
+ def shutdown
30
+ super
31
+ end
32
+
33
+ def emit(tag, es, chain)
34
+ es.each {|time,record|
35
+ t = tag.dup
36
+ filter_record(t, time, record)
37
+ Engine.emit(t, time, record)
38
+ }
39
+ chain.next
40
+ end
41
+
42
+ def filter_record(tag, time, record)
43
+ begin
44
+ original_time = Time.parse(record[key])
45
+ tz = TZInfo::Timezone.get(time_zone.capitalize)
46
+
47
+ converted_time = tz.utc_to_local(original_time)
48
+
49
+ date = converted_time.to_date.to_s
50
+ hour = converted_time.hour.to_s
51
+ record['date'] = date
52
+ record['hour'] = hour
53
+ rescue ArgumentError => error
54
+ $log.warn("out_extract_query_params: #{error.message}")
55
+ rescue TZInfo::InvalidTimezoneIdentifier
56
+ $log.warn("Timezone Not Valid, please refer to http://tzinfo.rubyforge.org/doc/classes/TZInfo/Timezone.html for valid timezones")
57
+ end
58
+
59
+ super(tag, time, record)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,141 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'time'
4
+ require 'tzinfo'
5
+ require 'debugger'
6
+
7
+ class TimeParserOutputTest < Test::Unit::TestCase
8
+
9
+ TIME = "2013-04-14T06:14:36Z"
10
+
11
+ def setup
12
+ Fluent::Test.setup
13
+ end
14
+
15
+ def create_driver(conf, tag = 'test')
16
+ Fluent::Test::OutputTestDriver.new(
17
+ Fluent::TimeParserOutput, tag
18
+ ).configure(conf)
19
+ end
20
+
21
+ def test_configure
22
+ d = create_driver(%[
23
+ key test
24
+ add_tag_prefix extracted.
25
+ time_zone Tokyo
26
+ ])
27
+ assert_equal 'test', d.instance.key
28
+ assert_equal 'extracted.', d.instance.add_tag_prefix
29
+ assert_equal 'Tokyo', d.instance.time_zone
30
+
31
+ #Default Key
32
+ d = create_driver(%[
33
+ add_tag_prefix extracted.
34
+ time_zone Tokyo
35
+ ])
36
+ assert_equal 'time', d.instance.key
37
+ assert_equal 'extracted.', d.instance.add_tag_prefix
38
+ assert_equal 'Tokyo', d.instance.time_zone
39
+
40
+ #No Prefix
41
+ assert_raise(Fluent::ConfigError) do
42
+ create_driver(%[
43
+ time_zone Tokyo
44
+ ])
45
+ end
46
+ #No TimeZone
47
+ assert_raise(Fluent::ConfigError) do
48
+ create_driver(%[
49
+ key test
50
+ add_tag_prefix extracted.
51
+ ])
52
+ end
53
+ end
54
+
55
+ def test_filter_record
56
+ d = create_driver(%[
57
+ key time
58
+ add_tag_prefix extracted.
59
+ time_zone Japan
60
+ ])
61
+ tag = 'test'
62
+ record = {'time' => TIME}
63
+ d.instance.filter_record('test', Time.now, record)
64
+ tz = TZInfo::Timezone.get('Japan')
65
+ converted_time = tz.utc_to_local(Time.parse(TIME))
66
+
67
+ assert_equal record['date'], converted_time.to_date.to_s
68
+ assert_equal record['hour'], converted_time.hour.to_s
69
+ end
70
+
71
+ def test_filter_record_bad_parameters
72
+ d = create_driver(%[
73
+ key time
74
+ add_tag_prefix extracted.
75
+ time_zone myPlace
76
+ ])
77
+ tag = 'test'
78
+ record = {'time' => TIME}
79
+
80
+ d.instance.filter_record('test', Time.now, record)
81
+ assert_equal record['date'], nil
82
+ assert_equal record['hour'], nil
83
+
84
+ record = {'time' => "this is not a date"}
85
+ d.instance.filter_record('test', Time.now, record)
86
+ assert_equal record['date'], nil
87
+ assert_equal record['hour'], nil
88
+ end
89
+
90
+ def test_emit
91
+ d = create_driver(%[
92
+ key time
93
+ add_tag_prefix extracted.
94
+ time_zone Japan
95
+ ])
96
+
97
+ d.run { d.emit('time' => TIME) }
98
+ emits = d.emits
99
+
100
+ assert_equal 1, emits.count
101
+ assert_equal 'extracted.test', emits[0][0]
102
+ assert_equal TIME, emits[0][2]['time']
103
+ end
104
+
105
+ def test_emit_multi
106
+ d = create_driver(%[
107
+ key time
108
+ add_tag_prefix extracted.
109
+ time_zone Japan
110
+ ])
111
+
112
+ d.run do
113
+ d.emit('time' => TIME)
114
+ d.emit('time' => TIME)
115
+ d.emit('time' => TIME)
116
+ end
117
+ emits = d.emits
118
+
119
+ assert_equal 3, emits.count
120
+ 0.upto(2) do |i|
121
+ assert_equal 'extracted.test', emits[i][0]
122
+ assert_equal TIME, emits[i][2]['time']
123
+ end
124
+ end
125
+
126
+ def test_emit_with_invalid_time
127
+ d = create_driver(%[
128
+ key time
129
+ add_tag_prefix extracted.
130
+ time_zone Japan
131
+ ])
132
+ wrong_time = 'wrong time'
133
+ d.run { d.emit('time' => wrong_time) }
134
+ emits = d.emits
135
+
136
+ assert_equal 1, emits.count
137
+ assert_equal 'extracted.test', emits[0][0]
138
+ assert_equal wrong_time, emits[0][2]['time']
139
+ end
140
+
141
+ end
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require 'fluent/test'
7
+
8
+ unless ENV.has_key?('VERBOSE')
9
+ nulllogger = Object.new
10
+ nulllogger.instance_eval {|obj|
11
+ def method_missing(method, *args)
12
+ # pass
13
+ end
14
+ }
15
+ $log = nulllogger
16
+ end
17
+
18
+ require 'fluent/plugin/out_time_parser'
19
+
20
+ class Test::Unit::TestCase
21
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-time_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Donderis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-26 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: fluentd
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: fluentd
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
+ description: Fluentd plugin to parse the time parameter.
56
+ email:
57
+ - cdonderis@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - fluent-plugin-time_parser.gemspec
67
+ - lib/fluent/.DS_Store
68
+ - lib/fluent/plugin/out_time_parser.rb
69
+ - test/plugin/test_out_time_parser.rb
70
+ - test/test_helper.rb
71
+ homepage: http://github.com/cads/fluent-plugin-time_parser
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Fluentd plugin to parse the time parameter.
94
+ test_files:
95
+ - test/plugin/test_out_time_parser.rb
96
+ - test/test_helper.rb