Skip to content

fix(cost): --cost-limit counts public IPv4, EBS IOPS/throughput and late-attached resources; one accounting for enforcement and displays (#51) - #1340

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/51-cost-accounting
Oct 27, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/51-cost-accounting

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #51

Every under-count makes --cost-limit fire late, which leaks cost. This PR closes the under-counts it found and puts enforcement and every display on one accounting function, pkg/costmodel.Accrue. spored's accumulatedComputeCost, spored status, the not-running spawn status, spawn cost <instance>, the billable-resources block and spawn extend's decideCostLimit all call it.

Inventory: what a spawn instance bills, and whether the cap counts it

Component running stopped hibernated terminated Before this PR Now
Compute (instance rate) bills – – – counted, compute-seconds × on-demand unchanged
EBS root volume (size) bills bills bills – (DeleteOnTermination) counted, wall clock unchanged
EBS attached data volumes (size) bills bills bills – counted only if attached at first boot (cached for life) re-measured every start + every 15 min
EBS provisioned IOPS (gp3 >3000, io1, io2) and gp3 throughput >125 MB/s bills bills bills – not counted counted
Hibernation RAM image on root volume bills (as root EBS) – counted via root size unchanged
Auto-assigned public IPv4 ($0.005/h) bills – (released) – – not counted counted over compute time
Elastic IP ($0.005/h), incl. the launch EIP of a multi-interface EFA launch bills bills bills – (released by spored / terminate / reaper) not counted counted over the wall clock
ENIs / EFA interfaces free free free – n/a n/a
FSx / EFS bills bills bills outlives the instance not counted by design, refused up front by costLimitPreflight and listed separately unchanged
Data transfer, snapshots, NAT varies out of scope out of scope

Rate used: enforcement prices compute at the on-demand spawn:price-per-hour, which can only over-count a spot instance. Displays price compute at the spot price where one was recorded, and the stopped spawn status now also shows "Cost limit: $X counted at the on-demand rate".

Static rates are labelled in pkg/costmodel (RatesAsOf = 2026-10-26). They come from https://aws.amazon.com/vpc/pricing/ ("Hourly charge for In-use Public IPv4 Address $0.005", the same for idle) and https://aws.amazon.com/ebs/pricing/, both fetched 2026-10-26. The VPC page carries no date of its own. The charge has applied since 2024-02-01. They are static because spored enforces from inside the instance with no Price List permission. Where the page was ambiguous I took the higher figure: gp3 throughput at $0.06 per MB/s-month, and io2 above 64k IOPS at the tier-2 rate.

Other under-counts fixed

  • Footprint measured once per life: LookupAndTagEBSCost returned early once spawn:ebs-hourly-cost was set. MeasureCostFootprint replaces it and measures on every start and every 15 ticks, off the ticker goroutine and never stacked. The launch EIP is associated after the instance is running, so spored's first look can come before it. A recorded value never decreases: the current footprint is priced over the whole life, so lowering it would forgive cost already incurred.
  • No spawn:launch-time tag meant EBS dropped out of the cap: the wall-clock anchor now falls back to IMDS pendingTime.
  • spored status priced EBS over stopped/hibernated time only, so a running instance showed none of the EBS the cap counted. It now uses the wall clock since launch, and its cumulative figure matches cost-limit used on an on-demand instance.

IPv4 counts come from the instance's own addresses (costFootprintOf): every interface, every private address's association, deduplicated. IpOwnerId == "amazon" means auto-assigned, anything else is an Elastic IP. New tags: spawn:public-ipv4-count and spawn:elastic-ipv4-count, written by spored with the same self-CreateTags it already used. DescribeInstances and DescribeVolumes were already granted (#517). No new permission.

Over-counts (the less harmful direction, noted, not fixed)

Residual under-counts (not fixed)

Gates (each watched failing against a compiling revert)

  • TestAccrue_EachComponentInEachState: the table above as code, components × {running, stopped, hibernated, terminated}, expectations taken from the doc's billing rules. Fails with IPv4 zeroed, with the EIP on the compute clock, and with EBS on the compute clock.
  • TestVolumeHourly_CountsProvisionedPerformance fails with io2 IOPS dropped. TestCostFootprintOf_PricesEveryAttachedVolume fails with root-only pricing.
  • TestCostFootprintOf_CountsEveryPublicIPv4 covers the multi-interface EFA launch EIP, a single interface, an EIP on a secondary private IP, and no address. It fails with secondary associations ignored and with EIPs not counted. TestCountIPv4 also fails in the second case.
  • TestCostDisplaysAndExtendShareTheEnforcedAccounting checks that the stopped status, spawn cost "Est. total", decideCostLimit Needed/PerHour and the billable IPv4 row all equal Accrue over the enforced footprint. It fails when extend uses the old formula, when the stopped total drops IPv4, and when spawn cost uses its own footprint.
  • TestNoCostRateTagReadOutsideTheModel walks the module, fails on empty discovery, and matches tag names used as values, not prose. It fails on a direct tags["spawn:public-ipv4-count"] read in cmd/.
  • TestAccumulatedCost_IsCostmodelAccrue fails when enforcement ignores the IPv4 counts. TestAccumulatedCost_NoLaunchTagFallsBackToPendingTime fails without the fallback.
  • TestCheckAndAct_RemeasuresCostFootprint fails with the tick call disabled. TestMaybeMeasureCostFootprint_NeverStacks fails without the CAS guard. Both use seams, with no timing.
  • TestFootprintTagWrites_NeverLowers fails when a lower value is written.
  • TestStatusCostIsTheEnforcedAccounting (spored status) fails with storage priced over not-running time only.

Checks

make check: fmt, vet (all GOOS), staticcheck, golangci-lint (0 issues, 10 modules), hermetic short tests, nested modules and tidy all pass. check-build-tags fails on the /private/tmp path issue, which also fails on unmodified main. make check-docs passes (docs-gen regenerated for the cost help and --cost-limit flag text). semgrep scan --config=auto --error . is clean. No exported pkg/aws or pkg/launcher API change. User data is untouched.

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

…esources in --cost-limit; one accounting for enforcement and displays (#51)

Enforcement (spored) and every cost display (spored status, spawn status,
spawn cost, spawn extend's decideCostLimit) now go through
pkg/costmodel.Accrue, so what is shown is what is enforced.

- public IPv4 ($0.005/hr per address, VPC pricing page 2026-10-26):
  auto-assigned over compute time, Elastic IPs (incl. the multi-interface
  EFA launch EIP) over the wall clock; counted from the instance's actual
  addresses on every interface
- EBS: provisioned IOPS (gp3>3000, io1, io2 tiered) and gp3 throughput
- footprint re-measured on every start and every 15 min, recorded rates
  never decrease (was: measured once at first boot)
- EBS no longer dropped from the cap when spawn:launch-time is missing
- spored status priced EBS over stopped time only; now wall clock

Fixes #51
@scttfrdmn
scttfrdmn force-pushed the fix/51-cost-accounting branch from 7f90ce5 to a57ad7a Compare October 26, 2026 23:52
@scttfrdmn
scttfrdmn merged commit b62e0cc into main Oct 27, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/51-cost-accounting branch October 27, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit cost accumulation for accuracy — include EBS, public IPv4, etc. across all instance states

1 participant