Skip to content

refactor(meter): usage reporting as a package every server can use - #608

Open
okdaichi wants to merge 6 commits into
mainfrom
ccr-2161bc2c-wkewvo-meter
Open

okdaichi wants to merge 6 commits into
mainfrom
ccr-2161bc2c-wkewvo-meter

Conversation

@okdaichi

Copy link
Copy Markdown
Collaborator

Description

This is the first of the usage-metering PRs. It moves the code that sends usage records to QUMO_USAGE_URL out of the relay's verifier into internal/meter, so ingest and hls can report through the same code. This PR changes no behaviour: the relay sends exactly the records it did before.

The plan for the next PRs follows one rule, the same one the relay already uses: usage goes to the kid of the tenant token the server itself verified.

  1. This PR: internal/meter, with the relay moved onto it.
  2. Next: the relay reports opt-in seconds (/.abr/, /.hls/) on the session that announces them. A tenant then pays for ABR and HLS by opt-in time, and qumo abr never needs to know whose broadcast it transcodes.
  3. Next: ingest reports bytes and opt-in seconds under the publisher's kid.
  4. Next: hls reports egress bytes under the viewer's kid.

Related Issue

None.

Changes

  • internal/meter: Reporter with Open, Report (cumulative, coalesced to the latest), Close, Forget, Total (one running total per key for the run, today's viewers record), Run and Flush.
    • Batching (500 records), retries and requeue, dropping a malformed batch on 400/422, the 50,000-event cap, signing, 401 logging and the last send at shutdown all move unchanged.
    • Log lines are prefixed with Config.Name; for the relay that is still relay:.
  • internal/auth/usage.go: keeps only the relay's own decisions:
    • a publisher is reported by itself;
    • viewers go into one total per key;
    • a viewer that starts publishing switches over;
    • an open after a close is ignored;
    • stale sessions are forgotten, and now also forgotten in the meter.
  • CHANGELOG (Changed).

Testing

  • internal/auth: the existing usage tests pass after renaming two accessors (runID → meter.RunID(); one test now asserts nothing is sent instead of reading the pending queue).
  • internal/meter:
    • The generic transport tests move here: record order, coalescing, retrying with newer usage, malformed batches dropped, records kept on 401/403/408/413/429/502, batches of 500, a mid-backlog failure.
    • New tests: usage or a close for a session that is not open is ignored; a forgotten session's usage is not resent; unchanged totals are not resent; each run gets its own run ID; Run sends at the tick and at shutdown (synctest).
    • The package passes with -race.
  • go test ./... passes; golangci-lint reports 0 issues; the relay session, ticket and anonymous integration tests pass.

Checklist

  • Comments added for complex logic
  • Documentation updated if needed
  • Tests added/updated

Generated by Claude Code

What sends usage records to QUMO_USAGE_URL moves out of the relay's
verifier into internal/meter: sessions (Open, Report, Close), running
totals per key (Total), coalescing, batches of 500, retries through an
outage, signing and the last send at shutdown. The relay keeps only its
own decisions (a publisher by itself, viewers in one total per key) in
internal/auth. The records it sends are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KPWMJL7PH3rPhK5UTGavaB

Copy link
Copy Markdown
Collaborator Author

Test failed on internal/relay TestServer_ServeAuth_TicketSupersedes/the_same_root,_nothing (admit_test.go:593, "the superseded ticket is counted once"). I don't think this PR caused it:

  • The diff touches only internal/meter, internal/auth/usage.go and their tests. The test server builds its verifier without UsageURL, so the usage reporter is nil and no usage code runs on this path.
  • Locally the test passes 15 times out of 15 with -race, and the full go test -race ./... passes.

What I think is happening:

  • The relay answers AUTH_OK for the first ticket (w.Grant) before it calls a.roots.registerTicket(h) (internal/relay/admit.go).
  • The test presents the second ticket as soon as the first is granted. If the second registers while the first handler is still between w.Grant and registerTicket, the first gets current == false.
  • The client still sees the first ticket withdrawn with NO_ERROR, so assertSuperseded passes.
  • But the metric counts refused, not superseded, so the counter assertion never holds. A nothing grant has no hold to wait on, which makes this ordering more likely in that subtest.

I'm re-running the failed job once. Fixing it belongs in its own change, either counting this case as superseded or registering before answering, not in this refactor.


Generated by Claude Code

Copy link
Copy Markdown
Collaborator Author

Integration failed on ce44a49 in TestFunnel_RecordsReachAMoQSubscriber (funnel_test.go:86, "subscriber endpoint never became reachable"). I don't think this PR caused it:

  • ce44a49 merges main, and this PR's own diff is internal/meter, internal/auth/usage.go and the CHANGELOG.
  • The funnel test builds no usage reporter.

What I think is happening:

  • startFunnel picks a port with freeUDPPort, which binds a port and then releases it.
  • It then starts moqSrv.ListenAndServeTLS in a goroutine and ignores the error.
  • If another test in the parallel integration run takes that port in between, the listener never comes up, and the 5 s reachability probe fails.

I'm re-running the failed job once. Fixing it belongs in its own change: listen on :0 and read back the bound address, or at least surface the ListenAndServeTLS error.


Generated by Claude Code

- The check that a key-set or usage URL is https, or http only to a
  loopback host, was in internal/auth and copied into internal/meter. It
  is now reqsign.CheckURL, beside the other rules for requests to the
  control plane (NoRedirectClient, Refusals), so the two can't drift.
- meter.Reporter.now was never overridden; it uses time.Now.
- usageReporter.flush existed only for tests; they call meter.Flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KPWMJL7PH3rPhK5UTGavaB

This branch has not been deployed

No deployments
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.

2 participants