Class: Fluent::Plugin::JqParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_jq.rb

Instance Method Summary collapse

Constructor Details

#initializeJqParser

Returns a new instance of JqParser.



27
28
29
30
# File 'lib/fluent/plugin/parser_jq.rb', line 27

def initialize
	super
	require "jq"
end

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
36
37
# File 'lib/fluent/plugin/parser_jq.rb', line 32

def configure(conf)
	super
	@jq_filter = JQ::Core.new @jq
rescue JQ::Error
	raise Fluent::ConfigError, "Could not parse jq filter: #{@jq}, error: #{$!.message}"
end

#parse(text) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/parser_jq.rb', line 39

def parse(text)
	record = [].tap { |buf|
	  @jq_filter.update(MultiJson.dump(text), false) { |r|
	    buf << MultiJson.load("[#{r}]").first
	  }
	}.first
	if record.is_a?(Hash)
	  yield parse_time(record), record
	else
	  log.error "jq filter #{@jq} did not return a hash, skip this record."
	end
rescue JQ::Error
	log.error "Failed to parse #{text} with #{@jq}, error: #{$!.message}"
	nil
end