Skip to content

feat(metrics): a mount publishes what it is spending at AWS (#226) - #408

Merged
scttfrdmn merged 2 commits into
mainfrom
feat/cost-metrics-226
Aug 9, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
feat/cost-metrics-226

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #226.

The rates were already right. internal/awsrates holds one S3 rate table, verified against the live AWS Pricing API by an integration test that re-reads 23 rates and fails on any difference (#220, #225) — and every path from a rate to a user was severed. The access-pattern report was gated behind a config key no mount could set, internal/cost had zero importers, and metrics.RecordCost had no caller anywhere.

What lands

objectfs_s3_cost, a gauge family with statistic, region and tier labels, carrying billable request counts by pricing group, bytes retrieved, three dollar figures, and beside each figure the rate it was computed at.

Counted in the AWS SDK's Deserialize step, not at the wrapper layer, and that is the substance of it. The eight places this backend records metrics are not one-to-one with billable calls: a 5 GB PutObject is one wrapper call and 641 requests to S3 — one CreateMultipartUpload, 639 UploadParts, one CompleteMultipartUpload. A large CopyObject, a parallel ranged read, and the capability probes fan out the same way, one of them counted nowhere at all. Every one of those errors is in the direction that flatters. A middleware where the SDK deserializes cannot disagree with what S3 received, because it is what S3 received; it goes in through clientOptions, so the standard client, the accelerated client and every client the pool factory builds are all counted — a factory that skipped it once sent four operations to real AWS.

Three billability decisions, each with a distinct failure mode:

  • Per attempt, since the retryer re-enters that step. Three tries at one PutObject are three requests, which is what AWS bills.
  • A response is required. A connect or DNS failure never reached S3 and is not billed.
  • Status below 500 only. AWS does not bill for its own server errors, and does bill 4xx — a HeadObject returning 404 is a charged read. Counting 5xx would inflate the figure during exactly the incident an operator is using it to investigate; dropping 4xx would hide a misconfigured mount retrying a 403.

Four pricing groups rather than AWS's two request tiers: writes, lists, reads, free. Lists are separate even though the price list calls both "Tier1", because on DEEP_ARCHIVE a PUT is $0.05/1,000 and a LIST is $0.005/1,000 — a factor of ten, and pricing a traversal at the write rate overstates it worst on the class where the error is largest. An unrecognized operation counts as a write, the most expensive group, so an operation this code has never heard of overstates rather than hides.

What the figures are is stated narrowly: this process's spend since it started, at list prices for the first volume band. Not a bill. bytes_stored is what this mount uploaded, not the bucket's size — nothing lists the bucket, since that would be a billed request per scrape to publish a metric. Monotonic with no reset, because rate-of-change is the form a useful alert on cost takes. A fresh mount reports read_requests of 1: NewBackend's health check is a HeadBucket, AWS bills it, and the tests assert that 1 rather than asserting zero.

Verification

The recording proxy is the oracle, not the counter's own arithmetic:

  • request counts compared group by group against what the endpoint received
  • a multipart write where both sides must agree and the count must exceed the part count, since both collapsing to 1 would pass the first check
  • an injected 500 where the counted total must equal observed total minus server errors
  • dollar assertions stating AWS's published rates as literals and dividing in the test, per the fix(cost): one rate table, checked against the live AWS Pricing API #220 lesson: the old tests passed `102410241024` bytes, called it one GB, and asserted the per-GB rate came back — an expectation that holds under both the right divisor and the wrong one

Seven mutations run, all caught, each by a test naming the actual defect:

mutation caught by, with
pool factory passes a nil tally "counted 0 requests, the proxy saw 7"
List case removed requestGroupOf("ListObjectsV2") = 0, want 1
>= 500 → >= 599 "counted 3 billable … so 2 are billable"
binary GB divisor ratio 0.9313 — exactly the decimal/binary gap
read rate used for writes ratio 0.1 — exactly the factor of ten
CostStats() hoisted out of the publish closure "write_requests is still 0 10s after a write"
tier label dropped `tier label = "", want "STANDARD_IA"`

Both SDK suites pass against the regenerated sdks/testdata/metrics-scrape.txt: Python 73 passed / 31 subtests, TypeScript tsc clean and 66 tests. The fixture pins a non-default region and tier (us-west-2, STANDARD_IA) so an extractor that reads the family as one series per statistic fails there, and the figures reconcile by hand.

Two deletions rather than a second path to a cost figure

internal/cost — 710 lines plus 540 of tests: Calculator, Reporter, AlertManager, per-tenant accumulation, ROI reporting, budget alerts. Zero importers, and no configuration path ever existed for the tenants or budgets it was built around; being under internal/ it cannot acquire an out-of-module caller either. Two cost-calculation packages where one is unreachable is precisely the arrangement that produced #209 — the same rate in five places, two disagreeing by a factor of ten. Its pricing_drift_test.go was the guard against that shape and the half worth keeping moved to internal/storage/s3, where it now asserts every rate reachable through PricingManager is awsrates' own value exactly, for every class the loader accepts — storage, PUT, GET, LIST and retrieval, not storage alone, since a partial regression leaving requests on constants passes a storage-only check. Verified by mutation: a private PUT rate fails it with the ratio named, and a round factor of ten in that ratio is reported as what it is.

metrics.RecordCost and CostMetrics — the issue asked to wire it or delete it but not to leave a third unreachable path. It could not usefully be wired: it took request, storage and transfer costs as float64 dollars, so every price would have been the call site's rather than awsrates'. Two of its calculations were also wrong past any amount of wiring — cost per GB over 1 << 30 where AWS bills decimal GB, and EstimatedMonthlyCost extrapolated from process uptime, so a mount thirty seconds old reported its first half-minute as the whole month's rate, most wrong exactly when someone is most likely to read it. The rest of DetailedPerformanceMetrics stays; #222 assigned its percentiles and fixed its histogram this release, so only the cost half was dead.

Items 1 and 2 of the issue — splitting MonitorAccessPatterns from the silent tier override, and correcting the stale race comment at adapter.go — landed earlier this release; SmallObjectsOnStandard is now its own key and the comment states the real reason. The issue's region caveat for internal/cost is moot with the package gone.

Gate

go build ./..., gofmt -l ., go vet ./..., golangci-lint run all clean; go test -race ./... passes with no data races. The internal/cost 98 coverage floor is out of .coverage-floors, with a note that 98% covered and not part of the product is the lesson pkg/api's floor carried.

The rates were already right. internal/awsrates holds one S3 rate table, verified
against the live Pricing API by an integration test that re-reads 23 rates and
fails on any difference — and every path from a rate to a user was severed. The
access-pattern report was gated behind a config key no mount could set,
internal/cost had zero importers, and metrics.RecordCost had no caller anywhere.

objectfs_s3_cost is the path: billable request counts by pricing group, bytes
retrieved, three dollar figures, and beside each the rate it was computed at,
labelled with the resolved region and the storage tier.

Counted in the SDK's Deserialize step, not at the wrapper layer. The eight places
this backend records metrics are not one-to-one with billable calls: a 5 GB
PutObject is one wrapper call and 641 requests to S3, and a large CopyObject, a
parallel ranged read and the capability probes fan out the same way — one of them
counted nowhere at all. Every one of those errors flatters. A middleware where the
SDK deserializes cannot disagree with what S3 received because it is what S3
received. Per attempt, since the retryer re-enters that step; only with a response,
since a connect failure never reached S3; only below 500, because AWS does not bill
its own server errors and does bill 4xx.

Four pricing groups rather than AWS's two request tiers, because on DEEP_ARCHIVE a
PUT is $0.05/1,000 and a LIST is $0.005/1,000 — a factor of ten. An unrecognized
operation counts as a write, the most expensive group, so a case this code has
never heard of overstates rather than hides.

Verified against a recording proxy rather than against the counter's own
arithmetic: the tests compare what the tally counted with what the endpoint
received, including a multipart write where the count must also exceed the part
count, and an injected 500 where the counted total must equal the observed total
minus the server errors. Seven mutations were run and each was caught by a test
naming the actual defect, including the binary GB divisor at ratio 0.9313.

Two things deleted rather than left as a second path to a cost figure:

internal/cost — 710 lines plus 540 of tests, a per-tenant calculator with no
importer and no configuration path to the tenants or budgets it was built around.
Two cost-calculation packages where one is unreachable is the arrangement that
produced #209's 10x disagreement. Its drift guard moved to internal/storage/s3 and
now asserts every rate reachable through PricingManager is awsrates' own value
exactly, for every class the loader accepts — verified by mutation.

metrics.RecordCost and CostMetrics — it took prices as float64 dollars, so every
rate would have been the call site's rather than awsrates'. Two of its
calculations were also wrong past any wiring: cost per GB over 1<<30 where AWS
bills decimal GB, and a monthly estimate extrapolated from process uptime, so a
mount thirty seconds old reported its first half-minute as the month's rate.
@scttfrdmn
scttfrdmn merged commit 07e42eb into main Aug 9, 2026
35 checks passed
@scttfrdmn
scttfrdmn deleted the feat/cost-metrics-226 branch August 9, 2026 10:42
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.

cost: the rates are accurate and nothing can observe them — wire cost to the metrics surface

1 participant