logsafe 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +45 -2
- data/lib/logsafe/check_job.rb +13 -0
- data/lib/logsafe/storage.rb +34 -15
- data/lib/logsafe/version.rb +1 -1
- data/lib/logsafe.rb +29 -0
- data/shipper/start +3 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5918443c94ad04fce558fd63919b3961772b9511b49c817cd6949bbf09d9fe8f
|
|
4
|
+
data.tar.gz: 4307a20637bb8163dcd20ddb526c31679229144acfb3e46e3bb8883adb12d124
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b13fb3d04bb767652dfb64fb2ce1bb01a12d7e2df21d5988c35198c188b356cf8b89a53fe1709428ef70765d95f6b8d03c35f36eef5aabc8428c11d9b7371736
|
|
7
|
+
data.tar.gz: 23567a9766e202e85a9ef38fa5de7289256e509f2a1570141544191f1f714be440ba04c773a641da69591e50f00b72715981fcd4c2b5c80d87de6754f2f16e95
|
data/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,19 @@
|
|
|
3
3
|
The versions follow [Semantic Versioning](https://semver.org). Until 1.0, a minor version can change
|
|
4
4
|
what is in the README's Reference.
|
|
5
5
|
|
|
6
|
+
## [0.4.0] - 2026-11-25
|
|
7
|
+
|
|
8
|
+
- **When no lines arrive:** `Logsafe::CheckJob`, or `Logsafe.check`, fails with `Logsafe::Quiet` when the
|
|
9
|
+
storage has received no lines for 90 minutes, or for as long as you say. Vector drops what the storage
|
|
10
|
+
refuses and tells only its own log, so until now nobody knew before they missed the lines. A storage
|
|
11
|
+
of your own needs one more method for it, `times`.
|
|
12
|
+
- **Vector reads its files again when they change**, if you take the new `shipper/start`. A file that is not
|
|
13
|
+
valid changes nothing. Keys come from the environment, and for those the container has to be made
|
|
14
|
+
again, as before. Set `VECTOR_WATCH_CONFIG=false` in Vector's environment to turn it off.
|
|
15
|
+
|
|
16
|
+
**If Vector is already running,** copy `shipper/start` to the servers again and restart Vector once. Until
|
|
17
|
+
then it goes on as before. Nothing else has to change.
|
|
18
|
+
|
|
6
19
|
## [0.3.0] - 2026-11-25
|
|
7
20
|
|
|
8
21
|
- **In a bucket** the logs are now in the folder `logs/`: `logs/2026/11/<stream>.<date>.log.gz` and
|
data/README.md
CHANGED
|
@@ -198,7 +198,19 @@ sent. The first time it starts, Vector also ships what is already in the log fil
|
|
|
198
198
|
**Check `docker logs vector` after the first minute.** If the bucket's name or the key is wrong, Vector
|
|
199
199
|
does not wait: it drops what the storage refuses. Vector's start-up health check also lists the bucket,
|
|
200
200
|
which a write-only key may not do; if that is the only error, set `healthcheck: { enabled: false }`
|
|
201
|
-
under the sink.
|
|
201
|
+
under the sink. [The check](#5-the-check-in-the-same-app) tells you when it happens later.
|
|
202
|
+
|
|
203
|
+
**When you change a file in `/etc/vector`,** Vector reads it again by itself within seconds; a file that is
|
|
204
|
+
not valid changes nothing, and Vector's log says so. Two things need a new container, with
|
|
205
|
+
`docker rm -f vector` and the same `docker run`: a key, because what is in `/etc/vector/keys` is read only
|
|
206
|
+
when the container is made, and a new version of Vector. A change to `start` needs a
|
|
207
|
+
`docker restart vector`, and so does a file that was mounted by itself, as Kamal does, and was replaced and
|
|
208
|
+
not written over. To have Vector read its files only when it starts, put `VECTOR_WATCH_CONFIG=false` in
|
|
209
|
+
`/etc/vector/keys`.
|
|
210
|
+
|
|
211
|
+
With Kamal, `kamal setup` copies the files to a Vector that is running, and leaves its container alone. So
|
|
212
|
+
Vector has the new files before the app is deployed, and also when that deploy then fails. After a change of
|
|
213
|
+
a key, run `kamal accessory reboot vector`.
|
|
202
214
|
|
|
203
215
|
### 4. The merge job, in one Rails app
|
|
204
216
|
|
|
@@ -294,6 +306,33 @@ lists the bucket.
|
|
|
294
306
|
- **Watch that it succeeds**, the way you watch your other jobs. A merge that fails silently leaves you
|
|
295
307
|
with parts, not with nothing, but the parts keep piling up.
|
|
296
308
|
|
|
309
|
+
### 5. The check, in the same app
|
|
310
|
+
|
|
311
|
+
A merge with nothing to do succeeds, so it does not notice that no lines come. Have `Logsafe::CheckJob`
|
|
312
|
+
run **every hour** too, and watch it the same way: it fails with `Logsafe::Quiet` when the storage has
|
|
313
|
+
received no lines for 90 minutes, whatever the reason: Vector stopped, or the bucket's name, the key or
|
|
314
|
+
its policy is wrong.
|
|
315
|
+
|
|
316
|
+
```yaml
|
|
317
|
+
# Solid Queue, config/recurring.yml
|
|
318
|
+
production:
|
|
319
|
+
check_logs:
|
|
320
|
+
class: Logsafe::CheckJob
|
|
321
|
+
schedule: "45 * * * *"
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
- **A quiet night is no failure** as long as the container that runs the job ships its lines here: Active
|
|
325
|
+
Job logs some each time a job runs, at the level info. If it does not, give the job more time:
|
|
326
|
+
`Logsafe::CheckJob.perform_later(within: 24 * 60 * 60)`, in seconds.
|
|
327
|
+
- **Without arguments one stream is enough,** whichever it is. With the names of streams, each of them must
|
|
328
|
+
have had lines: `Logsafe::CheckJob.perform_later("shop-prod-web", "shop-prod-worker")`. Use that to notice
|
|
329
|
+
when one of several servers stops, with a time that is long enough for the quietest of them.
|
|
330
|
+
- **It looks at when the parts were written** and not into them. Lines that arrive and cannot be decrypted
|
|
331
|
+
are noticed by the merge.
|
|
332
|
+
- **It says so every hour** until lines arrive again. Vector's own log, `docker logs vector` on the
|
|
333
|
+
servers, has the reason.
|
|
334
|
+
- **The key of the app needs no more rights** than the merge has: it lists the parts.
|
|
335
|
+
|
|
297
336
|
## Reading the logs
|
|
298
337
|
|
|
299
338
|
```sh
|
|
@@ -537,6 +576,7 @@ Set them in `config/initializers/logsafe.rb`. None is read from the environment
|
|
|
537
576
|
| Job | The same, without a job | What it does |
|
|
538
577
|
| --- | --- | --- |
|
|
539
578
|
| `Logsafe::MergeJob.perform_later(*streams)` | `Logsafe.merge(*streams)` | Merges the finished days of these streams, or of all. Run it every hour. |
|
|
579
|
+
| `Logsafe::CheckJob.perform_later(*streams, within: 5400)` | `Logsafe.check(*streams, within: 5400)` | Raises `Logsafe::Quiet`, which is a `Logsafe::Error`, when no lines arrived in that many seconds: in all, or in one of these streams. Run it every hour. |
|
|
540
580
|
| `Logsafe::ReencryptJob.perform_later` | `Logsafe.reencrypt` | Encrypts with the key in use what is plain or under an old key. Run it after the key was set or changed. |
|
|
541
581
|
| | `Logsafe.generate_key` | A new key, as `logsafe key` prints it. |
|
|
542
582
|
| | `Logsafe.decrypt(from, into)` | Writes the gzip of a day's encrypted file to a path. |
|
|
@@ -556,7 +596,7 @@ The keys come from `LOGSAFE_KEY` and `LOGSAFE_OLD_KEYS`. It needs neither Rails
|
|
|
556
596
|
|
|
557
597
|
### Storage of your own
|
|
558
598
|
|
|
559
|
-
Any object with
|
|
599
|
+
Any object with the first five of these methods can be `Logsafe.storage`. What each has to promise is written in
|
|
560
600
|
[lib/logsafe/storage.rb](lib/logsafe/storage.rb), and `Logsafe::Storage::GCS` there is a short example.
|
|
561
601
|
|
|
562
602
|
| Method | What it does |
|
|
@@ -566,6 +606,7 @@ Any object with these five methods can be `Logsafe.storage`. What each has to pr
|
|
|
566
606
|
| `version(key)` | What tells the content the key has now from any other, or nil when there is none. |
|
|
567
607
|
| `store(path, key, was)` | Copies a local file to the key if it still has the version `was`, and otherwise raises `Logsafe::Storage::Changed.new(key)`. |
|
|
568
608
|
| `delete(keys)` | Removes them. One that is gone already is no failure. |
|
|
609
|
+
| `times(prefix)` | The keys that start with the prefix, each with the `Time` it was last written, as a Hash. Only the check needs it. |
|
|
569
610
|
|
|
570
611
|
Give it a `to_s` that says which storage it is, so that the page keeps two of them apart.
|
|
571
612
|
|
|
@@ -597,6 +638,8 @@ Give it a `to_s` that says which storage it is, so that the page keeps two of th
|
|
|
597
638
|
- The whole path once more with a key, for the S3 stand-in: the parts hold only encrypted lines, and
|
|
598
639
|
the merge job opens what Vector encrypted.
|
|
599
640
|
- `shipper/start` with keys that are not keys, with a key and with none.
|
|
641
|
+
- A file of Vector's that is changed while lines keep coming: it goes on with the new settings, and no line
|
|
642
|
+
is lost.
|
|
600
643
|
|
|
601
644
|
`bundle exec rake` runs the part that needs no Docker: order, duplicates, late parts, a run that
|
|
602
645
|
fails, today left alone, encryption with a right, a wrong and a missing key, a change of key with
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_job"
|
|
4
|
+
|
|
5
|
+
module Logsafe
|
|
6
|
+
# Fails when no lines arrive any more, so that whatever reports your failed jobs reports this too.
|
|
7
|
+
# Takes what Logsafe.check takes.
|
|
8
|
+
class CheckJob < ActiveJob::Base
|
|
9
|
+
def perform(*streams, **options)
|
|
10
|
+
Logsafe.check(*streams, **options)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/logsafe/storage.rb
CHANGED
|
@@ -10,7 +10,9 @@ module Logsafe
|
|
|
10
10
|
# store(path, key, was) copy a local file to the key, if the key still has the version it was, and
|
|
11
11
|
# otherwise raise Changed: what was read before that is not the latest
|
|
12
12
|
# delete(keys)
|
|
13
|
-
# and
|
|
13
|
+
# and, for Logsafe.check and nothing else, a sixth:
|
|
14
|
+
# times(prefix) { key => Time it was last written }, also for a part that is still being written
|
|
15
|
+
# It says in to_s which storage it is, when one machine reads from more than one. One that cannot
|
|
14
16
|
# tell versions apart may give the same for every content: two runs at the same time are then not
|
|
15
17
|
# kept from each other.
|
|
16
18
|
module Storage
|
|
@@ -91,18 +93,9 @@ module Logsafe
|
|
|
91
93
|
"folder:#{File.expand_path(@root)}"
|
|
92
94
|
end
|
|
93
95
|
|
|
94
|
-
def keys(prefix)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
key = prefix + name
|
|
98
|
-
key if key.valid_encoding? && settled?(key, File.lstat(File.join(folder, name)))
|
|
99
|
-
rescue Errno::ENOENT
|
|
100
|
-
next # removed since it was listed
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
rescue Errno::ENOENT
|
|
104
|
-
[]
|
|
105
|
-
end
|
|
96
|
+
def keys(prefix) = stats(prefix).select { |key, stat| settled?(key, stat) }.keys
|
|
97
|
+
|
|
98
|
+
def times(prefix) = stats(prefix).select { |_, stat| stat.file? }.transform_values(&:mtime)
|
|
106
99
|
|
|
107
100
|
def fetch(key, path)
|
|
108
101
|
within(File.dirname(key)) do |folder|
|
|
@@ -142,6 +135,20 @@ module Logsafe
|
|
|
142
135
|
|
|
143
136
|
private
|
|
144
137
|
|
|
138
|
+
# { key => stat of what it is, which can also be a folder or a link }
|
|
139
|
+
def stats(prefix)
|
|
140
|
+
within(prefix) do |folder|
|
|
141
|
+
Dir.glob("**/*", base: folder).filter_map do |name|
|
|
142
|
+
key = prefix + name
|
|
143
|
+
[key, File.lstat(File.join(folder, name))] if key.valid_encoding?
|
|
144
|
+
rescue Errno::ENOENT
|
|
145
|
+
next # removed since it was listed
|
|
146
|
+
end.to_h
|
|
147
|
+
end
|
|
148
|
+
rescue Errno::ENOENT
|
|
149
|
+
{}
|
|
150
|
+
end
|
|
151
|
+
|
|
145
152
|
# Opened, and not only looked at: a network share then asks its server.
|
|
146
153
|
def stamp(path)
|
|
147
154
|
File.open(path, OPEN) { |file| [file.stat.ino, file.stat.size, file.stat.mtime.to_r].join("-") }
|
|
@@ -239,6 +246,10 @@ module Logsafe
|
|
|
239
246
|
def full(key)
|
|
240
247
|
@prefix + key
|
|
241
248
|
end
|
|
249
|
+
|
|
250
|
+
def short(name)
|
|
251
|
+
name.delete_prefix(@prefix)
|
|
252
|
+
end
|
|
242
253
|
end
|
|
243
254
|
|
|
244
255
|
# Amazon S3 and anything that speaks its protocol: DigitalOcean Spaces, MinIO, Garage, ...
|
|
@@ -249,7 +260,11 @@ module Logsafe
|
|
|
249
260
|
end
|
|
250
261
|
|
|
251
262
|
def keys(prefix)
|
|
252
|
-
@bucket.objects(prefix: full(prefix)).map { |object| object.key
|
|
263
|
+
@bucket.objects(prefix: full(prefix)).map { |object| short(object.key) }
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def times(prefix)
|
|
267
|
+
@bucket.objects(prefix: full(prefix)).to_h { |object| [short(object.key), object.last_modified] }
|
|
253
268
|
end
|
|
254
269
|
|
|
255
270
|
def to_s
|
|
@@ -314,7 +329,11 @@ module Logsafe
|
|
|
314
329
|
end
|
|
315
330
|
|
|
316
331
|
def keys(prefix)
|
|
317
|
-
@bucket.files(prefix: full(prefix)).all.map { |file| file.name
|
|
332
|
+
@bucket.files(prefix: full(prefix)).all.map { |file| short(file.name) }
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def times(prefix)
|
|
336
|
+
@bucket.files(prefix: full(prefix)).all.to_h { |file| [short(file.name), file.updated_at.to_time] }
|
|
318
337
|
end
|
|
319
338
|
|
|
320
339
|
def to_s
|
data/lib/logsafe/version.rb
CHANGED
data/lib/logsafe.rb
CHANGED
|
@@ -12,6 +12,10 @@ require_relative "logsafe/version"
|
|
|
12
12
|
module Logsafe
|
|
13
13
|
class Error < StandardError; end
|
|
14
14
|
|
|
15
|
+
# No lines arrive any more.
|
|
16
|
+
class Quiet < Error; end
|
|
17
|
+
|
|
18
|
+
autoload :CheckJob, "logsafe/check_job"
|
|
15
19
|
autoload :MergeJob, "logsafe/merge_job"
|
|
16
20
|
autoload :Reencrypt, "logsafe/reencrypt"
|
|
17
21
|
autoload :ReencryptJob, "logsafe/reencrypt_job"
|
|
@@ -29,6 +33,7 @@ module Logsafe
|
|
|
29
33
|
PART = %r{\A#{PARTS}(#{STREAM})/(\d{4}-\d{2}-\d{2})/[^[:cntrl:]]+\z}
|
|
30
34
|
AGE = 366 # days. A part of a date before that is left alone: there are too many such dates to make up.
|
|
31
35
|
RUN = 50 * 60 # seconds after which a merge starts with no other day, so that hourly runs do not pile up
|
|
36
|
+
QUIET = 90 * 60 # seconds without a line after which something is wrong: an hourly job logs in between
|
|
32
37
|
|
|
33
38
|
class << self
|
|
34
39
|
# Where the logs are: a folder, { s3: "bucket", ... } or { gcs: "bucket", ... }. See the README.
|
|
@@ -110,6 +115,20 @@ module Logsafe
|
|
|
110
115
|
raise failures.first if failures.any?
|
|
111
116
|
end
|
|
112
117
|
|
|
118
|
+
# Raises Quiet when no lines have arrived within that many seconds. Vector drops what the storage
|
|
119
|
+
# refuses and says so in its own log only, so without this nobody knows until they miss the lines.
|
|
120
|
+
# Each of the streams given must have had some. Without any, one of them all is enough: the app that
|
|
121
|
+
# runs the job hourly logs at least that, while another stream can be quiet for a whole night.
|
|
122
|
+
def check(*streams, within: QUIET)
|
|
123
|
+
oldest = Time.now - within
|
|
124
|
+
arrived = arrivals.select { |_, time| time >= oldest }.keys
|
|
125
|
+
quiet = streams.map(&:to_s) - arrived
|
|
126
|
+
return if streams.empty? ? arrived.any? : quiet.empty?
|
|
127
|
+
|
|
128
|
+
raise Quiet, "No lines have arrived#{" in #{quiet.join(", ")}" if quiet.any?} for #{within.to_i / 60} minutes. " \
|
|
129
|
+
"Vector says why in its log on the servers: docker logs vector"
|
|
130
|
+
end
|
|
131
|
+
|
|
113
132
|
# The parts among these keys, as { [stream, date] => keys }.
|
|
114
133
|
def parts(keys)
|
|
115
134
|
keys.group_by { |key| key.match(PART)&.captures }.select do |(stream, day), _|
|
|
@@ -145,6 +164,16 @@ module Logsafe
|
|
|
145
164
|
parts
|
|
146
165
|
end
|
|
147
166
|
|
|
167
|
+
# { stream => when its newest part was written }
|
|
168
|
+
def arrivals
|
|
169
|
+
raise Error, "#{storage.class} has no method times, see the README" unless storage.respond_to?(:times)
|
|
170
|
+
|
|
171
|
+
times = storage.times(PARTS)
|
|
172
|
+
parts(times.keys).each_with_object({}) do |((stream, _), keys), newest|
|
|
173
|
+
newest[stream] = [newest[stream], *times.values_at(*keys)].compact.max
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
148
177
|
# What is left alone is not lost, but it must be possible to notice.
|
|
149
178
|
def report(keys, strays)
|
|
150
179
|
return if strays.zero?
|
data/shipper/start
CHANGED
|
@@ -15,4 +15,7 @@ if [ -n "${LOGSAFE_KEY:-}" ]; then
|
|
|
15
15
|
fi
|
|
16
16
|
fi
|
|
17
17
|
umask 027
|
|
18
|
+
# Vector then reads its files again when they change. A file that is not valid changes nothing, and what
|
|
19
|
+
# came from the environment, such as the keys, stays as it was until the container is made again.
|
|
20
|
+
export VECTOR_WATCH_CONFIG="${VECTOR_WATCH_CONFIG:-true}"
|
|
18
21
|
exec vector "$@"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: logsafe
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tin.trung.duong
|
|
@@ -41,6 +41,7 @@ files:
|
|
|
41
41
|
- README.md
|
|
42
42
|
- exe/logsafe
|
|
43
43
|
- lib/logsafe.rb
|
|
44
|
+
- lib/logsafe/check_job.rb
|
|
44
45
|
- lib/logsafe/cipher.rb
|
|
45
46
|
- lib/logsafe/copies.rb
|
|
46
47
|
- lib/logsafe/day.rb
|
|
@@ -68,8 +69,8 @@ licenses:
|
|
|
68
69
|
metadata:
|
|
69
70
|
source_code_uri: https://github.com/TTD93/LogSafe
|
|
70
71
|
bug_tracker_uri: https://github.com/TTD93/LogSafe/issues
|
|
71
|
-
documentation_uri: https://github.com/TTD93/LogSafe/blob/v0.
|
|
72
|
-
changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.
|
|
72
|
+
documentation_uri: https://github.com/TTD93/LogSafe/blob/v0.4.0/README.md
|
|
73
|
+
changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.4.0/CHANGELOG.md
|
|
73
74
|
rubygems_mfa_required: 'true'
|
|
74
75
|
rdoc_options: []
|
|
75
76
|
require_paths:
|