Class: Fluent::Plugin::JsonSizeLimitFilter

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

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



23
24
25
# File 'lib/fluent/plugin/in_fluent_plugin_json_size_limit.rb', line 23

def configure(conf)
  super
end

#filter(tag, time, record) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/in_fluent_plugin_json_size_limit.rb', line 27

def filter(tag, time, record)
  json_str = record.to_json
  while json_str.bytesize > 250 * 1024
    sorted_fields = record.sort_by { |k, v| v.to_s.length }.reverse
    reduced = false

    sorted_fields.each do |key, value|
      next unless value.is_a?(String) && value.length > 10

      current_size = json_str.bytesize
      target_size = 250 * 1024
      excess_size = current_size - target_size

      # Уменьшаем текущее поле
      new_length = [value.length - excess_size, 0].max
      record[key] = value[0...new_length]

      json_str = record.to_json
      if json_str.bytesize <= 250 * 1024
        reduced = true
        break
      end
    end

    break unless reduced
  end
  record
end