Skip to content

Commit 55a8065

Browse files
committed
wallet: add keyCount metric
Signed-off-by: Gustavo Chain <[email protected]>
1 parent 11006f4 commit 55a8065

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

collectors/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package collectors
2+
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
)
7+
8+
// isUnimplemented returns true if the given error is a grpc unimplemented
9+
// error.
10+
func isUnimplemented(err error) bool {
11+
code := status.Code(err)
12+
return code == codes.Unimplemented
13+
}

collectors/wallet_collector.go

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

33+
// We'll use two gauges to keep track of both internal and external key
34+
// count.
35+
internalKeyCountDesc *prometheus.Desc
36+
externalKeyCountDesc *prometheus.Desc
37+
38+
// We'll use two gauges to keep track of the accounts internal and external
3339
// errChan is a channel that we send any errors that we encounter into.
3440
// This channel should be buffered so that it does not block sends.
3541
errChan chan<- error
@@ -39,6 +45,8 @@ type WalletCollector struct {
3945
func NewWalletCollector(lnd *lndclient.LndServices,
4046
errChan chan<- error) *WalletCollector {
4147

48+
// these labels are specific for key_count metrics
49+
keyCountLabels := []string{"account_name", "address_type"}
4250
return &WalletCollector{
4351
lnd: lnd,
4452
numUtxosConfDesc: prometheus.NewDesc(
@@ -71,6 +79,18 @@ func NewWalletCollector(lnd *lndclient.LndServices,
7179
"unconfirmed wallet balance",
7280
nil, nil,
7381
),
82+
internalKeyCountDesc: prometheus.NewDesc(
83+
"lnd_wallet_key_count_internal",
84+
"internal key count",
85+
keyCountLabels, nil,
86+
),
87+
88+
externalKeyCountDesc: prometheus.NewDesc(
89+
"lnd_wallet_key_count_external",
90+
"external key count",
91+
keyCountLabels, nil,
92+
),
93+
7494
errChan: errChan,
7595
}
7696
}
@@ -88,6 +108,8 @@ func (u *WalletCollector) Describe(ch chan<- *prometheus.Desc) {
88108
ch <- u.avgUtxoSizeDesc
89109
ch <- u.confirmedBalanceDesc
90110
ch <- u.unconfirmedBalanceDesc
111+
ch <- u.internalKeyCountDesc
112+
ch <- u.externalKeyCountDesc
91113
}
92114

93115
// Collect is called by the Prometheus registry when collecting metrics.
@@ -169,4 +191,25 @@ func (u *WalletCollector) Collect(ch chan<- prometheus.Metric) {
169191
u.unconfirmedBalanceDesc, prometheus.GaugeValue,
170192
float64(walletBal.Unconfirmed),
171193
)
194+
195+
accounts, err := u.lnd.WalletKit.ListAccounts(context.Background(), "", 0)
196+
// Nodes prior to version 0.13.0 don't implement ListAccounts and will
197+
// return a Unimplemented error.
198+
if err != nil && !isUnimplemented(err) {
199+
u.errChan <- fmt.Errorf("WalletCollector ListAccounts"+
200+
"failed with: %v", err)
201+
return
202+
}
203+
for _, account := range accounts {
204+
ch <- prometheus.MustNewConstMetric(
205+
u.internalKeyCountDesc, prometheus.GaugeValue,
206+
float64(account.InternalKeyCount),
207+
account.GetName(), account.GetAddressType().String(),
208+
)
209+
ch <- prometheus.MustNewConstMetric(
210+
u.externalKeyCountDesc, prometheus.GaugeValue,
211+
float64(account.ExternalKeyCount),
212+
account.GetName(), account.GetAddressType().String(),
213+
)
214+
}
172215
}

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ module github.com/lightninglabs/lndmon
33
require (
44
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
55
github.com/btcsuite/btcutil v1.0.3-0.20210527170813-e2ba6805a890
6-
github.com/coreos/bbolt v1.3.3 // indirect
7-
github.com/coreos/etcd v3.3.22+incompatible // indirect
86
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
97
github.com/jessevdk/go-flags v1.4.0
108
github.com/jrick/logrotate v1.0.0
119
github.com/lightninglabs/lndclient v1.0.1-0.20211001112454-5bb2b557e003
1210
github.com/lightningnetwork/lnd v0.13.0-beta.rc5.0.20210920062527-d9f0f07142ea
1311
github.com/prometheus/client_golang v1.11.0
1412
github.com/stretchr/testify v1.7.0
15-
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
13+
google.golang.org/grpc v1.38.0
1614
)
1715

1816
go 1.15

0 commit comments

Comments
 (0)