Skip to content
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

Update to support fluent v0.14.x #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fluent-plugin-json-in-json.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ["[email protected]"]
spec.description = %q{Parser plugin that parses JSON attributes with JSON strings in them}
Expand All @@ -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
97 changes: 48 additions & 49 deletions lib/fluent/plugin/parser_json_in_json.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
owner: OSRE-TAU
name: fluent-plugin-json-in-json