fluent-plugin-fluent-plugin-json-size-limit 0.1.28 → 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: 1e24f82280ef078291ee109be79150cfc9789676643ed84b87e2c32e472b6afe
4
- data.tar.gz: d862ce1033e2a3e443ed32c9571502edc5373c1b3851936e108aeba01f47f49a
3
+ metadata.gz: 591d68308d420614aa3ea9c6c4379a77f489afa72b42adaa22356686f403ee9e
4
+ data.tar.gz: 600efc817220c04458478cd2d1b44025f17a01eb77f2fadf43a650f382437938
5
5
  SHA512:
6
- metadata.gz: 1f747939411789f6c8102fa5d7f7a47f6728b6e6337e61740efdb5ae69fc5bc2d210e0711f693f2afe06f66fe068446b205e9b0288765bb2b03b66ee5a9eb4d8
7
- data.tar.gz: 692f8c2b477084a14963f8cf25920f6265aafc90bcc942a693eb435bd929b37516f42555e2832d008a813338c6ae38864424e0d4b6f138706bac691059e119e3
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.28"
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,31 +48,31 @@ 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, obj.to_json.bytesize)
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
60
  def perform_reduction(obj, excess_size, total_size)
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)
61
+ case obj
62
+ when Hash
63
+ obj.each { |key, value| apply_reduction(value, excess_size, total_size) }
64
+ when Array
56
65
  obj.each { |element| apply_reduction(element, excess_size, total_size) }
57
- elsif obj.is_a?(String)
66
+ when String
58
67
  reduce_string(obj, excess_size, total_size)
59
68
  end
60
69
  end
61
70
 
62
71
  def apply_reduction(element, excess_size, total_size)
63
- if element.is_a?(String)
72
+ case element
73
+ when String
64
74
  reduce_string(element, excess_size, total_size)
65
- elsif element.is_a?(Hash) || element.is_a?(Array)
75
+ when Hash, Array
66
76
  reduce_size(element, element.to_json.bytesize - (excess_size * (element.to_json.bytesize.to_f / total_size)))
67
77
  end
68
78
  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.28
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