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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::api::{ConnectAddr, ListenAddr};
use network_protocol::{Cid, Pid};
#[cfg(feature = "metrics")]
use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Opts, Registry};
use std::{error::Error, net::SocketAddr};

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) enum ProtocolInfo {
    Tcp(SocketAddr),
    Udp(SocketAddr),
    #[cfg(feature = "quic")]
    Quic(SocketAddr),
    Mpsc(u64),
}

impl From<ListenAddr> for ProtocolInfo {
    fn from(other: ListenAddr) -> ProtocolInfo {
        match other {
            ListenAddr::Tcp(s) => ProtocolInfo::Tcp(s),
            ListenAddr::Udp(s) => ProtocolInfo::Udp(s),
            #[cfg(feature = "quic")]
            ListenAddr::Quic(s, _) => ProtocolInfo::Quic(s),
            ListenAddr::Mpsc(s) => ProtocolInfo::Mpsc(s),
        }
    }
}

/// 1:1 relation between NetworkMetrics and Network
#[cfg(feature = "metrics")]
pub struct NetworkMetrics {
    pub listen_requests_total: IntCounterVec,
    pub connect_requests_total: IntCounterVec,
    pub incoming_connections_total: IntCounterVec,
    pub failed_handshakes_total: IntCounter,
    pub participants_connected_total: IntCounter,
    pub participants_disconnected_total: IntCounter,
    // channel id's, seperated by PARTICIPANT, max 5
    pub participants_channel_ids: IntGaugeVec,
    // upload to remote, averaged, seperated by PARTICIPANT
    pub participants_bandwidth: IntGaugeVec,
    // opened Channels, seperated by PARTICIPANT
    pub channels_connected_total: IntCounterVec,
    pub channels_disconnected_total: IntCounterVec,
    // opened streams, seperated by PARTICIPANT
    pub streams_opened_total: IntCounterVec,
    pub streams_closed_total: IntCounterVec,
    pub network_info: IntGauge,
}

#[cfg(not(feature = "metrics"))]
pub struct NetworkMetrics {}

#[cfg(feature = "metrics")]
impl NetworkMetrics {
    pub fn new(local_pid: &Pid) -> Result<Self, Box<dyn Error>> {
        let listen_requests_total = IntCounterVec::new(
            Opts::new(
                "listen_requests_total",
                "Shows the number of listen requests to the scheduler",
            ),
            &["protocol"],
        )?;
        let connect_requests_total = IntCounterVec::new(
            Opts::new(
                "connect_requests_total",
                "Shows the number of connect requests to the scheduler",
            ),
            &["protocol"],
        )?;
        let incoming_connections_total = IntCounterVec::new(
            Opts::new(
                "incoming_connections_total",
                "Shows the number of external requests to the scheduler",
            ),
            &["protocol"],
        )?;
        let failed_handshakes_total = IntCounter::with_opts(Opts::new(
            "failed_handshakes_total",
            "Shows the number of failed handshakes",
        ))?;
        let participants_connected_total = IntCounter::with_opts(Opts::new(
            "participants_connected_total",
            "Shows the number of participants connected to the network",
        ))?;
        let participants_disconnected_total = IntCounter::with_opts(Opts::new(
            "participants_disconnected_total",
            "Shows the number of participants disconnected to the network",
        ))?;
        let participants_channel_ids = IntGaugeVec::new(
            Opts::new(
                "participants_channel_ids",
                "Channel numbers belonging to a Participant in the network",
            ),
            &["participant", "no"],
        )?;
        let participants_bandwidth = IntGaugeVec::new(
            Opts::new(
                "participants_bandwidth",
                "max upload possible to Participant",
            ),
            &["participant"],
        )?;
        let channels_connected_total = IntCounterVec::new(
            Opts::new(
                "channels_connected_total",
                "Number of all channels currently connected on the network",
            ),
            &["participant"],
        )?;
        let channels_disconnected_total = IntCounterVec::new(
            Opts::new(
                "channels_disconnected_total",
                "Number of all channels currently disconnected on the network",
            ),
            &["participant"],
        )?;
        let streams_opened_total = IntCounterVec::new(
            Opts::new(
                "streams_opened_total",
                "Number of all streams currently open on the network",
            ),
            &["participant"],
        )?;
        let streams_closed_total = IntCounterVec::new(
            Opts::new(
                "streams_closed_total",
                "Number of all streams currently open on the network",
            ),
            &["participant"],
        )?;
        let opts = Opts::new("network_info", "Static Network information")
            .const_label(
                "version",
                format!(
                    "{}.{}.{}",
                    &network_protocol::VELOREN_NETWORK_VERSION[0],
                    &network_protocol::VELOREN_NETWORK_VERSION[1],
                    &network_protocol::VELOREN_NETWORK_VERSION[2]
                ),
            )
            .const_label("local_pid", format!("{}", &local_pid));
        let network_info = IntGauge::with_opts(opts)?;

        Ok(Self {
            listen_requests_total,
            connect_requests_total,
            incoming_connections_total,
            failed_handshakes_total,
            participants_connected_total,
            participants_disconnected_total,
            participants_channel_ids,
            participants_bandwidth,
            channels_connected_total,
            channels_disconnected_total,
            streams_opened_total,
            streams_closed_total,
            network_info,
        })
    }

    pub fn register(&self, registry: &Registry) -> Result<(), Box<dyn Error>> {
        registry.register(Box::new(self.listen_requests_total.clone()))?;
        registry.register(Box::new(self.connect_requests_total.clone()))?;
        registry.register(Box::new(self.incoming_connections_total.clone()))?;
        registry.register(Box::new(self.failed_handshakes_total.clone()))?;
        registry.register(Box::new(self.participants_connected_total.clone()))?;
        registry.register(Box::new(self.participants_disconnected_total.clone()))?;
        registry.register(Box::new(self.participants_channel_ids.clone()))?;
        registry.register(Box::new(self.participants_bandwidth.clone()))?;
        registry.register(Box::new(self.channels_connected_total.clone()))?;
        registry.register(Box::new(self.channels_disconnected_total.clone()))?;
        registry.register(Box::new(self.streams_opened_total.clone()))?;
        registry.register(Box::new(self.streams_closed_total.clone()))?;
        registry.register(Box::new(self.network_info.clone()))?;
        Ok(())
    }

    pub(crate) fn connect_requests_cache(&self, protocol: &ListenAddr) -> IntCounter {
        self.incoming_connections_total
            .with_label_values(&[protocollisten_name(protocol)])
    }

    pub(crate) fn channels_connected(&self, remote_p: &str, no: usize, cid: Cid) {
        self.channels_connected_total
            .with_label_values(&[remote_p])
            .inc();
        self.participants_channel_ids
            .with_label_values(&[remote_p, &no.to_string()])
            .set(cid as i64);
    }

    pub(crate) fn channels_disconnected(&self, remote_p: &str) {
        self.channels_disconnected_total
            .with_label_values(&[remote_p])
            .inc();
    }

    pub(crate) fn participant_bandwidth(&self, remote_p: &str, bandwidth: f32) {
        self.participants_bandwidth
            .with_label_values(&[remote_p])
            .set(bandwidth as i64);
    }

    pub(crate) fn streams_opened(&self, remote_p: &str) {
        self.streams_opened_total
            .with_label_values(&[remote_p])
            .inc();
    }

    pub(crate) fn streams_closed(&self, remote_p: &str) {
        self.streams_closed_total
            .with_label_values(&[remote_p])
            .inc();
    }

    pub(crate) fn listen_request(&self, protocol: &ListenAddr) {
        self.listen_requests_total
            .with_label_values(&[protocollisten_name(protocol)])
            .inc();
    }

    pub(crate) fn connect_request(&self, protocol: &ConnectAddr) {
        self.connect_requests_total
            .with_label_values(&[protocolconnect_name(protocol)])
            .inc();
    }

    pub(crate) fn cleanup_participant(&self, remote_p: &str) {
        for no in 0..5 {
            let _ = self
                .participants_channel_ids
                .remove_label_values(&[remote_p, &no.to_string()]);
        }
        let _ = self
            .channels_connected_total
            .remove_label_values(&[remote_p]);
        let _ = self
            .channels_disconnected_total
            .remove_label_values(&[remote_p]);
        let _ = self.participants_bandwidth.remove_label_values(&[remote_p]);
        let _ = self.streams_opened_total.remove_label_values(&[remote_p]);
        let _ = self.streams_closed_total.remove_label_values(&[remote_p]);
    }
}

#[cfg(feature = "metrics")]
fn protocolconnect_name(protocol: &ConnectAddr) -> &str {
    match protocol {
        ConnectAddr::Tcp(_) => "tcp",
        ConnectAddr::Udp(_) => "udp",
        ConnectAddr::Mpsc(_) => "mpsc",
        #[cfg(feature = "quic")]
        ConnectAddr::Quic(_, _, _) => "quic",
    }
}

#[cfg(feature = "metrics")]
fn protocollisten_name(protocol: &ListenAddr) -> &str {
    match protocol {
        ListenAddr::Tcp(_) => "tcp",
        ListenAddr::Udp(_) => "udp",
        ListenAddr::Mpsc(_) => "mpsc",
        #[cfg(feature = "quic")]
        ListenAddr::Quic(_, _) => "quic",
    }
}

#[cfg(not(feature = "metrics"))]
impl NetworkMetrics {
    pub fn new(_local_pid: &Pid) -> Result<Self, Box<dyn Error>> { Ok(Self {}) }

    pub(crate) fn channels_connected(&self, _remote_p: &str, _no: usize, _cid: Cid) {}

    pub(crate) fn channels_disconnected(&self, _remote_p: &str) {}

    pub(crate) fn participant_bandwidth(&self, _remote_p: &str, _bandwidth: f32) {}

    pub(crate) fn streams_opened(&self, _remote_p: &str) {}

    pub(crate) fn streams_closed(&self, _remote_p: &str) {}

    pub(crate) fn listen_request(&self, _protocol: &ListenAddr) {}

    pub(crate) fn connect_request(&self, _protocol: &ConnectAddr) {}

    pub(crate) fn cleanup_participant(&self, _remote_p: &str) {}
}

impl std::fmt::Debug for NetworkMetrics {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "NetworkMetrics()")
    }
}