Class: Fluent::MuleFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_mule.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



17
18
19
20
21
22
# File 'lib/fluent/plugin/filter_mule.rb', line 17

def configure(conf)
  super
  # TimeParser class is already given. It takes a single argument as the time format
  # to parse the time string with.
  @time_parser = Fluent::TextParser::TimeParser.new(@time_format)
end

#filter(tag, time, record) ⇒ Object



36
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/filter_mule.rb', line 36

def filter(tag, time, record)
  # This method implements the filtering logic for individual filters
  # It is internal to this class and called by filter_stream unless
  # the user overrides filter_stream.
  #
  # If returns nil, that records are ignored.
  text = record[@key_name]

  messageRegex = /
    ^
    (?<log_level>[\w]+)\s
    (?<log_time>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})\s
    \[(?<thread>.+)\]\s
    (?<position>[^:]+):\s
    (?<kv_pairs>.*?)\s?
    (?<payload>payload=.*)?
    $
  /x
  
  match = text.match(messageRegex)      

  # If message doesn't match regex we return the record as is
  if match.nil?
    return record
  end

  ['log_level', 'log_time', 'thread', 'position', 'kv_pairs'].each { |group|
    updateRecord(record, match, group, group == 'kv_pairs')
  }

  if (match.names.include? 'payload') && !match['payload'].nil?
    k, v = match['payload'].split('=', 2)
    record[@prefix + k] = v
  end
    
  if @time_parse && !record['log_time'].nil? 
    new_time = @time_parser.parse(record['mule_log_time'])
    record.delete('time')
    record['time'] = current_time
  end
  record.delete(@key_name)
  record
end

#shutdownObject



30
31
32
33
34
# File 'lib/fluent/plugin/filter_mule.rb', line 30

def shutdown
  super
  # This method is called when Fluentd is shutting down.
  # Use it to free up resources, etc.
end

#startObject



24
25
26
27
28
# File 'lib/fluent/plugin/filter_mule.rb', line 24

def start
  super
  # This is the first method to be called when it starts running
  # Use it to allocate resources, etc.
end

#updateRecord(record, match, group, kv_flag) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/plugin/filter_mule.rb', line 80

def updateRecord(record, match, group, kv_flag)
  if (match.names.include? group) && !match[group].nil?
    if kv_flag
      kv_match = match[group].match(/(\w+)=([\w|-]+)\s/)
      if kv_match.nil?
        return 
      end

      match[group].split(@kv_delimiter).each { |kv|
        k, v = kv.split(@kv_char, 2)
        record[@prefix + k] = v
      }
    else
      record[@prefix + group] = match[group]
    end
  end
end