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});
47
48#[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Constructor)]
49pub struct Libp2pMessageKindLabel(&'static str);
50
51impl EncodeLabelSet for Libp2pMessageKindLabel {
52    fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
53        let mut label_encoder = encoder.encode_label();
54        let mut label_key_encoder = label_encoder.encode_label_key()?;
55        EncodeLabelKey::encode(&"libp2p_message_kind", &mut label_key_encoder)?;
56        let mut label_value_encoder = label_key_encoder.encode_label_value()?;
57        EncodeLabelValue::encode(&self.0, &mut label_value_encoder)?;
58        label_value_encoder.finish()
59    }
60}
61
62pub mod values {
63    use super::Libp2pMessageKindLabel;
64
65    // libp2p_message_total
66    pub const HELLO_REQUEST_INBOUND: Libp2pMessageKindLabel =
67        Libp2pMessageKindLabel::new("hello_request_in");
68    pub const HELLO_RESPONSE_OUTBOUND: Libp2pMessageKindLabel =
69        Libp2pMessageKindLabel::new("hello_response_out");
70    pub const HELLO_REQUEST_OUTBOUND: Libp2pMessageKindLabel =
71        Libp2pMessageKindLabel::new("hello_request_out");
72    pub const HELLO_RESPONSE_INBOUND: Libp2pMessageKindLabel =
73        Libp2pMessageKindLabel::new("hello_response_in");
74    pub const PEER_CONNECTED: Libp2pMessageKindLabel =
75        Libp2pMessageKindLabel::new("peer_connected");
76    pub const PEER_DISCONNECTED: Libp2pMessageKindLabel =
77        Libp2pMessageKindLabel::new("peer_disconnected");
78    pub const PUBSUB_BLOCK: Libp2pMessageKindLabel =
79        Libp2pMessageKindLabel::new("pubsub_message_block");
80    pub const PUBSUB_MESSAGE: Libp2pMessageKindLabel =
81        Libp2pMessageKindLabel::new("pubsub_message_message");
82    pub const CHAIN_EXCHANGE_REQUEST_OUTBOUND: Libp2pMessageKindLabel =
83        Libp2pMessageKindLabel::new("chain_exchange_request_out");
84    pub const CHAIN_EXCHANGE_RESPONSE_INBOUND: Libp2pMessageKindLabel =
85        Libp2pMessageKindLabel::new("chain_exchange_response_in");
86    pub const CHAIN_EXCHANGE_REQUEST_INBOUND: Libp2pMessageKindLabel =
87        Libp2pMessageKindLabel::new("chain_exchange_request_in");
88    pub const CHAIN_EXCHANGE_RESPONSE_OUTBOUND: Libp2pMessageKindLabel =
89        Libp2pMessageKindLabel::new("chain_exchange_response_out");
90}