Skip to content

Commit 57faea9

Browse files
committed
collectors: add wt_client_collector.go
Add collector for the numBackups and numPendingBackups from the lnd watchtower client API.
1 parent a0891ee commit 57faea9

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

collectors/wt_client_collector.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package collectors
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"encoding/hex"
7+
8+
"github.com/lightninglabs/lndclient"
9+
"github.com/prometheus/client_golang/prometheus"
10+
)
11+
12+
// WtClientCollector is a collector that will export watchtower client related
13+
// metrics.
14+
type WtClientCollector struct {
15+
lnd *lndclient.LndServices
16+
17+
numBackupsDesc *prometheus.Desc
18+
numPendingBackupsDesc *prometheus.Desc
19+
20+
// errChan is a channel that we send any errors that we encounter into.
21+
// This channel should be buffered so that it does not block sends.
22+
errChan chan<- error
23+
}
24+
25+
// NewWalletCollector returns a new instance of the WalletCollector.
26+
func NewWtClientCollector(lnd *lndclient.LndServices,
27+
errChan chan<- error) *WtClientCollector {
28+
29+
return &WtClientCollector{
30+
lnd: lnd,
31+
numBackupsDesc: prometheus.NewDesc(
32+
"lnd_wt_client_num_backups",
33+
"watchtower client number of backups",
34+
[]string{
35+
"tower_pubkey",
36+
}, nil,
37+
),
38+
numPendingBackupsDesc: prometheus.NewDesc(
39+
"lnd_wt_client_num_pending_backups",
40+
"watchtower client number of pending backups",
41+
[]string{
42+
"tower_pubkey",
43+
}, nil,
44+
),
45+
46+
errChan: errChan,
47+
}
48+
}
49+
50+
// Describe sends the super-set of all possible descriptors of metrics
51+
// collected by this Collector to the provided channel and returns once the
52+
// last descriptor has been sent.
53+
//
54+
// NOTE: Part of the prometheus.Collector interface.
55+
func (c *WtClientCollector) Describe(ch chan<- *prometheus.Desc) {
56+
ch <- c.numBackupsDesc
57+
ch <- c.numPendingBackupsDesc
58+
}
59+
60+
// Collect is called by the Prometheus registry when collecting metrics.
61+
//
62+
// NOTE: Part of the prometheus.Collector interface.
63+
func (c *WtClientCollector) Collect(ch chan<- prometheus.Metric) {
64+
towers, err := c.lnd.WtClient.ListTowers(context.Background(),
65+
true, false)
66+
if err != nil {
67+
c.errChan <- fmt.Errorf("WtClientCollector ListTowers failed "+
68+
"with: %v", err)
69+
return
70+
}
71+
72+
for _, tower := range towers {
73+
pubkey := hex.EncodeToString(tower.Pubkey)
74+
var (
75+
numBackups uint32
76+
numPendingBackups uint32
77+
)
78+
for _, sessionInfo := range tower.SessionInfo {
79+
for _, session := range sessionInfo.Sessions {
80+
numBackups += session.NumBackups
81+
numPendingBackups += session.NumPendingBackups
82+
}
83+
}
84+
ch <- prometheus.MustNewConstMetric(
85+
c.numBackupsDesc, prometheus.GaugeValue,
86+
float64(numBackups), pubkey,
87+
)
88+
ch <- prometheus.MustNewConstMetric(
89+
c.numPendingBackupsDesc, prometheus.GaugeValue,
90+
float64(numPendingBackups), pubkey,
91+
)
92+
}
93+
}

0 commit comments

Comments
 (0)