|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/lightninglabs/lndclient" |
| 9 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 10 | + "github.com/lightningnetwork/lnd/lnrpc/routerrpc" |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // totalPayments tracks the total number of payments initiated, labeled |
| 16 | + // by final payment status. This permits computation of both throughput |
| 17 | + // and success/failure rates. |
| 18 | + totalPayments = prometheus.NewCounterVec( |
| 19 | + prometheus.CounterOpts{ |
| 20 | + Name: "lnd_total_payments", |
| 21 | + Help: "Total number of payments initiated, labeled by final status", |
| 22 | + }, |
| 23 | + []string{"status"}, |
| 24 | + ) |
| 25 | + |
| 26 | + // totalHTLCAttempts is a simple counter which, in combination with the |
| 27 | + // payment counter, permits tracking the number of attempts per payment. |
| 28 | + totalHTLCAttempts = prometheus.NewCounter( |
| 29 | + prometheus.CounterOpts{ |
| 30 | + Name: "lnd_total_htlc_attempts", |
| 31 | + Help: "Total number of HTLC attempts across all payments", |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + // paymentAttempts is a histogram for visualizing what portion of |
| 36 | + // payments complete within a given number of attempts. |
| 37 | + paymentAttempts = prometheus.NewHistogram( |
| 38 | + prometheus.HistogramOpts{ |
| 39 | + Name: "lnd_payment_attempts_per_payment", |
| 40 | + Help: "Histogram tracking the number of attempts per payment", |
| 41 | + Buckets: prometheus.ExponentialBucketsRange(1, 2, 10), |
| 42 | + }, |
| 43 | + ) |
| 44 | +) |
| 45 | + |
| 46 | +// paymentsMonitor listens for payments and updates Prometheus metrics. |
| 47 | +type paymentsMonitor struct { |
| 48 | + client routerrpc.RouterClient |
| 49 | + |
| 50 | + lnd *lndclient.LndServices |
| 51 | + |
| 52 | + errChan chan error |
| 53 | + |
| 54 | + // quit is closed to signal that we need to shutdown. |
| 55 | + quit chan struct{} |
| 56 | + |
| 57 | + wg sync.WaitGroup |
| 58 | +} |
| 59 | + |
| 60 | +// newPaymentsMonitor creates a new payments monitor and ensures the context |
| 61 | +// includes macaroon authentication. |
| 62 | +func newPaymentsMonitor(lnd *lndclient.LndServices, |
| 63 | + errChan chan error) (*paymentsMonitor, error) { |
| 64 | + |
| 65 | + return &paymentsMonitor{ |
| 66 | + client: routerrpc.NewRouterClient(lnd.ClientConn), |
| 67 | + lnd: lnd, |
| 68 | + errChan: errChan, |
| 69 | + quit: make(chan struct{}), |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +// start subscribes to `TrackPayments` and updates Prometheus metrics. |
| 74 | +func (p *paymentsMonitor) start() error { |
| 75 | + paymentLogger.Info("Starting payments monitor...") |
| 76 | + |
| 77 | + // Attach macaroon authentication for the router service. |
| 78 | + ctx, cancel := context.WithCancel(context.Background()) |
| 79 | + ctx, err := p.lnd.WithMacaroonAuthForService( |
| 80 | + ctx, lndclient.RouterServiceMac, |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + cancel() |
| 84 | + |
| 85 | + return fmt.Errorf("failed to get macaroon-authenticated "+ |
| 86 | + "context: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + stream, err := p.client.TrackPayments( |
| 90 | + ctx, &routerrpc.TrackPaymentsRequest{ |
| 91 | + // NOTE: We only need to know the final result of the |
| 92 | + // payment and all attempts. |
| 93 | + NoInflightUpdates: true, |
| 94 | + }, |
| 95 | + ) |
| 96 | + if err != nil { |
| 97 | + paymentLogger.Errorf("Failed to subscribe to TrackPayments: %v", |
| 98 | + err) |
| 99 | + |
| 100 | + cancel() |
| 101 | + |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + p.wg.Add(1) |
| 106 | + go func() { |
| 107 | + defer func() { |
| 108 | + cancel() |
| 109 | + p.wg.Done() |
| 110 | + }() |
| 111 | + |
| 112 | + for { |
| 113 | + select { |
| 114 | + case <-p.quit: |
| 115 | + return |
| 116 | + |
| 117 | + default: |
| 118 | + payment, err := stream.Recv() |
| 119 | + if err != nil { |
| 120 | + paymentLogger.Errorf("Error receiving "+ |
| 121 | + "payment update: %v", err) |
| 122 | + |
| 123 | + p.errChan <- err |
| 124 | + return |
| 125 | + } |
| 126 | + processPaymentUpdate(payment) |
| 127 | + } |
| 128 | + } |
| 129 | + }() |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// stop cancels the payments monitor subscription. |
| 135 | +func (p *paymentsMonitor) stop() { |
| 136 | + paymentLogger.Info("Stopping payments monitor...") |
| 137 | + |
| 138 | + close(p.quit) |
| 139 | + p.wg.Wait() |
| 140 | +} |
| 141 | + |
| 142 | +// collectors returns all of the collectors that the htlc monitor uses. |
| 143 | +func (p *paymentsMonitor) collectors() []prometheus.Collector { |
| 144 | + return []prometheus.Collector{ |
| 145 | + totalPayments, totalHTLCAttempts, paymentAttempts, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// processPaymentUpdate updates Prometheus metrics based on received payments. |
| 150 | +// |
| 151 | +// NOTE: It is expected that this receive the *final* payment update with the |
| 152 | +// complete list of all htlc attempts made for this payment. |
| 153 | +func processPaymentUpdate(payment *lnrpc.Payment) { |
| 154 | + var status string |
| 155 | + |
| 156 | + switch payment.Status { |
| 157 | + case lnrpc.Payment_SUCCEEDED: |
| 158 | + status = "succeeded" |
| 159 | + case lnrpc.Payment_FAILED: |
| 160 | + status = "failed" |
| 161 | + default: |
| 162 | + // We don't expect this given that this should be a terminal |
| 163 | + // payment update. |
| 164 | + status = "unknown" |
| 165 | + } |
| 166 | + |
| 167 | + totalPayments.WithLabelValues(status).Inc() |
| 168 | + attemptCount := len(payment.Htlcs) |
| 169 | + |
| 170 | + totalHTLCAttempts.Add(float64(attemptCount)) |
| 171 | + paymentAttempts.Observe(float64(attemptCount)) |
| 172 | + |
| 173 | + paymentLogger.Debugf("Payment %s updated: status=%s, %d attempts", |
| 174 | + payment.PaymentHash, status, attemptCount) |
| 175 | +} |
0 commit comments