Skip to content

metrics-plugin: Label lightning_funds_output_msat by output status and reservation - #16

Open
Nazarevsky wants to merge 4 commits into
masterfrom
nazarevsky/metric-onchain-reserved-funds
Open

Nazarevsky wants to merge 4 commits into
masterfrom
nazarevsky/metric-onchain-reserved-funds

Conversation

@Nazarevsky

@Nazarevsky Nazarevsky commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Currently on the master branch lightning_funds_output_msat shows the mixed onchain balance including both reserved and unreserved outputs.

lightning_funds_output_msat adds up every listfunds output, including unconfirmed, immature and reserved ones. That overstates what the node can spend now. For example, inputs reserved by a channel open that's in progress still count, so a "low on-chain funds" alert built on it fires too late.

This PR adds two labels to the metric, both taken from each listfunds output:

  • status: unconfirmed, confirmed, spent, immature
  • reserved: true, false

On-chain funds the node can spend now become a query:

lightning_funds_output_msat{status="confirmed", reserved="false"}

Also, Grafana dashboard for outputing all those metrics was missing - adding it in this PR.

Changes

  • cache.rs: FundsSnapshot keeps the sum for each status and reserved flag instead of one total.
  • metrics.rs: lightning_funds_output_msat is now exported with the status and reserved labels. All 8 combinations are exported on every scrape, at 0 when empty, so no series disappears when the wallet is empty. lightning_funds_total_msat is unchanged and keeps its value.
  • README.md: documents the labels, plus queries for spendable funds and for the old total.
  • spent is always 0, because the plugin calls listfunds without spent. It's exported anyway, so the label covers every status listfunds can return.
  • grafana dashboard.

Breaking change

lightning_funds_output_msat changes from one series to 8 labelled series.

  • Dashboards: a query without aggregation now returns 8 series. A stat panel, for example, shows 8 values (mostly 0) instead of the total.
  • Alerts: a rule like lightning_funds_output_msat < X would also fire on the zero-valued series.

To get the old value:

sum without (status, reserved) (lightning_funds_output_msat)

sum() works with both the old and the new version, so dashboards can be updated before upgrading.

Testing

  • cache.rs unit tests: confirmed, unconfirmed, reserved and immature outputs land in the right status/reserved bucket, and the buckets add up to the old total.
  • Collector test: scrapes the collector and checks every labelled series, the zero-valued ones, and lightning_funds_total_msat.
  • Checks: cargo test -p metrics-plugin (11 passed), cargo clippy --all-targets --all-features -- -D warnings and cargo fmt --check are clean.
  • Not tested on a live node: I haven't run this against a real CLN node.

lightning_funds_output_msat adds up every listfunds output: unconfirmed,
immature and reserved ones included. That overstates what the node can
spend now, e.g. to open a channel, so it can't drive a low-funds alert.

Keep the sum per output status (unconfirmed, confirmed, spent, immature)
and per reserved flag in FundsSnapshot instead of one total. The
exported series are unchanged in this commit: lightning_funds_output_msat
and lightning_funds_total_msat still show the same total, now computed
from the split.

Unit tests cover confirmed, unconfirmed, reserved and immature outputs,
check that the parts add up to the old total, and that the status order
matches the labels the next commit exports.
Export lightning_funds_output_msat as one series per output status
(unconfirmed, confirmed, spent, immature) and reserved flag (true,
false), so on-chain funds that can be spent now are a query:

    lightning_funds_output_msat{status="confirmed", reserved="false"}

All 8 series are exported on every scrape, at 0 when empty, so none
goes absent on an empty wallet. lightning_funds_total_msat keeps its
value.

This changes an existing series: a query on lightning_funds_output_msat
without sum() now returns 8 series instead of 1, and an alert like
`lightning_funds_output_msat < X` fires on the zero ones. Use
`sum without (status, reserved) (lightning_funds_output_msat)` for the
old total.

A collector test checks the exported series, labels and values.
Describe the status and reserved labels in the README's Funds section,
note that spent stays at 0 (listfunds is called without spent), and give
the queries for spendable on-chain funds and for the old total.
@Nazarevsky Nazarevsky self-assigned this Nov 25, 2026
Add metrics-plugin/dashboards/cln-metrics.json, a Grafana dashboard
(uid cln-metrics) for the series the metrics plugin exports.
@Nazarevsky
Nazarevsky force-pushed the nazarevsky/metric-onchain-reserved-funds branch from 05c0e37 to 414fded Compare November 25, 2026 15:22
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.

1 participant