-
Notifications
You must be signed in to change notification settings - Fork 18
DF-21989: OCR2 transmit outcome metrics (TXM v1/v2) #398
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
Draft
cl-efornaciari
wants to merge
3
commits into
smartcontractkit:develop
Choose a base branch
from
cl-efornaciari:feature/DF-21989/ocr2-transmit-metrics
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
472b479
feat(txm): add OCR2 transmit outcome and pending metrics (DF-21989)
cl-efornaciari c8651a4
chore(metrics): dual-export OCR2 transmit metrics via OpenTelemetry
cl-efornaciari 688fdf5
refactor(metrics): add OCR2TransmitMetrics type for Prom + OTel
cl-efornaciari 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package ocr2transmit | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/big" | ||
| "sync" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/metric" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/beholder" | ||
| ) | ||
|
|
||
| var ( | ||
| promTransmitConfirmed = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "ocr2_transmit_tx_confirmed_total", | ||
| Help: "OCR2 aggregator transmit transactions that received a successful on-chain receipt.", | ||
| }, []string{"chain_id", "contract_address", "from_address"}) | ||
| promTransmitReverted = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "ocr2_transmit_tx_reverted_total", | ||
| Help: "OCR2 aggregator transmit transactions that were included but reverted.", | ||
| }, []string{"chain_id", "contract_address", "from_address"}) | ||
| promTransmitFatal = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "ocr2_transmit_tx_fatal_total", | ||
| Help: "OCR2 aggregator transmit transactions marked fatally errored by TXM (e.g. could not get receipt).", | ||
| }, []string{"chain_id", "contract_address", "from_address"}) | ||
| promTransmitUnconfirmed = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "ocr2_transmit_unconfirmed_tx_count", | ||
| Help: "Count of unconfirmed in-memory transactions whose calldata is an OCR2 transmit call (TXM v2 per from_address).", | ||
| }, []string{"chain_id", "from_address"}) | ||
| ) | ||
|
|
||
| // OCR2TransmitMetrics records OCR2 transmit outcomes to Prometheus and OpenTelemetry (Beholder), following the same pattern as txmMetrics (package-level Prom vecs, OTel instruments on the metrics type). | ||
| type OCR2TransmitMetrics struct { | ||
| promConfirmed *prometheus.CounterVec | ||
| promReverted *prometheus.CounterVec | ||
| promFatal *prometheus.CounterVec | ||
| promUnconfirmed *prometheus.GaugeVec | ||
|
|
||
| otelConfirmed metric.Int64Counter | ||
| otelReverted metric.Int64Counter | ||
| otelFatal metric.Int64Counter | ||
| otelUnconfirmed metric.Int64Gauge | ||
| } | ||
|
|
||
| var ( | ||
| ocr2TransmitMetrics *OCR2TransmitMetrics | ||
| ocr2TransmitMetricsOnce sync.Once | ||
| ) | ||
|
|
||
| func newOCR2TransmitMetrics() *OCR2TransmitMetrics { | ||
| m := &OCR2TransmitMetrics{ | ||
| promConfirmed: promTransmitConfirmed, | ||
| promReverted: promTransmitReverted, | ||
| promFatal: promTransmitFatal, | ||
| promUnconfirmed: promTransmitUnconfirmed, | ||
| } | ||
| meter := beholder.GetMeter() | ||
| if c, err := meter.Int64Counter("ocr2_transmit_tx_confirmed_total"); err == nil { | ||
| m.otelConfirmed = c | ||
| } | ||
| if c, err := meter.Int64Counter("ocr2_transmit_tx_reverted_total"); err == nil { | ||
| m.otelReverted = c | ||
| } | ||
| if c, err := meter.Int64Counter("ocr2_transmit_tx_fatal_total"); err == nil { | ||
| m.otelFatal = c | ||
| } | ||
| if g, err := meter.Int64Gauge("ocr2_transmit_unconfirmed_tx_count"); err == nil { | ||
| m.otelUnconfirmed = g | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| func ocr2TransmitMetricsInstance() *OCR2TransmitMetrics { | ||
| ocr2TransmitMetricsOnce.Do(func() { | ||
| ocr2TransmitMetrics = newOCR2TransmitMetrics() | ||
| }) | ||
| return ocr2TransmitMetrics | ||
| } | ||
|
|
||
| func transmitAttrs(chainID *big.Int, contract, from string) metric.MeasurementOption { | ||
| return metric.WithAttributes( | ||
| attribute.String("chain_id", chainID.String()), | ||
| attribute.String("contract_address", contract), | ||
| attribute.String("from_address", from), | ||
| ) | ||
| } | ||
|
|
||
| func unconfirmedAttrs(chainID *big.Int, from string) metric.MeasurementOption { | ||
| return metric.WithAttributes( | ||
| attribute.String("chain_id", chainID.String()), | ||
| attribute.String("from_address", from), | ||
| ) | ||
| } | ||
|
|
||
| // RecordOutcome increments confirmed / reverted / fatal counters when calldata matches OCR2 transmit. | ||
| func (m *OCR2TransmitMetrics) RecordOutcome(ctx context.Context, chainID *big.Int, from, to common.Address, encodedPayload []byte, fwdrDest *common.Address, outcome string) { | ||
| if chainID == nil || !IsTransmitCalldata(encodedPayload) { | ||
| return | ||
| } | ||
| contract := ContractLabel(to, fwdrDest) | ||
| labels := []string{chainID.String(), contract, from.Hex()} | ||
| opts := transmitAttrs(chainID, contract, from.Hex()) | ||
| switch outcome { | ||
| case "confirmed": | ||
| m.promConfirmed.WithLabelValues(labels...).Inc() | ||
| if m.otelConfirmed != nil { | ||
| m.otelConfirmed.Add(ctx, 1, opts) | ||
| } | ||
| case "reverted": | ||
| m.promReverted.WithLabelValues(labels...).Inc() | ||
| if m.otelReverted != nil { | ||
| m.otelReverted.Add(ctx, 1, opts) | ||
| } | ||
| case "fatal": | ||
| m.promFatal.WithLabelValues(labels...).Inc() | ||
| if m.otelFatal != nil { | ||
| m.otelFatal.Add(ctx, 1, opts) | ||
| } | ||
| default: | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // SetUnconfirmedGauge sets the gauge for OCR2-shaped unconfirmed txs for TXM v2 (optional / phase 3). | ||
| func (m *OCR2TransmitMetrics) SetUnconfirmedGauge(ctx context.Context, chainID *big.Int, from common.Address, n int) { | ||
| if chainID == nil { | ||
| return | ||
| } | ||
| m.promUnconfirmed.WithLabelValues(chainID.String(), from.Hex()).Set(float64(n)) | ||
| if m.otelUnconfirmed != nil { | ||
| m.otelUnconfirmed.Record(ctx, int64(n), unconfirmedAttrs(chainID, from.Hex())) | ||
| } | ||
| } | ||
|
|
||
| // RecordOutcome increments confirmed / reverted / fatal counters when calldata matches OCR2 transmit. | ||
| func RecordOutcome(ctx context.Context, chainID *big.Int, from, to common.Address, encodedPayload []byte, fwdrDest *common.Address, outcome string) { | ||
| ocr2TransmitMetricsInstance().RecordOutcome(ctx, chainID, from, to, encodedPayload, fwdrDest, outcome) | ||
| } | ||
|
|
||
| // SetUnconfirmedGauge sets the gauge for OCR2-shaped unconfirmed txs for TXM v2 (optional / phase 3). | ||
| func SetUnconfirmedGauge(ctx context.Context, chainID *big.Int, from common.Address, n int) { | ||
| ocr2TransmitMetricsInstance().SetUnconfirmedGauge(ctx, chainID, from, n) | ||
| } | ||
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,47 @@ | ||
| // Package ocr2transmit detects OCR2 aggregator transmit calldata for metrics (DF-22761 / DF-22643). | ||
| package ocr2transmit | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
|
|
||
| "github.com/smartcontractkit/libocr/gethwrappers2/ocr2aggregator" | ||
| ) | ||
|
|
||
| var ( | ||
| transmitMethodID []byte | ||
| transmitMethodIDOnce sync.Once | ||
| ) | ||
|
|
||
| func transmitSig() []byte { | ||
| transmitMethodIDOnce.Do(func() { | ||
| parsed, err := ocr2aggregator.OCR2AggregatorMetaData.GetAbi() | ||
| if err != nil { | ||
| return | ||
| } | ||
| m, ok := parsed.Methods["transmit"] | ||
| if !ok { | ||
| return | ||
| } | ||
| transmitMethodID = m.ID | ||
| }) | ||
| return transmitMethodID | ||
| } | ||
|
|
||
| // IsTransmitCalldata returns true if data begins with the OCR2Aggregator transmit function selector. | ||
| func IsTransmitCalldata(data []byte) bool { | ||
| sig := transmitSig() | ||
| if len(sig) != 4 || len(data) < 4 { | ||
| return false | ||
| } | ||
| return data[0] == sig[0] && data[1] == sig[1] && data[2] == sig[2] && data[3] == sig[3] | ||
| } | ||
|
|
||
| // ContractLabel returns the logical aggregator address for metrics: meta forwarder dest if set, else to address. | ||
| func ContractLabel(to common.Address, fwdrDest *common.Address) string { | ||
| if fwdrDest != nil && *fwdrDest != (common.Address{}) { | ||
| return fwdrDest.Hex() | ||
| } | ||
| return to.Hex() | ||
| } |
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,31 @@ | ||
| package ocr2transmit | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/libocr/gethwrappers2/ocr2aggregator" | ||
| ) | ||
|
|
||
| func TestIsTransmitCalldata(t *testing.T) { | ||
| parsed, err := ocr2aggregator.OCR2AggregatorMetaData.GetAbi() | ||
| require.NoError(t, err) | ||
| m := parsed.Methods["transmit"] | ||
| require.NotNil(t, m) | ||
|
|
||
| // Minimal non-empty args for selector match only (Pack may fail on full args); we only need first 4 bytes. | ||
| data := append([]byte{}, m.ID...) | ||
| require.True(t, IsTransmitCalldata(data)) | ||
|
|
||
| require.False(t, IsTransmitCalldata([]byte{1, 2, 3})) | ||
| require.False(t, IsTransmitCalldata(nil)) | ||
| } | ||
|
|
||
| func TestContractLabel(t *testing.T) { | ||
| to := common.HexToAddress("0x0000000000000000000000000000000000000001") | ||
| dest := common.HexToAddress("0x0000000000000000000000000000000000000002") | ||
| require.Equal(t, dest.Hex(), ContractLabel(to, &dest)) | ||
| require.Equal(t, to.Hex(), ContractLabel(to, nil)) | ||
| } |
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
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,25 @@ | ||
| package txmgr | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-evm/pkg/ocr2transmit" | ||
| ) | ||
|
|
||
| func recordOCR2TransmitOutcomeV1(ctx context.Context, chainID *big.Int, tx *Tx, outcome string) { | ||
| if tx == nil { | ||
| return | ||
| } | ||
| var fwdr *common.Address | ||
| if meta, err := tx.GetMeta(); err == nil && meta != nil && meta.FwdrDestAddress != nil { | ||
| fwdr = meta.FwdrDestAddress | ||
| } | ||
| cid := chainID | ||
| if cid == nil { | ||
| cid = tx.ChainID | ||
| } | ||
| ocr2transmit.RecordOutcome(ctx, cid, tx.FromAddress, tx.ToAddress, tx.EncodedPayload, fwdr, outcome) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Please use otel metrics instead of (or next to) prom metrics, then they will be ingested by Beholder and NOP metrics will be visible in Grafana