Skip to content

Commit

Permalink
feat: add hostname tracking (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkman committed Nov 14, 2024
1 parent 4a962de commit 2b41ace
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion metrics/benches/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
})
}
Expand Down
15 changes: 12 additions & 3 deletions metrics/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ impl InfluxDbMetricsWriter {
}
}

pub fn serialize_points(points: &Vec<DataPoint>, host_id: &str) -> String {
pub fn serialize_points(points: &Vec<DataPoint>, 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 {
Expand All @@ -114,10 +115,15 @@ pub fn serialize_points(points: &Vec<DataPoint>, 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}");
}
Expand All @@ -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))
Expand Down

0 comments on commit 2b41ace

Please sign in to comment.