forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.rs
331 lines (276 loc) · 10.2 KB
/
metrics.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use axum::{
extract::Extension,
http::{header, StatusCode},
routing::get,
Router,
};
use mysten_network::metrics::MetricsCallbackProvider;
use prometheus::{
register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
register_int_gauge_vec_with_registry, Encoder, HistogramVec, IntCounterVec, IntGaugeVec,
Registry, TextEncoder, PROTOBUF_FORMAT,
};
use std::net::SocketAddr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use sui_network::tonic::Code;
use mysten_metrics::RegistryService;
use tracing::{error, warn};
const METRICS_ROUTE: &str = "/metrics";
// Creates a new http server that has as a sole purpose to expose
// and endpoint that prometheus agent can use to poll for the metrics.
// A RegistryService is returned that can be used to get access in prometheus Registries.
pub fn start_prometheus_server(addr: SocketAddr) -> RegistryService {
let registry = Registry::new();
let registry_service = RegistryService::new(registry);
if cfg!(msim) {
// prometheus uses difficult-to-support features such as TcpSocket::from_raw_fd(), so we
// can't yet run it in the simulator.
warn!("not starting prometheus server in simulator");
return registry_service;
}
let app = Router::new()
.route(METRICS_ROUTE, get(metrics))
.layer(Extension(registry_service.clone()));
tokio::spawn(async move {
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
});
registry_service
}
async fn metrics(Extension(registry_service): Extension<RegistryService>) -> (StatusCode, String) {
let metrics_families = registry_service.gather_all();
match TextEncoder.encode_to_string(&metrics_families) {
Ok(metrics) => (StatusCode::OK, metrics),
Err(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("unable to encode metrics: {error}"),
),
}
}
pub struct MetricsPushClient {
certificate: std::sync::Arc<sui_tls::SelfSignedCertificate>,
client: reqwest::Client,
}
impl MetricsPushClient {
pub fn new(network_key: sui_types::crypto::NetworkKeyPair) -> Self {
use fastcrypto::traits::KeyPair;
let certificate = std::sync::Arc::new(sui_tls::SelfSignedCertificate::new(
network_key.private(),
sui_tls::SUI_VALIDATOR_SERVER_NAME,
));
let identity = certificate.reqwest_identity();
let client = reqwest::Client::builder()
.identity(identity)
.build()
.unwrap();
Self {
certificate,
client,
}
}
pub fn certificate(&self) -> &sui_tls::SelfSignedCertificate {
&self.certificate
}
pub fn client(&self) -> &reqwest::Client {
&self.client
}
}
/// Starts a task to periodically push metrics to a configured endpoint if a metrics push endpoint
/// is configured.
pub fn start_metrics_push_task(config: &sui_config::NodeConfig, registry: RegistryService) {
use fastcrypto::traits::KeyPair;
use sui_config::node::MetricsConfig;
const DEFAULT_METRICS_PUSH_INTERVAL: Duration = Duration::from_secs(60);
let (interval, url) = match &config.metrics {
Some(MetricsConfig {
push_interval_seconds,
push_url: Some(url),
}) => {
let interval = push_interval_seconds
.map(Duration::from_secs)
.unwrap_or(DEFAULT_METRICS_PUSH_INTERVAL);
let url = reqwest::Url::parse(url).expect("unable to parse metrics push url");
(interval, url)
}
_ => return,
};
let client = MetricsPushClient::new(config.network_key_pair().copy());
async fn push_metrics(
client: &MetricsPushClient,
url: &reqwest::Url,
registry: &RegistryService,
) -> Result<(), anyhow::Error> {
// now represents a collection timestamp for all of the metrics we send to the proxy
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as i64;
let mut metric_families = registry.gather_all();
for mf in metric_families.iter_mut() {
for m in mf.mut_metric() {
m.set_timestamp_ms(now);
}
}
let mut buf: Vec<u8> = vec![];
let encoder = prometheus::ProtobufEncoder::new();
encoder.encode(&metric_families, &mut buf)?;
let mut s = snap::raw::Encoder::new();
let compressed = s.compress_vec(&buf).map_err(|err| {
error!("unable to snappy encode; {err}");
err
})?;
let response = client
.client()
.post(url.to_owned())
.header(reqwest::header::CONTENT_ENCODING, "snappy")
.header(header::CONTENT_TYPE, PROTOBUF_FORMAT)
.body(compressed)
.send()
.await?;
if !response.status().is_success() {
let status = response.status();
let body = match response.text().await {
Ok(body) => body,
Err(error) => format!("couldn't decode response body; {error}"),
};
return Err(anyhow::anyhow!(
"metrics push failed: [{}]:{}",
status,
body
));
}
tracing::debug!("successfully pushed metrics to {url}");
Ok(())
}
tokio::spawn(async move {
tracing::info!(push_url =% url, interval =? interval, "Started Metrics Push Service");
let mut interval = tokio::time::interval(interval);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
interval.tick().await;
if let Err(error) = push_metrics(&client, &url, ®istry).await {
tracing::warn!("unable to push metrics: {error}");
}
}
});
}
#[derive(Clone)]
pub struct GrpcMetrics {
inflight_grpc: IntGaugeVec,
grpc_requests: IntCounterVec,
grpc_request_latency: HistogramVec,
}
const LATENCY_SEC_BUCKETS: &[f64] = &[
0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1., 2.5, 5., 10., 20., 30., 60., 90.,
];
impl GrpcMetrics {
pub fn new(registry: &Registry) -> Self {
Self {
inflight_grpc: register_int_gauge_vec_with_registry!(
"inflight_grpc",
"Total in-flight GRPC requests per route",
&["path"],
registry,
)
.unwrap(),
grpc_requests: register_int_counter_vec_with_registry!(
"grpc_requests",
"Total GRPC requests per route",
&["path", "status"],
registry,
)
.unwrap(),
grpc_request_latency: register_histogram_vec_with_registry!(
"grpc_request_latency",
"Latency of GRPC requests per route",
&["path"],
LATENCY_SEC_BUCKETS.to_vec(),
registry,
)
.unwrap(),
}
}
}
impl MetricsCallbackProvider for GrpcMetrics {
fn on_request(&self, _path: String) {}
fn on_response(&self, path: String, latency: Duration, _status: u16, grpc_status_code: Code) {
self.grpc_requests
.with_label_values(&[path.as_str(), format!("{grpc_status_code:?}").as_str()])
.inc();
self.grpc_request_latency
.with_label_values(&[path.as_str()])
.observe(latency.as_secs_f64());
}
fn on_start(&self, path: &str) {
self.inflight_grpc.with_label_values(&[path]).inc();
}
fn on_drop(&self, path: &str) {
self.inflight_grpc.with_label_values(&[path]).dec();
}
}
#[cfg(test)]
mod tests {
use crate::metrics::start_prometheus_server;
use prometheus::{IntCounter, Registry};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
#[tokio::test]
pub async fn test_metrics_endpoint_with_multiple_registries_add_remove() {
let port: u16 = 8081;
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
let registry_service = start_prometheus_server(socket);
tokio::task::yield_now().await;
// now add a few registries to the service along side with metrics
let registry_1 = Registry::new_custom(Some("narwhal".to_string()), None).unwrap();
let counter_1 = IntCounter::new("counter_1", "a sample counter 1").unwrap();
registry_1.register(Box::new(counter_1)).unwrap();
let registry_2 = Registry::new_custom(Some("sui".to_string()), None).unwrap();
let counter_2 = IntCounter::new("counter_2", "a sample counter 2").unwrap();
registry_2.register(Box::new(counter_2.clone())).unwrap();
let registry_1_id = registry_service.add(registry_1);
let _registry_2_id = registry_service.add(registry_2);
// request the endpoint
let result = get_metrics(port).await;
assert!(result.contains(
"# HELP sui_counter_2 a sample counter 2
# TYPE sui_counter_2 counter
sui_counter_2 0"
));
assert!(result.contains(
"# HELP narwhal_counter_1 a sample counter 1
# TYPE narwhal_counter_1 counter
narwhal_counter_1 0"
));
// Now remove registry 1
assert!(registry_service.remove(registry_1_id));
// AND increase metric 2
counter_2.inc();
// Now pull again metrics
// request the endpoint
let result = get_metrics(port).await;
// Registry 1 metrics should not be present anymore
assert!(!result.contains(
"# HELP narwhal_counter_1 a sample counter 1
# TYPE narwhal_counter_1 counter
narwhal_counter_1 0"
));
// Registry 2 metric should have increased by 1
assert!(result.contains(
"# HELP sui_counter_2 a sample counter 2
# TYPE sui_counter_2 counter
sui_counter_2 1"
));
}
async fn get_metrics(port: u16) -> String {
let client = reqwest::Client::new();
let response = client
.get(format!("http://127.0.0.1:{}/metrics", port))
.send()
.await
.unwrap();
response.text().await.unwrap()
}
}