8000 🐛 Fix E2E by abahmed · Pull Request #530 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000 4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ Some quirks are load-bearing. Preserve them unless a change explicitly says othe
they may have caused.
- Decisions for out-of-scope incidents are dropped before delivery, but the
incident is still tracked so reasoning keeps its evidence.
- The audit log records every in-scope decision when it is made. A decision
the digest or the startup summary carries reaches the sink with
`Message.Carrier` set; delivery drops it, the replay keeps it apart from
delivered messages, and the audit entry says `delivery: digest`.
- The audit decision reason strings (`settled`, `material change`,
`flapping`, `healthy for ...`, `stable for ...`, `startup summary`,
`digest`) are stable strings people grep for.
Expand Down
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@
reported for about two minutes") instead of whichever pod was worst.
- **Skipped resource types are named.** The startup log line about kinds
left out by the watch budget now lists them.
- **Audit log records every decision.** Decisions the digest or the startup
summary carry are written to the audit log when they are made, with
`delivery: digest` or `delivery: startup summary`; announcements that go
only to paging tools carry `delivery: paging`.
- **Low-priority digest.** Digest-tier incidents (an autoscaler at its
maximum, a budget that selects nothing, a throttled container, learned
routines) are no longer announced one by one. One 🟡 digest every 30
Expand Down
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@ searchable history of everything it decided. `kwatch-scorecard` reads this log.
| `auditLog.enabled` | Write one structured JSON entry per incident decision (default: true) |
| `auditLog.output` | Destination: `stdout` (default) or a file path |

Every decision is recorded when it is made, including the ones people hear
through another message: `delivery` is `digest` or `startup summary` when the
decision was held for that message, `paging` when only paging tools and issue
trackers receive it, and absent for a message of its own.

File output is append-only. Configure rotation and retention in the container
runtime or log collector; kwatch does not rename or delete audit files.

Expand Down
5 changes: 5 additions & 0 deletions internal/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ type Entry struct {
ContentHash string `json:"contentHash,omitempty"`
// Previous is the resolved incident this one repeats, when linked.
Previous string `json:"previous,omitempty"`
// Delivery says how the decision reaches people when not as a message
// of its own: "digest" or "startup summary" when that message carries
// it, "paging" when only alert-tracking providers receive it. Empty
// for an ordinary message.
Delivery string `json:"delivery,omitempty"`
}

// Config selects where entries go.
Expand Down
25 changes: 25 additions & 0 deletions internal/delivery/carried_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package delivery

import (
"testing"

"github.com/stretchr/testify/assert"
)

// A message that names its carrier exists for the audit log; the digest
// or the startup summary tells people about it, so delivery drops it.
func TestManagerDropsCarriedMessages(t *testing.T) {
clk := newFakeClock()
provider := newMessageProvider(nil)
manager := fakeClockManager(t, clk, provider, nil)
startManager(t, manager)

carried := revision("held", 1, "🟡 held is at its maximum")
carried.Carrier = "digest"
manager.NotifyIncident(carried)
manager.NotifyIncident(revision("a", 1, "🔴 a is down"))

assert.Equal(t, "a", provider.receive(t).Key,
"only the message delivered on its own reaches the provider")
assert.Empty(t, provider.sent)
}
4 changes: 4 additions & 0 deletions internal/delivery/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
// NotifyIncident queues one incident message for every provider. It never
// performs provider I/O on the caller's goroutine.
func (m *Manager) NotifyIncident(msg notification.Message) {
if msg.Carrier != "" {
// Recorded for the audit log; the digest or summary carries it.
return
}
klog.V(2).InfoS("queue incident", "component", "delivery",
"conversation", msg.Key, "revision", msg.Revision)
incident := msg
Expand Down
5 changes: 5 additions & 0 deletions internal/notification/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Message struct {
// failures another incident took over. Chat channels read about both
// elsewhere; an alert opened by key must still be closed by key.
PagingOnly bool `json:",omitempty"`
// Carrier names the message that carries this one to people, when it
// is not delivered on its own: "digest" or "startup summary". Such a
// message exists for the audit log, which records every decision when
// it is made; delivery drops it.
Carrier string `json:",omitempty"`
}

// IsOpening reports whether the message announces its conversation.
Expand Down
4 changes: 4 additions & 0 deletions internal/pipeline/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func AuditEntry(
DecisionReason: d.Reason,
ContentHash: p.Digest,
Previous: p.Previous,
Delivery: m.Carrier,
}
if m.PagingOnly {
entry.Delivery = "paging"
}
if p.Cause != nil {
entry.RootCause = p.Cause.Summary
Expand Down
17 changes: 17 additions & 0 deletions internal/pipeline/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ func TestAuditEntryRecordsDecision(t *testing.T) {
}, entry)
}

// The entry says how a decision reaches people when not as a message of
// its own.
func TestAuditEntryRecordsDelivery(t *testing.T) {
pod := inventory.EntityID{Kind: "pod", Namespace: "shop", Name: "api-1"}
p := auditIncident(nil, pod)
d := incident.Decision{Action: incident.Announce, Incident: p}
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)

own := AuditEntry(d, notification.Message{}, at)
carried := AuditEntry(d, notification.Message{Carrier: "digest"}, at)
paging := AuditEntry(d, notification.Message{PagingOnly: true}, at)

assert.Empty(t, own.Delivery)
assert.Equal(t, "digest", carried.Delivery)
assert.Equal(t, "paging", paging.Delivery)
}

func TestAuditCauseState(t *testing.T) {
pod := inventory.EntityID{Kind: "pod", Namespace: "shop", Name: "api-1"}
self := &rootcause.CauseRecord{Root: pod}
Expand Down
17 changes: 16 additions & 1 deletion internal/pipeline/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,28 @@ func (a *announcer) collectDigest(
) ([]incident.Decision, bool) {
rest := make([]incident.Decision, 0, len(decisions))
for _, d := range decisions {
if out, held := a.holdDigest(now, d); !held {
out, held := a.holdDigest(now, d)
if !held {
rest = append(rest, out)
continue
}
a.recordCarried(ctx, now, d, "digest")
}
return rest, a.flushDigest(ctx, now)
}

// recordCarried hands a held decision to the sink for the audit log only:
// the message names its carrier and delivery drops it. The audit log then
// records every decision when it is made, whether people hear it on its
// own or through the digest or the startup summary.
func (a *announcer) recordCarried(
ctx context.Context, now time.Time, d incident.Decision, carrier string,
) {
msg := a.write(d, now)
msg.Carrier = carrier
a.sink(ctx, d, msg)
}

// holdDigest reports whether d was absorbed by the digest. A decision
// that passes through may come back changed: the first message of an
// incident promoted out of the digest is written as an announcement.
Expand Down
26 changes: 25 additions & 1 deletion internal/pipeline/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func digestHarness(
t.Helper()
var sent []notification.Message
sink := func(_ context.Context, _ incident.Decision, m notification.Message) {
sent = append(sent, m)
if m.Carrier == "" {
sent = append(sent, m)
}
}
return newTestEngine(t, &fakeClock{now: now}, sink, nil), &sent
}
Expand Down Expand Up @@ -122,3 +124,25 @@ func TestEngineDigestReleasesPromotedIncident(t *testing.T) {
t.Fatal("the promoted incident must leave the digest")
}
}

// A held digest-tier decision still reaches the sink, marked as carried
// by the digest, so the audit log records it when it is made.
func TestEngineDigestRecordsHeldDecisionsForTheAuditLog(t *testing.T) {
now := time.Date(2026, 9, 29, 10, 0, 0, 0, time.UTC)
var carried []notification.Message
sink := func(_ context.Context, _ incident.Decision, m notification.Message) {
if m.Carrier != "" {
carried = append(carried, m)
}
}
e := newTestEngine(t, &fakeClock{now: now}, sink, nil)

e.announcer.collectDigest(context.Background(), now,
[]incident.Decision{digestAnnounce("a"), announce("urgent")})

if len(carried) != 1 || carried[0].Key != announce("a").Incident.ID ||
carried[0].Carrier != "digest" {
t.Fatalf("want the held announcement marked carried, got %+v",
carried)
}
}
5 changes: 4 additions & 1 deletion internal/pipeline/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ type sinkLog struct {
}

func (s *sinkLog) sink(
_ context.Context, d incident.Decision, _ notification.Message,
_ context.Context, d incident.Decision, m notification.Message,
) {
if m.Carrier != "" {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.decisions = append(s.decisions, d)
Expand Down
3 changes: 3 additions & 0 deletions internal/pipeline/scenario_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func newHarnessWith(
Incidents: incident.NewManager(
incident.Config{IDNonce: harnessNonce}, explain.NewSolver()),
Sink: func(_ context.Context, d incident.Decision, m notification.Message) {
if m.Carrier != "" {
return
}
h.decisions = append(h.decisions, d)
h.messages = append(h.messages, m)
},
Expand Down
9 changes: 8 additions & 1 deletion internal/pipeline/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (a *announcer) collectStartup(
return rest, true
}

// holdStartup reports whether d was absorbed by the startup summary.
// holdStartup reports whether d was absorbed by the startup summary. A
// held decision still reaches the sink for the audit log: as a paging-only
// message when it is a page-tier announcement, otherwise marked as carried
// by the summary.
func (a *announcer) holdStartup(
ctx context.Context, now time.Time, d incident.Decision,
) bool {
Expand All @@ -106,15 +109,19 @@ func (a *announcer) holdStartup(
msg := a.write(d, now)
msg.PagingOnly = true
a.sink(ctx, d, msg)
return true
}
a.recordCarried(ctx, now, d, "startup summary")
return true
case held >= 0 && d.Action == incident.Update:
8 s.collected[held].Incident = d.Incident
a.recordCarried(ctx, now, d, "startup summary")
return true
case held >= 0 && d.Action == incident.Resolve:
// Resolved before anyone was told: leave it out of the summary.
s.collected = append(s.collected[:held], s.collected[held+1:]...)
a.incidents.ReleaseAnnouncement(id)
a.recordCarried(ctx, now, d, "startup summary")
return true
}
return false
Expand Down
35 changes: 34 additions & 1 deletion internal/pipeline/startup_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ func TestEngineStartupHoldsPageTierAndPagesSeparately(t *testing.T) {
now := time.Date(2026, 9, 29, 10, 0, 0, 0, time.UTC)
var sent []notification.Message
sink := func(_ context.Context, _ incident.Decision, m notification.Message) {
sent = append(sent, m)
if m.Carrier == "" {
sent = append(sent, m)
}
}
e := newTestEngine(t, &fakeClock{now: now}, sink, nil)
e.announcer.startup.until = now.Add(time.Minute)
Expand Down Expand Up @@ -279,3 +281,34 @@ func TestIncidentStoreStartupMarkerRoundTrip(t *testing.T) {
t.Fatalf("round trip = %+v found=%v err=%v", got, found, err)
}
}

// Decisions the startup summary holds reach the sink marked as carried,
// except a page-tier announcement, which goes out as paging-only.
func TestEngineStartupRecordsHeldDecisionsForTheAuditLog(t *testing.T) {
now := time.Date(2026, 9, 29, 10, 0, 0, 0, time.UTC)
var seen []notification.Message
sink := func(_ context.Context, _ incident.Decision, m notification.Message) {
seen = append(seen, m)
}
e := newTestEngine(t, &fakeClock{now: now}, sink, nil)
e.announcer.startup.until = now.Add(time.Minute)
page := announce("node")
page.Incident.Tier = incident.Page
resolve := announce("pod")
resolve.Action = incident.Resolve

e.announcer.collectStartup(context.Background(), now,
[]incident.Decision{page, announce("pod"), resolve})

if len(seen) != 3 {
t.Fatalf("want three sink calls, got %d: %+v", len(seen), seen)
}
if !seen[0].PagingOnly || seen[0].Carrier != "" {
t.Fatalf("the page goes out paging-only, got %+v", seen[0])
}
for _, m := range seen[1:] {
if m.Carrier != "startup summary" || m.PagingOnly {
t.Fatalf("held decisions are carried by the summary, got %+v", m)
}
}
}
7 changes: 7 additions & 0 deletions internal/replay/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Result struct {
Messages []notification.Message
// Times holds the simulated time each message was delivered.
Times []time.Time
// Carried are the decisions recorded for the audit log only, which
// the digest or the startup summary carried; see Message.Carrier.
Carried []incident.Decision
// Incidents are the tracked incidents when the replay ended.
Incidents []incident.Incident
// End is the simulated time the replay stopped at.
Expand Down Expand Up @@ -72,6 +75,10 @@ func Run(
deps.Sink = func(
ctx context.Context, d incident.Decision, m notification.Message,
) {
if m.Carrier != "" {
result.Carried = append(result.Carried, d)
return
}
result.Decisions = append(result.Decisions, d)
result.Messages = append(result.Messages, m)
result.Times = append(result.Times, sim.Now())
Expand Down
Loading
0