Fix audit events logged as INFO+2 instead of AUDIT#5256
Conversation
Audit events were logging as "INFO+2" which breaks level detection in log aggregation systems (Loki, Elasticsearch, Splunk). This caused audit events to appear as unknown level and prevented level-based filtering in log pipelines. Changed NewAuditLogger to add a ReplaceAttr function that renders the custom audit level (slog.Level(2)) as "AUDIT" string in JSON output, while preserving the numeric level for internal filtering. Preserves the original design intent of placing audit between INFO and WARN, while making events filterable via level="AUDIT" in LogQL and other query languages. Fixes stacklok#4296 Signed-off-by: June Kim <kim.june.01@gmail.com> Signed-off-by: June Kim <kimjune01@gmail.com>
Internal pipeline metadata should not be committed to the upstream repo.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5256 +/- ##
==========================================
- Coverage 68.42% 68.41% -0.01%
==========================================
Files 620 620
Lines 63354 63358 +4
==========================================
Hits 43349 43349
- Misses 16768 16774 +6
+ Partials 3237 3235 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
amirejaz
left a comment
There was a problem hiding this comment.
Nice fix, and a clear PR description — the problem ("INFO+2" leaking into log aggregators) is well-motivated. The implementation is small and correct. I have two suggestions below: one about making the level comparison more explicit, and one about the second test case re-implementing rather than exercising NewAuditLogger.
Per @amirejaz review: 1. auditor.go: Replace implicit any-equality (a.Value.Any() == LevelAudit) with an explicit slog.Level type assertion. Defensive against future slog changes that might wrap level attrs differently. 2. auditor_test.go: 'preserves standard log levels' subtest no longer re-implements ReplaceAttr — it now uses NewAuditLogger directly so a regression in the production handler would be caught. Tests WARN (above LevelAudit threshold) since INFO is filtered out before ReplaceAttr runs.
|
Thanks for the review @amirejaz — both points addressed in 9bd90a0.
|
|
Linter is flagging two things in the fixup commit:
Otherwise looks good — happy to approve once those are fixed. |
- Rename unused groups param to _ in ReplaceAttr - Replace nil context with context.TODO() in test
|
Lint findings addressed in 081101c.
Tests pass. |
Summary
ReplaceAttrto the audit logger's JSON handler so the custom audit level renders as"AUDIT"instead of"INFO+2"Fixes #4296
Type of change
Test plan
task test)task test-e2e)task lint-fix)Added two tests: one verifying audit events render as
"AUDIT", another verifying standard levels (INFO, WARN) are preserved.go test ./pkg/audit/... -count=1: 28 tests passed.API Compatibility
v1beta1API, OR theapi-break-allowedlabel is applied and the migration guidance is described above.Does this introduce a user-facing change?
No. Audit log output changes from
"INFO+2"to"AUDIT"in JSON logs, but this is a bug fix restoring intended behavior.