fluent-plugin-redis-enrichment 0.2.0 → 0.3.0

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: e523d3d718b67a60e86a6c37f7ba9a08d88ba6ee51570b03fcdb3600b24d9968
4
- data.tar.gz: ee9e08c30219913043fff587734019a4896f64aaf26883cc9bdafa34d78b3f71
3
+ metadata.gz: 227357c5db343e088b546cdea0fd1bb84216ae5882f6d3a2a63c8518f7bf9f1a
4
+ data.tar.gz: 758d5b6a499e4e0a6c2f14bee5c5758869fe53da4629a1f5543d02e17ebd1f7e
5
5
  SHA512:
6
- metadata.gz: 503448814a124ab6c3d912325887dfe87f9c0f1159ab1e34b821c968ab8305cdf9b32ec661a939083fc9b76f854143ac1899829741dbd61519a9879fde5c6f37
7
- data.tar.gz: c141425d3b669a4464a10df035956cee8da5957aef5c8bb7d4b5a5478339fe644881859e4bc822f502aff6a522ea75fd9dece70798cf4fee3243b221dd2cd34b
6
+ metadata.gz: 741f97f7c880925e4908623ea957a1ae3118bf6722f916a32cdb52d6f04270634e0e68304a016b54bf7cebf6d3362fbed46f61978208031213a14f8800e5d73a
7
+ data.tar.gz: 55c7c0b111e0ef094a6037527fabfe7b8c09356a962ed932529034421de83e2f77dca27c3dae200967d3c26bbb2999093f11e4906a0c5fdb072487a61746dae4
@@ -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.0'
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
206
+ end
183
207
  end
184
208
 
185
- private
209
+ class LazyCache < NoCache
210
+ def initialize(redis:, size: DEFAULT_CACHE_SIZE, ttl: DEFAULT_CACHE_TTL, log: nil)
211
+ super
212
+ end
213
+
214
+ def get(key)
215
+ cache.getset(key) { @redis.get(key) }
216
+ end
186
217
 
187
- def cache
188
- @cache ||= if @size == 0 || @ttl == 0
189
- NoCache.new
190
- else
191
- LruRedux::TTL::ThreadSafeCache.new(@size, @ttl)
192
- end
218
+ def cache
219
+ @cache ||= LruRedux::TTL::ThreadSafeCache.new(@size, @ttl)
220
+ end
193
221
  end
194
- end
195
222
 
196
- class NoCache
197
- def getset(_key)
198
- yield
223
+ class FullCache < NoCache
224
+ def initialize(*args, **kwargs)
225
+ super
226
+
227
+ initialize_cache
228
+ end
229
+
230
+ def initialize_cache
231
+ @cache = {}
232
+ @cache_mutex = Mutex.new
233
+
234
+ reload
235
+ @timer = timer(@ttl, &method(:reload))
236
+ end
237
+
238
+ def reload
239
+ log.warn :RELOAD if log
240
+ new_cache_content = @redis.get_all
241
+ @cache_mutex.synchronize { @cache.replace(new_cache_content) }
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
@@ -296,9 +375,11 @@ module Fluent
296
375
  def expand(__str_to_eval__, tag: nil, time: nil, record: nil, redis: nil, **_extra)
297
376
  instance_eval(__str_to_eval__)
298
377
  rescue NoMethodError => e
378
+ log.warn("while expanding #{__str_to_eval__}: #{e}")
299
379
  nil
300
380
  rescue StandardError => e
301
381
  log.warn("while expanding #{__str_to_eval__}: #{e}")
382
+ nil
302
383
  end
303
384
 
304
385
  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.0
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-24 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: []