Class: Fluent::Plugin::JqFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/fluent/plugin/formatter_jq.rb

Instance Method Summary collapse

Constructor Details

#initializeJqFormatter

Returns a new instance of JqFormatter.



33
34
35
36
# File 'lib/fluent/plugin/formatter_jq.rb', line 33

def initialize
	super
	require "jq"
end

Instance Method Details

#configure(conf) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/fluent/plugin/formatter_jq.rb', line 38

def configure(conf)
	super

	@jq = @jq_program unless @jq
	raise Fluent::ConfigError, "jq is required." unless @jq

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

#format(tag, time, record) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/plugin/formatter_jq.rb', line 49

def format(tag, time, record)
	item = [].tap { |buf|
	  @jq_filter.update(MultiJson.dump(record), false) { |r|
	    buf << MultiJson.load("[#{r}]").first
	  }
	}.first
	return item if item.instance_of?(String)
	MultiJson.dump item
rescue JQ::Error
	msg = "Failed to format #{record.to_json} with #{@jq}, error: #{$!.message}"
	log.error msg
	case @on_error
	when :skip
	  return ''
	when :ignore
	  return record.to_json
	when :raise_error
	  raise msg
	end
end