This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode_store.rs
407 lines (357 loc) · 13 KB
/
node_store.rs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use prometheus_client::encoding::text::Encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::registry::Registry;
use std::{
collections::HashMap,
convert::TryInto,
sync::atomic::Ordering,
time::{Duration, Instant},
};
/// Stores information about a set of nodes for a single Dht.
pub struct NodeStore {
nodes: HashMap<PeerId, Node>,
metrics: Metrics,
}
impl NodeStore {
pub fn new(metrics: Metrics) -> Self {
NodeStore {
nodes: HashMap::new(),
metrics,
}
}
/// Record observation of a specific node.
pub fn observed_node(&mut self, node: Node) {
if let Some(new_info) = node.identify_info.as_ref() {
if let Some(old_info) = self
.nodes
.get(&node.peer_id)
.as_ref()
.and_then(|n| n.identify_info.clone())
{
self.metrics
.identify
.get_or_create(&old_info.into())
.inner()
// TODO: Use `Gauge::dec` with `open-metrics-client` `v0.14.0`.
.fetch_sub(1, Ordering::Relaxed);
}
self.metrics
.identify
.get_or_create(&new_info.clone().into())
.inc();
}
match self.nodes.get_mut(&node.peer_id) {
Some(n) => {
n.merge(node);
}
None => {
self.nodes.insert(node.peer_id.clone(), node);
}
}
}
pub fn observed_down(&mut self, peer_id: &PeerId) {
if let Some(peer) = self.nodes.get_mut(peer_id) {
if let Some(info) = peer.identify_info.take() {
self.metrics
.identify
.get_or_create(&info.into())
.inner()
// TODO: Use `Gauge::dec` with `open-metrics-client` `v0.14.0`.
.fetch_sub(1, Ordering::Relaxed);
}
peer.up_since = None;
}
}
pub fn tick(&mut self) {
self.update_metrics();
// Remove old offline nodes.
let length = self.nodes.len();
let removed = self.nodes.drain_filter(|_, n| {
(Instant::now() - n.last_seen) < Duration::from_secs(60 * 60 * 12)
});
for (_, node) in removed {
if let Some(old_info) = node.identify_info.clone() {
self.metrics
.identify
.get_or_create(&old_info.into())
.inner()
// TODO: Use `Gauge::dec` with `open-metrics-client` `v0.14.0`.
.fetch_sub(1, Ordering::Relaxed);
}
}
self.metrics
.meta_offline_nodes_removed
.inc_by((length - self.nodes.len()).try_into().unwrap());
}
fn update_metrics(&self) {
let now = Instant::now();
//
// Seen within
//
let mut nodes_by_time_by_country_and_provider =
HashMap::<Duration, HashMap<(String, String), u64>>::new();
// Insert 3h, 6h, ... buckets.
for factor in &[3, 6, 12] {
nodes_by_time_by_country_and_provider
.insert(Duration::from_secs(60 * 60 * *factor), HashMap::new());
}
for node in self.nodes.values() {
let since_last_seen = now - node.last_seen;
for (time_barrier, countries) in &mut nodes_by_time_by_country_and_provider {
if since_last_seen < *time_barrier {
countries
.entry((
node.country
.clone()
.unwrap_or_else(|| "unknown".to_string()),
node.provider
.clone()
.unwrap_or_else(|| "unknown".to_string()),
))
.and_modify(|v| *v += 1)
.or_insert(1);
}
}
}
for (time_barrier, countries) in nodes_by_time_by_country_and_provider {
let last_seen_within = format!("{:?}h", time_barrier.as_secs() / 60 / 60);
for ((country, provider), count) in countries {
self.metrics
.nodes_seen_within
.get_or_create(&vec![
("country".to_string(), country.clone()),
("cloud_provider".to_string(), provider.clone()),
("last_seen_within".to_string(), last_seen_within.clone()),
])
.set(count);
}
}
//
// Up since
//
let mut nodes_by_time_by_country_and_provider =
HashMap::<Duration, HashMap<(String, String), u64>>::new();
// Insert 3h, 6h, ... buckets.
for factor in &[3, 6, 12, 24, 48, 96] {
nodes_by_time_by_country_and_provider
.insert(Duration::from_secs(60 * 60 * *factor), HashMap::new());
}
for node in self.nodes.values() {
// Safeguard in case exporter is behind on probing every nodes
// uptime.
if Instant::now() - node.last_seen > Duration::from_secs(60 * 60) {
continue;
}
let up_since = match node.up_since {
Some(instant) => instant,
None => continue,
};
for (time_barrier, countries) in &mut nodes_by_time_by_country_and_provider {
if Instant::now() - up_since > *time_barrier {
countries
.entry((
node.country
.clone()
.unwrap_or_else(|| "unknown".to_string()),
node.provider
.clone()
.unwrap_or_else(|| "unknown".to_string()),
))
.and_modify(|v| *v += 1)
.or_insert(1);
}
}
}
for (time_barrier, countries) in nodes_by_time_by_country_and_provider {
let up_since = format!("{:?}h", time_barrier.as_secs() / 60 / 60);
for ((country, provider), count) in countries {
self.metrics
.nodes_up_since
.get_or_create(&vec![
("country".to_string(), country.clone()),
("cloud_provider".to_string(), provider.clone()),
("up_since".to_string(), up_since.clone()),
])
.set(count);
}
}
self.metrics.meta_nodes_total.set(self.nodes.len() as u64);
}
pub fn iter(&self) -> impl Iterator<Item = &Node> {
self.nodes.values()
}
}
pub struct Node {
pub peer_id: PeerId,
pub country: Option<String>,
pub provider: Option<String>,
last_seen: Instant,
up_since: Option<Instant>,
identify_info: Option<libp2p::identify::IdentifyInfo>,
}
impl Node {
pub fn new(peer_id: PeerId) -> Self {
Node {
peer_id,
country: None,
provider: None,
last_seen: Instant::now(),
up_since: Some(Instant::now()),
identify_info: None,
}
}
pub fn with_country(mut self, country: String) -> Self {
self.country = Some(country);
self
}
pub fn with_cloud_provider(mut self, provider: String) -> Self {
self.provider = Some(provider);
self
}
pub fn with_identify_info(mut self, info: libp2p::identify::IdentifyInfo) -> Self {
self.identify_info = Some(info);
self
}
fn merge(&mut self, other: Node) {
self.country = self.country.take().or(other.country);
self.up_since = self.up_since.take().or(other.up_since);
if let Some(info) = other.identify_info {
self.identify_info = Some(info);
}
if self.last_seen < other.last_seen {
self.last_seen = other.last_seen;
}
}
}
#[derive(Clone)]
pub struct Metrics {
nodes_seen_within: Family<Vec<(String, String)>, Gauge>,
nodes_up_since: Family<Vec<(String, String)>, Gauge>,
identify: Family<IdentifyLabels, Gauge>,
meta_offline_nodes_removed: Counter,
meta_nodes_total: Gauge,
}
#[derive(Encode, Clone, PartialEq, Hash, Eq)]
struct IdentifyLabels {
protocols: String,
protocol_version: String,
agent_version: String,
listen_protocol_stacks: String,
}
impl From<libp2p::identify::IdentifyInfo> for IdentifyLabels {
fn from(mut info: libp2p::identify::IdentifyInfo) -> Self {
info.protocols.sort();
let re = regex::Regex::new(r"^[a-zA-Z0-9\.\-_/]*$").unwrap();
Self {
protocols: info
.protocols
.into_iter()
.filter(|p| re.is_match(p))
.intersperse(",".to_string())
.collect(),
protocol_version: if re.is_match(info.protocol_version.as_str()) {
info.protocol_version
} else {
println!("{:?}", info.protocol_version);
"invalid-protocol-version".to_string()
},
agent_version: if re.is_match(info.agent_version.as_str()) {
info.agent_version
} else {
println!("{:?}", info.agent_version);
"invalid-agent-version".to_string()
},
listen_protocol_stacks: info
.listen_addrs
.into_iter()
.map(|a| ProtocolStack::from(a).0)
.intersperse(",".to_string())
.collect(),
}
}
}
struct ProtocolStack(String);
impl From<Multiaddr> for ProtocolStack {
fn from(address: Multiaddr) -> Self {
Self(
address
.into_iter()
.map(|p| match p {
Protocol::Dccp(_) => "dccp",
Protocol::Dns(_) => "dns",
Protocol::Dns4(_) => "dns4",
Protocol::Dns6(_) => "dns6",
Protocol::Dnsaddr(_) => "dnsaddr",
Protocol::Http => "http",
Protocol::Https => "https",
Protocol::Ip4(_) => "ip4",
Protocol::Ip6(_) => "ip6",
Protocol::P2pWebRtcDirect => "p2pwebrtcdirect",
Protocol::P2pWebRtcStar => "p2pwebrtcstar",
Protocol::P2pWebSocketStar => "p2pwebsocketstar",
Protocol::Memory(_) => "memory",
Protocol::Onion(_, _) => "onion",
Protocol::Onion3(_) => "onion3",
Protocol::P2p(_) => "p2p",
Protocol::P2pCircuit => "p2pcircuit",
Protocol::Quic => "quic",
Protocol::Sctp(_) => "sctp",
Protocol::Tcp(_) => "tcp",
Protocol::Tls => "tls",
Protocol::Udp(_) => "udp",
Protocol::Udt => "udt",
Protocol::Unix(_) => "unix",
Protocol::Utp => "utp",
Protocol::Ws(_) => "ws",
Protocol::Wss(_) => "wss",
})
.intersperse("/")
.collect(),
)
}
}
impl Metrics {
pub fn register(registry: &mut Registry) -> Metrics {
let nodes_seen_within = Family::default();
registry.register(
"nodes_seen_within",
"Unique nodes discovered within the time bound through the Dht",
Box::new(nodes_seen_within.clone()),
);
let nodes_up_since = Family::default();
registry.register(
"nodes_up_since",
"Unique nodes discovered through the Dht and up since timebound",
Box::new(nodes_up_since.clone()),
);
let identify = Family::default();
registry.register(
"identify",
"Identify protocol info",
Box::new(identify.clone()),
);
let meta_offline_nodes_removed = Counter::default();
registry.register(
"meta_offline_nodes_removed",
"Number of nodes removed due to being offline longer than 12h",
Box::new(meta_offline_nodes_removed.clone()),
);
let meta_nodes_total = Gauge::default();
registry.register(
"meta_nodes_total",
"Number of nodes tracked",
Box::new(meta_nodes_total.clone()),
);
Metrics {
nodes_seen_within,
nodes_up_since,
identify,
meta_offline_nodes_removed,
meta_nodes_total,
}
}
}