From 2b41acef69305f482374c076ab93ea766287ae3a Mon Sep 17 00:00:00 2001 From: Liam Vovk <63673978+vovkman@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:25:12 -0500 Subject: [PATCH] feat: add hostname tracking (#7) --- metrics/benches/metrics.rs | 3 ++- metrics/src/metrics.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/metrics/benches/metrics.rs b/metrics/benches/metrics.rs index f8038ef51200b3..73a219843f1f5a 100644 --- a/metrics/benches/metrics.rs +++ b/metrics/benches/metrics.rs @@ -26,9 +26,10 @@ fn bench_write_points(bencher: &mut Bencher) { }) .collect(); let host_id = "benchmark-host-id"; + let host_name = "benchmark-host-name"; bencher.iter(|| { for _ in 0..10 { - test::black_box(serialize_points(&points, host_id)); + test::black_box(serialize_points(&points, host_id, host_name)); } }) } diff --git a/metrics/src/metrics.rs b/metrics/src/metrics.rs index 2c7c025296d14a..5f2c75d4cfd51a 100644 --- a/metrics/src/metrics.rs +++ b/metrics/src/metrics.rs @@ -99,9 +99,10 @@ impl InfluxDbMetricsWriter { } } -pub fn serialize_points(points: &Vec, host_id: &str) -> String { +pub fn serialize_points(points: &Vec, host_id: &str, host_name: &str) -> String { const TIMESTAMP_LEN: usize = 20; const HOST_ID_LEN: usize = 8; // "host_id=".len() + const HOST_NAME_LEN: usize = 10; // "host_name=".len() const EXTRA_LEN: usize = 2; // "=,".len() let mut len = 0; for point in points { @@ -114,10 +115,15 @@ pub fn serialize_points(points: &Vec, host_id: &str) -> String { len += point.name.len(); len += TIMESTAMP_LEN; len += host_id.len() + HOST_ID_LEN; + len += host_name.len() + HOST_NAME_LEN; } let mut line = String::with_capacity(len); for point in points { - let _ = write!(line, "{},host_id={}", &point.name, host_id); + let _ = write!( + line, + "{},host_id={},host_name={}", + &point.name, host_id, host_name + ); for (name, value) in point.tags.iter() { let _ = write!(line, ",{name}={value}"); } @@ -140,8 +146,11 @@ impl MetricsWriter for InfluxDbMetricsWriter { debug!("submitting {} points", points.len()); let host_id = HOST_ID.read().unwrap(); + let host_name = gethostname() + .into_string() + .unwrap_or_else(|_| "".to_string()); - let line = serialize_points(&points, &host_id); + let line = serialize_points(&points, &host_id, &host_name); let client = reqwest::blocking::Client::builder() .timeout(Duration::from_secs(5))