fluent-plugin-redis-enrichment 0.2.0 → 0.3.1

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: e523d3d718b67a60e86a6c37f7ba9a08d88ba6ee51570b03fcdb3600b24d9968
4
- data.tar.gz: ee9e08c30219913043fff587734019a4896f64aaf26883cc9bdafa34d78b3f71
3
+ metadata.gz: f89e7ebce619fc1ec0da2742b6db4bcf895bfb502da6c8e6601c252c263175bc
4
+ data.tar.gz: ed3424ca5f392752f8616c02d2f8e27dd6761e8d92d123e47ae65c4a8272d993
5
5
  SHA512:
6
- metadata.gz: 503448814a124ab6c3d912325887dfe87f9c0f1159ab1e34b821c968ab8305cdf9b32ec661a939083fc9b76f854143ac1899829741dbd61519a9879fde5c6f37
7
- data.tar.gz: c141425d3b669a4464a10df035956cee8da5957aef5c8bb7d4b5a5478339fe644881859e4bc822f502aff6a522ea75fd9dece70798cf4fee3243b221dd2cd34b
6
+ metadata.gz: ef5a75731e2f4734c8f42010d622ca515ffdba282e3e4c2a73b3229916fc66fee6ac825f3a3141073fdb315f57f0d6f9e862e34412be980dea90f4abd0ecaafa
7
+ data.tar.gz: 47a308b9ac389ef089e93ee752c76a14c97575c67b836fab5c92dfb5d7d0bbabe455696d07c24be9818899abba2d6842b2398729eeef9092a8ae0c2b7fd7d6e6
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'fluent-plugin-redis-enrichment'
8
- spec.version = '0.2.0'
8
+ spec.version = '0.3.1'
9
9
  spec.authors = ['Thomas Tych']
10
10
  spec.email = ['thomas.tych@gmail.com']
11
11
 
@@ -44,6 +44,7 @@ module Fluent
44
44
  DEFAULT_SENTINEL_PORT = 26_379
45
45
  DEFAULT_CACHE_TTL = 30 * 60
46
46
  DEFAULT_CACHE_SIZE = 5000
47
+ DEFAULT_CACHE_TYPE = :lazy
47
48
 
48
49
  desc 'Redis host'
49
50
  config_param :redis_host, :string, default: DEFAULT_REDIS_HOST
@@ -66,6 +67,8 @@ module Fluent
66
67
  desc 'Sentinel redis role'
67
68
  config_param :redis_role, :enum, list: %i[master slave replica], default: DEFAULT_REDIS_ROLE
68
69
 
70
+ desc 'local Cache type'
71
+ config_param :cache_type, :enum, list: %i[lazy full no], default: DEFAULT_CACHE_TYPE
69
72
  desc 'local Cache size'
70
73
  config_param :cache_size, :integer, default: DEFAULT_CACHE_SIZE
71
74
  desc 'local Cache ttl'
@@ -98,12 +101,13 @@ module Fluent
98
101
  def start
99
102
  super
100
103
 
101
- @cache = Cache.new(**cache_options)
102
- @redis = RedisPool.new(**redis_options)
104
+ @redis = RedisPool.new(log: log, **redis_options)
105
+ @cache = Cache.new(redis: @redis, log: log, **cache_options)
103
106
  end
104
107
 
105
108
  def shutdown
106
109
  @redis.quit
110
+ @cache.clean
107
111
 
108
112
  super
109
113
  end
@@ -114,7 +118,7 @@ module Fluent
114
118
  time: time,
115
119
  record: new_record })
116
120
  log.debug("filter_redis_enrichment: on tag:#{tag}, search #{expanded_key}")
117
- redis = @cache.getset(expanded_key) { @redis.get(expanded_key) }
121
+ redis = @cache.get(expanded_key)
118
122
  new_record_record_enrichment = @placeholder_expander.expand(@record_enrichment,
119
123
  { tag: tag,
120
124
  time: time,
@@ -125,6 +129,7 @@ module Fluent
125
129
 
126
130
  def cache_options
127
131
  {
132
+ type: cache_type,
128
133
  size: cache_size,
129
134
  ttl: cache_ttl
130
135
  }
@@ -172,39 +177,102 @@ module Fluent
172
177
  value_str # emit as string
173
178
  end
174
179
 
175
- class Cache
176
- def initialize(size: DEFAULT_CACHE_SIZE, ttl: DEFAULT_CACHE_TTL)
177
- @size = size
178
- @ttl = ttl
180
+ module Cache
181
+ def self.new(redis:, type: DEFAULT_CACHE_TYPE, size: DEFAULT_CACHE_SIZE, ttl: DEFAULT_CACHE_TTL, log: nil)
182
+ klass = case type
183
+ when :lazy then LazyCache
184
+ when :full then FullCache
185
+ else
186
+ NoCache
187
+ end
188
+ klass.new(redis: redis, size: size, ttl: ttl, log: log)
179
189
  end
180
190
 
181
- def getset(key, &block)
182
- cache.getset(key, &block)
191
+ class NoCache
192
+ attr_reader :log
193
+
194
+ def initialize(redis:, size: nil, ttl: nil, log: nil)
195
+ @redis = redis
196
+ @size = size
197
+ @ttl = ttl
198
+ @log = log
199
+ end
200
+
201
+ def get(key)
202
+ @redis.get(key)
203
+ end
204
+
205
+ def clean; end
183
206
  end
184
207
 
185
- private
208
+ class LazyCache < NoCache
209
+ def initialize(redis:, size: DEFAULT_CACHE_SIZE, ttl: DEFAULT_CACHE_TTL, log: nil)
210
+ super
211
+ end
212
+
213
+ def get(key)
214
+ cache.getset(key) { @redis.get(key) }
215
+ end
186
216
 
187
- def cache
188
- @cache ||= if @size == 0 || @ttl == 0
189
- NoCache.new
190
- else
191
- LruRedux::TTL::ThreadSafeCache.new(@size, @ttl)
192
- end
217
+ def cache
218
+ @cache ||= LruRedux::TTL::ThreadSafeCache.new(@size, @ttl)
219
+ end
193
220
  end
194
- end
195
221
 
196
- class NoCache
197
- def getset(_key)
198
- yield
222
+ class FullCache < NoCache
223
+ def initialize(*args, **kwargs)
224
+ super
225
+
226
+ initialize_cache
227
+ end
228
+
229
+ def initialize_cache
230
+ @cache = {}
231
+ @cache_mutex = Mutex.new
232
+
233
+ reload
234
+ @timer = timer(@ttl, &method(:reload))
235
+ end
236
+
237
+ def reload
238
+ log.debug 'filter_redis_enrichment: full cache reload' if log
239
+ new_cache_content = @redis.get_all
240
+ @cache_mutex.synchronize { @cache.replace(new_cache_content) }
241
+ log.debug 'filter_redis_enrichment: full cache reloaded' if log
242
+ end
243
+
244
+ def get(key)
245
+ @cache_mutex.synchronize { @cache[key] }
246
+ end
247
+
248
+ def timer(interval, &block)
249
+ Thread.new do
250
+ loop do
251
+ begin
252
+ sleep interval
253
+ block.call
254
+ rescue StandardError => e
255
+ log.warn(e) if log
256
+ end
257
+ end
258
+ end
259
+ end
260
+
261
+ def clean
262
+ @timer.exit if @timer
263
+ end
199
264
  end
200
265
  end
201
266
 
202
267
  # proxy for Redis client
203
268
  # allow extract caching of cache
204
269
  class RedisPool
270
+ attr_reader :log
271
+
205
272
  def initialize(sentinels: DEFAULT_SENTINELS, name: DEFAULT_SENTINEL_MASTER, role: DEFAULT_REDIS_ROLE,
206
273
  host: DEFAULT_REDIS_HOST, port: DEFAULT_REDIS_PORT, db: DEFAULT_REDIS_DB,
207
- password: DEFAULT_REDIS_PASSWORD, timeout: DEFAULT_REDIS_TIMEOUT, pool_size: DEFAULT_REDIS_POOL)
274
+ password: DEFAULT_REDIS_PASSWORD, timeout: DEFAULT_REDIS_TIMEOUT, pool_size: DEFAULT_REDIS_POOL,
275
+ log: nil)
208
276
  @sentinels = sentinels
209
277
  @name = name
210
278
  @role = role
@@ -214,6 +282,7 @@ module Fluent
214
282
  @password = password
215
283
  @timeout = timeout
216
284
  @pool_size = pool_size
285
+ @log = log
217
286
  end
218
287
 
219
288
  def get(key)
@@ -225,11 +294,21 @@ module Fluent
225
294
  when 'string' then redis.get(key)
226
295
  when 'hash' then redis.hgetall(key)
227
296
  else
228
- log.warn("redis key '#{key}' has an unmanaged type '#{key_type}'")
297
+ log.warn("redis key '#{key}' has an unmanaged type '#{key_type}'") if log
229
298
  nil
230
299
  end
231
300
  end
232
301
 
302
+ def get_all
303
+ redis.scan_each.with_object({}) do |key, all|
304
+ case @redis.type(key)
305
+ when 'hash' then all[key] = @redis.hgetall(key)
306
+ when 'string' then all[key] = @redis.get(key)
307
+ when 'set' then all[key] = @redis.smembers(key)
308
+ end
309
+ end
310
+ end
311
+
233
312
  def quit
234
313
  redis.quit
235
314
  end
@@ -299,6 +378,7 @@ module Fluent
299
378
  nil
300
379
  rescue StandardError => e
301
380
  log.warn("while expanding #{__str_to_eval__}: #{e}")
381
+ nil
302
382
  end
303
383
 
304
384
  Object.instance_methods.each do |m|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-redis-enrichment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Tych
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-23 00:00:00.000000000 Z
11
+ date: 2023-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -202,7 +202,7 @@ dependencies:
202
202
  - - ">="
203
203
  - !ruby/object:Gem::Version
204
204
  version: 5.0.5
205
- description:
205
+ description:
206
206
  email:
207
207
  - thomas.tych@gmail.com
208
208
  executables: []
@@ -221,7 +221,7 @@ licenses:
221
221
  - Apache-2.0
222
222
  metadata:
223
223
  rubygems_mfa_required: 'true'
224
- post_install_message:
224
+ post_install_message:
225
225
  rdoc_options: []
226
226
  require_paths:
227
227
  - lib
@@ -236,8 +236,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
236
  - !ruby/object:Gem::Version
237
237
  version: '0'
238
238
  requirements: []
239
- rubygems_version: 3.3.7
240
- signing_key:
239
+ rubygems_version: 3.4.6
240
+ signing_key:
241
241
  specification_version: 4
242
242
  summary: fluentd plugin to do data enrichment with redis.
243
243
  test_files: []