|
| 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 = prometheus.NewCounter( |
| 16 | + prometheus.CounterOpts{ |
| 17 | + Name: "lnd_total_payments", |
| 18 | + Help: "Total number of payments initiated", |
| 19 | + }, |
| 20 | + ) |
| 21 | + totalHTLCAttempts = prometheus.NewCounter( |
| 22 | + prometheus.CounterOpts{ |
| 23 | + Name: "lnd_total_htlc_attempts", |
| 24 | + Help: "Total number of HTLC attempts across all payments", |
| 25 | + }, |
| 26 | + ) |
| 27 | + paymentAttempts = prometheus.NewHistogram( |
| 28 | + prometheus.HistogramOpts{ |
| 29 | + Name: "lnd_payment_attempts_per_payment", |
| 30 | + Help: "Histogram tracking the number of attempts per payment", |
| 31 | + Buckets: prometheus.ExponentialBucketsRange(1, 2, 10), |
| 32 | + }, |
| 33 | + ) |
| 34 | +) |
| 35 | + |
| 36 | +// paymentsMonitor listens for payments and updates Prometheus metrics. |
| 37 | +type paymentsMonitor struct { |
| 38 | + client routerrpc.RouterClient |
| 39 | + |
| 40 | + ctx context.Context |
| 41 | + |
| 42 | + errChan chan error |
| 43 | + |
| 44 | + // quit is closed to signal that we need to shutdown. |
| 45 | + quit chan struct{} |
| 46 | + |
| 47 | + wg sync.WaitGroup |
| 48 | +} |
| 49 | + |
| 50 | +// newPaymentsMonitor creates a new payments monitor and ensures the context |
| 51 | +// includes macaroon authentication. |
| 52 | +func newPaymentsMonitor(lnd *lndclient.LndServices, |
| 53 | + errChan chan error) (*paymentsMonitor, error) { |
| 54 | + |
| 55 | + // Attach macaroon authentication for the router service. |
| 56 | + ctx := context.Background() |
| 57 | + ctx, err := lnd.WithMacaroonAuthForService( |
| 58 | + ctx, lndclient.RouterServiceMac, |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to get macaroon-authenticated "+ |
| 62 | + "context: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + return &paymentsMonitor{ |
| 66 | + client: routerrpc.NewRouterClient(lnd.ClientConn), |
| 67 | + errChan: errChan, |
| 68 | + quit: make(chan struct{}), |
| 69 | + ctx: ctx, |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +// start subscribes to `TrackPayments` and updates Prometheus metrics. |
| 74 | +func (p *paymentsMonitor) start() error { |
| 75 | + // Use the stored authenticated context. |
| 76 | + ctx, cancel := context.WithCancel(p.ctx) |
| 77 | + |
| 78 | + paymentLogger.Info("Starting payments monitor...") |
| 79 | + |
| 80 | + stream, err := p.client.TrackPayments( |
| 81 | + ctx, &routerrpc.TrackPaymentsRequest{ |
| 82 | + // NOTE: We only need to know the final result of the |
| 83 | + // payment and all attempts. |
| 84 | + NoInflightUpdates: true, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + paymentLogger.Errorf("Failed to subscribe to TrackPayments: %v", |
| 88 | + err) |
| 89 | + |
| 90 | + cancel() |
| 91 | + |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + p.wg.Add(1) |
| 96 | + go func() { |
| 97 | + defer func() { |
| 98 | + cancel() |
| 99 | + p.wg.Done() |
| 100 | + }() |
| 101 | + |
| 102 | + for { |
| 103 | + select { |
| 104 | + case <-p.quit: |
| 105 | + return |
| 106 | + |
| 107 | + default: |
| 108 | + payment, err := stream.Recv() |
| 109 | + if err != nil { |
| 110 | + paymentLogger.Errorf("Error receiving "+ |
| 111 | + "payment update: %v", err) |
| 112 | + |
| 113 | + p.errChan <- err |
| 114 | + return |
| 115 | + } |
| 116 | + processPaymentUpdate(payment) |
| 117 | + } |
| 118 | + } |
| 119 | + }() |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// stop cancels the payments monitor subscription. |
| 125 | +func (p *paymentsMonitor) stop() { |
| 126 | + paymentLogger.Info("Stopping payments monitor...") |
| 127 | + |
| 128 | + close(p.quit) |
| 129 | + p.wg.Wait() |
| 130 | +} |
| 131 | + |
| 132 | +// collectors returns all of the collectors that the htlc monitor uses. |
| 133 | +func (p *paymentsMonitor) collectors() []prometheus.Collector { |
| 134 | + return []prometheus.Collector{ |
| 135 | + totalPayments, totalHTLCAttempts, paymentAttempts, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// processPaymentUpdate updates Prometheus metrics based on received payments. |
| 140 | +// |
| 141 | +// NOTE: It is expected that this receive the *final* payment update with the |
| 142 | +// complete list of all htlc attempts made for this payment. |
| 143 | +func processPaymentUpdate(payment *lnrpc.Payment) { |
| 144 | + totalPayments.Inc() |
| 145 | + attemptCount := len(payment.Htlcs) |
| 146 | + |
| 147 | + totalHTLCAttempts.Add(float64(attemptCount)) |
| 148 | + paymentAttempts.Observe(float64(attemptCount)) |
| 149 | + |
| 150 | + paymentLogger.Debugf("Payment %s updated: %d attempts", |
| 151 | + payment.PaymentHash, attemptCount) |
| 152 | +} |
0 commit comments