status: traffic windows in the heartbeat
Task 1 of the fleet alerts Plan 2 plan: TrafficWindow and its list types, BoxMetrics.Traffic (private block), Probes.Traffic joining the attach condition, and TrimTrafficWindow's 12 KiB cap. Written by Astra; committed by the controller because the codex sandbox mounted the worktree's git metadata read-only.
Co-Authored-By: GPT-6 Astra (codex) <noreply@openai.com>
internal/status/snapshot.go+4 −1internal/status/traffic.go+93 −0internal/status/traffic_test.go+122 −0
Intent
<task-notification> <task-id>buywfquj7</task-id> <tool-use-id>toolu_01BpL2gvpEA1TKGC8oqQunMm</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1f8653ea-6b78-4712-b1ac-1eb863a870a6/tasks/buywfquj7.output</output-file> <status>failed</status> <summary>Background command "Wait for Astra build run A to finish" failed with exit code 2</summary> </task-notification>
Attributed to this commit: 20.0 g CO₂e (2,533,163 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on fleet-alerts-2-traffic, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/status/snapshot.go b/internal/status/snapshot.go| index 7e3e54eb4..6e5b3ec88 100644 |
| --- a/internal/status/snapshot.go |
| +++ b/internal/status/snapshot.go |
| @@ -39,6 +39,7 @@ import ( |
| // Probes carries everything CollectBox may not read from the registry — |
| // each nil means "unknown", and unknown publishes as null, never a guess. |
| type Probes struct { |
| + Traffic []TrafficWindow |
| ConsoleProcesses []ConsoleProcess |
| ActivationID string |
| ActivationComplete bool |
| @@ -530,6 +531,7 @@ type SystemState struct { |
| // publicly served fleet.json keeps the banded-words-only commitment. See the |
| // package doc comment for why this block is safe to exist at all. |
| type BoxMetrics struct { |
| + Traffic []TrafficWindow `json:"traffic,omitempty"` // Private Metrics only: names hosts and paths |
| ConsoleProcesses []ConsoleProcess `json:"console_processes,omitempty"` |
| ActivationID string `json:"activation_id,omitempty"` |
| ActivationComplete bool `json:"activation_complete,omitempty"` |
| @@ -1152,7 +1154,7 @@ func CollectBox(routes []registry.Route, p Probes, now time.Time) Box { |
| // alias) has nothing to say, not something empty to say. The one |
| // exception is AliasesRead (#447): a successful read of no alias rows IS |
| // something to say, so it attaches the block on its own. |
| - if p.MemTotalBytes != nil || p.MemAvailBytes != nil || p.Load1 != nil || |
| + if len(p.Traffic) > 0 || p.MemTotalBytes != nil || p.MemAvailBytes != nil || p.Load1 != nil || |
| p.DiskFreeBytes != nil || p.DiskFullETAHours != nil || p.Release != "" || p.Tunnel != "" || |
| p.InstanceID != "" || p.Platform != "" || p.SystemChannel != "" || p.System != nil || |
| len(routeVersions) > 0 || len(routeServing) > 0 || len(aliasReadings) > 0 || p.AliasesRead || len(p.Upstreams) > 0 || |
| @@ -1160,6 +1162,7 @@ func CollectBox(routes []registry.Route, p Probes, now time.Time) Box { |
| p.Alerts != nil || p.GeoIP != nil || len(p.BuildStates) > 0 || p.BuildStatesErr != "" || |
| p.UpdateSettings != 0 || len(p.Updates) > 0 || len(p.Egress) > 0 || p.SizeCommittedMiB != nil { |
| box.Metrics = &BoxMetrics{ |
| + Traffic: p.Traffic, |
| ConsoleProcesses: p.ConsoleProcesses, |
| ActivationID: p.ActivationID, ActivationComplete: p.ActivationComplete, |
| Enrollment: p.Enrollment, |
diff --git a/internal/status/traffic.go b/internal/status/traffic.go| new file mode 100644 |
| index 000000000..5bd09ab91 |
| --- /dev/null |
| +++ b/internal/status/traffic.go |
| @@ -0,0 +1,93 @@ |
| +package status |
| + |
| +import ( |
| + "encoding/json" |
| + "time" |
| +) |
| + |
| +const ( |
| + TrafficWindowLen = 5 * time.Minute |
| + TrafficWindowsKept = 3 |
| + TrafficTopN = 10 |
| + TrafficWindowMaxBytes = 12 << 10 |
| +) |
| + |
| +type TrafficWindow struct { |
| + Start int64 `json:"start"` |
| + Requests int64 `json:"requests"` |
| + S2xx int64 `json:"s2xx"` |
| + S3xx int64 `json:"s3xx"` |
| + S4xx int64 `json:"s4xx"` |
| + S404 int64 `json:"s404"` |
| + S5xx int64 `json:"s5xx"` |
| + Refused int64 `json:"refused"` |
| + Blocked int64 `json:"blocked"` |
| + Watched int64 `json:"watched"` |
| + RateShed int64 `json:"rate_shed"` |
| + RateCeiling int64 `json:"rate_ceiling"` |
| + RateObserved int64 `json:"rate_observed"` |
| + PlanThrottled int64 `json:"plan_throttled"` |
| + UnknownHost int64 `json:"unknown_host"` |
| + Apps []TrafficApp `json:"apps,omitempty"` |
| + RateApps []TrafficApp `json:"rate_apps,omitempty"` |
| + Paths []TrafficCount `json:"paths,omitempty"` |
| + Sources []TrafficSource `json:"sources,omitempty"` |
| + Hosts []TrafficCount `json:"hosts,omitempty"` |
| +} |
| + |
| +type TrafficApp struct { |
| + Account string `json:"account"` |
| + App string `json:"app"` |
| + Requests int64 `json:"requests"` |
| + S5xx int64 `json:"s5xx"` |
| + Refused int64 `json:"refused"` |
| + Observed int64 `json:"observed"` |
| +} |
| + |
| +type TrafficCount struct { |
| + Key string `json:"key"` |
| + Hits int64 `json:"hits"` |
| +} |
| + |
| +type TrafficSource struct { |
| + Client string `json:"client"` |
| + Country string `json:"country"` |
| + Hits int64 `json:"hits"` |
| +} |
| + |
| +func (w TrafficWindow) Answered() int64 { |
| + return w.S2xx + w.S3xx + w.S4xx + w.S404 + w.S5xx |
| +} |
| + |
| +func TrimTrafficWindow(w *TrafficWindow, maxBytes int) { |
| + if w == nil { |
| + return |
| + } |
| + for { |
| + b, _ := json.Marshal(w) |
| + if len(b) <= maxBytes { |
| + return |
| + } |
| + largest, chosen := 0, -1 |
| + for i, list := range []any{w.Paths, w.Hosts, w.Apps, w.RateApps, w.Sources} { |
| + encoded, _ := json.Marshal(list) |
| + if len(encoded) > largest && string(encoded) != "null" && string(encoded) != "[]" { |
| + largest, chosen = len(encoded), i |
| + } |
| + } |
| + switch chosen { |
| + case 0: |
| + w.Paths = w.Paths[:len(w.Paths)-1] |
| + case 1: |
| + w.Hosts = w.Hosts[:len(w.Hosts)-1] |
| + case 2: |
| + w.Apps = w.Apps[:len(w.Apps)-1] |
| + case 3: |
| + w.RateApps = w.RateApps[:len(w.RateApps)-1] |
| + case 4: |
| + w.Sources = w.Sources[:len(w.Sources)-1] |
| + default: |
| + return |
| + } |
| + } |
| +} |
diff --git a/internal/status/traffic_test.go b/internal/status/traffic_test.go| new file mode 100644 |
| index 000000000..68d032819 |
| --- /dev/null |
| +++ b/internal/status/traffic_test.go |
| @@ -0,0 +1,122 @@ |
| +package status |
| + |
| +import ( |
| + "encoding/json" |
| + "reflect" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestTrafficWindowJSONNames(t *testing.T) { |
| + w := TrafficWindow{Apps: []TrafficApp{{}}, RateApps: []TrafficApp{{}}, Paths: []TrafficCount{{}}, Sources: []TrafficSource{{}}, Hosts: []TrafficCount{{}}} |
| + b, err := json.Marshal(w) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var got map[string]json.RawMessage |
| + if err := json.Unmarshal(b, &got); err != nil { |
| + t.Fatal(err) |
| + } |
| + names := strings.Fields("start requests s2xx s3xx s4xx s404 s5xx refused blocked watched rate_shed rate_ceiling rate_observed plan_throttled unknown_host apps rate_apps paths sources hosts") |
| + if len(got) != len(names) { |
| + t.Fatalf("keys: %s", b) |
| + } |
| + for _, name := range names { |
| + if _, ok := got[name]; !ok { |
| + t.Errorf("missing %s", name) |
| + } |
| + } |
| + for _, tc := range []struct{ name, keys string }{{"apps", "account app requests s5xx refused observed"}, {"rate_apps", "account app requests s5xx refused observed"}, {"paths", "key hits"}, {"hosts", "key hits"}, {"sources", "client country hits"}} { |
| + var rows []map[string]any |
| + if err := json.Unmarshal(got[tc.name], &rows); err != nil { |
| + t.Fatal(err) |
| + } |
| + keys := strings.Fields(tc.keys) |
| + if len(rows[0]) != len(keys) { |
| + t.Errorf("%s: %v", tc.name, rows) |
| + } |
| + for _, k := range keys { |
| + if _, ok := rows[0][k]; !ok { |
| + t.Errorf("%s missing %s", tc.name, k) |
| + } |
| + } |
| + } |
| + b, _ = json.Marshal(BoxMetrics{}) |
| + if strings.Contains(string(b), `"traffic"`) { |
| + t.Fatal(string(b)) |
| + } |
| +} |
| + |
| +func TestCollectBoxCarriesTraffic(t *testing.T) { |
| + want := []TrafficWindow{{Start: 300, Requests: 2, S2xx: 2}} |
| + box := CollectBox(nil, Probes{Traffic: want}, time.Now()) |
| + if box.Metrics == nil { |
| + t.Fatal("traffic alone must attach metrics") |
| + } |
| + b, err := json.Marshal(box) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var got Box |
| + if err := json.Unmarshal(b, &got); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !reflect.DeepEqual(got.Metrics.Traffic, want) { |
| + t.Fatalf("got %+v", got.Metrics.Traffic) |
| + } |
| + if !strings.Contains(string(b), `"traffic"`) { |
| + t.Fatal("missing traffic key") |
| + } |
| +} |
| + |
| +func TestTrafficWindowAnswered(t *testing.T) { |
| + w := TrafficWindow{S2xx: 1, S3xx: 2, S4xx: 3, S404: 4, S5xx: 5, Refused: 99, UnknownHost: 100} |
| + if got := w.Answered(); got != 15 { |
| + t.Fatal(got) |
| + } |
| +} |
| + |
| +func TestTrimTrafficWindowWorstCase(t *testing.T) { |
| + w := TrafficWindow{Start: 300, Requests: 21, S2xx: 1, S3xx: 2, S4xx: 3, S404: 4, S5xx: 5, Refused: 6, Blocked: 7, Watched: 8, RateShed: 9, RateCeiling: 10, RateObserved: 11, PlanThrottled: 12, UnknownHost: 13} |
| + scalars := w |
| + for i := 0; i < TrafficTopN; i++ { |
| + hits := int64(TrafficTopN - i) |
| + a := TrafficApp{Account: strings.Repeat("<", 128), App: strings.Repeat("<", 128), Requests: hits, Refused: hits} |
| + w.Apps = append(w.Apps, a) |
| + w.RateApps = append(w.RateApps, a) |
| + w.Paths = append(w.Paths, TrafficCount{strings.Repeat("<", 512), hits}) |
| + w.Hosts = append(w.Hosts, TrafficCount{strings.Repeat("<", 253), hits}) |
| + w.Sources = append(w.Sources, TrafficSource{strings.Repeat("<", 64), "IE", hits}) |
| + } |
| + before := w |
| + TrimTrafficWindow(&w, TrafficWindowMaxBytes) |
| + b, err := json.Marshal(w) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(b) > TrafficWindowMaxBytes { |
| + t.Fatalf("%d bytes", len(b)) |
| + } |
| + if !reflect.DeepEqual(w.Apps, before.Apps[:len(w.Apps)]) || !reflect.DeepEqual(w.RateApps, before.RateApps[:len(w.RateApps)]) || !reflect.DeepEqual(w.Paths, before.Paths[:len(w.Paths)]) || !reflect.DeepEqual(w.Hosts, before.Hosts[:len(w.Hosts)]) || !reflect.DeepEqual(w.Sources, before.Sources[:len(w.Sources)]) { |
| + t.Fatal("trim did not retain list prefixes") |
| + } |
| + w.Apps = nil |
| + w.RateApps = nil |
| + w.Paths = nil |
| + w.Hosts = nil |
| + w.Sources = nil |
| + if !reflect.DeepEqual(w, scalars) { |
| + t.Fatalf("scalars changed: %+v", w) |
| + } |
| +} |
| + |
| +func TestTrimTrafficWindowSmallUnchanged(t *testing.T) { |
| + w := TrafficWindow{Start: 300, Paths: []TrafficCount{{"/missing", 2}}} |
| + before, _ := json.Marshal(w) |
| + TrimTrafficWindow(&w, TrafficWindowMaxBytes) |
| + after, _ := json.Marshal(w) |
| + if string(before) != string(after) { |
| + t.Fatalf("%s != %s", before, after) |
| + } |
| +} |