diff --git a/fluent-plugin-json-in-json.gemspec b/fluent-plugin-json-in-json.gemspec index bcdf9dd..be3168f 100644 --- a/fluent-plugin-json-in-json.gemspec +++ b/fluent-plugin-json-in-json.gemspec @@ -1,7 +1,7 @@ # coding: utf-8 Gem::Specification.new do |spec| spec.name = "fluent-plugin-json-in-json" - spec.version = "0.1.4" + spec.version = "0.2.0" spec.authors = ["Gavin M. Roy"] spec.email = ["gavinmroy@gmail.com"] spec.description = %q{Parser plugin that parses JSON attributes with JSON strings in them} @@ -15,6 +15,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_runtime_dependency "yajl-ruby", "~> 1.0" + spec.add_runtime_dependency "fluentd", ['>= 0.14.0', '< 2'] spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" end diff --git a/lib/fluent/plugin/parser_json_in_json.rb b/lib/fluent/plugin/parser_json_in_json.rb index ff932c3..75c0e2c 100644 --- a/lib/fluent/plugin/parser_json_in_json.rb +++ b/lib/fluent/plugin/parser_json_in_json.rb @@ -1,67 +1,66 @@ require 'yajl' +require 'fluent/plugin/parser' -module Fluent - class TextParser - class JSONInJSONParser < Parser - Fluent::Plugin.register_parser('json_in_json', self) +module Fluent::Plugin + class JSONInJSONParser < Parser + Fluent::Plugin.register_parser('json_in_json', self) - config_param :time_key, :string, :default => 'time' - config_param :time_format, :string, :default => nil + config_param :time_key, :string, :default => 'time' + config_param :time_format, :string, :default => nil - def configure(conf) - super + def configure(conf) + super - unless @time_format.nil? - @time_parser = TimeParser.new(@time_format) - @mutex = Mutex.new - end + unless @time_format.nil? + @time_parser = Fluent::TimeParser.new(@time_format) + @mutex = Mutex.new end + end - def parse(text) - record = Yajl.load(text) + def parse(text) + record = Yajl.load(text) - value = @keep_time_key ? record[@time_key] : record.delete(@time_key) - if value - if @time_format - time = @mutex.synchronize { @time_parser.parse(value) } - else - begin - time = value.to_i - rescue => e - raise ParserError, "invalid time value: value = #{value}, error_class = #{e.class.name}, error = #{e.message}" - end - end + value = @keep_time_key ? record[@time_key] : record.delete(@time_key) + if value + if @time_format + time = @mutex.synchronize { @time_parser.parse(value) } else - if @estimate_current_event - time = Engine.now - else - time = nil + begin + time = value.to_i + rescue => e + raise ParserError, "invalid time value: value = #{value}, error_class = #{e.class.name}, error = #{e.message}" end end + else + if @estimate_current_event + time = Fluent::Engine.now + else + time = nil + end + end - values = Hash.new - record.each do |k, v| - if v[0] == '{' - deserialized = Yajl.load(v) - if deserialized.is_a?(Hash) - values.merge!(deserialized) - record.delete k - end + values = Hash.new + record.each do |k, v| + if v[0] == '{' + deserialized = Yajl.load(v) + if deserialized.is_a?(Hash) + values.merge!(deserialized) + record.delete k end end - record.merge!(values) + end + record.merge!(values) - if block_given? - yield time, record - else - return time, record - end - rescue Yajl::ParseError - if block_given? - yield nil, nil - else - return nil, nil - end + if block_given? + yield time, record + else + return time, record + end + rescue Yajl::ParseError + if block_given? + yield nil, nil + else + return nil, nil end end end diff --git a/metadata.yaml b/metadata.yaml new file mode 100644 index 0000000..297a704 --- /dev/null +++ b/metadata.yaml @@ -0,0 +1,2 @@ +owner: OSRE-TAU +name: fluent-plugin-json-in-json