host: demo disk alert reads any reading and names the filesystem
The disk rule on a demo box takes the highest of the readings it has, so a full /run fires with no host disk reading. Its message names what it measured: /data, /data inodes or /run (metrics.Event.Where). Other boxes' messages are unchanged. Copy not yet reviewed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+135
−34
internal/host/alertsink.go+16 −2internal/host/demodisk_status.go+28 −13internal/host/demodisk_status_test.go+63 −9internal/host/metrics_loop.go+23 −10internal/metrics/alert.go+5 −0
diff --git a/internal/host/alertsink.go b/internal/host/alertsink.go| index d07e3d421..58b6d3eac 100644 |
| --- a/internal/host/alertsink.go |
| +++ b/internal/host/alertsink.go |
| @@ -419,7 +419,14 @@ func alertSentence(ev metrics.Event) string { |
| name, subject := metrics.SplitRule(ev.Rule) |
| switch name { |
| case "disk": |
| - return fmt.Sprintf("disk is %s full on %s", pct(ev.Value), ev.Box) |
| + switch ev.Where { |
| + case "": |
| + return fmt.Sprintf("disk is %s full on %s", pct(ev.Value), ev.Box) |
| + case diskWhereDataInodes: |
| + return fmt.Sprintf("/data has used %s of its inodes on %s", pct(ev.Value), ev.Box) |
| + default: |
| + return fmt.Sprintf("%s is %s full on %s", ev.Where, pct(ev.Value), ev.Box) |
| + } |
| case "mem": |
| return fmt.Sprintf("memory use is %s on %s", pct(ev.Value), ev.Box) |
| case "cpu": |
| @@ -443,7 +450,14 @@ func alertClearSentence(ev metrics.Event) string { |
| name, subject := metrics.SplitRule(ev.Rule) |
| switch name { |
| case "disk": |
| - return fmt.Sprintf("disk is back to %s full on %s", pct(ev.Value), ev.Box) |
| + switch ev.Where { |
| + case "": |
| + return fmt.Sprintf("disk is back to %s full on %s", pct(ev.Value), ev.Box) |
| + case diskWhereDataInodes: |
| + return fmt.Sprintf("/data is back to %s of its inodes used on %s", pct(ev.Value), ev.Box) |
| + default: |
| + return fmt.Sprintf("%s is back to %s full on %s", ev.Where, pct(ev.Value), ev.Box) |
| + } |
| case "mem": |
| return fmt.Sprintf("memory use is back to %s on %s", pct(ev.Value), ev.Box) |
| case "cpu": |
diff --git a/internal/host/demodisk_status.go b/internal/host/demodisk_status.go| index 93f7f5291..7e8e705f6 100644 |
| --- a/internal/host/demodisk_status.go |
| +++ b/internal/host/demodisk_status.go |
| @@ -68,28 +68,43 @@ func readDemoBuilds(open func() (rootstate.Store, error)) (*status.DemoBuilds, e |
| return &status.DemoBuilds{Outputs: rec.Outputs, OutputBytes: rec.OutputBytes, At: rec.At, Backlog: rec.Backlog, BacklogSince: rec.BacklogSince}, nil |
| } |
| -// demoDiskFill is the highest fraction used of anything d read: /data's |
| -// inodes, /run's bytes and inodes, and /data's bytes, which the disk rule |
| -// already reads as DiskFrac. nil when d read nothing. |
| -func demoDiskFill(d *status.DemoDisk) *float64 { |
| +// diskReading is one filesystem's use and what was measured, which the |
| +// disk alert names. |
| +type diskReading struct { |
| + frac float64 |
| + where string |
| +} |
| + |
| +// What a demo box's disk alert measured (alertSentence names it). |
| +const ( |
| + diskWhereData = "/data" |
| + diskWhereDataInodes = "/data inodes" |
| + diskWhereRun = "/run" |
| +) |
| + |
| +// demoDiskFill is the highest use of anything d read: /data's blocks and |
| +// inodes, and /run's bytes and inodes. nil when d read nothing. |
| +func demoDiskFill(d *status.DemoDisk) *diskReading { |
| if d == nil { |
| return nil |
| } |
| - var fill *float64 |
| - higher := func(free, total int64) { |
| + var fill *diskReading |
| + higher := func(free, total int64, where string) { |
| if total <= 0 || free < 0 || free > total { |
| return |
| } |
| f := 1 - float64(free)/float64(total) |
| - if fill == nil || f > *fill { |
| - fill = &f |
| + if fill == nil || f > fill.frac { |
| + fill = &diskReading{frac: f, where: where} |
| } |
| } |
| - for _, r := range []*status.FilesystemRoom{d.Data, d.Run} { |
| - if r != nil { |
| - higher(r.FreeBytes, r.Bytes) |
| - higher(r.FreeInodes, r.Inodes) |
| - } |
| + if r := d.Data; r != nil { |
| + higher(r.FreeBytes, r.Bytes, diskWhereData) |
| + higher(r.FreeInodes, r.Inodes, diskWhereDataInodes) |
| + } |
| + if r := d.Run; r != nil { |
| + higher(r.FreeBytes, r.Bytes, diskWhereRun) |
| + higher(r.FreeInodes, r.Inodes, diskWhereRun) |
| } |
| return fill |
| } |
diff --git a/internal/host/demodisk_status_test.go b/internal/host/demodisk_status_test.go| index 1a4637cb0..3ed0adf99 100644 |
| --- a/internal/host/demodisk_status_test.go |
| +++ b/internal/host/demodisk_status_test.go |
| @@ -7,6 +7,7 @@ import ( |
| "math" |
| "os" |
| "path/filepath" |
| + "strings" |
| "testing" |
| "time" |
| @@ -83,12 +84,16 @@ func TestDemoDiskFillIsTheHighestUse(t *testing.T) { |
| Data: &status.FilesystemRoom{FreeBytes: 90, Bytes: 100, FreeInodes: 4, Inodes: 100}, |
| Run: &status.FilesystemRoom{FreeBytes: 50, Bytes: 100, FreeInodes: 80, Inodes: 100}, |
| } |
| - if got := demoDiskFill(d); got == nil || math.Abs(*got-0.96) > 1e-9 { |
| - t.Fatalf("fill = %v, want the /data inodes' 0.96", got) |
| + if got := demoDiskFill(d); got == nil || math.Abs(got.frac-0.96) > 1e-9 || got.where != diskWhereDataInodes { |
| + t.Fatalf("fill = %+v, want the /data inodes' 0.96", got) |
| } |
| d.Run.FreeBytes = 1 |
| - if got := demoDiskFill(d); got == nil || math.Abs(*got-0.99) > 1e-9 { |
| - t.Fatalf("fill = %v, want /run bytes' 0.99", got) |
| + if got := demoDiskFill(d); got == nil || math.Abs(got.frac-0.99) > 1e-9 || got.where != diskWhereRun { |
| + t.Fatalf("fill = %+v, want /run bytes' 0.99", got) |
| + } |
| + d.Run.FreeBytes, d.Data.FreeBytes = 50, 0 |
| + if got := demoDiskFill(d); got == nil || got.frac != 1 || got.where != diskWhereData { |
| + t.Fatalf("fill = %+v, want /data's blocks", got) |
| } |
| if demoDiskFill(nil) != nil || demoDiskFill(&status.DemoDisk{Data: &status.FilesystemRoom{}}) != nil { |
| t.Fatal("no reading made a fill") |
| @@ -105,7 +110,7 @@ func TestAlertTickFiresDiskOnInodesAlone(t *testing.T) { |
| rules := metrics.DefaultRules() |
| blocks := 0.10 |
| at := time.Date(2026, 11, 29, 12, 0, 0, 0, time.UTC) |
| - tick := func(fill *float64) { |
| + tick := func(fill *diskReading) { |
| t.Helper() |
| at = at.Add(time.Minute) |
| if err := alertTick(ctx, s, "box-1", rules, &st, metrics.Sample{At: at, Host: metrics.HostSample{DiskFrac: &blocks}}, alertReadings{demoFill: fill}, statePath, nil, nil, nil); err != nil { |
| @@ -116,13 +121,11 @@ func TestAlertTickFiresDiskOnInodesAlone(t *testing.T) { |
| if st.Rules["disk"].Firing { |
| t.Fatal("disk fired at 10% blocks") |
| } |
| - full := 0.97 |
| - tick(&full) |
| + tick(&diskReading{frac: 0.97, where: diskWhereDataInodes}) |
| if !st.Rules["disk"].Firing { |
| t.Fatalf("disk did not fire with inodes 97%% used: %+v", st.Rules["disk"]) |
| } |
| - freed := 0.30 |
| - tick(&freed) |
| + tick(&diskReading{frac: 0.30, where: diskWhereDataInodes}) |
| if st.Rules["disk"].Firing { |
| t.Fatalf("disk still firing after the inodes were freed: %+v", st.Rules["disk"]) |
| } |
| @@ -135,3 +138,54 @@ func TestAlertTickFiresDiskOnInodesAlone(t *testing.T) { |
| t.Fatalf("persisted state: %s %v", raw, err) |
| } |
| } |
| + |
| +// With no host disk reading on a tick, a full /run alone still fires the |
| +// disk incident: the rule reads the highest of the readings it has. The |
| +// message names /run, the filesystem it measured. |
| +func TestAlertTickFiresDiskOnRunWithoutAHostReading(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + st := metrics.AlertState{V: 1, Box: "box-1", Rules: map[string]metrics.RuleState{}} |
| + var bodies [][]byte |
| + sinks := webhookSinks("https://example.test/hook", func(_ string, body []byte) error { |
| + bodies = append(bodies, body) |
| + return nil |
| + }) |
| + at := time.Date(2026, 11, 29, 12, 0, 0, 0, time.UTC) |
| + if err := alertTick(context.Background(), s, "box-1", metrics.DefaultRules(), &st, metrics.Sample{At: at}, alertReadings{demoFill: &diskReading{frac: 0.99, where: diskWhereRun}}, filepath.Join(t.TempDir(), "alerts.json"), sinks, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !st.Rules["disk"].Firing { |
| + t.Fatalf("disk did not fire with /run 99%% used and no host reading: %+v", st.Rules["disk"]) |
| + } |
| + var ev metrics.Event |
| + if len(bodies) != 1 || json.Unmarshal(bodies[0], &ev) != nil || ev.Where != diskWhereRun { |
| + t.Fatalf("delivered %s, want the event naming /run", bodies) |
| + } |
| + subject, _ := formatAlert(metrics.DefaultRules(), at, ev) |
| + if want := "carlos alert: /run is 99% full on box-1"; subject != want { |
| + t.Fatalf("subject %q, want %q", subject, want) |
| + } |
| +} |
| + |
| +// A demo box's disk message names what it measured; any other box's is as |
| +// it was. |
| +func TestFormatAlertNamesTheFilesystemMeasured(t *testing.T) { |
| + at := time.Date(2026, 11, 29, 12, 0, 0, 0, time.UTC) |
| + for _, c := range []struct { |
| + where string |
| + firing bool |
| + wantSubject string |
| + }{ |
| + {"", true, "carlos alert: disk is 97% full on box-1"}, |
| + {diskWhereData, true, "carlos alert: /data is 97% full on box-1"}, |
| + {diskWhereDataInodes, true, "carlos alert: /data has used 97% of its inodes on box-1"}, |
| + {diskWhereRun, true, "carlos alert: /run is 97% full on box-1"}, |
| + {diskWhereDataInodes, false, "carlos alert cleared: /data is back to 97% of its inodes used on box-1"}, |
| + {diskWhereRun, false, "carlos alert cleared: /run is back to 97% full on box-1"}, |
| + } { |
| + subject, body := formatAlert(metrics.DefaultRules(), at, metrics.Event{Rule: "disk", Firing: c.firing, Value: 0.97, Box: "box-1", Where: c.where}) |
| + if subject != c.wantSubject || !strings.Contains(body, strings.TrimPrefix(strings.TrimPrefix(c.wantSubject, "carlos alert cleared: "), "carlos alert: ")) { |
| + t.Errorf("%q firing=%v: subject %q, body %q", c.where, c.firing, subject, body) |
| + } |
| + } |
| +} |
diff --git a/internal/host/metrics_loop.go b/internal/host/metrics_loop.go| index 5874d3524..8ac3df446 100644 |
| --- a/internal/host/metrics_loop.go |
| +++ b/internal/host/metrics_loop.go |
| @@ -539,12 +539,20 @@ func alertTick(ctx context.Context, s release.Store, box string, rules metrics.R |
| sinks *alertSinks, trend *diskTrend, beat *engineBeatWatch) error { |
| rules.DiskFrac = diskAlertFrac(rules.DiskFrac, extra.diskTotal, extra.staticTree) |
| - // A demo box's disk rule reads the higher of the data disk's blocks and |
| - // demoFill (/data's inodes, /run's bytes and inodes). Only on a tick |
| - // with host readings: a sample without them leaves the rule as it was. |
| - diskFrac := latest.Host.DiskFrac |
| - if diskFrac != nil && extra.demoFill != nil && *extra.demoFill > *diskFrac { |
| - diskFrac = extra.demoFill |
| + // A demo box's disk rule reads the highest of the readings it has: the |
| + // data disk's blocks from the host sample, and demoFill (/data's blocks |
| + // and inodes, /run's bytes and inodes). Either alone is an observation; |
| + // with neither the rule stays as it was. |
| + var disk *diskReading |
| + if latest.Host.DiskFrac != nil { |
| + disk = &diskReading{frac: *latest.Host.DiskFrac, where: diskWhereData} |
| + } |
| + if extra.demoFill != nil && (disk == nil || extra.demoFill.frac > disk.frac) { |
| + disk = extra.demoFill |
| + } |
| + var diskFrac *float64 |
| + if disk != nil { |
| + diskFrac = &disk.frac |
| } |
| obs := metrics.Observation{ |
| At: latest.At, |
| @@ -591,6 +599,10 @@ func alertTick(ctx context.Context, s release.Store, box string, rules metrics.R |
| if !boxSinkRule(ev.Rule) { |
| continue |
| } |
| + if ev.Rule == "disk" && extra.demoFill != nil && disk != nil { |
| + // A demo box's message names the filesystem it measured. |
| + ev.Where = disk.where |
| + } |
| sinks.deliver(ctx, rules, latest.At, ev) |
| } |
| @@ -623,10 +635,11 @@ type alertReadings struct { |
| // verify is the restore drill's standing, read from its local copy |
| // each tick (verifyWatch); nil leaves the verify rules as they were. |
| verify *metrics.VerifyReading |
| - // demoFill is a demo box's highest use of /data's inodes and /run's |
| - // bytes and inodes (demoDiskFill); the disk rule reads the higher of it |
| - // and the data disk's blocks. nil on any other box. |
| - demoFill *float64 |
| + // demoFill is a demo box's highest use of /data's blocks and inodes and |
| + // /run's bytes and inodes, and which it was (demoDiskFill); the disk |
| + // rule reads the higher of it and the data disk's blocks. nil on any |
| + // other box. |
| + demoFill *diskReading |
| } |
| // verifyWatch reads the restore drill's local copy of this box's record |
diff --git a/internal/metrics/alert.go b/internal/metrics/alert.go| index d28256cce..7b2c389b7 100644 |
| --- a/internal/metrics/alert.go |
| +++ b/internal/metrics/alert.go |
| @@ -195,6 +195,11 @@ type Event struct { |
| Firing bool `json:"firing"` // true=fired, false=cleared |
| Value float64 `json:"value"` |
| Box string `json:"box"` |
| + // Where is what a disk event measured, when the box says ("/data", |
| + // "/data inodes", "/run"): a demo box's disk rule reads several |
| + // filesystems, and its message names the one it read. Evaluate never |
| + // sets it. |
| + Where string `json:"where,omitempty"` |
| } |
| // AlertKey is the bucket key an AlertState for box is stored/published |