1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use once_cell::sync::Lazy;
use parking_lot::MappedRwLockReadGuard;
use prometheus_client::{
    encoding::EncodeLabelSet,
    metrics::{counter::Counter, family::Family, gauge::Gauge, histogram::Histogram},
    registry::Registry,
};

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
struct TypeLabel {
    r#type: &'static str,
}

impl TypeLabel {
    fn new(t: &'static str) -> Self {
        Self { r#type: t }
    }
}

static MESSAGE_COUNTER: Lazy<Family<TypeLabel, Counter>> = Lazy::new(Default::default);
static CONTAINER_CAPACITIES: Lazy<Family<TypeLabel, Gauge>> = Lazy::new(Default::default);
pub(in crate::libp2p_bitswap) static GET_BLOCK_TIME: Lazy<Histogram> = Lazy::new(|| {
    Histogram::new(
        [
            0.1, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0,
        ]
        .into_iter(),
    )
});

/// Register bitswap metrics
pub fn register_metrics(registry: &mut Registry) {
    registry.register(
        "bitswap_message_count",
        "Number of bitswap messages",
        MESSAGE_COUNTER.clone(),
    );
    registry.register(
        "bitswap_container_capacities",
        "Capacity for each bitswap container",
        CONTAINER_CAPACITIES.clone(),
    );
    registry.register(
        "bitswap_get_block_time",
        "Duration of get_block",
        GET_BLOCK_TIME.clone(),
    );
}

pub(in crate::libp2p_bitswap) fn inbound_stream_count<'a>() -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_stream_count"))
}

pub(in crate::libp2p_bitswap) fn outbound_stream_count<'a>() -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_stream_count"))
}

pub(in crate::libp2p_bitswap) fn message_counter_get_block_success<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("get_block_success"))
}

pub(in crate::libp2p_bitswap) fn message_counter_get_block_failure<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("get_block_failure"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_request_have<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_request_have"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_request_block<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_request_block"))
}

pub(in crate::libp2p_bitswap) fn message_counter_outbound_request_cancel<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_request_cancel"))
}

pub(in crate::libp2p_bitswap) fn message_counter_outbound_request_block<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_request_block"))
}

pub(in crate::libp2p_bitswap) fn message_counter_outbound_request_have<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_request_have"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_have_yes<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_have_yes"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_have_no<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_have_no"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_block<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_block"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_block_update_db<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_block_update_db"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_block_already_exists_in_db<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new(
        "inbound_response_block_already_exists_in_db",
    ))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_block_not_requested<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_block_not_requested"))
}

pub(in crate::libp2p_bitswap) fn message_counter_inbound_response_block_update_db_failure<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("inbound_response_block_update_db_failure"))
}

pub(in crate::libp2p_bitswap) fn message_counter_outbound_response_have<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_response_have"))
}

pub(in crate::libp2p_bitswap) fn message_counter_outbound_response_block<'a>(
) -> MappedRwLockReadGuard<'a, Counter> {
    MESSAGE_COUNTER.get_or_create(&TypeLabel::new("outbound_response_block"))
}

pub(in crate::libp2p_bitswap) fn peer_container_capacity<'a>() -> MappedRwLockReadGuard<'a, Gauge> {
    CONTAINER_CAPACITIES.get_or_create(&TypeLabel::new("peer_container_capacity"))
}

pub(in crate::libp2p_bitswap) fn response_channel_container_capacity<'a>(
) -> MappedRwLockReadGuard<'a, Gauge> {
    CONTAINER_CAPACITIES.get_or_create(&TypeLabel::new("response_channel_container_capacity"))
}