Skip to content

Commit 91ca050

Browse files
authored
Merge pull request #51 from Elenpay/hotfix_checking_existing_channels_before_deletion
Delete only metrics for channels that are no longer in the list
2 parents b6718ee + 0cb509b commit 91ca050

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

.justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set positional-arguments
2+
set fallback := true
23

34
fmt:
45
go fmt github.com/Elenpay/...

liquidator.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,11 @@ func monitorChannels(info MonitorChannelsInfo) {
277277

278278
//Loop provider
279279
loopProvider := provider.LoopProvider{}
280+
281+
prevChannels := []*lnrpc.Channel{}
282+
280283
//Infinite loop to monitor channels
281284
for {
282-
prometheusMetrics.channelBalanceGauge.Reset()
283-
284285
//Call ListChannels method of lightning client with metadata headers
285286
response, err := info.lightningClient.ListChannels(info.nodeCtx, &lnrpc.ListChannelsRequest{
286287
ActiveOnly: false,
@@ -298,6 +299,22 @@ func monitorChannels(info MonitorChannelsInfo) {
298299
continue
299300
}
300301

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
301318
//TODO Support rules without nodeguard in the future
302319

303320
//Get liquidation rule from cache
@@ -769,6 +786,49 @@ func recordChannelBalanceMetric(nodeHost string, channel *lnrpc.Channel, channel
769786
}).Set(channelBalanceRatio)
770787
}
771788

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+
772832
// Gets the info from the node which we have the macaroon
773833
func getInfo(lightningClient lnrpc.LightningClient, context context.Context) (lnrpc.GetInfoResponse, error) {
774834

0 commit comments

Comments
 (0)