fluent-plugin-fluent-plugin-json-size-limit 0.1.29 → 0.1.30

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 591d68308d420614aa3ea9c6c4379a77f489afa72b42adaa22356686f403ee9e
4
- data.tar.gz: 600efc817220c04458478cd2d1b44025f17a01eb77f2fadf43a650f382437938
3
+ metadata.gz: 73d82be0721ff66686b415ed8264f699b7ec5943c343442246591d7ffff2a691
4
+ data.tar.gz: 0c7373794b14bb31a8378a5095959f29baba102a28c7060d5b5dbc59e62a48ee
5
5
  SHA512:
6
- metadata.gz: 2c865638c22f747a6f42c9f0a2a6469463d85567837c6381858fcc569f8cffd5a46e55b65142bba9d00f4f7fa629e3a1870821bcc3c27c0556c947e0b6f6c29e
7
- data.tar.gz: b285917dba21b70b9b2937b89d91da7273b33bbd4f3f2855ee4a6eef541ff2f8657266ba81be3bc26e932702b7376b68f432170bd3936e605db4a6ad0d9c0a19
6
+ metadata.gz: 2e59c251899fdc1172ab144af0cc4d5aaa53b1c667d0b2ed7f22cbdf11d1b3f02992ce6e930ac604c8418108b5eec77502aee1272ab2261cf475769e19e7f635
7
+ data.tar.gz: e05efd4111d3a46ac60bd2b0b856359b408b98b1daf7cb831138ff64e76d456397670eaea83652980f428206cbc091dd5e2b6fbf24d453b58ee75d316f0f27d1
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-fluent-plugin-json-size-limit"
6
- spec.version = "0.1.29"
6
+ spec.version = "0.1.30"
7
7
  spec.authors = ["Mykola Panin"]
8
8
  spec.email = ["mykola.panin@creatoriq.com"]
9
9
 
@@ -20,26 +20,16 @@ module Fluent
20
20
  class JsonSizeLimitFilter < Filter
21
21
  Fluent::Plugin.register_filter('jsonsizelimit', self)
22
22
 
23
- # Configuration parameter for maximum JSON size
24
23
  config_param :max_size, :size, default: 250 * 1024
25
- # Configuration parameter for maximum attempts to reduce JSON size
26
- config_param :max_attempts, :integer, default: 100
27
24
 
28
25
  def configure(conf)
29
26
  super
30
- log.debug "Configuring JsonSizeLimitFilter with max_size: #{@max_size} and max_attempts: #{@max_attempts}"
27
+ log.debug "Configuring JsonSizeLimitFilter with max_size: #{@max_size}"
31
28
  end
32
29
 
33
30
  def filter(tag, time, record)
34
- # Ensure that the record is a Hash before processing
35
- return record unless record.is_a?(Hash)
36
-
37
31
  original_size = record.to_json.bytesize
38
- begin
39
- reduce_size(record, @max_size)
40
- rescue => e
41
- log.error "Error during size reduction: #{e.message}"
42
- end
32
+ reduce_size(record, @max_size)
43
33
  final_size = record.to_json.bytesize
44
34
 
45
35
  log.debug "Reduced record size from #{original_size} to #{final_size} bytes" if original_size != final_size
@@ -48,31 +38,31 @@ module Fluent
48
38
 
49
39
  private
50
40
 
51
- def reduce_size(obj, target_size, attempts = 0)
52
- json_str = obj.to_json
53
- return if json_str.bytesize <= target_size || attempts >= @max_attempts
54
-
55
- excess_size = json_str.bytesize - target_size
56
- perform_reduction(obj, excess_size, json_str.bytesize)
57
- reduce_size(obj, target_size, attempts + 1) # Recursive call with incremented attempts
41
+ def reduce_size(obj, target_size)
42
+ attempts = 0
43
+ while obj.to_json.bytesize > target_size && attempts < 100
44
+ excess_size = obj.to_json.bytesize - target_size
45
+ perform_reduction(obj, excess_size, obj.to_json.bytesize)
46
+ attempts += 1
47
+ end
58
48
  end
59
49
 
60
50
  def perform_reduction(obj, excess_size, total_size)
61
- case obj
62
- when Hash
63
- obj.each { |key, value| apply_reduction(value, excess_size, total_size) }
64
- when Array
51
+ if obj.is_a?(Hash)
52
+ obj.each do |key, value|
53
+ apply_reduction(value, excess_size, total_size)
54
+ end
55
+ elsif obj.is_a?(Array)
65
56
  obj.each { |element| apply_reduction(element, excess_size, total_size) }
66
- when String
57
+ elsif obj.is_a?(String)
67
58
  reduce_string(obj, excess_size, total_size)
68
59
  end
69
60
  end
70
61
 
71
62
  def apply_reduction(element, excess_size, total_size)
72
- case element
73
- when String
63
+ if element.is_a?(String)
74
64
  reduce_string(element, excess_size, total_size)
75
- when Hash, Array
65
+ elsif element.is_a?(Hash) || element.is_a?(Array)
76
66
  reduce_size(element, element.to_json.bytesize - (excess_size * (element.to_json.bytesize.to_f / total_size)))
77
67
  end
78
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-fluent-plugin-json-size-limit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.29
4
+ version: 0.1.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mykola Panin