-
Notifications
You must be signed in to change notification settings - Fork 0
chore: add additional metric to show time since last block #27
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,8 @@ type Metrics struct { | |
| BlockTime *prometheus.HistogramVec | ||
| // BlockTimeSummary tracks block time with percentiles over a rolling window. | ||
| BlockTimeSummary *prometheus.SummaryVec | ||
| // TimeSinceLastBlock tracks seconds since last block was received. | ||
| TimeSinceLastBlock *prometheus.GaugeVec | ||
| // BlockReceiveDelay tracks the delay between block creation and reception with histogram buckets. | ||
| BlockReceiveDelay *prometheus.HistogramVec | ||
| // JsonRpcRequestDuration tracks the duration of JSON-RPC requests to the EVM node. | ||
|
|
@@ -181,6 +183,14 @@ func NewWithRegistry(namespace string, registerer prometheus.Registerer) *Metric | |
| }, | ||
| []string{"chain_id"}, | ||
| ), | ||
| TimeSinceLastBlock: factory.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: namespace, | ||
| Name: "time_since_last_block_seconds", | ||
| Help: "seconds since last block was received", | ||
| }, | ||
| []string{"chain_id"}, | ||
| ), | ||
| BlockReceiveDelay: factory.NewHistogramVec( | ||
| prometheus.HistogramOpts{ | ||
| Namespace: namespace, | ||
|
|
@@ -554,6 +564,21 @@ func (m *Metrics) RecordBlockTime(chainID string, arrivalTime time.Time) { | |
|
|
||
| // update last seen arrival time | ||
| m.lastBlockArrivalTime[chainID] = arrivalTime | ||
| // reset time since last block to 0 | ||
| m.TimeSinceLastBlock.WithLabelValues(chainID).Set(0) | ||
|
Comment on lines
566
to
+568
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a potential issue here with out-of-order block arrivals (based on A full refactoring of the func (m *Metrics) RecordBlockTime(chainID string, arrivalTime time.Time) {
m.mu.Lock()
defer m.mu.Unlock()
lastArrival, exists := m.lastBlockArrivalTime[chainID]
if exists {
if !arrivalTime.After(lastArrival) {
// Ignore out-of-order or same-time arrival to prevent incorrect metrics.
return
}
blockTime := arrivalTime.Sub(lastArrival)
m.BlockTime.WithLabelValues(chainID).Observe(blockTime.Seconds())
m.BlockTimeSummary.WithLabelValues(chainID).Observe(blockTime.Seconds())
}
// update last seen arrival time
m.lastBlockArrivalTime[chainID] = arrivalTime
// reset time since last block to 0
m.TimeSinceLastBlock.WithLabelValues(chainID).Set(0)
} |
||
| } | ||
|
|
||
| // UpdateTimeSinceLastBlock updates the time_since_last_block metric for all chains | ||
| // should be called periodically to keep the metric current. | ||
| func (m *Metrics) UpdateTimeSinceLastBlock() { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| now := time.Now() | ||
| for chainID, lastArrival := range m.lastBlockArrivalTime { | ||
| timeSince := now.Sub(lastArrival).Seconds() | ||
| m.TimeSinceLastBlock.WithLabelValues(chainID).Set(timeSince) | ||
| } | ||
| } | ||
|
|
||
| // RecordBlockReceiveDelay records the delay between block creation and reception | ||
|
|
||
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.
The new section headers like
Block Time Metricsuse the same heading level (###) as the individual metric names that follow (e.g.,###ev_metrics_block_time_seconds``). This creates a flat and somewhat confusing document structure. For better hierarchy and readability, consider demoting the individual metric headings to a lower level (####).For example:
This would apply to all new metrics documented under the
Block Time Metricsgroup.