Readers: following the journal and log files
Packages internal/ingest, ingest/journal and ingest/file.
The contract
A reader turns a log into batches:
Go
type Batch struct {
Events []detect.Event // recognized failures, possibly none
Cursor *store.Cursor // where reading has reached
Counters map[string]int64 // reader statistics
}It hands each batch to the engine and waits until it is saved. The engine saves the events and the cursor together.
Diagram source
flowchart LR
L[("log")] --> R["reader"] -->|batch| E["engine"] -->|one transaction| D[("events + cursor")]
D -. "after a restart, resume here" .-> RThis gives two properties:
- Counted once. A crash before saving leaves both unsaved; after saving, both saved.
- Nothing dropped under load. A slow database makes the reader wait; the log itself is the queue.
ingest.Deliver retries a failed save with delays doubling up to 30 seconds, and
shows the error in the reader's health.
Shared pieces in ingest.go:
| Piece | Purpose |
|---|---|
Tracker / Health |
state, counts, last error, last 5 notices |
ReadLine |
reads one line with a size limit; over-long lines are consumed and reported; an unfinished final line is not returned |
The journal reader
The command it runs
Plain text
journalctl --follow --no-tail --no-pager --quiet --output=json
--output-fields=MESSAGE,_SYSTEMD_UNIT,_EXE,_COMM,_UID
[--all] # for a Caddy unit, so long lines are not omitted
--cursor=<saved> OR --since=@<now - window>
<matches>It is started with an argument list (no shell), a fixed environment, and a check that the binary cannot be replaced by other users. On Linux it is killed if the daemon dies.
Matches: for SSH, the three _COMM names; for Caddy, _SYSTEMD_UNIT=<unit>.
Resuming
Diagram source
flowchart TD
A["saved cursor?"] -- no --> B["--since = now minus the window"]
A -- yes --> C["--cursor = saved"]
C --> D{"first entry returned equals the saved one?"}
D -- yes --> E["skip it: already counted"]
D -- no --> F["notice: position gone, events may be missed"]
C --> G{"journalctl rejects the cursor?"}
G -- yes --> H["forget it, notice, restart from the window"]journalctl silently moves to the nearest entry when a cursor no longer exists,
which is why the first entry is compared. This was confirmed against systemd 257.
Batching and limits
| Setting | Value |
|---|---|
| flush at | 200 events, or 2,000 records, or every 500 ms |
| longest entry | 128 KiB |
| restart delay | 1 s, doubling to 60 s |
Outcomes per entry: matched (an event), untrusted (looks like a failure,
fails the sender checks: journal.untrusted), ignored.
The file tailer
file.Tailer (tail.go) reads complete new lines from one path. file.Source
(source.go) wraps it in a loop.
Where it starts
| Situation | Start |
|---|---|
| first time, file exists | the end (history is not replayed); a half-written line is skipped |
| file appears later | the beginning |
| saved position, same file, content unchanged | the saved position |
| saved position, but rotated or rewritten while stopped | the beginning of the current file |
"Same file" means the same device and inode numbers. "Unchanged" means the first (up to) 64 bytes still hash to the saved value.
Rotation and truncation
Diagram source
stateDiagram-v2
[*] --> Following
Following --> Following: new lines
Following --> Draining: path now names a different, non-empty file
Draining --> Following: old file read to the end, switch to new
Following --> Restarting: size shrank or the start changed
Restarting --> Following: read from the beginning, noticeWhile a rotated file's replacement is still empty, the tailer keeps reading the old
one, because the web server may not have reopened its log yet. With copytruncate,
lines written between copy and truncate are lost.
Safety
Opened without following symlinks and without blocking on special files; only regular files. A log directory is often writable by the web server's user.
Identifying records
Each line's key is device:inode:offset:hash(line). Reading the same bytes again
gives the same key, so the engine counts it once.
Pace
Each poll reads up to 256 KiB. wake_linux.go watches the log's directory with
inotify (which survives rotation), waiting at most 2 seconds between looks and at
least 50 ms. Without inotify it simply waits.
A run of 20 unreadable lines adds a "format" notice.
Tests
| File | Proves |
|---|---|
ingest_test.go |
line limits, retrying the same batch, bounded notices |
journal/journal_test.go |
first start window, resume, vanished and rejected cursors, visible failures, untrusted and malformed entries, with a scripted stand-in for journalctl |
file/tail_linux_test.go |
start position, rotation, truncation, long lines, byte budget, restart cases, stable keys, symlink and FIFO refusal |
file/source_linux_test.go |
end-to-end following, resume without recounting, missing file and format mismatch |