Skip to content

Commit

Permalink
Merge pull request #906 from fluent/compat-for-parser-formatter
Browse files Browse the repository at this point in the history
Add Fluent::Plugin::Formatter, Fluent::Plugin::Parser and formatter/parser plugin files
  • Loading branch information
tagomoris committed Apr 21, 2016
2 parents 985a912 + 2f26188 commit b93c648
Show file tree
Hide file tree
Showing 25 changed files with 1,518 additions and 911 deletions.
109 changes: 109 additions & 0 deletions lib/fluent/compat/formatter.rb
@@ -0,0 +1,109 @@
#
# 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/plugin'
require 'fluent/plugin/formatter'

require 'fluent/plugin/formatter_out_file'
require 'fluent/plugin/formatter_stdout'
require 'fluent/plugin/formatter_json'
require 'fluent/plugin/formatter_hash'
require 'fluent/plugin/formatter_msgpack'
require 'fluent/plugin/formatter_ltsv'
require 'fluent/plugin/formatter_csv'
require 'fluent/plugin/formatter_single_value'

module Fluent
module Compat
class Formatter < Fluent::Plugin::Formatter
# TODO: warn when deprecated
end

module TextFormatter
def self.register_template(type, template)
# TODO: warn when deprecated to use Plugin.register_formatter directly
if template.is_a?(Class)
Fluent::Plugin.register_formatter(type, template)
elsif template.respond_to?(:call) && template.arity == 3 # Proc.new { |tag, time, record| }
Fluent::Plugin.register_formatter(type, Proc.new { ProcWrappedFormatter.new(template) })
elsif template.respond_to?(:call)
Fluent::Plugin.register_formatter(type, template)
else
raise ArgumentError, "Template for formatter must be a Class or callable object"
end
end

def self.lookup(type)
# TODO: warn when deprecated to use Plugin.new_formatter(type, parent: plugin)
Fluent::Plugin.new_formatter(type)
end

# Keep backward-compatibility
def self.create(conf)
# TODO: warn when deprecated
format = conf['format']
if format.nil?
raise ConfigError, "'format' parameter is required"
end

formatter = lookup(format)
if formatter.respond_to?(:configure)
formatter.configure(conf)
end
formatter
end

HandleTagAndTimeMixin = Fluent::Plugin::Formatter::HandleTagAndTimeMixin
StructuredFormatMixin = Fluent::Plugin::Formatter::StructuredFormatMixin

class ProcWrappedFormatter < Fluent::Plugin::ProcWrappedFormatter
# TODO: warn when deprecated
end

class OutFileFormatter < Fluent::Plugin::OutFileFormatter
# TODO: warn when deprecated
end

class StdoutFormatter < Fluent::Plugin::StdoutFormatter
# TODO: warn when deprecated
end

class JSONFormatter < Fluent::Plugin::JSONFormatter
# TODO: warn when deprecated
end

class HashFormatter < Fluent::Plugin::HashFormatter
# TODO: warn when deprecated
end

class MessagePackFormatter < Fluent::Plugin::MessagePackFormatter
# TODO: warn when deprecated
end

class LabeledTSVFormatter < Fluent::Plugin::LabeledTSVFormatter
# TODO: warn when deprecated
end

class CsvFormatter < Fluent::Plugin::CsvFormatter
# TODO: warn when deprecated
end

class SingleValueFormatter < Fluent::Plugin::SingleValueFormatter
# TODO: warn when deprecated
end
end
end
end
163 changes: 163 additions & 0 deletions lib/fluent/compat/parser.rb
@@ -0,0 +1,163 @@
#
# 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/plugin'
require 'fluent/plugin/parser'
require 'fluent/mixin'

require 'fluent/plugin/parser_json'
require 'fluent/plugin/parser_tsv'
require 'fluent/plugin/parser_ltsv'
require 'fluent/plugin/parser_csv'
require 'fluent/plugin/parser_none'
require 'fluent/plugin/parser_apache2'
require 'fluent/plugin/parser_syslog'
require 'fluent/plugin/parser_multiline'

module Fluent
module Compat
class Parser < Fluent::Plugin::Parser
# TODO: warn when deprecated
end

class TextParser
# Keep backward compatibility for existing plugins
ParserError = Fluent::Plugin::Parser::ParserError
# TODO: will be removed at v1
TypeConverter = Fluent::TypeConverter

def initialize
# TODO: warn when deprecated
@parser = nil
@estimate_current_event = nil
end

attr_reader :parser

# SET false BEFORE CONFIGURE, to return nil when time not parsed
# 'configure()' may raise errors for unexpected configurations
attr_accessor :estimate_current_event

def configure(conf, required=true)
format = conf['format']

@parser = TextParser.lookup(format)
if ! @estimate_current_event.nil? && @parser.respond_to?(:'estimate_current_event=')
@parser.estimate_current_event = @estimate_current_event
end

if @parser.respond_to?(:configure)
@parser.configure(conf)
end

return true
end

def parse(text, &block)
if block
@parser.parse(text, &block)
else
@parser.parse(text) { |time, record|
return time, record
}
end
end

def self.register_template(type, template, time_format=nil)
# TODO: warn when deprecated to use Plugin.register_parser directly
if template.is_a?(Class) || template.respond_to?(:call)
Fluent::Plugin.register_parser(type, template)
elsif template.is_a?(Regexp)
Fluent::Plugin.register_parser(type, Proc.new { RegexpParser.new(template, {'time_format' => time_format}) })
else
raise ArgumentError, "Template for parser must be a Class, callable object or regular expression object"
end
end

def self.lookup(format)
# TODO: warn when deprecated to use Plugin.new_parser or RegexpParser.new directly
if format.nil?
raise ConfigError, "'format' parameter is required"
end

if format[0] == ?/ && format[format.length-1] == ?/
# regexp
begin
regexp = Regexp.new(format[1..-2])
if regexp.named_captures.empty?
raise "No named captures"
end
rescue
raise ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
end

RegexpParser.new(regexp)
else
# built-in template
begin
Fluent::Plugin.new_parser(format)
rescue ConfigError # keep same error message
raise ConfigError, "Unknown format template '#{format}'"
end
end
end

class TimeParser < Fluent::Plugin::Parser::TimeParser
# TODO: warn when deprecated
end

class RegexpParser < Fluent::Plugin::RegexpParser
# TODO: warn when deprecated
end

class ValuesParser < Fluent::Plugin::ValuesParser
# TODO: warn when deprecated
end

class JSONParser < Fluent::Plugin::JSONParser
# TODO: warn when deprecated
end

class TSVParser < Fluent::Plugin::TSVParser
# TODO: warn when deprecated
end

class LabeledTSVParser < Fluent::Plugin::LabeledTSVParser
# TODO: warn when deprecated
end

class CSVParser < Fluent::Plugin::CSVParser
# TODO: warn when deprecated
end

class NoneParser < Fluent::Plugin::NoneParser
# TODO: warn when deprecated
end

class ApacheParser < Fluent::Plugin::Apache2Parser
# TODO: warn when deprecated
end

class SyslogParser < Fluent::Plugin::SyslogParser
# TODO: warn when deprecated
end

class MultilineParser < Fluent::Plugin::MultilineParser
# TODO: warn when deprecated
end
end
end
end

0 comments on commit b93c648

Please sign in to comment.