Skip to content

Commit 40111bc

Browse files
committed
wallet: add keyCount metric
Signed-off-by: Gustavo Chain <[email protected]>
1 parent cf01864 commit 40111bc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

collectors/wallet_collector.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type WalletCollector struct {
3030
confirmedBalanceDesc *prometheus.Desc
3131
unconfirmedBalanceDesc *prometheus.Desc
3232

33+
// We'll use one gauge to keep track of both internal and external key
34+
// count.
35+
keyCountDesc *prometheus.Desc
36+
37+
// We'll use two gauges to keep track of the accounts internal and external
3338
// errChan is a channel that we send any errors that we encounter into.
3439
// This channel should be buffered so that it does not block sends.
3540
errChan chan<- error
@@ -39,6 +44,8 @@ type WalletCollector struct {
3944
func NewWalletCollector(lnd *lndclient.LndServices,
4045
errChan chan<- error) *WalletCollector {
4146

47+
// these labels are specific for key_count metrics
48+
keyCountLabels := []string{"account_name", "address_type", "derivation_path", "key_type"}
4249
return &WalletCollector{
4350
lnd: lnd,
4451
numUtxosConfDesc: prometheus.NewDesc(
@@ -71,6 +78,12 @@ func NewWalletCollector(lnd *lndclient.LndServices,
7178
"unconfirmed wallet balance",
7279
nil, nil,
7380
),
81+
keyCountDesc: prometheus.NewDesc(
82+
"lnd_wallet_key_count",
83+
"wallet key count",
84+
keyCountLabels, nil,
85+
),
86+
7487
errChan: errChan,
7588
}
7689
}
@@ -88,6 +101,7 @@ func (u *WalletCollector) Describe(ch chan<- *prometheus.Desc) {
88101
ch <- u.avgUtxoSizeDesc
89102
ch <- u.confirmedBalanceDesc
90103
ch <- u.unconfirmedBalanceDesc
104+
ch <- u.keyCountDesc
91105
}
92106

93107
// Collect is called by the Prometheus registry when collecting metrics.
@@ -169,4 +183,30 @@ func (u *WalletCollector) Collect(ch chan<- prometheus.Metric) {
169183
u.unconfirmedBalanceDesc, prometheus.GaugeValue,
170184
float64(walletBal.Unconfirmed),
171185
)
186+
187+
accounts, err := u.lnd.WalletKit.ListAccounts(context.Background(), "", 0)
188+
if err != nil {
189+
u.errChan <- fmt.Errorf("WalletCollector ListAccounts"+
190+
"failed with: %v", err)
191+
return
192+
}
193+
194+
for _, account := range accounts {
195+
name, addrType, path := account.GetName(),
196+
account.GetAddressType().String(), account.GetDerivationPath()
197+
198+
// internal key count.
199+
ch <- prometheus.MustNewConstMetric(
200+
u.keyCountDesc, prometheus.CounterValue,
201+
float64(account.InternalKeyCount),
202+
name, addrType, path, "internal",
203+
)
204+
205+
// external key count.
206+
ch <- prometheus.MustNewConstMetric(
207+
u.keyCountDesc, prometheus.CounterValue,
208+
float64(account.ExternalKeyCount),
209+
name, addrType, path, "external",
210+
)
211+
}
172212
}

0 commit comments

Comments
 (0)