-
Notifications
You must be signed in to change notification settings - Fork 2k
DF-21989: OCR2 on-chain view gauges (median relay) #21684
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/onchain-view-gauges
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
86a737f
feat(relay): add OCR2 on-chain view gauges for median (DF-21989)
cl-efornaciari af1cd71
chore(metrics): dual-export on-chain OCR2 gauges via OpenTelemetry
cl-efornaciari 1217f59
refactor(metrics): structure median on-chain view metrics like txmMet…
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
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,131 @@ | ||
| package evm | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/big" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "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" | ||
| ) | ||
|
|
||
| // Prometheus gauges for the node's view of on-chain OCR2 median aggregator state (DF-22676). | ||
| // latest_answer is exported as float64; values above ~2^53 may lose integer precision (documented in HELP). | ||
| var ( | ||
| promOCR2OnchainLatestAnswer = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "ocr2_onchain_transmission_latest_answer", | ||
| Help: "Latest median answer from the node's on-chain read of LatestTransmissionDetails (OCR2 aggregator). Float64 may not represent full int256 precision.", | ||
| }, []string{"chain_id", "contract_address", "transmitter_id"}) | ||
| promOCR2OnchainLatestTimestampUnix = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "ocr2_onchain_transmission_latest_timestamp_unix", | ||
| Help: "Unix seconds of latest on-chain transmission timestamp from the node's LatestTransmissionDetails read.", | ||
| }, []string{"chain_id", "contract_address", "transmitter_id"}) | ||
| promOCR2OnchainEpoch = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "ocr2_onchain_transmission_epoch", | ||
| Help: "Epoch from the node's on-chain LatestTransmissionDetails read.", | ||
| }, []string{"chain_id", "contract_address", "transmitter_id"}) | ||
| promOCR2OnchainRound = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
| Name: "ocr2_onchain_transmission_round", | ||
| Help: "Round from the node's on-chain LatestTransmissionDetails read.", | ||
| }, []string{"chain_id", "contract_address", "transmitter_id"}) | ||
| ) | ||
|
|
||
| var ( | ||
| medianOnchainOtelOnce sync.Once | ||
| medianOtelLatestAnswer metric.Float64Gauge | ||
| medianOtelLatestTimestampUnix metric.Float64Gauge | ||
| medianOtelEpoch metric.Int64Gauge | ||
| medianOtelRound metric.Int64Gauge | ||
| ) | ||
|
|
||
| func initMedianOnchainOtel() { | ||
| medianOnchainOtelOnce.Do(func() { | ||
| meter := beholder.GetMeter() | ||
| la, err := meter.Float64Gauge("ocr2_onchain_transmission_latest_answer") | ||
| if err != nil { | ||
| return | ||
| } | ||
| ltu, err := meter.Float64Gauge("ocr2_onchain_transmission_latest_timestamp_unix") | ||
| if err != nil { | ||
| return | ||
| } | ||
| e, err := meter.Int64Gauge("ocr2_onchain_transmission_epoch") | ||
| if err != nil { | ||
| return | ||
| } | ||
| r, err := meter.Int64Gauge("ocr2_onchain_transmission_round") | ||
| if err != nil { | ||
| return | ||
| } | ||
| medianOtelLatestAnswer = la | ||
| medianOtelLatestTimestampUnix = ltu | ||
| medianOtelEpoch = e | ||
| medianOtelRound = r | ||
| }) | ||
| } | ||
|
|
||
| // medianOnchainViewMetrics records the same on-chain OCR2 view series to Prometheus and OpenTelemetry (Beholder), matching the txmMetrics pattern (package-level Prom vecs, OTel instruments on the metrics type). | ||
| type medianOnchainViewMetrics struct { | ||
| chainID string | ||
| contract string | ||
| transmitterID string | ||
|
|
||
| latestAnswer metric.Float64Gauge | ||
| latestTimestampUnix metric.Float64Gauge | ||
| epoch metric.Int64Gauge | ||
| round metric.Int64Gauge | ||
| } | ||
|
|
||
| func newMedianOnchainViewMetrics(chainID, contractAddress, transmitterID string) *medianOnchainViewMetrics { | ||
| if transmitterID == "" { | ||
| transmitterID = "unknown" | ||
| } | ||
| initMedianOnchainOtel() | ||
| return &medianOnchainViewMetrics{ | ||
| chainID: chainID, | ||
| contract: contractAddress, | ||
| transmitterID: transmitterID, | ||
| latestAnswer: medianOtelLatestAnswer, | ||
| latestTimestampUnix: medianOtelLatestTimestampUnix, | ||
| epoch: medianOtelEpoch, | ||
| round: medianOtelRound, | ||
| } | ||
| } | ||
|
|
||
| func (m *medianOnchainViewMetrics) onchainAttrs() metric.MeasurementOption { | ||
| return metric.WithAttributes( | ||
| attribute.String("chain_id", m.chainID), | ||
| attribute.String("contract_address", m.contract), | ||
| attribute.String("transmitter_id", m.transmitterID), | ||
| ) | ||
| } | ||
|
|
||
| func (m *medianOnchainViewMetrics) record(ctx context.Context, latestAnswer *big.Int, epoch uint32, round uint8, updatedAt time.Time) { | ||
| lv := bigIntToPromGaugeValue(latestAnswer) | ||
| promOCR2OnchainLatestAnswer.WithLabelValues(m.chainID, m.contract, m.transmitterID).Set(lv) | ||
| promOCR2OnchainLatestTimestampUnix.WithLabelValues(m.chainID, m.contract, m.transmitterID).Set(float64(updatedAt.Unix())) | ||
| promOCR2OnchainEpoch.WithLabelValues(m.chainID, m.contract, m.transmitterID).Set(float64(epoch)) | ||
| promOCR2OnchainRound.WithLabelValues(m.chainID, m.contract, m.transmitterID).Set(float64(round)) | ||
|
|
||
| if m.latestAnswer == nil { | ||
| return | ||
| } | ||
| opts := m.onchainAttrs() | ||
| m.latestAnswer.Record(ctx, lv, opts) | ||
| m.latestTimestampUnix.Record(ctx, float64(updatedAt.Unix()), opts) | ||
| m.epoch.Record(ctx, int64(epoch), opts) | ||
| m.round.Record(ctx, int64(round), opts) | ||
| } | ||
|
|
||
| func bigIntToPromGaugeValue(i *big.Int) float64 { | ||
| if i == nil { | ||
| return 0 | ||
| } | ||
| f, _ := new(big.Float).SetInt(i).Float64() | ||
| return f | ||
| } | ||
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.
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.
Same as here