metrics: update to latest version

Really the only reason I bothered was because the old version brings
in an ahash that fails to compile on macOS Arm or something like that,
but since I'd done the upgrade before for my own packages it wasn't
hard.
This commit is contained in:
Augie Fackler 2024-10-28 12:23:47 -04:00
parent 5341153a86
commit feb6684e8f
4 changed files with 834 additions and 225 deletions

934
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -12,9 +12,9 @@ tracing-subscriber = "0.3.14"
anyhow = "1"
gflags = "^0.3"
nursery = "^0.0.1"
metrics = "0.20.1"
metrics-exporter-prometheus = "0.11.0"
metrics = "0.24.0"
metrics-exporter-prometheus = "0.16.0"
tiny_http = "0.12.0"
socket2 = "0.5.1"
socket2 = "0.5.1"
icmp-socket = "0.2.0"
resolve = "^0.2.0"
resolve = "^0.2.0"

View File

@ -25,7 +25,7 @@ use icmp_socket::{
packet::{Icmpv4Message, Icmpv6Message, WithEchoRequest},
IcmpSocket, IcmpSocket4, IcmpSocket6, Icmpv4Packet, Icmpv6Packet,
};
use metrics::{gauge, histogram, increment_counter, Label};
use metrics::{counter, gauge, histogram};
use nursery::{thread, Nursery};
use tracing::{debug, error, info, instrument, warn};
@ -67,10 +67,10 @@ struct State<AddrType> {
// TODO(jwall): Add histogram for latency as well.
}
fn make_ping_count_labels(domain_name: &str, result: &str) -> Vec<Label> {
vec![
Label::new("domain", domain_name.to_owned()),
Label::new("result", result.to_owned()),
fn make_ping_count_labels(domain_name: &str, result: &str) -> [(&'static str, String); 2] {
[
("domain", domain_name.to_owned()),
("result", result.to_owned()),
]
}
@ -89,18 +89,13 @@ impl<AddrType: std::fmt::Display> State<AddrType> {
seq = sequence,
"Reply",
);
increment_counter!("ping_counter", make_ping_count_labels(domain_name, "ok"),);
counter!("ping_counter", &make_ping_count_labels(domain_name, "ok")).increment(1);
if elapsed as i32 != 0 {
gauge!(
"ping_latency",
elapsed,
vec![Label::new("domain", domain_name.to_owned()),],
);
histogram!(
"ping_latency_hist_ms",
elapsed,
vec![Label::new("domain", domain_name.to_owned()),],
);
let labels = [("domain", domain_name.to_owned())];
let latency = gauge!("ping_latency", &labels);
latency.increment(elapsed);
let latency_hist = histogram!("ping_latency_hist_ms", &labels);
latency_hist.record(elapsed);
}
self.time_tracker
.get_mut(&identifier)
@ -124,10 +119,11 @@ impl<AddrType: std::fmt::Display> State<AddrType> {
seq = sequence,
"Dropped"
);
increment_counter!(
counter!(
"ping_counter",
make_ping_count_labels(domain_name, "dropped"),
);
&make_ping_count_labels(domain_name, "dropped")
)
.increment(1);
for_delete.push(*k);
}
}
@ -186,10 +182,11 @@ impl<'a> PacketHandler<Icmpv6Packet, Ipv6Addr> for &'a mut State<Ipv6Addr> {
},
}) => {
if let Some((domain_name, _addr)) = self.destinations.get(&identifier) {
increment_counter!(
counter!(
"ping_counter",
make_ping_count_labels(domain_name, "unreachable")
);
&make_ping_count_labels(domain_name, "unreachable")
)
.increment(1);
return true;
}
}
@ -299,7 +296,7 @@ where
);
match self.send_to_destination(dest, identifier, sequence) {
Err(e) => {
increment_counter!("ping_counter", make_ping_count_labels(domain_name, "err"),);
counter!("ping_counter", &make_ping_count_labels(domain_name, "err")).increment(1);
error!(
domain=domain_name, %dest, err=?e,
"Error sending. Trying again later",
@ -392,7 +389,8 @@ where
}
Err(e) => {
error!(err = ?e, "Error receiving packet");
increment_counter!("ping_counter", make_ping_count_labels("unknown", "err"),);
counter!("ping_counter", &make_ping_count_labels("unknown", "err"))
.increment(1);
return;
}
}

View File

@ -13,7 +13,7 @@
// limitations under the License.
use gflags;
use metrics::{gauge, increment_counter, Label};
use metrics::{counter, gauge};
use std::convert::From;
use std::io;
use std::net::{SocketAddr, UdpSocket};
@ -70,10 +70,10 @@ fn attempt_stun_connect(addr: SocketAddr) -> Result<SystemTime, ConnectError> {
Ok(SystemTime::now())
}
fn make_count_labels(domain_name: &str, result: &str) -> Vec<Label> {
vec![
Label::new("domain", domain_name.to_owned()),
Label::new("result", result.to_owned()),
fn make_count_labels(domain_name: &str, result: &str) -> [(&'static str, String); 2] {
[
("domain", domain_name.to_owned()),
("result", result.to_owned()),
]
}
@ -82,6 +82,9 @@ fn make_count_labels(domain_name: &str, result: &str) -> Vec<Label> {
fields(domain=domain_name, socket=%s),
)]
pub fn start_listen_thread(domain_name: &str, s: SocketAddr) {
let labels: [(&str, String); 1] = [("domain", domain_name.to_owned())];
let success = gauge!("stun_success", &labels);
debug!("starting thread");
loop {
let now = SystemTime::now();
@ -94,17 +97,14 @@ pub fn start_listen_thread(domain_name: &str, s: SocketAddr) {
millis = finish_time.duration_since(now).unwrap().as_millis(),
conn_type = "Stun connection",
);
increment_counter!("stun_attempt_counter", make_count_labels(domain_name, "ok"));
gauge!(
"stun_attempt_latency_ms",
finish_time.duration_since(now).unwrap().as_millis() as f64,
vec![Label::new("domain", domain_name.to_owned())]
);
gauge!(
"stun_success",
1 as f64,
vec![Label::new("domain", domain_name.to_owned())]
);
counter!(
"stun_attempt_counter",
&make_count_labels(domain_name, "ok")
)
.increment(1);
gauge!("stun_attempt_latency_ms", &labels)
.increment(finish_time.duration_since(now).unwrap().as_millis() as f64);
success.set(1);
}
Err(ConnectError::Timeout(finish_time)) => {
info!(
@ -113,30 +113,24 @@ pub fn start_listen_thread(domain_name: &str, s: SocketAddr) {
millis = finish_time.duration_since(now).unwrap().as_millis(),
conn_type = "Stun connection",
);
increment_counter!(
counter!(
"stun_attempt_counter",
make_count_labels(domain_name, "timeout")
);
gauge!(
"stun_success",
0 as f64,
vec![Label::new("domain", domain_name.to_owned())]
);
&make_count_labels(domain_name, "timeout")
)
.increment(1);
success.set(0);
}
Err(ConnectError::Err(e)) => {
error!(
timeout=true, success=false, err = ?e,
conn_type="Stun connection",
);
increment_counter!(
counter!(
"stun_attempt_counter",
make_count_labels(domain_name, "err")
);
gauge!(
"stun_success",
0 as f64,
vec![Label::new("domain", domain_name.to_owned())]
);
&make_count_labels(domain_name, "err")
)
.increment(1);
success.set(0);
}
Err(ConnectError::Incomplete) => {
error!(
@ -145,15 +139,12 @@ pub fn start_listen_thread(domain_name: &str, s: SocketAddr) {
err = "Incomplete",
conn_type = "Stun connection",
);
increment_counter!(
counter!(
"stun_attempt_counter",
make_count_labels(domain_name, "incomplete")
);
gauge!(
"stun_success",
0 as f64,
vec![Label::new("domain", domain_name.to_owned())]
);
&make_count_labels(domain_name, "incomplete")
)
.increment(1);
success.set(0);
}
}