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