Escape metric label values

This commit is contained in:
clerie 2024-07-21 12:38:36 +02:00
parent 011fbdd2b7
commit a1bcaf93d1

View File

@ -62,6 +62,13 @@ async fn get_baeder_names() -> Result<HashMap<String, String>, String> {
}
fn escape_metric_label_value(value: &String) -> String {
let value = value.replace(r#"\"#, r#"\\"#);
let value = value.replace("\n", r"\n");
let value = value.replace(r#"""#, r#"\""#);
return value;
}
#[derive(Clone)]
struct AppState {
baedernames: HashMap<String, String>,
@ -143,9 +150,13 @@ async fn route_metrics(
let mut out = String::new();
for bad in trafficdata {
let badid = escape_metric_label_value(&bad.id);
match state.baedernames.get(&bad.id) {
Some(name) => out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\", badname=\"{}\"}} {}\n", bad.id, name, bad.counter.unwrap_or(-1))),
None => out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\"}} {}\n", bad.id, bad.counter.unwrap_or(-1))),
Some(name) => {
let badname = escape_metric_label_value(name);
out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\", badname=\"{}\"}} {}\n", badid, badname, bad.counter.unwrap_or(-1)))}
,
None => out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\"}} {}\n", badid, bad.counter.unwrap_or(-1))),
};
}