Class: Fluent::GrepOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_grep.rb

Constant Summary collapse

REGEXP_MAX_NUM =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrepOutput

Returns a new instance of GrepOutput.


32
33
34
35
# File 'lib/fluent/plugin/out_grep.rb', line 32

def initialize
  require 'string/scrub' if RUBY_VERSION.to_f < 2.1
  super
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes


20
21
22
# File 'lib/fluent/plugin/out_grep.rb', line 20

def excludes
  @excludes
end

#regexpsObject (readonly)

for test


19
20
21
# File 'lib/fluent/plugin/out_grep.rb', line 19

def regexps
  @regexps
end

Instance Method Details

#configure(conf) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/out_grep.rb', line 37

def configure(conf)
  super

  @regexps = {}
  @regexps[@input_key] = Regexp.compile(@regexp) if @input_key and @regexp
  (1..REGEXP_MAX_NUM).each do |i|
    next unless conf["regexp#{i}"]
    key, regexp = conf["regexp#{i}"].split(/ /, 2)
    raise Fluent::ConfigError, "regexp#{i} does not contain 2 parameters" unless regexp
    raise Fluent::ConfigError, "regexp#{i} contains a duplicated key, #{key}" if @regexps[key]
    @regexps[key] = Regexp.compile(regexp)
  end

  @excludes = {}
  @excludes[@input_key] = Regexp.compile(@exclude) if @input_key and @exclude
  (1..REGEXP_MAX_NUM).each do |i|
    next unless conf["exclude#{i}"]
    key, exclude = conf["exclude#{i}"].split(/ /, 2)
    raise Fluent::ConfigError, "exclude#{i} does not contain 2 parameters" unless exclude
    raise Fluent::ConfigError, "exclude#{i} contains a duplicated key, #{key}" if @excludes[key]
    @excludes[key] = Regexp.compile(exclude)
  end

  if conf['@label'].nil? and @tag.nil? and @add_tag_prefix.nil? and @remove_tag_prefix.nil? and @add_tag_suffix.nil? and @remove_tag_suffix.nil?
    @add_tag_prefix = 'greped' # not ConfigError to support lower version compatibility
  end
  @tag_proc = tag_proc
end

#emit(tag, es, chain) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fluent/plugin/out_grep.rb', line 66

def emit(tag, es, chain)
  emit_tag = @tag_proc.call(tag)

  es.each do |time,record|
    catch(:break_loop) do
      @regexps.each do |key, regexp|
        throw :break_loop unless match(regexp, record[key].to_s)
      end
      @excludes.each do |key, exclude|
        throw :break_loop if match(exclude, record[key].to_s)
      end
      router.emit(emit_tag, time, record)
    end
  end

  chain.next
rescue => e
  log.warn "out_grep: #{e.class} #{e.message} #{e.backtrace.first}"
end