-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract more mixin module to compat #1052
Merged
tagomoris
merged 7 commits into
fluent:master
from
cosmo0920:extract-more-mixin-module-to-compat
Jun 20, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c8d86f
Extract HandleTagNameMixin from mixin.rb
cosmo0920 24de801
Extract SetTimeKeyMixin from mixin.rb
cosmo0920 8441f35
Extract SetTagKeyMixin from mixin.rb
cosmo0920 69028e5
Extract TypeConverter from mixin.rb
cosmo0920 9311eca
Extract TimeFormatter from mixin.rb
cosmo0920 13b954c
Fix namespaces and requires in extracted compat modules
cosmo0920 b03d8f4
Remove needless requires in mixin.rb
cosmo0920 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/compat/record_filter_mixin' | ||
|
||
module Fluent | ||
module Compat | ||
module HandleTagNameMixin | ||
include RecordFilterMixin | ||
|
||
attr_accessor :remove_tag_prefix, :remove_tag_suffix, :add_tag_prefix, :add_tag_suffix | ||
def configure(conf) | ||
super | ||
|
||
@remove_tag_prefix = if conf.has_key?('remove_tag_prefix') | ||
Regexp.new('^' + Regexp.escape(conf['remove_tag_prefix'])) | ||
else | ||
nil | ||
end | ||
|
||
@remove_tag_suffix = if conf.has_key?('remove_tag_suffix') | ||
Regexp.new(Regexp.escape(conf['remove_tag_suffix']) + '$') | ||
else | ||
nil | ||
end | ||
|
||
@add_tag_prefix = conf['add_tag_prefix'] | ||
@add_tag_suffix = conf['add_tag_suffix'] | ||
end | ||
|
||
def filter_record(tag, time, record) | ||
tag.sub!(@remove_tag_prefix, '') if @remove_tag_prefix | ||
tag.sub!(@remove_tag_suffix, '') if @remove_tag_suffix | ||
tag.insert(0, @add_tag_prefix) if @add_tag_prefix | ||
tag << @add_tag_suffix if @add_tag_suffix | ||
super(tag, time, record) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/config/error' | ||
require 'fluent/config/types' | ||
require 'fluent/compat/record_filter_mixin' | ||
|
||
module Fluent | ||
module Compat | ||
module SetTagKeyMixin | ||
include RecordFilterMixin | ||
|
||
attr_accessor :include_tag_key, :tag_key | ||
|
||
def configure(conf) | ||
@include_tag_key = false | ||
|
||
super | ||
|
||
if s = conf['include_tag_key'] | ||
include_tag_key = Fluent::Config.bool_value(s) | ||
raise Fluent::ConfigError, "Invalid boolean expression '#{s}' for include_tag_key parameter" if include_tag_key.nil? | ||
|
||
@include_tag_key = include_tag_key | ||
end | ||
|
||
@tag_key = conf['tag_key'] || 'tag' if @include_tag_key | ||
end | ||
|
||
def filter_record(tag, time, record) | ||
super | ||
|
||
record[@tag_key] = tag if @include_tag_key | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/config/error' | ||
require 'fluent/compat/record_filter_mixin' | ||
require 'fluent/compat/time_formatter' | ||
|
||
module Fluent | ||
module Compat | ||
module SetTimeKeyMixin | ||
require 'fluent/timezone' | ||
include RecordFilterMixin | ||
|
||
attr_accessor :include_time_key, :time_key, :localtime, :timezone | ||
|
||
def configure(conf) | ||
@include_time_key = false | ||
@localtime = false | ||
@timezone = nil | ||
|
||
super | ||
|
||
if s = conf['include_time_key'] | ||
include_time_key = Fluent::Config.bool_value(s) | ||
raise Fluent::ConfigError, "Invalid boolean expression '#{s}' for include_time_key parameter" if include_time_key.nil? | ||
|
||
@include_time_key = include_time_key | ||
end | ||
|
||
if @include_time_key | ||
@time_key = conf['time_key'] || 'time' | ||
@time_format = conf['time_format'] | ||
|
||
if conf['localtime'] | ||
@localtime = true | ||
elsif conf['utc'] | ||
@localtime = false | ||
end | ||
|
||
if conf['timezone'] | ||
@timezone = conf['timezone'] | ||
Fluent::Timezone.validate!(@timezone) | ||
end | ||
|
||
@timef = Fluent::Compat::TimeFormatter.new(@time_format, @localtime, @timezone) | ||
end | ||
end | ||
|
||
def filter_record(tag, time, record) | ||
super | ||
|
||
record[@time_key] = @timef.format(time) if @include_time_key | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/timezone' | ||
require 'fluent/time' | ||
|
||
module Fluent | ||
module Compat | ||
class TimeFormatter | ||
def initialize(format, localtime, timezone = nil) | ||
@tc1 = 0 | ||
@tc1_str = nil | ||
@tc2 = 0 | ||
@tc2_str = nil | ||
|
||
if format && format =~ /(^|[^%])(%%)*%L|(^|[^%])(%%)*%\d*N/ | ||
define_singleton_method(:format) {|time| | ||
format_with_subsec(time) | ||
} | ||
else | ||
define_singleton_method(:format) {|time| | ||
format_without_subsec(time) | ||
} | ||
end | ||
|
||
if formatter = Fluent::Timezone.formatter(timezone, format) | ||
define_singleton_method(:format_nocache) {|time| | ||
formatter.call(time) | ||
} | ||
return | ||
end | ||
|
||
if format | ||
if localtime | ||
define_singleton_method(:format_nocache) {|time| | ||
Time.at(time).strftime(format) | ||
} | ||
else | ||
define_singleton_method(:format_nocache) {|time| | ||
Time.at(time).utc.strftime(format) | ||
} | ||
end | ||
else | ||
if localtime | ||
define_singleton_method(:format_nocache) {|time| | ||
Time.at(time).iso8601 | ||
} | ||
else | ||
define_singleton_method(:format_nocache) {|time| | ||
Time.at(time).utc.iso8601 | ||
} | ||
end | ||
end | ||
end | ||
|
||
def format_without_subsec(time) | ||
if @tc1 == time | ||
return @tc1_str | ||
elsif @tc2 == time | ||
return @tc2_str | ||
else | ||
str = format_nocache(time) | ||
if @tc1 < @tc2 | ||
@tc1 = time | ||
@tc1_str = str | ||
else | ||
@tc2 = time | ||
@tc2_str = str | ||
end | ||
return str | ||
end | ||
end | ||
|
||
def format_with_subsec(time) | ||
if Fluent::EventTime.eq?(@tc1, time) | ||
return @tc1_str | ||
elsif Fluent::EventTime.eq?(@tc2, time) | ||
return @tc2_str | ||
else | ||
str = format_nocache(time) | ||
if @tc1 < @tc2 | ||
@tc1 = time | ||
@tc1_str = str | ||
else | ||
@tc2 = time | ||
@tc2_str = str | ||
end | ||
return str | ||
end | ||
end | ||
|
||
def format(time) | ||
# will be overridden in initialize | ||
end | ||
|
||
def format_nocache(time) | ||
# will be overridden in initialize | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
module Fluent | ||
module Compat | ||
module TypeConverter | ||
Converters = { | ||
'string' => lambda { |v| v.to_s }, | ||
'integer' => lambda { |v| v.to_i }, | ||
'float' => lambda { |v| v.to_f }, | ||
'bool' => lambda { |v| | ||
case v.downcase | ||
when 'true', 'yes', '1' | ||
true | ||
else | ||
false | ||
end | ||
}, | ||
'time' => lambda { |v, time_parser| | ||
time_parser.parse(v) | ||
}, | ||
'array' => lambda { |v, delimiter| | ||
v.to_s.split(delimiter) | ||
} | ||
} | ||
|
||
def self.included(klass) | ||
klass.instance_eval { | ||
config_param :types, :string, default: nil | ||
config_param :types_delimiter, :string, default: ',' | ||
config_param :types_label_delimiter, :string, default: ':' | ||
} | ||
end | ||
|
||
def configure(conf) | ||
super | ||
|
||
@type_converters = nil | ||
@type_converters = parse_types_parameter unless @types.nil? | ||
end | ||
|
||
private | ||
|
||
def convert_type(name, value) | ||
converter = @type_converters[name] | ||
converter.nil? ? value : converter.call(value) | ||
end | ||
|
||
def parse_types_parameter | ||
converters = {} | ||
|
||
@types.split(@types_delimiter).each { |pattern_name| | ||
name, type, format = pattern_name.split(@types_label_delimiter, 3) | ||
raise ConfigError, "Type is needed" if type.nil? | ||
|
||
case type | ||
when 'time' | ||
require 'fluent/parser' | ||
t_parser = Fluent::TextParser::TimeParser.new(format) | ||
converters[name] = lambda { |v| | ||
Converters[type].call(v, t_parser) | ||
} | ||
when 'array' | ||
delimiter = format || ',' | ||
converters[name] = lambda { |v| | ||
Converters[type].call(v, delimiter) | ||
} | ||
else | ||
converters[name] = Converters[type] | ||
end | ||
} | ||
|
||
converters | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess these additional spaces are used to clarify getting localtime/utc information from conf object.
Former is to get
localtime
, latter is to getutc
from conf object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... Okay, understood.