Skip to content

Commit c6060d5

Browse files
Implement lean_attestation_committee_subnet metric (#243)
## Motivation The [leanMetrics specification](https://github.com/leanEthereum/leanMetrics/blob/main/metrics.md) defines `lean_attestation_committee_subnet` as a Network Metric (Gauge, set on node start) that all lean clients should expose. ethlambda already computes the subnet ID at startup (`validator_id % attestation_committee_count`) for gossipsub topic subscription, but does not expose it as a Prometheus gauge. This brings ethlambda closer to full leanMetrics compliance and helps operators verify subnet assignments via Grafana dashboards. ## Description Register a new `lean_attestation_committee_subnet` Prometheus `IntGauge` in the P2P metrics module and set it at startup with the computed subnet ID. ### Changes - **`crates/net/p2p/src/metrics.rs`**: New `set_attestation_committee_subnet(subnet_id: u64)` function with a function-scoped `LazyLock<IntGauge>`, following the same pattern used by `set_attestation_committee_count` in the blockchain metrics module. - **`crates/net/p2p/src/lib.rs`**: Call the setter after computing `subnet_id` (line 213), passing `subnet_id.unwrap_or(0)`. Non-validator nodes default to subnet 0, which matches their fanout publishing behavior. - **`docs/metrics.md`**: Added row to the Network Metrics table documenting the new metric as supported. ### Design decisions - **Placed in P2P metrics** (not blockchain metrics): The subnet is a network-layer concept — it governs gossipsub topic subscription and is computed within the P2P module. - **Non-validator nodes expose subnet 0**: The leanMetrics spec says "on node start" with no validator-only qualifier. Non-validators use subnet 0 for fanout publishing, so exposing 0 is accurate. ## How to Test ```bash make fmt # passes make lint # passes make test # all tests pass ``` To verify at runtime: start a node and check `curl http://localhost:5054/metrics | grep lean_attestation_committee_subnet`. --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 455611c commit c6060d5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

crates/net/p2p/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,11 @@ pub fn build_swarm(
209209
// attestation_committee_count is validated to be >= 1 by clap at CLI parse time.
210210
let subnet_id = config
211211
.validator_id
212-
.map(|vid| vid % config.attestation_committee_count);
213-
let attestation_topic_kind = match subnet_id {
214-
Some(id) => format!("{ATTESTATION_SUBNET_TOPIC_PREFIX}_{id}"),
215-
// Non-validators use subnet 0 for publishing
216-
None => format!("{ATTESTATION_SUBNET_TOPIC_PREFIX}_0"),
217-
};
212+
.map(|vid| vid % config.attestation_committee_count)
213+
.unwrap_or(0);
214+
metrics::set_attestation_committee_subnet(subnet_id);
215+
216+
let attestation_topic_kind = format!("{ATTESTATION_SUBNET_TOPIC_PREFIX}_{subnet_id}");
218217
let attestation_topic_str =
219218
format!("/leanconsensus/{network}/{attestation_topic_kind}/ssz_snappy");
220219
let attestation_topic = libp2p::gossipsub::IdentTopic::new(attestation_topic_str);

crates/net/p2p/src/metrics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ static LEAN_PEER_DISCONNECTION_EVENTS_TOTAL: LazyLock<IntCounterVec> = LazyLock:
6868
.unwrap()
6969
});
7070

71+
/// Set the attestation committee subnet gauge.
72+
pub fn set_attestation_committee_subnet(subnet_id: u64) {
73+
static LEAN_ATTESTATION_COMMITTEE_SUBNET: LazyLock<IntGauge> = LazyLock::new(|| {
74+
register_int_gauge!(
75+
"lean_attestation_committee_subnet",
76+
"Node's attestation committee subnet"
77+
)
78+
.unwrap()
79+
});
80+
LEAN_ATTESTATION_COMMITTEE_SUBNET.set(subnet_id.try_into().unwrap_or_default());
81+
}
82+
7183
/// Notify that a peer connection event occurred.
7284
///
7385
/// If `result` is "success", the connected peer count is incremented.

docs/metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ The exposed metrics follow [the leanMetrics specification](https://github.com/le
7474
| Name | Type | Usage | Sample collection event | Labels | Supported |
7575
|--------|-------|-------|-------------------------|--------|-----------|
7676
|`lean_attestation_committee_count`| Gauge | Number of attestation committees | On node start | ||
77+
|`lean_attestation_committee_subnet`| Gauge | Node's attestation committee subnet | On node start | ||
7778
|`lean_connected_peers`| Gauge | Number of connected peers | On scrape | client=ethlambda,grandine,lantern,lighthouse,qlean,ream,zeam | ✅(*) |
7879
|`lean_peer_connection_events_total`| Counter | Total number of peer connection events | On peer connection | direction=inbound,outbound<br>result=success,timeout,error ||
7980
|`lean_peer_disconnection_events_total`| Counter | Total number of peer disconnection events | On peer disconnection | direction=inbound,outbound<br>reason=timeout,remote_close,local_close,error ||

0 commit comments

Comments
 (0)