|
| 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", "watchtower client number of backups", |
| 33 | + []string{ |
| 34 | + "tower_pubkey", |
| 35 | + }, nil, |
| 36 | + ), |
| 37 | + numPendingBackupsDesc: prometheus.NewDesc( |
| 38 | + "lnd_wt_client_num_pending_backups", "watchtower client number of pending backups", |
| 39 | + []string{ |
| 40 | + "tower_pubkey", |
| 41 | + }, nil, |
| 42 | + ), |
| 43 | + |
| 44 | + errChan: errChan, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Describe sends the super-set of all possible descriptors of metrics |
| 49 | +// collected by this Collector to the provided channel and returns once the |
| 50 | +// last descriptor has been sent. |
| 51 | +// |
| 52 | +// NOTE: Part of the prometheus.Collector interface. |
| 53 | +func (c *WtClientCollector) Describe(ch chan<- *prometheus.Desc) { |
| 54 | + ch <- c.numBackupsDesc |
| 55 | + ch <- c.numPendingBackupsDesc |
| 56 | +} |
| 57 | + |
| 58 | +// Collect is called by the Prometheus registry when collecting metrics. |
| 59 | +// |
| 60 | +// NOTE: Part of the prometheus.Collector interface. |
| 61 | +func (c *WtClientCollector) Collect(ch chan<- prometheus.Metric) { |
| 62 | + towers, err := c.lnd.WtClient.ListTowers(context.Background(), true, false) |
| 63 | + if err != nil { |
| 64 | + c.errChan <- fmt.Errorf("WtClientCollector ListTowers failed "+ |
| 65 | + "with: %v", err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + for _, tower := range towers { |
| 70 | + pubkey := hex.EncodeToString(tower.Pubkey) |
| 71 | + numBackups := uint32(0) |
| 72 | + numPendingBackups := uint32(0) |
| 73 | + for _, sessionInfo := range tower.SessionInfo { |
| 74 | + for _, session := range sessionInfo.Sessions { |
| 75 | + numBackups += session.NumBackups |
| 76 | + numPendingBackups += session.NumPendingBackups |
| 77 | + } |
| 78 | + } |
| 79 | + ch <- prometheus.MustNewConstMetric( |
| 80 | + c.numBackupsDesc, prometheus.GaugeValue, float64(numBackups), pubkey, |
| 81 | + ) |
| 82 | + ch <- prometheus.MustNewConstMetric( |
| 83 | + c.numPendingBackupsDesc, prometheus.GaugeValue, float64(numPendingBackups), pubkey, |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
0 commit comments