|
| 1 | +package txm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 9 | + "go.opentelemetry.io/otel/attribute" |
| 10 | + "go.opentelemetry.io/otel/metric" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/chainlink-common/pkg/beholder" |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/metrics" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + // Successful transactions |
| 18 | + promTonTxmSuccessTxs = promauto.NewCounterVec(prometheus.CounterOpts{ |
| 19 | + Name: "ton_txm_tx_success", |
| 20 | + Help: "Number of finalized transactions that are included and successfully executed on chain", |
| 21 | + }, []string{"chainID"}) |
| 22 | + promTonTxmFinalizedTxs = promauto.NewCounterVec(prometheus.CounterOpts{ |
| 23 | + Name: "ton_txm_tx_finalized", |
| 24 | + Help: "Number of transactions that are finalized on chain. Can include both successful and reverted txs", |
| 25 | + }, []string{"chainID"}) |
| 26 | + |
| 27 | + // Inflight transactions |
| 28 | + promTonTxmPendingTxs = promauto.NewGaugeVec(prometheus.GaugeOpts{ |
| 29 | + Name: "ton_txm_tx_pending", |
| 30 | + Help: "Number of transactions that are pending confirmation", |
| 31 | + }, []string{"chainID"}) |
| 32 | + |
| 33 | + // Error cases |
| 34 | + promTonTxmFailedToBroadcastTxs = promauto.NewCounterVec(prometheus.CounterOpts{ |
| 35 | + Name: "ton_txm_tx_error_broadcast", |
| 36 | + Help: "Number of transactions that failed to be broadcasted even after all retries", |
| 37 | + }, []string{"chainID"}) |
| 38 | + promTonTxmRevertTxs = promauto.NewCounterVec(prometheus.CounterOpts{ |
| 39 | + Name: "ton_txm_tx_error_revert", |
| 40 | + Help: "Number of finalized transactions that are included and failed onchain", |
| 41 | + }, []string{"chainID"}) |
| 42 | +) |
| 43 | + |
| 44 | +type tonTxmMetrics struct { |
| 45 | + metrics.Labeler |
| 46 | + chainID string |
| 47 | + |
| 48 | + // successful transactions |
| 49 | + successTxs metric.Int64Counter |
| 50 | + finalizedTxs metric.Int64Counter |
| 51 | + |
| 52 | + // inflight transactions |
| 53 | + pendingTxs metric.Int64Gauge |
| 54 | + |
| 55 | + // error cases |
| 56 | + failedToBroadcastTxs metric.Int64Counter |
| 57 | + revertTxs metric.Int64Counter |
| 58 | +} |
| 59 | + |
| 60 | +func newTonTxmMetrics(chainID string) (*tonTxmMetrics, error) { |
| 61 | + m := beholder.GetMeter() |
| 62 | + var err error |
| 63 | + |
| 64 | + successTxs, err := m.Int64Counter("ton_txm_tx_success") |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to register ton success txs: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + finalizedTxs, err := m.Int64Counter("ton_txm_tx_finalized") |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("failed to register ton finalized txs: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + pendingTxs, err := m.Int64Gauge("ton_txm_tx_pending") |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to register ton pending txs: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + failedToBroadcastTxs, err := m.Int64Counter("ton_txm_tx_error_broadcast") |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("failed to register ton failed to broadcast txs: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + revertTxs, err := m.Int64Counter("ton_txm_tx_error_revert") |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to register ton revert txs: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return &tonTxmMetrics{ |
| 90 | + chainID: chainID, |
| 91 | + Labeler: metrics.NewLabeler().With("chainID", chainID), |
| 92 | + |
| 93 | + successTxs: successTxs, |
| 94 | + finalizedTxs: finalizedTxs, |
| 95 | + pendingTxs: pendingTxs, |
| 96 | + |
| 97 | + failedToBroadcastTxs: failedToBroadcastTxs, |
| 98 | + revertTxs: revertTxs, |
| 99 | + }, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (m *tonTxmMetrics) GetOtelAttributes() []attribute.KeyValue { |
| 103 | + return beholder.OtelAttributes(m.Labels).AsStringAttributes() |
| 104 | +} |
| 105 | + |
| 106 | +func (m *tonTxmMetrics) IncrementSuccessTxs(ctx context.Context) { |
| 107 | + promTonTxmSuccessTxs.WithLabelValues(m.chainID).Add(1) |
| 108 | + m.successTxs.Add(ctx, 1, metric.WithAttributes(m.GetOtelAttributes()...)) |
| 109 | +} |
| 110 | + |
| 111 | +func (m *tonTxmMetrics) IncrementFinalizedTxs(ctx context.Context) { |
| 112 | + promTonTxmFinalizedTxs.WithLabelValues(m.chainID).Add(1) |
| 113 | + m.finalizedTxs.Add(ctx, 1, metric.WithAttributes(m.GetOtelAttributes()...)) |
| 114 | +} |
| 115 | + |
| 116 | +func (m *tonTxmMetrics) SetPendingTxs(ctx context.Context, count int) { |
| 117 | + promTonTxmPendingTxs.WithLabelValues(m.chainID).Set(float64(count)) |
| 118 | + m.pendingTxs.Record(ctx, int64(count), metric.WithAttributes(m.GetOtelAttributes()...)) |
| 119 | +} |
| 120 | + |
| 121 | +func (m *tonTxmMetrics) IncrementFailedToBroadcastTxs(ctx context.Context) { |
| 122 | + promTonTxmFailedToBroadcastTxs.WithLabelValues(m.chainID).Add(1) |
| 123 | + m.failedToBroadcastTxs.Add(ctx, 1, metric.WithAttributes(m.GetOtelAttributes()...)) |
| 124 | +} |
| 125 | + |
| 126 | +func (m *tonTxmMetrics) IncrementRevertTxs(ctx context.Context) { |
| 127 | + promTonTxmRevertTxs.WithLabelValues(m.chainID).Add(1) |
| 128 | + m.revertTxs.Add(ctx, 1, metric.WithAttributes(m.GetOtelAttributes()...)) |
| 129 | +} |
0 commit comments