@@ -277,10 +277,11 @@ func monitorChannels(info MonitorChannelsInfo) {
277
277
278
278
//Loop provider
279
279
loopProvider := provider.LoopProvider {}
280
+
281
+ prevChannels := []* lnrpc.Channel {}
282
+
280
283
//Infinite loop to monitor channels
281
284
for {
282
- prometheusMetrics .channelBalanceGauge .Reset ()
283
-
284
285
//Call ListChannels method of lightning client with metadata headers
285
286
response , err := info .lightningClient .ListChannels (info .nodeCtx , & lnrpc.ListChannelsRequest {
286
287
ActiveOnly : false ,
@@ -298,6 +299,22 @@ func monitorChannels(info MonitorChannelsInfo) {
298
299
continue
299
300
}
300
301
302
+ // remove channel metrics that are not in list of channels anymore
303
+ for _ , channel := range prevChannels {
304
+ found := false
305
+ for _ , newChannel := range response .Channels {
306
+ if channel .GetChanId () == newChannel .GetChanId () {
307
+ found = true
308
+ break
309
+ }
310
+ }
311
+ if ! found {
312
+ //Remove channel balance metric
313
+ deleteChannelBalanceMetric (info .nodeHost , channel , info .lightningClient , info .nodeCtx )
314
+ }
315
+ }
316
+
317
+ prevChannels = response .Channels
301
318
//TODO Support rules without nodeguard in the future
302
319
303
320
//Get liquidation rule from cache
@@ -769,6 +786,49 @@ func recordChannelBalanceMetric(nodeHost string, channel *lnrpc.Channel, channel
769
786
}).Set (channelBalanceRatio )
770
787
}
771
788
789
+ // Delete the channel balance metric in a prometheus gauge for a specific channel
790
+ func deleteChannelBalanceMetric (nodeHost string , channel * lnrpc.Channel , lightningClient lnrpc.LightningClient , context context.Context ) {
791
+ //Start span
792
+ _ , span := otel .Tracer ("monitorChannel" ).Start (context , "deleteChannelBalanceMetric" )
793
+ defer span .End ()
794
+
795
+ channelId := fmt .Sprint (channel .GetChanId ())
796
+
797
+ localNodeInfo , err := getInfo (lightningClient , context )
798
+
799
+ if err != nil {
800
+ span .SetStatus (codes .Error , err .Error ())
801
+ span .RecordError (err )
802
+ log .WithField ("span" , span ).Errorf ("error getting local node info: %v" , err )
803
+ }
804
+
805
+ remoteNodeInfo , err := getNodeInfo (channel .RemotePubkey , lightningClient , context )
806
+
807
+ if err != nil {
808
+ span .SetStatus (codes .Error , err .Error ())
809
+ span .RecordError (err )
810
+ log .WithField ("span" , span ).Errorf ("error getting remote node info: %v" , err )
811
+ }
812
+
813
+ localPubKey := localNodeInfo .GetIdentityPubkey ()
814
+ remotePubKey := channel .GetRemotePubkey ()
815
+ localAlias := localNodeInfo .GetAlias ()
816
+ remoteAlias := remoteNodeInfo .GetNode ().GetAlias ()
817
+
818
+ active := strconv .FormatBool (channel .GetActive ())
819
+ initiator := strconv .FormatBool (channel .GetInitiator ())
820
+
821
+ prometheusMetrics .channelBalanceGauge .Delete (prometheus.Labels {
822
+ "chan_id" : channelId ,
823
+ "local_node_pubkey" : localPubKey ,
824
+ "remote_node_pubkey" : remotePubKey ,
825
+ "local_node_alias" : localAlias ,
826
+ "remote_node_alias" : remoteAlias ,
827
+ "active" : active ,
828
+ "initiator" : initiator ,
829
+ })
830
+ }
831
+
772
832
// Gets the info from the node which we have the macaroon
773
833
func getInfo (lightningClient lnrpc.LightningClient , context context.Context ) (lnrpc.GetInfoResponse , error ) {
774
834
0 commit comments