iota_network/discovery/
metrics.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::sync::Arc;
6
7use prometheus::{IntGauge, Registry, register_int_gauge_with_registry};
8use tap::Pipe;
9
10#[derive(Clone)]
11pub(super) struct Metrics(Option<Arc<Inner>>);
12
13impl std::fmt::Debug for Metrics {
14    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
15        fmt.debug_struct("Metrics").finish()
16    }
17}
18
19impl Metrics {
20    pub fn enabled(registry: &Registry) -> Self {
21        Metrics(Some(Inner::new(registry)))
22    }
23
24    pub fn disabled() -> Self {
25        Metrics(None)
26    }
27
28    pub fn inc_num_peers_with_external_address(&self) {
29        if let Some(inner) = &self.0 {
30            inner.num_peers_with_external_address.inc();
31        }
32    }
33
34    pub fn dec_num_peers_with_external_address(&self) {
35        if let Some(inner) = &self.0 {
36            inner.num_peers_with_external_address.dec();
37        }
38    }
39}
40
41struct Inner {
42    num_peers_with_external_address: IntGauge,
43}
44
45impl Inner {
46    pub fn new(registry: &Registry) -> Arc<Self> {
47        Self {
48            num_peers_with_external_address: register_int_gauge_with_registry!(
49                "num_peers_with_external_address",
50                "Number of peers with an external address configured for discovery",
51                registry
52            )
53            .unwrap(),
54        }
55        .pipe(Arc::new)
56    }
57}