Skip to content

feat: Add counter metrics for consumed cycles - #9922

Merged
michael-weigelt merged 1 commit into
dfinity:masterfrom
dsarlis:dimitris/counter-metrics-cycles
May 5, 2026
Merged

michael-weigelt merged 1 commit into
dfinity:masterfrom
dsarlis:dimitris/counter-metrics-cycles

Conversation

@dsarlis

@dsarlis dsarlis commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

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 the ReplicatedState that 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 HttpsOutcalls in 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 HttpsOutcalls use case additionally included as well as some changes in tests to support the new metrics.

@dsarlis dsarlis left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open points:

  1. Naming is something that I deliberately did not spend too much on. I can totally understand if a suffix _as_counters is not considered good enough and open to ideas for alternate names.
  2. 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_all would be the best candidate and you get majority of tests to work but you still miss some special ones, highly concentrated in hypervisor_tests). I'd leave as an improvement if anyone wants to pick it up.

@dsarlis
dsarlis marked this pull request as ready for review April 17, 2026 11:02
@dsarlis
dsarlis requested review from a team as code owners April 17, 2026 11:02
@github-actions

Copy link
Copy Markdown
Contributor

@basvandijk basvandijk added the security-review-passed IDX or InfraSec have concluded it's safe to run CI on the external PR. label Apr 17, 2026

@pierugo-dfinity pierugo-dfinity left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving changes in rs/monitoring/metrics

@michael-weigelt
michael-weigelt added this pull request to the merge queue May 5, 2026
Merged via the queue into dfinity:master with commit 0bf1d54 May 5, 2026
12 of 13 checks passed
@michael-weigelt
michael-weigelt deleted the dimitris/counter-metrics-cycles branch May 5, 2026 08:32
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@consensus external-contributor feat security-review-passed IDX or InfraSec have concluded it's safe to run CI on the external PR. @team-dsm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants