-
Notifications
You must be signed in to change notification settings - Fork 51
collectors: collect payment and attempt counts #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Roasbeef
merged 2 commits into
lightninglabs:master
from
calvinrzachman:payment-collector
Apr 10, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package collectors | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/lightninglabs/lndclient" | ||
| "github.com/lightningnetwork/lnd/lnrpc" | ||
| "github.com/lightningnetwork/lnd/lnrpc/routerrpc" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| var ( | ||
| // totalPayments tracks the total number of payments initiated, labeled | ||
| // by final payment status. This permits computation of both throughput | ||
| // and success/failure rates. | ||
| totalPayments = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: "lnd_total_payments", | ||
| Help: "Total number of payments initiated, labeled by final status", | ||
| }, | ||
| []string{"status"}, | ||
| ) | ||
|
|
||
| // totalHTLCAttempts is a simple counter which, in combination with the | ||
| // payment counter, permits tracking the number of attempts per payment. | ||
| totalHTLCAttempts = prometheus.NewCounter( | ||
| prometheus.CounterOpts{ | ||
| Name: "lnd_total_htlc_attempts", | ||
| Help: "Total number of HTLC attempts across all payments", | ||
| }, | ||
| ) | ||
|
|
||
| // paymentAttempts is a histogram for visualizing what portion of | ||
| // payments complete within a given number of attempts. | ||
| paymentAttempts = prometheus.NewHistogram( | ||
| prometheus.HistogramOpts{ | ||
| Name: "lnd_payment_attempts_per_payment", | ||
| Help: "Histogram tracking the number of attempts per payment", | ||
| Buckets: prometheus.ExponentialBuckets(1, 2, 10), | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| // paymentsMonitor listens for payments and updates Prometheus metrics. | ||
| type paymentsMonitor struct { | ||
| client routerrpc.RouterClient | ||
|
|
||
| lnd *lndclient.LndServices | ||
|
|
||
| errChan chan error | ||
|
|
||
| // quit is closed to signal that we need to shutdown. | ||
| quit chan struct{} | ||
|
|
||
| wg sync.WaitGroup | ||
| } | ||
|
|
||
| // newPaymentsMonitor creates a new payments monitor and ensures the context | ||
| // includes macaroon authentication. | ||
| func newPaymentsMonitor(lnd *lndclient.LndServices, | ||
| errChan chan error) *paymentsMonitor { | ||
|
|
||
| return &paymentsMonitor{ | ||
| client: routerrpc.NewRouterClient(lnd.ClientConn), | ||
| lnd: lnd, | ||
| errChan: errChan, | ||
| quit: make(chan struct{}), | ||
| } | ||
| } | ||
|
|
||
| // start subscribes to `TrackPayments` and updates Prometheus metrics. | ||
| func (p *paymentsMonitor) start() error { | ||
| paymentLogger.Info("Starting payments monitor...") | ||
|
|
||
| // Attach macaroon authentication for the router service. | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| ctx, err := p.lnd.WithMacaroonAuthForService( | ||
| ctx, lndclient.RouterServiceMac, | ||
| ) | ||
| if err != nil { | ||
| cancel() | ||
|
|
||
| return fmt.Errorf("failed to get macaroon-authenticated "+ | ||
| "context: %w", err) | ||
| } | ||
|
|
||
| stream, err := p.client.TrackPayments( | ||
| ctx, &routerrpc.TrackPaymentsRequest{ | ||
| // NOTE: We only need to know the final result of the | ||
| // payment and all attempts. | ||
| NoInflightUpdates: true, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| paymentLogger.Errorf("Failed to subscribe to TrackPayments: %v", | ||
| err) | ||
|
|
||
| cancel() | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| p.wg.Add(1) | ||
| go func() { | ||
| defer func() { | ||
| cancel() | ||
| p.wg.Done() | ||
| }() | ||
|
|
||
| for { | ||
| select { | ||
| case <-p.quit: | ||
| return | ||
|
|
||
| default: | ||
| payment, err := stream.Recv() | ||
| if err != nil { | ||
| paymentLogger.Errorf("Error receiving "+ | ||
| "payment update: %v", err) | ||
|
|
||
| p.errChan <- err | ||
| return | ||
| } | ||
| processPaymentUpdate(payment) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // stop cancels the payments monitor subscription. | ||
| func (p *paymentsMonitor) stop() { | ||
| paymentLogger.Info("Stopping payments monitor...") | ||
|
|
||
| close(p.quit) | ||
| p.wg.Wait() | ||
| } | ||
|
|
||
| // collectors returns all of the collectors that the htlc monitor uses. | ||
| func (p *paymentsMonitor) collectors() []prometheus.Collector { | ||
| return []prometheus.Collector{ | ||
| totalPayments, totalHTLCAttempts, paymentAttempts, | ||
| } | ||
| } | ||
|
|
||
| // processPaymentUpdate updates Prometheus metrics based on received payments. | ||
| // | ||
| // NOTE: It is expected that this receive the *final* payment update with the | ||
| // complete list of all htlc attempts made for this payment. | ||
| func processPaymentUpdate(payment *lnrpc.Payment) { | ||
| var status string | ||
|
|
||
| switch payment.Status { | ||
| case lnrpc.Payment_SUCCEEDED: | ||
| status = "succeeded" | ||
| case lnrpc.Payment_FAILED: | ||
| status = "failed" | ||
| default: | ||
| // We don't expect this given that this should be a terminal | ||
| // payment update. | ||
| status = "unknown" | ||
| } | ||
|
|
||
| totalPayments.WithLabelValues(status).Inc() | ||
| attemptCount := len(payment.Htlcs) | ||
|
|
||
| totalHTLCAttempts.Add(float64(attemptCount)) | ||
| paymentAttempts.Observe(float64(attemptCount)) | ||
|
|
||
| paymentLogger.Debugf("Payment %s updated: status=%s, %d attempts", | ||
| payment.PaymentHash, status, attemptCount) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I don't think this can quite be used as a proxy for attempts. For that we'd need to watch a payment over time, and increment this counter with each attempt.
We may also want to introspect into the payment state itself: https://lightning.engineering/api-docs/api/lnd/router/track-payment-v2/#lnrpcpaymentpaymentstatus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double checked, and I think I'm actually wrong about this. We get a new element here for each new attempt, as it isn't just the set of final HTLCs that were settled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prior to learning about this
TrackPaymentsRPC, I had created a draft PR to add these metrics as real time counters in lnd directly. Implemented there, the ChannelRouter can increment counters as each attempt is registered. My hope here was that supplying theNoInflightUpdatesdirective to theTrackPaymentscall will give us only the final payment update (settle or fail) from which we can make an accurate determination of how many total attempts were made.