Skip to main content

forest/chain_sync/
metrics.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use prometheus_client::{
5    encoding::{EncodeLabelKey, EncodeLabelSet, EncodeLabelValue, LabelSetEncoder},
6    metrics::{counter::Counter, family::Family, histogram::Histogram},
7};
8use std::sync::LazyLock;
9
10pub static TIPSET_PROCESSING_TIME: LazyLock<Histogram> = LazyLock::new(|| {
11    let metric = crate::metrics::default_histogram();
12    crate::metrics::default_registry().register(
13        "tipset_processing_time",
14        "Duration of routine which processes Tipsets to include them in the store",
15        metric.clone(),
16    );
17    metric
18});
19pub static BLOCK_VALIDATION_TIME: LazyLock<Histogram> = LazyLock::new(|| {
20    let metric = crate::metrics::default_histogram();
21    crate::metrics::default_registry().register(
22        "block_validation_time",
23        "Duration of routine which validate blocks with no cache hit",
24        metric.clone(),
25    );
26    metric
27});
28pub static LIBP2P_MESSAGE_TOTAL: LazyLock<Family<Libp2pMessageKindLabel, Counter>> =
29    LazyLock::new(|| {
30        let metric = Family::default();
31        crate::metrics::default_registry().register(
32            "libp2p_messsage_total",
33            "Total number of libp2p messages by type",
34            metric.clone(),
35        );
36        metric
37    });
38pub static INVALID_TIPSET_TOTAL: LazyLock<Counter> = LazyLock::new(|| {
39    let metric = Counter::default();
40    crate::metrics::default_registry().register(
41        "invalid_tipset_total",
42        "Total number of invalid tipsets received over gossipsub",
43        metric.clone(),
44    );
45    metric
46});
47pub static GOSSIP_BLOCK_REJECTED_TOTAL: LazyLock<Family<GossipRejectReasonLabel, Counter>> =
48    LazyLock::new(|| {
49        let metric = Family::default();
50        crate::metrics::default_registry().register(
51            "gossip_block_rejected_total",
52            "Total number of gossip blocks rejected by pre-validation",
53            metric.clone(),
54        );
55        metric
56    });
57
58#[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Constructor)]
59pub struct Libp2pMessageKindLabel(&'static str);
60
61#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet, derive_more::Constructor)]
62pub struct GossipRejectReasonLabel {
63    pub reason: &'static str,
64}
65
66impl EncodeLabelSet for Libp2pMessageKindLabel {
67    fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
68        let mut label_encoder = encoder.encode_label();
69        let mut label_key_encoder = label_encoder.encode_label_key()?;
70        EncodeLabelKey::encode(&"libp2p_message_kind", &mut label_key_encoder)?;
71        let mut label_value_encoder = label_key_encoder.encode_label_value()?;
72        EncodeLabelValue::encode(&self.0, &mut label_value_encoder)?;
73        label_value_encoder.finish()
74    }
75}
76
77pub mod values {
78    use super::Libp2pMessageKindLabel;
79
80    // libp2p_message_total
81    pub const HELLO_REQUEST_INBOUND: Libp2pMessageKindLabel =
82        Libp2pMessageKindLabel::new("hello_request_in");
83    pub const HELLO_RESPONSE_OUTBOUND: Libp2pMessageKindLabel =
84        Libp2pMessageKindLabel::new("hello_response_out");
85    pub const HELLO_REQUEST_OUTBOUND: Libp2pMessageKindLabel =
86        Libp2pMessageKindLabel::new("hello_request_out");
87    pub const HELLO_RESPONSE_INBOUND: Libp2pMessageKindLabel =
88        Libp2pMessageKindLabel::new("hello_response_in");
89    pub const PEER_CONNECTED: Libp2pMessageKindLabel =
90        Libp2pMessageKindLabel::new("peer_connected");
91    pub const PEER_DISCONNECTED: Libp2pMessageKindLabel =
92        Libp2pMessageKindLabel::new("peer_disconnected");
93    pub const PUBSUB_BLOCK: Libp2pMessageKindLabel =
94        Libp2pMessageKindLabel::new("pubsub_message_block");
95    pub const PUBSUB_MESSAGE: Libp2pMessageKindLabel =
96        Libp2pMessageKindLabel::new("pubsub_message_message");
97    pub const CHAIN_EXCHANGE_REQUEST_OUTBOUND: Libp2pMessageKindLabel =
98        Libp2pMessageKindLabel::new("chain_exchange_request_out");
99    pub const CHAIN_EXCHANGE_RESPONSE_INBOUND: Libp2pMessageKindLabel =
100        Libp2pMessageKindLabel::new("chain_exchange_response_in");
101    pub const CHAIN_EXCHANGE_REQUEST_INBOUND: Libp2pMessageKindLabel =
102        Libp2pMessageKindLabel::new("chain_exchange_request_in");
103    pub const CHAIN_EXCHANGE_RESPONSE_OUTBOUND: Libp2pMessageKindLabel =
104        Libp2pMessageKindLabel::new("chain_exchange_response_out");
105}