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
use std::net::SocketAddr;
use std::time::Duration;
use metrics_runtime::observers::PrometheusBuilder;
use metrics_runtime::Receiver;
use rand::prelude::*;
use crate::utils::thread::get_runtime;
use crate::channel::bounded;
use crate::metrics::exporter_http::HttpExporter;
mod exporter_http;
pub mod global_metrics;
mod worker_proxy;
pub use global_metrics::register_counter;
pub use global_metrics::register_gauge;
pub use global_metrics::Tag;
pub(crate) fn init_metrics2(bind_ip: &str, with_proxy: bool) -> SocketAddr {
let receiver = Receiver::builder()
.histogram(Duration::from_secs(5), Duration::from_millis(100))
.build()
.expect("failed to build receiver");
let controller = receiver.controller();
let (tx, rx) = bounded(1);
let bind_ip = bind_ip.to_string();
let tx_clone = tx.clone();
std::thread::spawn(move || {
get_runtime().block_on(async move {
let mut rng = rand::thread_rng();
let loops = 30;
for _index in 0..loops {
let port = rng.gen_range(10000, 30000);
let address = format!("{}:{}", bind_ip, port);
let addr: SocketAddr = address
.parse()
.expect("failed to parse http listen address");
let builder = PrometheusBuilder::new();
let exporter = HttpExporter::new(controller.clone(), builder);
match exporter.try_bind(&addr) {
Ok(addr_incoming) => {
tx_clone.send(addr.clone()).unwrap();
exporter
.async_run1(addr_incoming, with_proxy)
.await
.unwrap();
break;
}
Err(e) => warn!("exporter run error. {}", e),
}
}
});
});
let addr = rx.recv().unwrap();
info!(
"metrics http exporter listen on http://{}, proxy mode={}",
addr.to_string(),
with_proxy
);
receiver.install();
info!("receiver configured");
addr
}