11package ocr2transmit
22
33import (
4+ "context"
45 "math/big"
6+ "sync"
57
68 "github.com/ethereum/go-ethereum/common"
79 "github.com/prometheus/client_golang/prometheus"
810 "github.com/prometheus/client_golang/prometheus/promauto"
11+ "go.opentelemetry.io/otel/attribute"
12+ "go.opentelemetry.io/otel/metric"
13+
14+ "github.com/smartcontractkit/chainlink-common/pkg/beholder"
915)
1016
1117var (
2733 }, []string {"chain_id" , "from_address" })
2834)
2935
36+ type ocr2TransmitOtelInstruments struct {
37+ confirmed metric.Int64Counter
38+ reverted metric.Int64Counter
39+ fatal metric.Int64Counter
40+ unconfirmed metric.Int64Gauge
41+ }
42+
43+ var (
44+ ocr2TransmitOtelInst * ocr2TransmitOtelInstruments
45+ ocr2TransmitOtelOnce sync.Once
46+ )
47+
48+ func loadOCR2TransmitOtel () * ocr2TransmitOtelInstruments {
49+ ocr2TransmitOtelOnce .Do (func () {
50+ m := beholder .GetMeter ()
51+ confirmed , err := m .Int64Counter ("ocr2_transmit_tx_confirmed_total" )
52+ if err != nil {
53+ return
54+ }
55+ reverted , err := m .Int64Counter ("ocr2_transmit_tx_reverted_total" )
56+ if err != nil {
57+ return
58+ }
59+ fatal , err := m .Int64Counter ("ocr2_transmit_tx_fatal_total" )
60+ if err != nil {
61+ return
62+ }
63+ unconfirmed , err := m .Int64Gauge ("ocr2_transmit_unconfirmed_tx_count" )
64+ if err != nil {
65+ return
66+ }
67+ ocr2TransmitOtelInst = & ocr2TransmitOtelInstruments {
68+ confirmed : confirmed ,
69+ reverted : reverted ,
70+ fatal : fatal ,
71+ unconfirmed : unconfirmed ,
72+ }
73+ })
74+ return ocr2TransmitOtelInst
75+ }
76+
77+ func transmitAttrs (chainID * big.Int , contract , from string ) metric.MeasurementOption {
78+ return metric .WithAttributes (
79+ attribute .String ("chain_id" , chainID .String ()),
80+ attribute .String ("contract_address" , contract ),
81+ attribute .String ("from_address" , from ),
82+ )
83+ }
84+
85+ func unconfirmedAttrs (chainID * big.Int , from string ) metric.MeasurementOption {
86+ return metric .WithAttributes (
87+ attribute .String ("chain_id" , chainID .String ()),
88+ attribute .String ("from_address" , from ),
89+ )
90+ }
91+
3092// RecordOutcome increments confirmed / reverted / fatal counters when calldata matches OCR2 transmit.
31- func RecordOutcome (chainID * big.Int , from , to common.Address , encodedPayload []byte , fwdrDest * common.Address , outcome string ) {
93+ func RecordOutcome (ctx context. Context , chainID * big.Int , from , to common.Address , encodedPayload []byte , fwdrDest * common.Address , outcome string ) {
3294 if chainID == nil || ! IsTransmitCalldata (encodedPayload ) {
3395 return
3496 }
@@ -41,13 +103,29 @@ func RecordOutcome(chainID *big.Int, from, to common.Address, encodedPayload []b
41103 promTransmitReverted .WithLabelValues (labels ... ).Inc ()
42104 case "fatal" :
43105 promTransmitFatal .WithLabelValues (labels ... ).Inc ()
106+ default :
107+ return
108+ }
109+ if ot := loadOCR2TransmitOtel (); ot != nil {
110+ opts := transmitAttrs (chainID , contract , from .Hex ())
111+ switch outcome {
112+ case "confirmed" :
113+ ot .confirmed .Add (ctx , 1 , opts )
114+ case "reverted" :
115+ ot .reverted .Add (ctx , 1 , opts )
116+ case "fatal" :
117+ ot .fatal .Add (ctx , 1 , opts )
118+ }
44119 }
45120}
46121
47122// SetUnconfirmedGauge sets the gauge for OCR2-shaped unconfirmed txs for TXM v2 (optional / phase 3).
48- func SetUnconfirmedGauge (chainID * big.Int , from common.Address , n int ) {
123+ func SetUnconfirmedGauge (ctx context. Context , chainID * big.Int , from common.Address , n int ) {
49124 if chainID == nil {
50125 return
51126 }
52127 promTransmitUnconfirmed .WithLabelValues (chainID .String (), from .Hex ()).Set (float64 (n ))
128+ if ot := loadOCR2TransmitOtel (); ot != nil {
129+ ot .unconfirmed .Record (ctx , int64 (n ), unconfirmedAttrs (chainID , from .Hex ()))
130+ }
53131}
0 commit comments