Class: Fluent::Plugin::ConcatFilter

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

Defined Under Namespace

Classes: TimeoutError

Instance Method Summary collapse

Constructor Details

#initializeConcatFilter

Returns a new instance of ConcatFilter.



55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/filter_concat.rb', line 55

def initialize
  super

  @buffer = Hash.new {|h, k| h[k] = [] }
  @buffer_size = Hash.new(0)
  @timeout_map_mutex = Thread::Mutex.new
  @timeout_map_mutex.synchronize do
    @timeout_map = Hash.new {|h, k| h[k] = Fluent::Engine.now }
  end
end

Instance Method Details

#configure(conf) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fluent/plugin/filter_concat.rb', line 72

def configure(conf)
  super

  params, names = required_params
  if params.all?
    raise Fluent::ConfigError, "Either #{[names[0..-2].join(", "), names[-1]].join(" or ")} is required"
  end
  if @n_lines && (@multiline_start_regexp || @multiline_end_regexp)
    raise Fluent::ConfigError, "n_lines and multiline_start_regexp/multiline_end_regexp are exclusive"
  end
  if @partial_key && @n_lines
    raise Fluent::ConfigError, "partial_key and n_lines are exclusive"
  end
  if @partial_key && (@multiline_start_regexp || @multiline_end_regexp)
    raise Fluent::ConfigError, "partial_key and multiline_start_regexp/multiline_end_regexp are exclusive"
  end
  if @partial_key && @partial_value.nil?
    raise Fluent::ConfigError, "partial_value is required when partial_key is specified"
  end
  if @use_partial_metadata && @n_lines
    raise Fluent::ConfigError, "use_partial_metadata and n_lines are exclusive"
  end
  if @use_partial_metadata && (@multiline_start_regexp || @multiline_end_regexp)
    raise Fluent::ConfigError, "use_partial_metadata and multiline_start_regexp/multiline_end_regexp are exclusive"
  end
  if @use_partial_metadata && @partial_key
    raise Fluent::ConfigError, "use_partial_metadata and partial_key are exclusive"
  end
  if @use_partial_cri_logtag && @n_lines
    raise Fluent::ConfigError, "use_partial_cri_logtag and n_lines are exclusive"
  end
  if @use_partial_cri_logtag && (@multiline_start_regexp || @multiline_end_regexp)
    raise Fluent::ConfigError, "use_partial_cri_logtag and multiline_start_regexp/multiline_end_regexp are exclusive"
  end
  if @use_partial_cri_logtag && @partial_key
    raise Fluent::ConfigError, "use_partial_cri_logtag and partial_key are exclusive"
  end

  @mode = nil
  case
  when @n_lines
    @mode = :line
  when @partial_key
    @mode = :partial
  when @use_partial_metadata
    @mode = :partial_metadata
    case @partial_metadata_format
    when :"docker-fluentd"
      @partial_message_field     = "partial_message".freeze
      @partial_id_field          = "partial_id".freeze
      @partial_ordinal_field     = "partial_ordinal".freeze
      @partial_last_field        = "partial_last".freeze
      @partial_message_indicator = @partial_message_field
    when :"docker-journald"
      @partial_message_field     = "CONTAINER_PARTIAL_MESSAGE".freeze
      @partial_id_field          = "CONTAINER_PARTIAL_ID".freeze
      @partial_ordinal_field     = "CONTAINER_PARTIAL_ORDINAL".freeze
      @partial_last_field        = "CONTAINER_PARTIAL_LAST".freeze
      # the journald log driver does not add CONTAINER_PARTIAL_MESSAGE to the last message
      # so we help ourself by using another indicator
      @partial_message_indicator = @partial_id_field
    when :"docker-journald-lowercase"
      @partial_message_field     = "container_partial_message".freeze
      @partial_id_field          = "container_partial_id".freeze
      @partial_ordinal_field     = "container_partial_ordinal".freeze
      @partial_last_field        = "container_partial_last".freeze
      @partial_message_indicator = @partial_id_field
    end
  when @use_partial_cri_logtag
    @mode = :partial_cri
    @partial_logtag_delimiter = ":".freeze
    @partial_logtag_continue = "P".freeze
    @partial_logtag_full = "F".freeze
  when @multiline_start_regexp || @multiline_end_regexp
    @mode = :regexp
    if @multiline_start_regexp
      @multiline_start_regexp = Regexp.compile(@multiline_start_regexp[1..-2])
    end
    if @multiline_end_regexp
      @multiline_end_regexp = Regexp.compile(@multiline_end_regexp[1..-2])
    end
    if @continuous_line_regexp
      @continuous_line_regexp = Regexp.compile(@continuous_line_regexp[1..-2])
    end
  end
end

#filter_stream(tag, es) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/fluent/plugin/filter_concat.rb', line 171

def filter_stream(tag, es)
  if /\Afluent\.(?:trace|debug|info|warn|error|fatal)\z/ =~ tag
    return es
  end

  new_es = Fluent::MultiEventStream.new
  es.each do |time, record|
    unless record.key?(@key)
      new_es.add(time, record)
      next
    end
    if @mode == :partial
      unless record.key?(@partial_key)
        new_es.add(time, record)
        next
      end
    end
    if @mode == :partial_metadata
      unless record.key?(@partial_message_indicator)
        new_es.add(time, record)
        next
      end
    end
    begin
      flushed_es = process(tag, time, record)
      unless flushed_es.empty?
        flushed_es.each do |_time, new_record|
          time = _time if @use_first_timestamp
          merged_record = record.merge(new_record)
          case @mode
          when :partial
            merged_record.delete(@partial_key) unless @keep_partial_key
          when :partial_metadata
            unless @keep_partial_metadata
              merged_record.delete(@partial_message_field)
              merged_record.delete(@partial_id_field)
              merged_record.delete(@partial_ordinal_field)
              merged_record.delete(@partial_last_field)
            end
          when :partial_cri
            merged_record.delete(@partial_cri_logtag_key) unless @keep_partial_key
            merged_record.delete(@partial_cri_stream_key)
          end
          new_es.add(time, merged_record)
        end
      end
    rescue => e
      router.emit_error_event(tag, time, record, e)
    end
  end
  new_es
end

#required_paramsObject



66
67
68
69
70
# File 'lib/fluent/plugin/filter_concat.rb', line 66

def required_params
  params = [@n_lines.nil?, @multiline_start_regexp.nil?, @multiline_end_regexp.nil?, @partial_key.nil?, !@use_partial_metadata, !@use_partial_cri_logtag]
  names = ["n_lines", "multiline_start_regexp", "multiline_end_regexp", "partial_key", "use_partial_metadata", "use_partial_cri_logtag"]
  return params, names
end

#shutdownObject



165
166
167
168
169
# File 'lib/fluent/plugin/filter_concat.rb', line 165

def shutdown
  @finished = true
  flush_remaining_buffer
  super
end

#startObject



159
160
161
162
163
# File 'lib/fluent/plugin/filter_concat.rb', line 159

def start
  super
  @finished = false
  timer_execute(:filter_concat_timer, 1, &method(:on_timer))
end