|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use axum::{ |
| 5 | + extract::Path, |
| 6 | + response::{IntoResponse, Response as AxumResponse}, |
| 7 | + Extension, Json, |
| 8 | +}; |
| 9 | +use graphql_client::GraphQLQuery; |
| 10 | +use indexer_config::GraphNodeConfig; |
| 11 | +use reqwest::StatusCode; |
| 12 | +use serde_json::json; |
| 13 | +use thiserror::Error; |
| 14 | + |
| 15 | +#[derive(GraphQLQuery)] |
| 16 | +#[graphql( |
| 17 | + schema_path = "../graphql/indexing_status.schema.graphql", |
| 18 | + query_path = "../graphql/subgraph_health.query.graphql", |
| 19 | + response_derives = "Debug", |
| 20 | + variables_derives = "Clone" |
| 21 | +)] |
| 22 | +pub struct HealthQuery; |
| 23 | + |
| 24 | +#[derive(Debug, Error)] |
| 25 | +pub enum CheckHealthError { |
| 26 | + #[error("Failed to send request")] |
| 27 | + RequestFailed, |
| 28 | + #[error("Failed to decode response")] |
| 29 | + BadResponse, |
| 30 | + #[error("Deployment not found")] |
| 31 | + DeploymentNotFound, |
| 32 | + #[error("Invalid health status found")] |
| 33 | + InvalidHealthStatus, |
| 34 | +} |
| 35 | + |
| 36 | +impl IntoResponse for CheckHealthError { |
| 37 | + fn into_response(self) -> AxumResponse { |
| 38 | + let status = match &self { |
| 39 | + CheckHealthError::DeploymentNotFound => StatusCode::NOT_FOUND, |
| 40 | + CheckHealthError::InvalidHealthStatus | CheckHealthError::BadResponse => { |
| 41 | + StatusCode::INTERNAL_SERVER_ERROR |
| 42 | + } |
| 43 | + CheckHealthError::RequestFailed => StatusCode::BAD_GATEWAY, |
| 44 | + }; |
| 45 | + let body = serde_json::json!({ |
| 46 | + "error": self.to_string(), |
| 47 | + }); |
| 48 | + (status, Json(body)).into_response() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub async fn health( |
| 53 | + Path(deployment_id): Path<String>, |
| 54 | + Extension(graph_node): Extension<GraphNodeConfig>, |
| 55 | +) -> Result<impl IntoResponse, CheckHealthError> { |
| 56 | + let req_body = HealthQuery::build_query(health_query::Variables { |
| 57 | + ids: vec![deployment_id], |
| 58 | + }); |
| 59 | + |
| 60 | + let client = reqwest::Client::new(); |
| 61 | + let response = client |
| 62 | + .post(graph_node.status_url) |
| 63 | + .json(&req_body) |
| 64 | + .send() |
| 65 | + .await |
| 66 | + .map_err(|_| CheckHealthError::RequestFailed)?; |
| 67 | + |
| 68 | + let graphql_response: graphql_client::Response<health_query::ResponseData> = response |
| 69 | + .json() |
| 70 | + .await |
| 71 | + .map_err(|_| CheckHealthError::BadResponse)?; |
| 72 | + |
| 73 | + let data = match (graphql_response.data, graphql_response.errors) { |
| 74 | + (Some(data), None) => data, |
| 75 | + _ => return Err(CheckHealthError::BadResponse), |
| 76 | + }; |
| 77 | + |
| 78 | + let Some(status) = data.indexing_statuses.first() else { |
| 79 | + return Err(CheckHealthError::DeploymentNotFound); |
| 80 | + }; |
| 81 | + let health_response = match status.health { |
| 82 | + health_query::Health::healthy => json!({ "health": status.health }), |
| 83 | + health_query::Health::unhealthy => { |
| 84 | + let errors: Vec<&String> = status |
| 85 | + .non_fatal_errors |
| 86 | + .iter() |
| 87 | + .map(|msg| &msg.message) |
| 88 | + .collect(); |
| 89 | + json!({ "health": status.health, "nonFatalErrors": errors }) |
| 90 | + } |
| 91 | + health_query::Health::failed => { |
| 92 | + json!({ "health": status.health, "fatalError": status.fatal_error.as_ref().map_or("null", |msg| &msg.message) }) |
| 93 | + } |
| 94 | + health_query::Health::Other(_) => return Err(CheckHealthError::InvalidHealthStatus), |
| 95 | + }; |
| 96 | + Ok(Json(health_response)) |
| 97 | +} |
0 commit comments