From c9b1af56a32ab5e6ec355aa26a4b9764313e56dd Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Fri, 17 Feb 2023 20:08:37 +0100 Subject: [PATCH] collectors: summarize limbo channel balances to avoid stuck values This commit changes how we collect limbo balances to calculate summaries instead of separate series by channel, peer, status etc. Having just the summaries makes it possible to correctly select the last known total value which was previously not feasible when summarizing over longer periods due to values with channel/peer specific labels getting "stuck". --- collectors/channels_collector.go | 41 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/collectors/channels_collector.go b/collectors/channels_collector.go index b9087fe..409b309 100644 --- a/collectors/channels_collector.go +++ b/collectors/channels_collector.go @@ -75,12 +75,12 @@ func NewChannelsCollector(lnd lndclient.LightningClient, errChan chan<- error, pendingForceCloseBalanceDesc: prometheus.NewDesc( "lnd_channels_pending_force_close_balance_sat", "force closed channel balances in satoshis", - append(labels, "blocks_until_maturity"), nil, + []string{"status"}, nil, ), waitingCloseBalanceDesc: prometheus.NewDesc( "lnd_channels_waiting_close_balance_sat", "waiting to close channel balances in satoshis", - labels, nil, + nil, nil, ), incomingChanSatDesc: prometheus.NewDesc( "lnd_channels_bandwidth_incoming_sat", @@ -408,32 +408,33 @@ func (c *ChannelsCollector) Collect(ch chan<- prometheus.Metric) { "waiting_close", ) + // Preinitialize the map with all possible anchor state labels to avoid + // "stuck" values when selecting a longer time range. + forceCloseTotal := map[string]btcutil.Amount{ + anchorStateToString(lndclient.ForceCloseAnchorStateLimbo): 0, + anchorStateToString(lndclient.ForceCloseAnchorStateRecovered): 0, + anchorStateToString(lndclient.ForceCloseAnchorStateLost): 0, + } for _, forceClose := range pendingChannelsResp.PendingForceClose { - // Labels are: "chan_id", "status", "initiator", "peer", - // "blocks_until_maturity". We'll use status to hold the anchor - // state. + forceCloseTotal[anchorStateToString(forceClose.AnchorState)] += + forceClose.RecoveredBalance + } + + for anchorState, balance := range forceCloseTotal { ch <- prometheus.MustNewConstMetric( c.pendingForceCloseBalanceDesc, prometheus.GaugeValue, - float64(forceClose.RecoveredBalance), - forceClose.ChannelPoint.String(), - anchorStateToString(forceClose.AnchorState), - forceClose.ChannelInitiator.String(), - forceClose.PubKeyBytes.String(), - fmt.Sprintf("%d", forceClose.BlocksUntilMaturity), + float64(balance), anchorState, ) } + var waitingClosetotal btcutil.Amount for _, waitingClose := range pendingChannelsResp.WaitingClose { - // Labels are: "chan_id", "status", "initiator", "peer". - ch <- prometheus.MustNewConstMetric( - c.waitingCloseBalanceDesc, prometheus.GaugeValue, - float64(waitingClose.LocalBalance), - waitingClose.ChannelPoint.String(), - waitingClose.ChanStatusFlags, - waitingClose.ChannelInitiator.String(), - waitingClose.PubKeyBytes.String(), - ) + waitingClosetotal += waitingClose.LocalBalance } + ch <- prometheus.MustNewConstMetric( + c.waitingCloseBalanceDesc, prometheus.GaugeValue, + float64(waitingClosetotal), + ) // Get the list of closed channels. closedChannelsResp, err := c.lnd.ClosedChannels(context.Background())