Skip to content

Commit

Permalink
Merge pull request #51 from Elenpay/hotfix_checking_existing_channels…
Browse files Browse the repository at this point in the history
…_before_deletion

Delete only metrics for channels that are no longer in the list
  • Loading branch information
Jossec101 authored Sep 14, 2023
2 parents b6718ee + 0cb509b commit 91ca050
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set positional-arguments
set fallback := true

fmt:
go fmt github.com/Elenpay/...
Expand Down
64 changes: 62 additions & 2 deletions liquidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,11 @@ func monitorChannels(info MonitorChannelsInfo) {

//Loop provider
loopProvider := provider.LoopProvider{}

prevChannels := []*lnrpc.Channel{}

//Infinite loop to monitor channels
for {
prometheusMetrics.channelBalanceGauge.Reset()

//Call ListChannels method of lightning client with metadata headers
response, err := info.lightningClient.ListChannels(info.nodeCtx, &lnrpc.ListChannelsRequest{
ActiveOnly: false,
Expand All @@ -298,6 +299,22 @@ func monitorChannels(info MonitorChannelsInfo) {
continue
}

// remove channel metrics that are not in list of channels anymore
for _, channel := range prevChannels {
found := false
for _, newChannel := range response.Channels {
if channel.GetChanId() == newChannel.GetChanId() {
found = true
break
}
}
if !found {
//Remove channel balance metric
deleteChannelBalanceMetric(info.nodeHost, channel, info.lightningClient, info.nodeCtx)
}
}

prevChannels = response.Channels
//TODO Support rules without nodeguard in the future

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

// Delete the channel balance metric in a prometheus gauge for a specific channel
func deleteChannelBalanceMetric(nodeHost string, channel *lnrpc.Channel, lightningClient lnrpc.LightningClient, context context.Context) {
//Start span
_, span := otel.Tracer("monitorChannel").Start(context, "deleteChannelBalanceMetric")
defer span.End()

channelId := fmt.Sprint(channel.GetChanId())

localNodeInfo, err := getInfo(lightningClient, context)

if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
log.WithField("span", span).Errorf("error getting local node info: %v", err)
}

remoteNodeInfo, err := getNodeInfo(channel.RemotePubkey, lightningClient, context)

if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
log.WithField("span", span).Errorf("error getting remote node info: %v", err)
}

localPubKey := localNodeInfo.GetIdentityPubkey()
remotePubKey := channel.GetRemotePubkey()
localAlias := localNodeInfo.GetAlias()
remoteAlias := remoteNodeInfo.GetNode().GetAlias()

active := strconv.FormatBool(channel.GetActive())
initiator := strconv.FormatBool(channel.GetInitiator())

prometheusMetrics.channelBalanceGauge.Delete(prometheus.Labels{
"chan_id": channelId,
"local_node_pubkey": localPubKey,
"remote_node_pubkey": remotePubKey,
"local_node_alias": localAlias,
"remote_node_alias": remoteAlias,
"active": active,
"initiator": initiator,
})
}

// Gets the info from the node which we have the macaroon
func getInfo(lightningClient lnrpc.LightningClient, context context.Context) (lnrpc.GetInfoResponse, error) {

Expand Down

0 comments on commit 91ca050

Please sign in to comment.