Repository navigation
feat: Add counter metrics for consumed cycles - #9922
Merged
michael-weigelt merged 1 commit intoMay 5, 2026
Merged
Conversation
dsarlis
commented
Apr 17, 2026
dsarlis
left a comment
Contributor
Author
There was a problem hiding this comment.
Open points:
- Naming is something that I deliberately did not spend too much on. I can totally understand if a suffix
_as_countersis not considered good enough and open to ideas for alternate names. - In tests you'll notice that I've added a few places where I check the new counters additionally to old ones. One might wonder "what if we try to add such checks universally in
ExecutionTest". I tried it and as it turns out it's not easy to make all tests work as some of them cut corners and you're not always able to do the check at "clear points", i.e. points where you know that there can be no outstanding callbacks (execute_allwould be the best candidate and you get majority of tests to work but you still miss some special ones, highly concentrated inhypervisor_tests). I'd leave as an improvement if anyone wants to pick it up.
Contributor
pierugo-dfinity
approved these changes
Apr 20, 2026
pierugo-dfinity
left a comment
Contributor
There was a problem hiding this comment.
Approving changes in rs/monitoring/metrics
michael-weigelt
approved these changes
May 4, 2026
mraszyk
added a commit
that referenced
this pull request
Jul 21, 2026
Refine the HTTP/ECDSA outcall cycles migration so that it: - Runs from `observe_consumed_cycles_with_use_case` (on every observed use case) instead of from the scalar-specific observe methods, so it runs independently of whether the scalar fields are observed. This lets the use-case entries catch up even on subnets that no longer perform outcalls. The migration runs after the increments, and since the scalar fields are bumped before the matching use-case observation at the call sites, the `max` reconciliation does not double count. - Only migrates the `consumed_cycles_by_use_case` map. The monotonic `consumed_cycles_by_use_case_as_counters` map is left untouched: backfilling it would introduce a spurious counter jump, which is exactly the transition that #9922 (which introduced the counter map) deliberately deferred pending a proper cutoff for in-flight callbacks. Tests are updated to trigger the migration via an unrelated use case and to assert the counters map is not modified. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
pull Bot
pushed a commit
to bit-cook/ic
that referenced
this pull request
Aug 24, 2026
…finity#11219) The migration added in dfinity#10844, which brings the HTTPOutcalls / ECDSAOutcalls entries of SubnetMetrics::consumed_cycles_by_use_case up to the legacy scalar fields, was invoked from observe_consumed_cycles_with_use_case. That only fires when the subnet observes a subnet-level use case, and those are only observed on HTTP outcalls, threshold signature outcalls, canister deletion and cycles lost to dropped messages. A subnet that does none of these never runs the migration and its use-case entries stay stale indefinitely. This is not hypothetical: on mainnet subnet 2fq7c-slacv-26cgz-vzbx2-2jrcs-5edph-i5s2j-tck77-c3rlz-iobzx-mqe, consumed_cycles_ecdsa_outcalls is 244_900_000_000_000 while the ECDSAOutcalls use-case entry is 228_370_000_000_000, a gap of 16_530_000_000_000 cycles. Its consumed_cycles_by_use_case_as_counters map is empty, which (since that map is incremented on the very same code path) shows that no subnet-level use case has been observed there since the counters were introduced in dfinity#9922, i.e. well before the migration was deployed. The migration now runs from the scheduler's finish_round, which is documented as running unconditionally after each round and is also reached by the heap-delta early return and by checkpoint_round_with_no_execution. Running it once per round instead of per observation is equivalent, because the call sites bump the scalar field and the matching use-case entry by the same amount: max(entry, scalar) + delta == max(entry + delta, scalar + delta). It is also idempotent, so the extra invocations are harmless. Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
pull Bot
pushed a commit
to bit-cook/ic
that referenced
this pull request
Sep 22, 2026
…se (dfinity#11585) `CanisterMetrics::consumed_cycles_by_use_cases_monotonic` was introduced in May 2026 (dfinity#9922) and decodes as empty from checkpoints written before that, so it starts from zero rather than from what the canister has really consumed. Its gauge counterpart, `consumed_cycles_by_use_cases`, predates it and reaches further back. This backfills the monotonic amounts from those gauges, the way dfinity#11490 already does for the scalar `consumed_cycles_monotonic`, and in the same checkpoint-round pass. The derivation is exact per use case, by the same invariant as for the scalar: a gauge exceeds its monotonic counterpart by exactly the prepayments still outstanding for it, all of which are recorded in the replicated state. `SystemState::outstanding_prepayments` now returns that breakdown per use case. ## How far back each amount reaches The backfill can only recover as much as its source holds, and the two sources differ: | Field | Tracked since | | ----- | ------------- | | `consumed_cycles` (scalar gauge, tag 22) | the initial import, 2021 — the canister's full history | | `consumed_cycles_by_use_cases` (gauge map, tag 36) | April 2023 (EXC-1376) | | `consumed_cycles_by_use_cases_monotonic` (tag 65) | May 2026, and April 2023 once backfilled | | ⤷ its `HTTPOutcalls` entry | May 2026, permanently | The by-use-case breakdown was introduced in March 2023 (EXC-1345, deployed 2023-03-13 to 2023-03-20), but the fix that followed (EXC-1376, elected on 2023-04-17 by [proposal 118023](https://dashboard.internetcomputer.org/proposal/118023) and deployed 2023-04-18 to 2023-04-24) moved it from proto field 35 to field 36, discarding what the first five weeks had recorded. Nothing records what a canister consumed per use case before April 2023, so that is how far back the backfilled amounts reach. A consequence: the scalar `consumed_cycles_monotonic` is backfilled from a gauge reaching back to 2021, the by-use-case amounts from gauges reaching back to April 2023, so a canister that consumed cycles before then holds more in the scalar than its by-use-case amounts add up to. The pre-existing doc calling the scalar *exactly* their sum is qualified accordingly. `HTTPOutcalls` is never backfilled: HTTPS outcalls are only tracked as a gauge at the subnet level, so this monotonic entry has no canister-level gauge to derive it from and keeps covering only what it has recorded since May 2026. The public `CyclesConsumed` docs served by `canister_metrics` state how far back the amounts reach. Only the backfill's own comments describe the backfill. The comments on the fields, their getters, the `canister_metrics` endpoint and the canister deletion path state just what each amount is and which use cases it covers, so that dropping the backfill one day leaves no stale traces. ## Backfilling unconditionally The backfill runs for every canister on every checkpoint round, rather than only for canisters that need it, in exchange for dropping the "is a backfill due" bookkeeping, which with the by-use-case amounts would have to span the scalar total and every use case. That costs an `Arc::make_mut` per canister per checkpoint round, and it is a real copy: the tip a round works on shares every canister's `Arc` with the last committed state, so every canister not already modified earlier in the round gets copied. The cost is small next to what already happens: the resource-charging pass does the same to every canister every 10 seconds, i.e. dozens of times per checkpoint interval. ## Visible effects Unlike the scalar monotonic total, which nothing exports yet, `consumed_cycles_by_use_cases_monotonic` is served in two places, both of which will step up once, on the first checkpoint round after the upgrade: - The `canister_metrics` management canister endpoint reports its `cycles_consumed` from it. Every field except `http_outcalls` will jump from what the canister consumed since May 2026 to what it consumed since April 2023. - The `replicated_state_consumed_cycles_from_replica_start_as_counters` Prometheus counter, by the sum over the subnet's canisters of what each has been under-reporting since May 2026. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
alin-at-dfinity
pushed a commit
that referenced
this pull request
Sep 22, 2026
…se (#11585) `CanisterMetrics::consumed_cycles_by_use_cases_monotonic` was introduced in May 2026 (#9922) and decodes as empty from checkpoints written before that, so it starts from zero rather than from what the canister has really consumed. Its gauge counterpart, `consumed_cycles_by_use_cases`, predates it and reaches further back. This backfills the monotonic amounts from those gauges, the way #11490 already does for the scalar `consumed_cycles_monotonic`, and in the same checkpoint-round pass. The derivation is exact per use case, by the same invariant as for the scalar: a gauge exceeds its monotonic counterpart by exactly the prepayments still outstanding for it, all of which are recorded in the replicated state. `SystemState::outstanding_prepayments` now returns that breakdown per use case. ## How far back each amount reaches The backfill can only recover as much as its source holds, and the two sources differ: | Field | Tracked since | | ----- | ------------- | | `consumed_cycles` (scalar gauge, tag 22) | the initial import, 2021 — the canister's full history | | `consumed_cycles_by_use_cases` (gauge map, tag 36) | April 2023 (EXC-1376) | | `consumed_cycles_by_use_cases_monotonic` (tag 65) | May 2026, and April 2023 once backfilled | | ⤷ its `HTTPOutcalls` entry | May 2026, permanently | The by-use-case breakdown was introduced in March 2023 (EXC-1345, deployed 2023-03-13 to 2023-03-20), but the fix that followed (EXC-1376, elected on 2023-04-17 by [proposal 118023](https://dashboard.internetcomputer.org/proposal/118023) and deployed 2023-04-18 to 2023-04-24) moved it from proto field 35 to field 36, discarding what the first five weeks had recorded. Nothing records what a canister consumed per use case before April 2023, so that is how far back the backfilled amounts reach. A consequence: the scalar `consumed_cycles_monotonic` is backfilled from a gauge reaching back to 2021, the by-use-case amounts from gauges reaching back to April 2023, so a canister that consumed cycles before then holds more in the scalar than its by-use-case amounts add up to. The pre-existing doc calling the scalar *exactly* their sum is qualified accordingly. `HTTPOutcalls` is never backfilled: HTTPS outcalls are only tracked as a gauge at the subnet level, so this monotonic entry has no canister-level gauge to derive it from and keeps covering only what it has recorded since May 2026. The public `CyclesConsumed` docs served by `canister_metrics` state how far back the amounts reach. Only the backfill's own comments describe the backfill. The comments on the fields, their getters, the `canister_metrics` endpoint and the canister deletion path state just what each amount is and which use cases it covers, so that dropping the backfill one day leaves no stale traces. ## Backfilling unconditionally The backfill runs for every canister on every checkpoint round, rather than only for canisters that need it, in exchange for dropping the "is a backfill due" bookkeeping, which with the by-use-case amounts would have to span the scalar total and every use case. That costs an `Arc::make_mut` per canister per checkpoint round, and it is a real copy: the tip a round works on shares every canister's `Arc` with the last committed state, so every canister not already modified earlier in the round gets copied. The cost is small next to what already happens: the resource-charging pass does the same to every canister every 10 seconds, i.e. dozens of times per checkpoint interval. ## Visible effects Unlike the scalar monotonic total, which nothing exports yet, `consumed_cycles_by_use_cases_monotonic` is served in two places, both of which will step up once, on the first checkpoint round after the upgrade: - The `canister_metrics` management canister endpoint reports its `cycles_consumed` from it. Every field except `http_outcalls` will jump from what the canister consumed since May 2026 to what it consumed since April 2023. - The `replicated_state_consumed_cycles_from_replica_start_as_counters` Prometheus counter, by the sum over the subnet's canisters of what each has been under-reporting since May 2026. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add counter versions of metrics for consumed cycles that are stored in
ReplicatedState. The existing ones behave like gauges (so their values can go down when prepayments are made and up when refunds are issued) which makes it more challenging for consumers to build automated monitoring tools to perform aggregations over them. By having them monotonically increase, it's easier to calculate rates of change, show aggregates over time etc.The key idea is to introduce a second map of
<CyclesUseCase, NominalCycles>in theReplicatedStatethat will only be updated once per use case: either at the payment stage if we know the precise amount or only at refund stage if a prepayment is made with an expected refund later. The second map is quite similar to the existing in all other aspects (how they are stored in checkpoints or how they are exposed as prometheus metrics) besides how the values are updated.A new map is introduced to ease the transition as migrating from the old map to new is non-trivial given that a proper cutoff point needs to be introduced to handle outstanding callbacks that might have been created before the metric introduction. This is left for a follow-up if and when people decide to do it. The new map will be used in a follow up that will implement the new management canister endpoint to retrieve canister level metrics.
Additionally, the new metrics include the use case
HttpsOutcallsin the canister level metrics as it's useful to determine how much each canister uses this feature. I've opted to not change existing metrics to do the same as it would make things less clean imo than the current approach -- a single specific API is used to perform this update in exactly one place where it's needed.The changes in the PR are mostly driven by the addition of the new map of metrics, updates in protobuf files to store the new metrics, the changes to support having the
HttpsOutcallsuse case additionally included as well as some changes in tests to support the new metrics.