Repository navigation
fix(logpipe): prevent pgaudit records being silently dropped by zap sampler - #11109
Closed
akashbhujbalwebsite wants to merge 6 commits into
Closed
akashbhujbalwebsite wants to merge 6 commits into
akashbhujbalwebsite wants to merge 6 commits into
Conversation
Contributor
|
❗ By default, the pull request is configured to backport to all release branches.
|
akashbhujbalwebsite
force-pushed
the
fix/pgaudit-audit-log-sampling-drop
branch
from
July 1, 2026 09:38
8127307 to
38a0d0d
Compare
mnencia
force-pushed
the
fix/pgaudit-audit-log-sampling-drop
branch
from
July 5, 2026 15:41
e6fe659 to
20644e7
Compare
Member
|
/test |
Contributor
|
@mnencia, here's the link to the E2E on CNPG workflow run: https://github.com/cloudnative-pg/cloudnative-pg/actions/runs/28751345123 |
mnencia
force-pushed
the
fix/pgaudit-audit-log-sampling-drop
branch
from
July 6, 2026 07:46
418f5ab to
4f77c5b
Compare
…ampler
Every PostgreSQL audit record emitted by LogRecordWriter uses an identical
log message key ("record"). The zap sampler installed by controller-runtime in
production mode (100 initial pass-through, then 1-in-100 per second) treats
these as duplicates and silently drops all records beyond the first 100 within
any given second.
For a cluster with pgaudit.log=all a short burst of SQL activity can easily
exceed this threshold, causing security-relevant events such as DDL errors or
privilege escalations to be lost from the pod's log stream with no indication
of the gap.
Fix by constructing a dedicated unsampled logger for LogRecordWriter via
zap.NewProductionConfig() with cfg.Sampling = nil. The logger is initialized
once with sync.Once and shares the same JSON encoding and RFC3339Nano
timestamps as the global instance-manager logger so log aggregators see a
consistent schema.
Add unit tests that (a) confirm the unsampled logger emits all records during
a burst of 200 identical messages, and (b) confirm that a sampler-enabled
logger WOULD drop records under the same conditions, proving the fix is
necessary.
Fixes cloudnative-pg#10815
Signed-off-by: Akash Bhujbal <[email protected]>
go-logr/zapr is now a direct import but was still marked indirect, and gci flagged the import grouping in both files. Signed-off-by: Marco Nenciarini <[email protected]>
buildUnsampledLogger hardcoded zap.NewProductionConfig(), so audit records always logged at Info regardless of --log-level and always used the stock level/timestamp field names regardless of --log-field-level/--log-field-timestamp, diverging from the rest of the pod's log stream. It now reads the effective level off the global instance-manager logger (via zapr.Underlier) and reuses machinery's GetFieldsRemapFlags() for the field remapping, so audit records only differ from other log lines by never being sampler-dropped. --log-destination isn't handled: it's never passed to the instance run process, only to the wal-archive/wal-restore subcommands. Building the core directly instead of going through zap.Config.Build() also drops the silent fallback to the sampled global logger on build error (there's no error path left) and stops adding caller info that the rest of the pod's logs don't have. Extended writer_test.go to call buildUnsampledLogger directly instead of only exercising zap's sampler in isolation, so a regression in the level-lookup or the no-sampling behavior is actually caught. Signed-off-by: Marco Nenciarini <[email protected]>
applyFieldRemap read log.GetFieldsRemapFlags() internally, so its only real logic (the two TrimPrefix branches) never ran in a go test binary, since no --log-field-* flags are ever parsed there. Take the flags as a parameter instead so a test can drive them directly. Also assert on globalLoggerLevel() itself in the existing "honors the effective level" spec, and tighten buildUnsampledLogger's docstring: it mirrors the global logger's encoding for Info-level output only, it doesn't replicate the custom Warning/Debug/Trace level-name encoding machinery/pkg/log installs (LogRecordWriter never logs above Info, so this was already dead code, just previously overclaimed). Signed-off-by: Marco Nenciarini <[email protected]>
getRecordLogger's sync.Once-backed package-level singleton was lazy
self-initializing global state: whichever goroutine happened to log
the first audit record built and cached the logger for the rest of
the process. LogPipe.Start is only ever called once per process (it's
a controller-runtime Runnable), so there's already a well-defined
single point to build it eagerly instead.
LogRecordWriter now takes its logger via NewLogRecordWriter instead of
building one lazily on first use. LogPipe builds it once in Start, as
a local variable, before the collection goroutine is spawned, and
threads it through every retry of collectLogsFromFile (which
previously constructed a fresh &LogRecordWriter{} - and, before this
PR, a session with the sampler - on every reconnect). It's a plain
local captured by the closures rather than a struct field, so there's
no mutable shared state to reason about: the value is fixed before any
goroutine that reads it is started, and Go's happens-before guarantee
for goroutine starts covers every retry, not just the first.
Signed-off-by: Marco Nenciarini <[email protected]>
NewLogRecordWriter's and applyFieldRemap's doc comments each restated the same "why" already explained in full on buildUnsampledLogger, where the design decision actually lives. collectLogsFromFile's comment repeated the "reused across every retry" reasoning already given at the call site in Start. Trimmed all three to state contracts without repeating the justification. Write's comment still described the pre-refactor behavior of routing through the shared global logger; it now writes through the writer's own dedicated logger. buildUnsampledLogger's --log-destination note overstated the guarantee as enforced when it's only true by current deployment convention. Signed-off-by: Marco Nenciarini <[email protected]>
mnencia
force-pushed
the
fix/pgaudit-audit-log-sampling-drop
branch
from
July 6, 2026 09:08
4f77c5b to
f901866
Compare
Member
|
Closing this in favor of #11131, which fixes the same issue at the process level rather than inside the log pipe. Credit to you and to @willmurnane, whose diagnosis in #10815 identified the sampler mechanism from the start, it's cited directly in #11131's description and is what made that fix possible. Thanks for digging into this. |
Contributor
|
❗ By default, the pull request is configured to backport to all release branches.
|
mnencia
added a commit
that referenced
this pull request
Jul 28, 2026
Every process the manager binary runs inside a Cluster's pods now logs without sampling. The controller keeps the duplicate-message sampler that controller-runtime applies to every operator. The manager's logger passed only the first 100 messages sharing the same (level, message) pair each second, and roughly 1 in 100 afterwards. Every PostgreSQL record forwarded by the instance manager's log pipe is logged with the same message (`record`), so a burst of database activity, such as pgaudit output under load, was silently collapsed and most records were lost from the pod's log stream with no indication of the gap. The sampler is installed by the controller-runtime zap builder at construction time and cannot be stripped afterwards, so the fix lives in machinery: `ConfigureLogging` gained a `WithDisabledSampling()` option, and the manager now passes it for every subcommand except `controller`. The operator keeps sampling, since a reconciliation storm can repeat one message well beyond the threshold, which is the case the sampler exists for. Credit to @willmurnane for the initial diagnosis in #10815, and to @akashbhujbalwebsite for narrowing it down to the sampler mechanism in #11109. Closes #10815 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
cnpg-bot
pushed a commit
to cloudnative-pg/api
that referenced
this pull request
Jul 28, 2026
…pg/cloudnative-pg#11131) Every process the manager binary runs inside a Cluster's pods now logs without sampling. The controller keeps the duplicate-message sampler that controller-runtime applies to every operator. The manager's logger passed only the first 100 messages sharing the same (level, message) pair each second, and roughly 1 in 100 afterwards. Every PostgreSQL record forwarded by the instance manager's log pipe is logged with the same message (`record`), so a burst of database activity, such as pgaudit output under load, was silently collapsed and most records were lost from the pod's log stream with no indication of the gap. The sampler is installed by the controller-runtime zap builder at construction time and cannot be stripped afterwards, so the fix lives in machinery: `ConfigureLogging` gained a `WithDisabledSampling()` option, and the manager now passes it for every subcommand except `controller`. The operator keeps sampling, since a reconciliation storm can repeat one message well beyond the threshold, which is the case the sampler exists for. Credit to @willmurnane for the initial diagnosis in cloudnative-pg/cloudnative-pg#10815, and to @akashbhujbalwebsite for narrowing it down to the sampler mechanism in cloudnative-pg/cloudnative-pg#11109. Closes cloudnative-pg/cloudnative-pg#10815 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit cloudnative-pg/cloudnative-pg@bf07333)
cnpg-bot
pushed a commit
that referenced
this pull request
Jul 28, 2026
Every process the manager binary runs inside a Cluster's pods now logs without sampling. The controller keeps the duplicate-message sampler that controller-runtime applies to every operator. The manager's logger passed only the first 100 messages sharing the same (level, message) pair each second, and roughly 1 in 100 afterwards. Every PostgreSQL record forwarded by the instance manager's log pipe is logged with the same message (`record`), so a burst of database activity, such as pgaudit output under load, was silently collapsed and most records were lost from the pod's log stream with no indication of the gap. The sampler is installed by the controller-runtime zap builder at construction time and cannot be stripped afterwards, so the fix lives in machinery: `ConfigureLogging` gained a `WithDisabledSampling()` option, and the manager now passes it for every subcommand except `controller`. The operator keeps sampling, since a reconciliation storm can repeat one message well beyond the threshold, which is the case the sampler exists for. Credit to @willmurnane for the initial diagnosis in #10815, and to @akashbhujbalwebsite for narrowing it down to the sampler mechanism in #11109. Closes #10815 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit bf07333)
cnpg-bot
pushed a commit
that referenced
this pull request
Jul 28, 2026
Every process the manager binary runs inside a Cluster's pods now logs without sampling. The controller keeps the duplicate-message sampler that controller-runtime applies to every operator. The manager's logger passed only the first 100 messages sharing the same (level, message) pair each second, and roughly 1 in 100 afterwards. Every PostgreSQL record forwarded by the instance manager's log pipe is logged with the same message (`record`), so a burst of database activity, such as pgaudit output under load, was silently collapsed and most records were lost from the pod's log stream with no indication of the gap. The sampler is installed by the controller-runtime zap builder at construction time and cannot be stripped afterwards, so the fix lives in machinery: `ConfigureLogging` gained a `WithDisabledSampling()` option, and the manager now passes it for every subcommand except `controller`. The operator keeps sampling, since a reconciliation storm can repeat one message well beyond the threshold, which is the case the sampler exists for. Credit to @willmurnane for the initial diagnosis in #10815, and to @akashbhujbalwebsite for narrowing it down to the sampler mechanism in #11109. Closes #10815 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit bf07333)
cnpg-bot
pushed a commit
that referenced
this pull request
Jul 28, 2026
Every process the manager binary runs inside a Cluster's pods now logs without sampling. The controller keeps the duplicate-message sampler that controller-runtime applies to every operator. The manager's logger passed only the first 100 messages sharing the same (level, message) pair each second, and roughly 1 in 100 afterwards. Every PostgreSQL record forwarded by the instance manager's log pipe is logged with the same message (`record`), so a burst of database activity, such as pgaudit output under load, was silently collapsed and most records were lost from the pod's log stream with no indication of the gap. The sampler is installed by the controller-runtime zap builder at construction time and cannot be stripped afterwards, so the fix lives in machinery: `ConfigureLogging` gained a `WithDisabledSampling()` option, and the manager now passes it for every subcommand except `controller`. The operator keeps sampling, since a reconciliation storm can repeat one message well beyond the threshold, which is the case the sampler exists for. Credit to @willmurnane for the initial diagnosis in #10815, and to @akashbhujbalwebsite for narrowing it down to the sampler mechanism in #11109. Closes #10815 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]> (cherry picked from commit bf07333)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a silent audit-log loss in
LogRecordWriter. Every PostgreSQL audit record it emits shares the same log message key ("record"), so the zap sampler controller-runtime installs in production mode (passing the first 100 identical messages per second, then roughly 1 in 100 after that) treats a burst of audit activity as duplicates and drops most of it, including security-relevant events such as DDL errors or privilege escalations, with no indication of the gap.LogRecordWriternow writes through a dedicated logger with sampling disabled, built once when the log pipe starts and reused across every reconnect. That logger otherwise mirrors the global instance-manager logger for Info-level output: the same JSON/RFC3339Nano encoding, the same effective--log-level, and the same--log-field-level/--log-field-timestampremapping, so audit records only differ from the rest of the pod's log lines in that they're never sampler-dropped.Fixes #10815
Root cause
controller-runtime v0.24.1 adds this sampler automatically when
Development=false:That's fine for operator reconcile-loop noise. It's not fine for audit records, where every entry represents a distinct real-world database event.
How to verify
pgaudit.log: allandshared_preload_libraries: pgauditkubectl logs <pod> -c postgres | grep marker_should_appearTests
writer_test.gocallsbuildUnsampledLoggerdirectly: one spec floods it with a burst and confirms every record is forwarded, another confirms it honors the effective level of the global instance-manager logger, and two more coverapplyFieldRemap's key-renaming logic. A separate spec demonstrates the underlying zap sampler dropping records in isolation (not through CNPG code) to document why the bug is real.Add to the release notes:
Fixed an issue where a burst of PostgreSQL audit activity (
pgaudit) could cause most audit records to be silently dropped from the pod's log stream due to log sampling.