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
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::sync::Arc;

use prometheus::{IntGauge, Registry, register_int_gauge_with_registry};

use crate::NodeConfig;

pub struct NodeConfigMetrics {
    tx_deny_config_user_transaction_disabled: IntGauge,
    tx_deny_config_shared_object_disabled: IntGauge,
    tx_deny_config_package_publish_disabled: IntGauge,
    tx_deny_config_package_upgrade_disabled: IntGauge,
    tx_deny_config_num_denied_objects: IntGauge,
    tx_deny_config_num_denied_packages: IntGauge,
    tx_deny_config_num_denied_addresses: IntGauge,
}

impl NodeConfigMetrics {
    pub fn new(registry: &Registry) -> Arc<Self> {
        let this = Self {
            tx_deny_config_user_transaction_disabled: register_int_gauge_with_registry!(
                "tx_deny_config_user_transaction_disabled",
                "Whether all user transactions are disabled",
                registry
            )
            .unwrap(),
            tx_deny_config_shared_object_disabled: register_int_gauge_with_registry!(
                "tx_deny_config_shared_object_disabled",
                "Whether all shared object transactions are disabled",
                registry
            )
            .unwrap(),
            tx_deny_config_package_publish_disabled: register_int_gauge_with_registry!(
                "tx_deny_config_package_publish_disabled",
                "Whether all package publish transactions are disabled",
                registry
            )
            .unwrap(),
            tx_deny_config_package_upgrade_disabled: register_int_gauge_with_registry!(
                "tx_deny_config_package_upgrade_disabled",
                "Whether all package upgrade transactions are disabled",
                registry
            )
            .unwrap(),
            tx_deny_config_num_denied_objects: register_int_gauge_with_registry!(
                "tx_deny_config_num_denied_objects",
                "Number of denied objects",
                registry
            )
            .unwrap(),
            tx_deny_config_num_denied_packages: register_int_gauge_with_registry!(
                "tx_deny_config_num_denied_packages",
                "Number of denied packages",
                registry
            )
            .unwrap(),
            tx_deny_config_num_denied_addresses: register_int_gauge_with_registry!(
                "tx_deny_config_num_denied_addresses",
                "Number of denied addresses",
                registry
            )
            .unwrap(),
        };
        Arc::new(this)
    }

    pub fn record_metrics(&self, config: &NodeConfig) {
        self.tx_deny_config_user_transaction_disabled
            .set(config.transaction_deny_config.user_transaction_disabled() as i64);
        self.tx_deny_config_shared_object_disabled
            .set(config.transaction_deny_config.shared_object_disabled() as i64);
        self.tx_deny_config_package_publish_disabled
            .set(config.transaction_deny_config.package_publish_disabled() as i64);
        self.tx_deny_config_package_upgrade_disabled
            .set(config.transaction_deny_config.package_upgrade_disabled() as i64);
        self.tx_deny_config_num_denied_objects
            .set(config.transaction_deny_config.get_object_deny_set().len() as i64);
        self.tx_deny_config_num_denied_packages
            .set(config.transaction_deny_config.get_package_deny_set().len() as i64);
        self.tx_deny_config_num_denied_addresses
            .set(config.transaction_deny_config.get_address_deny_set().len() as i64);
    }
}