@@ -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 {
3945func 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}
0 commit comments