perf(observability): optimize metric flushing to reduce memory allocations

Use a pre-allocated buffer for metric serialization to improve efficiency when processing large batches.
This commit is contained in:
Nils Andreas Svee committed 2026-04-13 16:54:33 +07:00
1 parent 530a715bdf
commit 0f902e27a3
1 file changed
+35 -25
+35 -25
View File
@@ -185,16 +185,14 @@ impl Metrics {
} }
fn flush(&self, transport: &mut Transport, batch: &[Metric], host_tag: &str) { fn flush(&self, transport: &mut Transport, batch: &[Metric], host_tag: &str) {
let lines: Vec<String> = batch let mut data = String::with_capacity(batch.len() * 300);
.iter() for metric in batch {
.map(|m| self.to_influx_line(m, host_tag)) self.write_lines(metric, host_tag, &mut data);
.collect(); }
let data = lines.join("\n");
transport.send(&data); transport.send(&data);
} }
pub fn to_influx_line(&self, metric: &Metric, host_tag: &str) -> String { fn write_lines(&self, metric: &Metric, host_tag: &str, out: &mut String) {
match metric { match metric {
Metric::Ping { Metric::Ping {
reflector, reflector,
@@ -203,10 +201,14 @@ impl Metrics {
up_time, up_time,
down_time, down_time,
timestamp_ns, timestamp_ns,
} => format!( } => {
"sqm_ping,host={host_tag},reflector={reflector},type={measurement_type} \ writeln!(
rtt={rtt:.3},up_time={up_time:.3},down_time={down_time:.3} {timestamp_ns}i\n" out,
), "sqm_ping,host={host_tag},reflector={reflector},type={measurement_type} \
rtt={rtt:.3},up_time={up_time:.3},down_time={down_time:.3} {timestamp_ns}i"
)
.unwrap_or(());
}
Metric::Rate { Metric::Rate {
dl_rate, dl_rate,
ul_rate, ul_rate,
@@ -215,14 +217,16 @@ impl Metrics {
delta_delay_down, delta_delay_down,
delta_delay_up, delta_delay_up,
timestamp_ns, timestamp_ns,
} => format!( } => {
"sqm_rate,host={host_tag},direction=download \ writeln!(out,
rate_kbps={dl:.0},load={rx_load:.4},delta_delay={delta_delay_down:.3} {timestamp_ns}i\n\ "sqm_rate,host={host_tag},direction=download \
sqm_rate,host={host_tag},direction=upload \ rate_kbps={dl_rate:.0},load={rx_load:.4},delta_delay={delta_delay_down:.3} {timestamp_ns}i"
rate_kbps={ul:.0},load={tx_load:.4},delta_delay={delta_delay_up:.3} {timestamp_ns}i\n", ).unwrap_or(());
dl = dl_rate, writeln!(out,
ul = ul_rate, "sqm_rate,host={host_tag},direction=upload \
), rate_kbps={ul_rate:.0},load={tx_load:.4},delta_delay={delta_delay_up:.3} {timestamp_ns}i"
).unwrap_or(());
}
Metric::Baseline { Metric::Baseline {
reflector, reflector,
baseline_up_ewma, baseline_up_ewma,
@@ -230,10 +234,16 @@ impl Metrics {
recent_up_ewma, recent_up_ewma,
recent_down_ewma, recent_down_ewma,
timestamp_ns, timestamp_ns,
} => format!( } => {
"sqm_baseline,host={host_tag},reflector={reflector},direction=up baseline_ewma={baseline_up_ewma:.3},recent_ewma={recent_up_ewma:.3} {timestamp_ns}i\n writeln!(out,
sqm_baseline,host={host_tag},reflector={reflector},direction=down baseline_ewma={baseline_down_ewma:.3},recent_ewma={recent_down_ewma:.3} {timestamp_ns}i\n" "sqm_baseline,host={host_tag},reflector={reflector},direction=up \
), baseline_ewma={baseline_up_ewma:.3},recent_ewma={recent_up_ewma:.3} {timestamp_ns}i"
).unwrap_or(());
writeln!(out,
"sqm_baseline,host={host_tag},reflector={reflector},direction=down \
baseline_ewma={baseline_down_ewma:.3},recent_ewma={recent_down_ewma:.3} {timestamp_ns}i"
).unwrap_or(());
}
Metric::Event { Metric::Event {
name, name,
reason, reason,
@@ -249,8 +259,8 @@ impl Metrics {
if !reason.is_empty() { if !reason.is_empty() {
write!(tags, ",reason={reason}").unwrap_or(()); write!(tags, ",reason={reason}").unwrap_or(());
} }
format!("sqm_event,{tags} count=1i {timestamp_ns}\n") writeln!(out, "sqm_event,{tags} count=1i {timestamp_ns}").unwrap_or(());
}, }
} }
} }
} }