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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc2d0b868e3e3879068c4c4adb44caf832379ad4d927f09e72f02223cc9efe9b
4
- data.tar.gz: cfa85d22a6b21b41ff7541f6040ca22c4ed5fbf20b13f818392928e1f5d5624a
3
+ metadata.gz: 591d68308d420614aa3ea9c6c4379a77f489afa72b42adaa22356686f403ee9e
4
+ data.tar.gz: 600efc817220c04458478cd2d1b44025f17a01eb77f2fadf43a650f382437938
5
5
  SHA512:
6
- metadata.gz: 18f99e8b8e7841be35f024b481676e7cb0da6b9f045d4ff67479c6cb6325a8ca74452f469f8d3631878c3bf2dbaa179af6dd4ab76392e912656cd44a70c9fc23
7
- data.tar.gz: b3eae5c689ed71874327ba01d30803321b19f5848f8977f8e2b57862650adf1504deb263181ad13232ee97dcc6fa1a5c396c5014cb30a0257ddd8865d9b759bc
6
+ metadata.gz: 2c865638c22f747a6f42c9f0a2a6469463d85567837c6381858fcc569f8cffd5a46e55b65142bba9d00f4f7fa629e3a1870821bcc3c27c0556c947e0b6f6c29e
7
+ data.tar.gz: b285917dba21b70b9b2937b89d91da7273b33bbd4f3f2855ee4a6eef541ff2f8657266ba81be3bc26e932702b7376b68f432170bd3936e605db4a6ad0d9c0a19
@@ -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.27"
6
+ spec.version = "0.1.29"
7
7
  spec.authors = ["Mykola Panin"]
8
8
  spec.email = ["mykola.panin@creatoriq.com"]
9
9
 
@@ -20,16 +20,26 @@ module Fluent
20
20
  class JsonSizeLimitFilter < Filter
21
21
  Fluent::Plugin.register_filter('jsonsizelimit', self)
22
22
 
23
+ # Configuration parameter for maximum JSON size
23
24
  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
24
27
 
25
28
  def configure(conf)
26
29
  super
27
- log.debug "Configuring JsonSizeLimitFilter with max_size: #{@max_size}"
30
+ log.debug "Configuring JsonSizeLimitFilter with max_size: #{@max_size} and max_attempts: #{@max_attempts}"
28
31
  end
29
32
 
30
33
  def filter(tag, time, record)
34
+ # Ensure that the record is a Hash before processing
35
+ return record unless record.is_a?(Hash)
36
+
31
37
  original_size = record.to_json.bytesize
32
- reduce_size(record, @max_size)
38
+ begin
39
+ reduce_size(record, @max_size)
40
+ rescue => e
41
+ log.error "Error during size reduction: #{e.message}"
42
+ end
33
43
  final_size = record.to_json.bytesize
34
44
 
35
45
  log.debug "Reduced record size from #{original_size} to #{final_size} bytes" if original_size != final_size
@@ -38,37 +48,37 @@ module Fluent
38
48
 
39
49
  private
40
50
 
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)
46
- attempts += 1
47
- end
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
48
58
  end
49
59
 
50
- def perform_reduction(obj, excess_size)
51
- if obj.is_a?(Hash)
52
- obj.each do |key, value|
53
- apply_reduction(value, excess_size)
54
- end
55
- elsif obj.is_a?(Array)
56
- obj.each { |element| apply_reduction(element, excess_size) }
57
- elsif obj.is_a?(String)
58
- reduce_string(obj, excess_size)
60
+ 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
65
+ obj.each { |element| apply_reduction(element, excess_size, total_size) }
66
+ when String
67
+ reduce_string(obj, excess_size, total_size)
59
68
  end
60
69
  end
61
70
 
62
- def apply_reduction(element, excess_size)
63
- if element.is_a?(String)
64
- reduce_string(element, excess_size)
65
- elsif element.is_a?(Hash) || element.is_a?(Array)
66
- reduce_size(element, element.to_json.bytesize - excess_size)
71
+ def apply_reduction(element, excess_size, total_size)
72
+ case element
73
+ when String
74
+ reduce_string(element, excess_size, total_size)
75
+ when Hash, Array
76
+ reduce_size(element, element.to_json.bytesize - (excess_size * (element.to_json.bytesize.to_f / total_size)))
67
77
  end
68
78
  end
69
79
 
70
- def reduce_string(str, excess_size)
71
- reduction_amount = (str.length.to_f / str.to_json.bytesize) * excess_size
80
+ def reduce_string(str, excess_size, total_size)
81
+ reduction_amount = (str.length.to_f / total_size) * excess_size
72
82
  new_length = [str.length - reduction_amount, 0].max
73
83
  str.replace(str[0...new_length])
74
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.27
4
+ version: 0.1.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mykola Panin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-18 00:00:00.000000000 Z
11
+ date: 2023-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler