Skip to content

refactor: refactor rs/ic_os/metrics: shared write helper, replace hand-rolled formatter, rename tools - #9330

Merged
shilingwang merged 10 commits into
masterfrom
shiling/metrics-tool
Mar 16, 2026
Merged

shilingwang merged 10 commits into
masterfrom
shiling/metrics-tool

Conversation

@shilingwang

@shilingwang shilingwang commented Mar 12, 2026 •

Copy link
Copy Markdown
Contributor

#NODE-1524

Summary

This PR cleans up the five metric-writing tools under rs/ic_os/ in three ways:

  1. Shared atomic write helper (rs/ic_os/metrics/utils)
    A new ic-os-metrics-utils crate provides a single write_registry_to_file(registry, path) function. It encodes a Prometheus registry and writes the result atomically via a temp-file rename (using ic_sys::write_string_using_tmp_file), so a partially-written .prom file is never visible to node_exporter.
    Previously, metrics_tool, nft_exporter, and guest_vm_runner all duplicated the same 6-line BufWriter / TextEncoder / flush block and wrote non-atomically. All four are now updated to use the shared helper.
  2. Replace hostos_tool's hand-rolled Prometheus formatter
    hostos_tool/src/prometheus_metric.rs (158 lines) reimplemented the Prometheus text exposition format from scratch, used f32 for metric values, and only supported writing a single metric. It is deleted and replaced with the standard prometheus crate (GaugeVec with a gen label), consistent with the rest of the codebase.
  3. Rename tools for consistency
    The _tool suffix was used inconsistently — hostos_tool, guestos_tool, setupos_tool are all configuration/provisioning binaries, while fstrim_tool and metrics_tool are periodic metric exporters that live in rs/ic_os/metrics/. Both are renamed to match the folder's naming convention:
  • metrics_tool → custom_metrics (binary, crate, systemd units)
  • fstrim_tool → fstrim_metrics (binary, crate, library, systemd units)

The public entry-point function fstrim_tool(...) in the library crate is also simplified to run(...).
4. Fix integration test metric ordering
fstrim_metrics integration tests were asserting on a hardcoded metric ordering that did not match the alphabetical order Prometheus registries actually produce (fstrim_datadir_* < fstrim_last_* < fstrim_runs_*). All three affected expected strings are corrected.

Files changed
Area Change
rs/ic_os/metrics/utils/ New shared crate
rs/ic_os/metrics/custom_metrics/ Renamed from metrics_tool; uses shared helper
rs/ic_os/metrics/fstrim_metrics/ Renamed from fstrim_tool; integration test assertions fixed
rs/ic_os/metrics/nft_exporter/ Uses shared helper (atomic writes)
rs/ic_os/os_tools/guest_vm_runner/src/metrics.rs Uses shared helper (atomic writes)
rs/ic_os/os_tools/hostos_tool/ Deletes prometheus_metric.rs; uses prometheus crate + shared helper
ic-os/components/monitoring/guestos/ Renamed metrics_tool.* and fstrim_tool.* systemd units
rs/tests/crypto/ Updated for fstrim_metrics rename and new crate name

@github-actions github-actions Bot added the chore label Mar 12, 2026
@shilingwang shilingwang changed the title chore: use prometheus trait for all metrics tools of guestos refactor: refactor rs/ic_os/metrics: shared write helper, replace hand-rolled formatter, rename tools Mar 13, 2026
@github-actions github-actions Bot added refactor and removed chore labels Mar 13, 2026
@shilingwang
shilingwang marked this pull request as ready for review March 13, 2026 13:27
@shilingwang
shilingwang requested review from a team as code owners March 13, 2026 13:27

@andrewbattat andrewbattat 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.

Nice!

@shilingwang
shilingwang force-pushed the shiling/metrics-tool branch from a3e69af to 1f65f13 Compare March 16, 2026 10:46
@shilingwang
shilingwang force-pushed the shiling/metrics-tool branch from 2b6c248 to 7d61623 Compare March 16, 2026 12:26
@shilingwang
shilingwang enabled auto-merge March 16, 2026 13:23
@shilingwang
shilingwang added this pull request to the merge queue Mar 16, 2026
Merged via the queue into master with commit 7984f7b Mar 16, 2026
36 checks passed
@shilingwang
shilingwang deleted the shiling/metrics-tool branch March 16, 2026 13:45
AleDema pushed a commit to AleDema/ic that referenced this pull request Oct 18, 2026
…dfinity#11828)

## Summary

Resolves the 8 open cargo Dependabot alerts, which are filed against
`Cargo.lock` and `Cargo.Bazel.toml.lock`.

| Crate | Old → New | Alerts closed |
|---|---|---|
| rustls | 0.23.37 (Cargo) / 0.23.27 (Bazel) → 0.23.45 | dfinity#630, dfinity#634
(GHSA-2mjx-qc3c-rqvc) |
| hickory-resolver, hickory-net, hickory-proto | 0.26.1 → 0.26.3 |
dfinity#627-dfinity#629, dfinity#631-dfinity#633 (GHSA-5j98-2g5x-46v6, GHSA-6w6g-hm98-mhgm,
GHSA-6f2x-v7q7-m7m5) |

The new versions force these transitive bumps:
- rustls ≥ 0.23.44 requires aws-lc-rs ^1.18. That moves aws-lc-rs 1.17.3
→ 1.18.1, aws-lc-sys 0.43.0 → 0.45.0 (AWS-LC 5.7.0) and rustls-webpki
0.103.13 → 0.103.15.
- Bazel lock only: hickory 0.26.3 requires resolv-conf ^0.7.6 and ipnet
^2.11. That moves resolv-conf 0.7.0 → 0.7.6 and ipnet 2.10.1 → 2.12.2,
and drops hostname 0.3.1 and match_cfg.

Notes:
- **The version floors are raised in both `Cargo.toml` and
`bazel/rust.MODULE.bazel`.** This keeps the two builds aligned and stops
them from regressing into the vulnerable ranges.
- The Bazel lock had been on rustls 0.23.27 since dfinity#5114, while the Cargo
lock moved to 0.23.37 in dfinity#9330. Production (Bazel) binaries therefore
jump 18 rustls releases, which is why this PR runs with
`CI_ALL_BAZEL_TARGETS`.
- **hickory 0.26.3 instead of the advisories' first patched version,
0.26.2.** 0.26.2 declares `hickory-net ^0.26.0` but does not compile
against hickory-net 0.26.1 (hickory-dns/hickory-dns#3881). `cargo update
--precise 0.26.2` keeps hickory-net at 0.26.1.
- **PocketIC DNSSEC opt-out.**
- Since 0.26.2, hickory returns an error for answers that fail DNSSEC
validation. That is the GHSA-5j98 fix; 0.26.1 returned such answers as
`Ok`.
- 0.26.2 and 0.26.3 also have an open regression,
hickory-dns/hickory-dns#3963, introduced by the GHSA-6w6g fix. Validated
lookups fail when two or more nameservers return truncated replies. That
is routine for the DNSKEY sets of .io, .org, .systems and .network.
- ic-bn-lib validates by default. As a result, a PocketIC HTTP gateway
created with `forward_to: Replica("https://icp-api.io")` hung in `POST
/http_gateway`, because `setup_router(...).unwrap()` panicked in the
spawned task.
- The gateway resolver now passes `--dns-dnssec-disabled`. That matches
the previous effective behaviour, since bogus results were ignored. A
TODO tracks dropping the flag once a hickory release includes
hickory-dns/hickory-dns#4062.
  - Nothing else in the repo validates DNSSEC:
- the replica's nns_delegation_manager and reqwest's hickory resolver
run without validation;
    - ic-boundary resolves through the registry;
- the GuestOS ic-gateway sidecar is not launched, and its `IC_URL` is an
IP literal.

## How regenerated

```
cargo update -p rustls --precise 0.23.45
./bin/bazel-pin.sh rustls=0.23.45
cargo update -p hickory-resolver --precise 0.26.3
./bin/bazel-pin.sh hickory-resolver=0.26.3
```

`./bin/bazel-pin.sh <crate>=<version>` runs `cargo update --package
<crate> --precise <version>`. The script's `<crate>@<version>` form is
only a package-ID spec, not `--precise`.

## Test plan

- [x] `cargo update --workspace && ./bin/bazel-pin.sh` is a no-op
("Locking 0 packages", "Nothing to repin."). This mirrors CI's
lock-generate.
- [x] `cargo check --all-targets --all-features -p pocket-ic-server`,
`cargo fmt` and `./ci/scripts/rust-lint.sh`
- [x] `bazel run //bazel:buildifier.check`
- [x] `bazel build //... --nobuild`: clean except the known host-only
`@libvirt` repo
- [x] `bazel build //rs/pocket_ic_server:pocket-ic-server`: the first
build of aws-lc-sys 0.45.0 under the hermetic toolchain
- [x] `bazel test
//rs/http_endpoints/nns_delegation_manager:nns_delegation_manager_test
//rs/crypto:integration_suite_tests/tls_handshake_test`
- [x] PocketIC HTTP gateway with `forward_to:
Replica("https://icp-api.io")`:
- with this PR, `GET /api/v2/status` through the gateway returns HTTP
200;
- with the hickory bump but without the opt-out, `POST /http_gateway`
hangs and the server panics with "unable to fetch IC root key ... client
error (Connect)".

  Both results reproduced 2/2.
- [x] CI passes with `CI_ALL_BAZEL_TARGETS`. This covers Bazel Test All
over `//...` including long and system tests, arm64-linux, arm64-darwin
and PocketIC Lint/Tests on Windows (aws-lc-sys 0.45 on MSVC). Build
Determinism compared 62k execlog lines with no mismatches.
- [ ] Dependabot closes the alerts listed above after merge. The
`Cargo.Bazel.toml.lock` ones close once `bazel-dependency-submission`
has run on master.

## Follow-ups

- Drop the PocketIC `--dns-dnssec-disabled` flag (the TODO in
`state.rs`) once we upgrade to a hickory-dns release that includes
hickory-dns/hickory-dns#4062.
- A panic in PocketIC's spawned HTTP gateway setup task leaves `POST
/http_gateway` waiting on `listening()` forever.
`setup_router(...).await.unwrap()` is one such panic, and it is how the
DNSSEC failure surfaced. Propagating setup errors back to the caller
would turn hangs like this into errors. This is pre-existing and left to
the PocketIC owners.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants