-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add subgraph health endpoint #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3aa66e4
feat: add check subgraph health endpoint
shiyasmohd ff2a9f0
feat: add error message if subgraph is unhealthy/failed
shiyasmohd b15c8dd
docs: add subgraph health example requests & responses
shiyasmohd bd5cf36
Merge branch 'main' of https://github.com/graphprotocol/indexer-rs in…
shiyasmohd d836123
feat: add rate limiting to subgraph health endpoint
shiyasmohd f2cbb6f
refactor: use graph client in subgraph health api
shiyasmohd 4cbac14
fix: use graph_client for response in subgraph health req
shiyasmohd fe50e2f
refactor: syntax improvements
shiyasmohd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use axum::{ | ||
| extract::Path, | ||
| response::{IntoResponse, Response as AxumResponse}, | ||
| Extension, Json, | ||
| }; | ||
| use graphql_client::GraphQLQuery; | ||
| use indexer_config::GraphNodeConfig; | ||
| use reqwest::StatusCode; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde_json::json; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| struct Response { | ||
| data: SubgraphData, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| #[allow(non_snake_case)] | ||
| struct SubgraphData { | ||
| indexingStatuses: Vec<IndexingStatus>, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| #[allow(non_snake_case)] | ||
| struct IndexingStatus { | ||
| health: Health, | ||
| fatalError: Option<Message>, | ||
| nonFatalErrors: Vec<Message>, | ||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize, Debug)] | ||
| struct Message { | ||
| message: String, | ||
| } | ||
|
|
||
| #[derive(GraphQLQuery)] | ||
| #[graphql( | ||
| schema_path = "../graphql/indexing_status.schema.graphql", | ||
| query_path = "../graphql/subgraph_health.query.graphql", | ||
| response_derives = "Debug", | ||
| variables_derives = "Clone" | ||
| )] | ||
| pub struct HealthQuery; | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| #[allow(non_camel_case_types)] | ||
| enum Health { | ||
| healthy, | ||
| unhealthy, | ||
| failed, | ||
| } | ||
|
|
||
| impl Health { | ||
| fn as_str(&self) -> &str { | ||
| match self { | ||
| Health::healthy => "healthy", | ||
| Health::unhealthy => "unhealthy", | ||
| Health::failed => "failed", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum CheckHealthError { | ||
| #[error("Deployment not found")] | ||
| DeploymentNotFound, | ||
| #[error("Failed to process query")] | ||
| QueryForwardingError, | ||
| } | ||
|
|
||
| impl IntoResponse for CheckHealthError { | ||
| fn into_response(self) -> AxumResponse { | ||
| let (status, error_message) = match &self { | ||
| CheckHealthError::DeploymentNotFound => (StatusCode::NOT_FOUND, "Deployment not found"), | ||
| CheckHealthError::QueryForwardingError => { | ||
| (StatusCode::INTERNAL_SERVER_ERROR, "Failed to process query") | ||
| } | ||
| }; | ||
|
|
||
| let body = serde_json::json!({ | ||
| "error": error_message, | ||
| }); | ||
|
|
||
| (status, Json(body)).into_response() | ||
| } | ||
| } | ||
|
|
||
| pub async fn health( | ||
| Path(deployment_id): Path<String>, | ||
| Extension(graph_node): Extension<GraphNodeConfig>, | ||
| ) -> Result<impl IntoResponse, CheckHealthError> { | ||
| let req_body = HealthQuery::build_query(health_query::Variables { | ||
| ids: vec![deployment_id], | ||
| }); | ||
|
|
||
| let client = reqwest::Client::new(); | ||
| let response = client | ||
| .post(graph_node.status_url) | ||
| .json(&req_body) | ||
| .send() | ||
| .await; | ||
| let res = response.expect("Failed to get response"); | ||
| let response_json: Result<Response, reqwest::Error> = res.json().await; | ||
|
|
||
| match response_json { | ||
| Ok(res) => { | ||
| if res.data.indexingStatuses.is_empty() { | ||
| return Err(CheckHealthError::DeploymentNotFound); | ||
| }; | ||
| let status = &res.data.indexingStatuses[0]; | ||
| let health_response = match status.health { | ||
| Health::healthy => json!({ "health": status.health.as_str() }), | ||
| Health::unhealthy => { | ||
| let errors: Vec<&String> = status | ||
| .nonFatalErrors | ||
| .iter() | ||
| .map(|msg| &msg.message) | ||
| .collect(); | ||
| json!({ "health": status.health.as_str(), "nonFatalErrors": errors }) | ||
| } | ||
| Health::failed => { | ||
| json!({ "health": status.health.as_str(), "fatalError": status.fatalError.as_ref().map_or("null", |msg| &msg.message) }) | ||
| } | ||
| }; | ||
| Ok(Json(health_response)) | ||
| } | ||
| Err(_) => Err(CheckHealthError::QueryForwardingError), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.