Compare commits

..

4 Commits

View File

@ -53,7 +53,7 @@ async fn get_baeder_names() -> Result<HashMap<String, String>, String> {
for c in r.captures_iter(baedernames_body.as_str()) { for c in r.captures_iter(baedernames_body.as_str()) {
baedernames.insert( baedernames.insert(
c.get(1).unwrap().as_str().to_string(), // id c.get(1).unwrap().as_str().trim_start_matches('0').to_string(), // id
c.get(2).unwrap().as_str().to_string(), // name c.get(2).unwrap().as_str().to_string(), // name
); );
} }
@ -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)] #[derive(Clone)]
struct AppState { struct AppState {
baedernames: HashMap<String, String>, baedernames: HashMap<String, String>,
@ -114,6 +121,7 @@ async fn main() {
let listener = tokio::net::TcpListener::bind(listen).await.unwrap(); let listener = tokio::net::TcpListener::bind(listen).await.unwrap();
println!("Server listening on: http://{}", listener.local_addr().unwrap()); println!("Server listening on: http://{}", listener.local_addr().unwrap());
println!("Matrics exposed as: http://{}/metrics", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
@ -143,7 +151,14 @@ async fn route_metrics(
let mut out = String::new(); let mut out = String::new();
for bad in trafficdata { for bad in trafficdata {
out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\" badname=\"{}\"}} {}\n", bad.id, state.baedernames.get(&bad.id).unwrap_or(&String::new()), bad.counter.unwrap_or(-1))); let badid = escape_metric_label_value(&bad.id);
match state.baedernames.get(&bad.id) {
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))),
};
} }
return Ok(out); return Ok(out);