metrics.rs
Overview
This file defines metrics instrumentation for monitoring components related to block management and asynchronous runtime behavior within the system. It utilizes the opentelemetry crate for metric collection and reporting, specifically focusing on gauge metrics for tracking numeric values that can go up or down over time. Additionally, it integrates Tokio runtime metrics through the TokioMetrics struct from the telemetry_utils crate.
The primary purpose of this file is to encapsulate metric definitions and reporting methods that facilitate observability of block finalization progress and Tokio runtime status, enabling performance analysis and operational monitoring.
Structs and Their Functionality
BlockManagerMetrics
Represents metrics specific to the block management subsystem.
Fields:
last_finalized_seqno: Gauge<u64>
A gauge metric that tracks the sequence number of the last finalized block. This value is expected to increase as blocks are finalized.
Methods:
pub fn new(meter: &Meter) -> Self
Constructs a new instance ofBlockManagerMetricsby creating a gauge metric named"bm_last_finalized_seqno"using the providedMeter.Parameters:
meter: A reference to anopentelemetry::metrics::Meterused to create new metrics.
Returns:
A new initialized
BlockManagerMetricsinstance.
pub fn report_last_finalized_seqno(&self, seq_no: u32, thread_id: String)
Records the current last finalized sequence number into the gauge metric, associating it with a thread identifier label. This facilitates filtering and aggregation of metrics by thread during analysis.Parameters:
seq_no: The sequence number of the last finalized block (as
u32).thread_id: A string identifying the thread reporting the metric.
Usage Example:
let metrics = BlockManagerMetrics::new(&meter); metrics.report_last_finalized_seqno(123, "worker-thread-1".to_string());
Implementation Note:
There is a commented-out placeholder indicating that counting the total number of finalized blocks could also be implemented here, potentially leveraging a counter metric.
Metrics
Acts as a container for all metric categories used by the system, currently including block manager metrics and Tokio runtime metrics.
Fields:
bm: BlockManagerMetrics
Encapsulates block manager related metrics.tokio: TokioMetrics
Encapsulates Tokio runtime metrics, imported from thetelemetry_utilscrate.
Methods:
pub fn new(meter: &Meter) -> Self
Initializes theMetricscontainer by creating instances of bothBlockManagerMetricsandTokioMetricsusing the sameMeterinstance. This ensures consistent instrumentation setup across metrics categories.Parameters:
meter: A reference to anopentelemetry::metrics::Meterused for metric creation.
Returns:
A new
Metricsinstance with initialized fields.
Usage Example:
let meter = opentelemetry::global::meter("my_app"); let metrics = Metrics::new(&meter); metrics.bm.report_last_finalized_seqno(42, "main-thread".to_string());
Important Implementation Details
Metric Type Choice:
The choice of aGauge<u64>for the last finalized sequence number is appropriate because the sequence number can increase or potentially reset/reorganize, and gauges are designed for values that fluctuate rather than strictly increase.Labels/Attributes Usage:
Thereport_last_finalized_seqnomethod attaches athreadattribute viaKeyValue::new("thread", thread_id)to each recorded metric value. This labeling supports multi-threaded environments by allowing metrics to be segmented or filtered by thread in observability backends.Integration with Tokio Metrics:
TheMetricsstruct embedsTokioMetrics, which likely provides runtime-level metrics such as task counts, blocking times, or scheduler statistics, complementing the block manager metrics and enabling a broader system observability scope.
Interactions with Other System Components
Telemetry Utilities:
TheTokioMetricsstruct is imported fromtelemetry_utils, indicating this file depends on that crate/module to capture Tokio runtime metrics. This integration helps correlate block manager performance with async runtime behavior.OpenTelemetry SDK:
The use ofopentelemetry::metricstraits and types ties this file directly into the OpenTelemetry metrics ecosystem, allowing the collected metrics to be exported to various telemetry backends.Block Manager Subsystem:
The metrics defined here are intended to reflect the state and progress of the block manager, a core component responsible for handling finalized blocks in the system. Thelast_finalized_seqnometric directly corresponds to the latest block data processed.
Visual Diagram of File Structure
classDiagram
class Metrics {
+bm: BlockManagerMetrics
+tokio: TokioMetrics
+new()
}
class BlockManagerMetrics {
-last_finalized_seqno: Gauge<u64>
+new()
+report_last_finalized_seqno()
}
Metrics --> BlockManagerMetrics
Metrics --> TokioMetrics
This diagram shows the Metrics struct as a container aggregating two metric categories: BlockManagerMetrics and TokioMetrics. The BlockManagerMetrics struct encapsulates a gauge metric and methods to create and report it. The arrow indicates aggregation relationships.