1
0
Fork 0

Beautify error handling

This commit is contained in:
clerie 2023-03-23 18:43:09 +01:00
parent 19120d4d0a
commit 7b674a8262
3 changed files with 34 additions and 83 deletions

2
Cargo.lock generated
View File

@ -450,7 +450,7 @@ dependencies = [
[[package]] [[package]]
name = "nixos-exporter" name = "nixos-exporter"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"axum", "axum",
"reqwest", "reqwest",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "nixos-exporter" name = "nixos-exporter"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -1,7 +1,7 @@
use axum::{ use axum::{
extract::{State, Query}, extract::{State, Query},
http::StatusCode, http::StatusCode,
response::{IntoResponse, Response}, response::IntoResponse,
routing::get, routing::get,
Router, Router,
}; };
@ -144,113 +144,64 @@ async fn main() {
} }
fn get_current_system() -> Result<(String, String), String> { fn get_current_system() -> Result<(String, String), String> {
let symlink = match std::fs::read_link("/run/current-system") { let symlink = std::fs::read_link("/run/current-system").map_err(|err| err.to_string())?;
Ok(symlink) => symlink,
Err(err) => return Err(err.to_string()),
};
let (hash, name) = parse_nix_store_path(symlink)?; let (hash, name) = parse_nix_store_path(symlink)?;
Ok((String::from(hash), String::from(name))) Ok((String::from(hash), String::from(name)))
} }
async fn metrics() -> Response { async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
let current_system = get_current_system(); let (hash, name) = get_current_system().map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, ""))?;
let (hash, name) = match current_system {
Ok((hash, name)) => (hash, name),
Err(err) => {
println!("failed: {}", err);
return (StatusCode::INTERNAL_SERVER_ERROR, "").into_response();
}
};
( return Ok((
StatusCode::OK, StatusCode::OK,
format!("nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name) format!("nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
).into_response() ));
} }
async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<String, String>>) -> Response { async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<String, String>>) -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
let target = match params.get("target") { let target = params.get("target")
Some(target) => target, .ok_or_else(|| (StatusCode::NOT_FOUND, "specify target"))?;
None => {
return (StatusCode::NOT_FOUND, "specify target").into_response();
},
};
if target.contains("\"") { if target.contains("\"") {
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid target name").into_response(); return Err((StatusCode::INTERNAL_SERVER_ERROR, "Invalid target name"));
} }
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let prometheus_req = match client.get(format!("{}/api/v1/query?query=nixos_current_system_hash{{{}}}", app_state.prometheus_url, app_state.prometheus_query_tag_template.clone().replace("{}", target))) let prometheus_req = client.get(format!("{}/api/v1/query?query=nixos_current_system_hash{{{}}}", app_state.prometheus_url, app_state.prometheus_query_tag_template.clone().replace("{}", target)))
.header("Accept", "application/json") .header("Accept", "application/json")
.send().await { .send().await
Ok(req) => req, .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Promehteus can't get reached"))?;
Err(_) => {
return (StatusCode::INTERNAL_SERVER_ERROR, "Promehteus can't get reached").into_response();
},
};
if prometheus_req.status() != reqwest::StatusCode::OK {
match prometheus_req.status() { return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra"));
reqwest::StatusCode::OK => (),
_ => {
return (StatusCode::NOT_FOUND, "Target does not exist in Hydra").into_response();
},
} }
let prometheus_body = match prometheus_req.json::<serde_json::Value>().await { let prometheus_body = prometheus_req.json::<serde_json::Value>().await
Ok(body) => body, .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra"))?;
Err(_) => {
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra").into_response();
},
};
let current_system_hash = match prometheus_body["data"]["result"][0]["metric"]["hash"].as_str() { let current_system_hash = prometheus_body["data"]["result"][0]["metric"]["hash"].as_str()
Some(nix_store_path) => nix_store_path, .ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra"))?;
_ => {
return (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra").into_response();
},
};
let hydra_req = match client.get(format!("{}/job/{}/latest", app_state.hydra_url, app_state.hydra_job_template.clone().replace("{}", target))) let hydra_req = client.get(format!("{}/job/{}/latest", app_state.hydra_url, app_state.hydra_job_template.clone().replace("{}", target)))
.header("Accept", "application/json") .header("Accept", "application/json")
.send().await { .send().await
Ok(req) => req, .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Hydra can't get reached"))?;
Err(_) => {
return (StatusCode::INTERNAL_SERVER_ERROR, "Hydra can't get reached").into_response();
},
};
match hydra_req.status() { if hydra_req.status() != reqwest::StatusCode::OK {
reqwest::StatusCode::OK => (), return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra"));
_ => {
return (StatusCode::NOT_FOUND, "Target does not exist in Hydra").into_response();
},
} }
let hydra_body = match hydra_req.json::<serde_json::Value>().await { let hydra_body = hydra_req.json::<serde_json::Value>().await
Ok(body) => body, .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra"))?;
Err(_) => {
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra").into_response();
},
};
let nix_store_path = match hydra_body["buildoutputs"]["out"]["path"].as_str() { let nix_store_path = hydra_body["buildoutputs"]["out"]["path"].as_str()
Some(nix_store_path) => nix_store_path, .ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra"))?;
_ => {
return (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra").into_response();
},
};
let (hydra_system_hash, _) = match parse_nix_store_path(std::path::PathBuf::from(nix_store_path)) { let (hydra_system_hash, _) = parse_nix_store_path(std::path::PathBuf::from(nix_store_path))
Ok((hash, name)) => (hash, name), .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid store path returned by Hydra"))?;
Err(_) => {
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid store path returned by Hydra").into_response();
},
};
let mut status = "0"; let mut status = "0";
@ -258,8 +209,8 @@ async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<S
status = "1"; status = "1";
} }
return ( return Ok((
StatusCode::OK, StatusCode::OK,
format!("nixos_current_system_is_sync{{}} {}\n", status) format!("nixos_current_system_is_sync{{}} {}\n", status)
).into_response(); ));
} }