redis_stream_logger 0.1.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/redis_stream_logger.rb +7 -0
- data/lib/redis_stream_logger/config.rb +31 -0
- data/lib/redis_stream_logger/log_device.rb +136 -0
- data/lib/redis_stream_logger/version.rb +3 -0
- data/redis_stream_logger.gemspec +28 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ea9895c1c5c8f4b5694ef368b53ff70d1472017f859a24bfe7680067eb5b2964
|
4
|
+
data.tar.gz: 67fc064a242db94edd55baf60df633b852a1cc7ba7377626eb5cafc45c515830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9af9e64b0008d920b21d2fa7f34a1fc85179bf959010f4bc01dc2fab221a19fbbebfc5af9827eefcaa61fe52f5328a7b2a79f61a2f91bf5c15450de0c877775f
|
7
|
+
data.tar.gz: 636e1dc374ca3877513da738bbde9153ce3f38f7b35ff0fda9c87c72e8f7523b51b152e9a8318d2ac3f0715b25e90abaf7662d581798722e506e403382a33554
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
redis_stream_logger (0.1.0)
|
5
|
+
redis (~> 4.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
rake (12.3.3)
|
12
|
+
redis (4.2.5)
|
13
|
+
rspec (3.8.0)
|
14
|
+
rspec-core (~> 3.8.0)
|
15
|
+
rspec-expectations (~> 3.8.0)
|
16
|
+
rspec-mocks (~> 3.8.0)
|
17
|
+
rspec-core (3.8.2)
|
18
|
+
rspec-support (~> 3.8.0)
|
19
|
+
rspec-expectations (3.8.6)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.8.0)
|
22
|
+
rspec-mocks (3.8.2)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.8.0)
|
25
|
+
rspec-support (3.8.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
rake (~> 12.0)
|
32
|
+
redis_stream_logger!
|
33
|
+
rspec (~> 3.0)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Mike Harris
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# RedisStreamLogger
|
2
|
+
|
3
|
+
This gem creates a [log device](https://github.com/ruby/logger/blob/bf6d5aa37ee954afc49a407e67fb96064a52af62/lib/logger/log_device.rb) to send your logs to a Redis stream.
|
4
|
+
|
5
|
+
The log device will buffer requests internally until either a time interval or buffer size is hit and then it will send all the log entries as a pipeline to minimize network
|
6
|
+
traffic.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'redis_stream_logger'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
To use in a Rails application add something like this to your config:
|
19
|
+
|
20
|
+
```rb
|
21
|
+
redis_log = RedisStreamLogger::LogDevice.new do |config|
|
22
|
+
config.connection = Redis.new
|
23
|
+
end
|
24
|
+
config.logger = Logger.new(redis_log)
|
25
|
+
```
|
26
|
+
|
27
|
+
It is _highly_ recommended that you set timeouts on your Redis connection. See the [Redis docs](https://github.com/redis/redis-rb/#timeouts).
|
28
|
+
|
29
|
+
### Configuration
|
30
|
+
|
31
|
+
* buffer_size: Max number of items to hold in queue before attempting to write a batch. Defaults to 100
|
32
|
+
* send_interval: Number of seconds to wait before sending the buffer, despite buffer_size. Defaults to 10
|
33
|
+
* connection: Redis connection to use for logger
|
34
|
+
* batch_size: Number of of log items to send to Redis at a time. Defaults to buffer_size
|
35
|
+
* stream_name: Stream to publish messages to
|
36
|
+
* max_len: Maximum size of stream. Ideally you will have a log consumer set up that calls xtrim after persisting your logs somewhere.
|
37
|
+
If that's more than you need, and just want a simple way to cap the log size set max_len to some sufficiently large number to keep your logs around long enough to be useful.
|
38
|
+
* log_set_key: Loggers will store the name of their stream into this key for discoverability
|
39
|
+
|
40
|
+
## Path to 1.0
|
41
|
+
|
42
|
+
1. Custom formatter: Right now it just takes the string from the logger and writes it to Redis. Ideally, the timestamp, log level, and tags would be written as separate keys.
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake spec` to run the tests.
|
47
|
+
|
48
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/mlh758/redis_stream_logger).
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redis_stream_logger"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module RedisStreamLogger
|
2
|
+
#
|
3
|
+
# Config provides configuration options for the log device.
|
4
|
+
# buffer_size: Max number of items to hold in queue before attempting to write a batch
|
5
|
+
# defaults to 100
|
6
|
+
# send_interval: Number of seconds to wait before sending the buffer, despite buffer_size
|
7
|
+
# defaults to 10
|
8
|
+
# connection: Redis connection to use for logger
|
9
|
+
# batch_size: Number of of log items to send to Redis at a time
|
10
|
+
# defaults to buffer_size
|
11
|
+
# stream_name: Stream to publish messages to
|
12
|
+
# max_len: Maximum size of stream. Ideally you will have a log
|
13
|
+
# consumer set up that calls xtrim after persisting your logs somewhere.
|
14
|
+
# If that's more than you need, and just want a simple way to cap the log size
|
15
|
+
# set max_len to some sufficiently large number to keep your logs around long
|
16
|
+
# enough to be useful.
|
17
|
+
#
|
18
|
+
class Config
|
19
|
+
attr_accessor :buffer_size, :send_interval, :connection, :batch_size, :stream_name, :max_len, :log_set_key
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@buffer_size = 100
|
23
|
+
@send_interval = 10
|
24
|
+
@connection = nil
|
25
|
+
@batch_size = @buffer_size
|
26
|
+
@stream_name = nil
|
27
|
+
@max_len = nil
|
28
|
+
@log_set_key = "log-streams"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require "redis_stream_logger/config"
|
3
|
+
module RedisStreamLogger
|
4
|
+
class LogDevice
|
5
|
+
attr_reader :config
|
6
|
+
#
|
7
|
+
# Creates a new LogDevice that can be used as a sink for Ruby Logger
|
8
|
+
#
|
9
|
+
# @param [Redis] conn connection to Redis
|
10
|
+
# @param [String] stream name of key to write to
|
11
|
+
#
|
12
|
+
def initialize(conn = nil, stream: 'rails-log')
|
13
|
+
@config = Config.new
|
14
|
+
@closed = false
|
15
|
+
yield @config if block_given?
|
16
|
+
@config.connection ||= conn
|
17
|
+
@config.stream_name ||= stream
|
18
|
+
raise ArgumentError, 'must provide connection' if @config.connection.nil?
|
19
|
+
|
20
|
+
@q = Queue.new
|
21
|
+
@error_logger = ::Logger.new(STDERR)
|
22
|
+
@ticker = Thread.new do
|
23
|
+
ticker(@config.send_interval)
|
24
|
+
end
|
25
|
+
@writer = Thread.new do
|
26
|
+
writer(@config.buffer_size, @config.send_interval)
|
27
|
+
end
|
28
|
+
at_exit { close }
|
29
|
+
end
|
30
|
+
|
31
|
+
def write(msg)
|
32
|
+
@q.push msg
|
33
|
+
end
|
34
|
+
|
35
|
+
def reopen(log = nil)
|
36
|
+
# no op
|
37
|
+
end
|
38
|
+
|
39
|
+
def close
|
40
|
+
return if @closed
|
41
|
+
@q.push :exit
|
42
|
+
@ticker.exit
|
43
|
+
@writer.join
|
44
|
+
@config.connection.close
|
45
|
+
@closed = true
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def send_options
|
51
|
+
return {} if @config.max_len.nil?
|
52
|
+
|
53
|
+
{ maxlen: @config.max_len, approximate: true }
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Writes a batch of log lines to the Redis stream
|
58
|
+
#
|
59
|
+
# @param [Array<String>] messages to write to the stream
|
60
|
+
#
|
61
|
+
#
|
62
|
+
def write_batch(messages)
|
63
|
+
redis = @config.connection
|
64
|
+
opt = send_options
|
65
|
+
messages.each_slice(@config.batch_size) do
|
66
|
+
attempt = 0
|
67
|
+
begin
|
68
|
+
redis.pipelined do
|
69
|
+
messages.each do |msg|
|
70
|
+
redis.xadd(@config.stream_name, {m: msg}, **opt)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
rescue StandardError => exception
|
74
|
+
attempt += 1
|
75
|
+
retry if attempt <= 3
|
76
|
+
@error_logger.warn "unable to write redis logs: #{exception}"
|
77
|
+
messages.each { |m| @error_logger.info(m) }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Pushes a message into the queue at the given interval
|
84
|
+
# to wake the writer thread up to ensure it sends partial
|
85
|
+
# buffers if no new logs come in.
|
86
|
+
#
|
87
|
+
# @param [Integer] interval to wake the writer up on
|
88
|
+
#
|
89
|
+
def ticker(interval)
|
90
|
+
loop do
|
91
|
+
sleep(interval)
|
92
|
+
@q.push(:nudge)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def control_msg?(msg)
|
97
|
+
msg == :nudge || msg == :exit
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# Stores the name of the logger in the configured set so other tools can
|
102
|
+
# locate the list of available log streams
|
103
|
+
#
|
104
|
+
#
|
105
|
+
def store_logger_name
|
106
|
+
@config.connection.sadd(@config.log_set_key, @config.stream_name)
|
107
|
+
rescue StandardError => exception
|
108
|
+
@error_logger.warn "unable to store name of log: #{exception}"
|
109
|
+
end
|
110
|
+
|
111
|
+
#
|
112
|
+
# Used in a thread to pull log messages from a queue and store them in batches into a redis
|
113
|
+
# stream.
|
114
|
+
#
|
115
|
+
# @param [Integer] buffer_max maximum number of log entries to buffer before sending
|
116
|
+
# @param [Integer] interval maximum amount of time to wait before sending a partial buffer
|
117
|
+
#
|
118
|
+
#
|
119
|
+
def writer(buffer_max, interval)
|
120
|
+
last_sent = Time.now
|
121
|
+
buffered = []
|
122
|
+
store_logger_name
|
123
|
+
loop do
|
124
|
+
msg = @q.pop
|
125
|
+
buffered.push(msg) unless control_msg?(msg)
|
126
|
+
now = Time.new
|
127
|
+
if buffered.count >= buffer_max || (now - last_sent) > interval || msg == :exit
|
128
|
+
write_batch(buffered)
|
129
|
+
return if msg == :exit
|
130
|
+
last_sent = Time.now
|
131
|
+
buffered = []
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/redis_stream_logger/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "redis_stream_logger"
|
5
|
+
spec.version = RedisStreamLogger::VERSION
|
6
|
+
spec.authors = ["Mike Harris"]
|
7
|
+
spec.email = ["mike.harris@cerner.com"]
|
8
|
+
|
9
|
+
spec.summary = "Provides a log device to send logs to a Redis stream"
|
10
|
+
spec.homepage = "https://github.com/mlh758/redis_stream_logger"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
16
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_dependency 'redis', '~> 4.0'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_stream_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- mike.harris@cerner.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".travis.yml"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- bin/console
|
43
|
+
- bin/setup
|
44
|
+
- lib/redis_stream_logger.rb
|
45
|
+
- lib/redis_stream_logger/config.rb
|
46
|
+
- lib/redis_stream_logger/log_device.rb
|
47
|
+
- lib/redis_stream_logger/version.rb
|
48
|
+
- redis_stream_logger.gemspec
|
49
|
+
homepage: https://github.com/mlh758/redis_stream_logger
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
homepage_uri: https://github.com/mlh758/redis_stream_logger
|
54
|
+
source_code_uri: https://github.com/mlh758/redis_stream_logger
|
55
|
+
changelog_uri: https://github.com/mlh758/redis_stream_logger
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.3.0
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.1.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Provides a log device to send logs to a Redis stream
|
75
|
+
test_files: []
|