mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 13:15:41 +00:00
Reduce overhead of generating network event metrics (#10839)
This commit is contained in:
@@ -37,6 +37,7 @@ use futures::{channel::mpsc, prelude::*, ready, stream::FusedStream};
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use prometheus_endpoint::{register, CounterVec, GaugeVec, Opts, PrometheusError, Registry, U64};
|
use prometheus_endpoint::{register, CounterVec, GaugeVec, Opts, PrometheusError, Registry, U64};
|
||||||
use std::{
|
use std::{
|
||||||
|
cell::RefCell,
|
||||||
convert::TryFrom as _,
|
convert::TryFrom as _,
|
||||||
fmt,
|
fmt,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
@@ -187,6 +188,23 @@ struct Metrics {
|
|||||||
num_channels: GaugeVec<U64>,
|
num_channels: GaugeVec<U64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static LABEL_BUFFER: RefCell<String> = RefCell::new(String::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_label(prefix: &str, protocol: &str, callback: impl FnOnce(&str)) {
|
||||||
|
LABEL_BUFFER.with(|label_buffer| {
|
||||||
|
let mut label_buffer = label_buffer.borrow_mut();
|
||||||
|
label_buffer.clear();
|
||||||
|
label_buffer.reserve(prefix.len() + protocol.len() + 2);
|
||||||
|
label_buffer.push_str(prefix);
|
||||||
|
label_buffer.push_str("\"");
|
||||||
|
label_buffer.push_str(protocol);
|
||||||
|
label_buffer.push_str("\"");
|
||||||
|
callback(&label_buffer);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
impl Metrics {
|
impl Metrics {
|
||||||
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
|
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -232,20 +250,26 @@ impl Metrics {
|
|||||||
.inc_by(num);
|
.inc_by(num);
|
||||||
},
|
},
|
||||||
Event::NotificationStreamOpened { protocol, .. } => {
|
Event::NotificationStreamOpened { protocol, .. } => {
|
||||||
self.events_total
|
format_label("notif-open-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-open-{:?}", protocol), "sent", name])
|
self.events_total
|
||||||
.inc_by(num);
|
.with_label_values(&[protocol_label, "sent", name])
|
||||||
|
.inc_by(num);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
Event::NotificationStreamClosed { protocol, .. } => {
|
Event::NotificationStreamClosed { protocol, .. } => {
|
||||||
self.events_total
|
format_label("notif-closed-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-closed-{:?}", protocol), "sent", name])
|
self.events_total
|
||||||
.inc_by(num);
|
.with_label_values(&[protocol_label, "sent", name])
|
||||||
|
.inc_by(num);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
Event::NotificationsReceived { messages, .. } =>
|
Event::NotificationsReceived { messages, .. } =>
|
||||||
for (protocol, message) in messages {
|
for (protocol, message) in messages {
|
||||||
self.events_total
|
format_label("notif-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-{:?}", protocol), "sent", name])
|
self.events_total
|
||||||
.inc_by(num);
|
.with_label_values(&[protocol_label, "sent", name])
|
||||||
|
.inc_by(num);
|
||||||
|
});
|
||||||
self.notifications_sizes.with_label_values(&[protocol, "sent", name]).inc_by(
|
self.notifications_sizes.with_label_values(&[protocol, "sent", name]).inc_by(
|
||||||
num.saturating_mul(u64::try_from(message.len()).unwrap_or(u64::MAX)),
|
num.saturating_mul(u64::try_from(message.len()).unwrap_or(u64::MAX)),
|
||||||
);
|
);
|
||||||
@@ -267,20 +291,22 @@ impl Metrics {
|
|||||||
.inc();
|
.inc();
|
||||||
},
|
},
|
||||||
Event::NotificationStreamOpened { protocol, .. } => {
|
Event::NotificationStreamOpened { protocol, .. } => {
|
||||||
self.events_total
|
format_label("notif-open-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-open-{:?}", protocol), "received", name])
|
self.events_total.with_label_values(&[protocol_label, "received", name]).inc();
|
||||||
.inc();
|
});
|
||||||
},
|
},
|
||||||
Event::NotificationStreamClosed { protocol, .. } => {
|
Event::NotificationStreamClosed { protocol, .. } => {
|
||||||
self.events_total
|
format_label("notif-closed-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-closed-{:?}", protocol), "received", name])
|
self.events_total.with_label_values(&[protocol_label, "received", name]).inc();
|
||||||
.inc();
|
});
|
||||||
},
|
},
|
||||||
Event::NotificationsReceived { messages, .. } =>
|
Event::NotificationsReceived { messages, .. } =>
|
||||||
for (protocol, message) in messages {
|
for (protocol, message) in messages {
|
||||||
self.events_total
|
format_label("notif-", &protocol, |protocol_label| {
|
||||||
.with_label_values(&[&format!("notif-{:?}", protocol), "received", name])
|
self.events_total
|
||||||
.inc();
|
.with_label_values(&[protocol_label, "received", name])
|
||||||
|
.inc();
|
||||||
|
});
|
||||||
self.notifications_sizes
|
self.notifications_sizes
|
||||||
.with_label_values(&[&protocol, "received", name])
|
.with_label_values(&[&protocol, "received", name])
|
||||||
.inc_by(u64::try_from(message.len()).unwrap_or(u64::MAX));
|
.inc_by(u64::try_from(message.len()).unwrap_or(u64::MAX));
|
||||||
|
|||||||
Reference in New Issue
Block a user