-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add atlas-server observability metrics #47
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
Open
pthmas
wants to merge
5
commits into
main
Choose a base branch
from
pthmas/observability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df1ebb2
feat: add atlas-server observability metrics
pthmas 9675bff
fix: address observability review feedback
pthmas 5f3ada6
fix: satisfy backend clippy
pthmas b629530
feat: extend observability with lag, missing blocks, and processing d…
pthmas 948d0fc
fix: drive head-block metrics from batch watermark, not end_block
pthmas 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; | ||
| use chrono::{DateTime, Utc}; | ||
| use serde::Serialize; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::api::AppState; | ||
|
|
||
| const MAX_INDEXER_AGE_MINUTES: i64 = 5; | ||
|
|
||
| #[derive(Serialize)] | ||
| struct HealthResponse { | ||
| status: &'static str, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| reason: Option<String>, | ||
| } | ||
|
|
||
| fn readiness_status( | ||
| latest_indexed_at: Option<DateTime<Utc>>, | ||
| now: DateTime<Utc>, | ||
| ) -> (StatusCode, HealthResponse) { | ||
| let Some(indexed_at) = latest_indexed_at else { | ||
| return ( | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| HealthResponse { | ||
| status: "not_ready", | ||
| reason: Some("indexer state unavailable".to_string()), | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| let age = now - indexed_at; | ||
| if age > chrono::Duration::minutes(MAX_INDEXER_AGE_MINUTES) { | ||
| return ( | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| HealthResponse { | ||
| status: "not_ready", | ||
| reason: Some(format!( | ||
| "indexer stale: last block indexed {}s ago", | ||
| age.num_seconds() | ||
| )), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| ( | ||
| StatusCode::OK, | ||
| HealthResponse { | ||
| status: "ready", | ||
| reason: None, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| /// GET /health/live — liveness probe (process is alive) | ||
| pub async fn liveness() -> impl IntoResponse { | ||
| Json(HealthResponse { | ||
| status: "ok", | ||
| reason: None, | ||
| }) | ||
| } | ||
|
|
||
| /// GET /health/ready — readiness probe (DB reachable, indexer fresh) | ||
| pub async fn readiness(State(state): State<Arc<AppState>>) -> impl IntoResponse { | ||
| // Check DB connectivity | ||
| if let Err(e) = sqlx::query("SELECT 1").execute(&state.pool).await { | ||
| tracing::warn!(error = %e, "readiness database check failed"); | ||
| return ( | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| Json(HealthResponse { | ||
| status: "not_ready", | ||
| reason: Some("database unreachable".to_string()), | ||
| }), | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| let latest = match super::status::latest_indexed_block(state.as_ref()).await { | ||
| Ok(latest) => latest, | ||
| Err(e) => { | ||
| tracing::warn!(error = %e, "readiness indexer state check failed"); | ||
| return ( | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| Json(HealthResponse { | ||
| status: "not_ready", | ||
| reason: Some("indexer state unavailable".to_string()), | ||
| }), | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| let (status, body) = readiness_status(latest.map(|(_, indexed_at)| indexed_at), Utc::now()); | ||
| (status, Json(body)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::head::HeadTracker; | ||
| use crate::metrics::Metrics; | ||
| use axum::body::to_bytes; | ||
| use sqlx::postgres::PgPoolOptions; | ||
| use std::sync::Arc; | ||
| use tokio::sync::broadcast; | ||
|
|
||
| fn app_state(pool: sqlx::PgPool, head_tracker: Arc<HeadTracker>) -> Arc<AppState> { | ||
| let (block_tx, _) = broadcast::channel(1); | ||
| let (da_tx, _) = broadcast::channel(1); | ||
| let prometheus_handle = metrics_exporter_prometheus::PrometheusBuilder::new() | ||
| .build_recorder() | ||
| .handle(); | ||
|
|
||
| Arc::new(AppState { | ||
| pool, | ||
| block_events_tx: block_tx, | ||
| da_events_tx: da_tx, | ||
| head_tracker, | ||
| rpc_url: String::new(), | ||
| da_tracking_enabled: false, | ||
| faucet: None, | ||
| chain_id: 1, | ||
| chain_name: "Test Chain".to_string(), | ||
| chain_logo_url: None, | ||
| accent_color: None, | ||
| background_color_dark: None, | ||
| background_color_light: None, | ||
| success_color: None, | ||
| error_color: None, | ||
| metrics: Metrics::new(), | ||
| prometheus_handle, | ||
| }) | ||
| } | ||
|
|
||
| async fn json_response(response: axum::response::Response) -> (StatusCode, serde_json::Value) { | ||
| let status = response.status(); | ||
| let body = to_bytes(response.into_body(), usize::MAX) | ||
| .await | ||
| .expect("read response body"); | ||
| let json = serde_json::from_slice(&body).expect("parse json response"); | ||
| (status, json) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn liveness_returns_ok() { | ||
| let (status, json) = json_response(liveness().await.into_response()).await; | ||
|
|
||
| assert_eq!(status, StatusCode::OK); | ||
| assert_eq!(json["status"], "ok"); | ||
| assert!(json.get("reason").is_none()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn readiness_returns_unavailable_when_database_is_down() { | ||
| let pool = PgPoolOptions::new() | ||
| .connect_lazy("postgres://postgres:postgres@127.0.0.1:1/atlas") | ||
| .expect("create lazy pool"); | ||
| let state = app_state(pool, Arc::new(HeadTracker::empty(10))); | ||
|
|
||
| let (status, json) = json_response(readiness(State(state)).await.into_response()).await; | ||
|
|
||
| assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); | ||
| assert_eq!(json["status"], "not_ready"); | ||
| assert_eq!(json["reason"], "database unreachable"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn readiness_returns_unavailable_when_indexer_state_is_missing() { | ||
| let (status, body) = readiness_status(None, Utc::now()); | ||
| assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); | ||
| assert_eq!(body.status, "not_ready"); | ||
| assert_eq!(body.reason.as_deref(), Some("indexer state unavailable")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn readiness_returns_unavailable_for_stale_indexer_state() { | ||
| let (status, body) = readiness_status( | ||
| Some(Utc::now() - chrono::Duration::minutes(MAX_INDEXER_AGE_MINUTES + 1)), | ||
| Utc::now(), | ||
| ); | ||
| assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); | ||
| assert_eq!(body.status, "not_ready"); | ||
| assert!(body | ||
| .reason | ||
| .as_deref() | ||
| .expect("reason string") | ||
| .contains("indexer stale")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn readiness_returns_ready_for_fresh_indexer_state() { | ||
| let (status, body) = readiness_status(Some(Utc::now()), Utc::now()); | ||
| assert_eq!(status, StatusCode::OK); | ||
| assert_eq!(body.status, "ready"); | ||
| assert!(body.reason.is_none()); | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,63 @@ | ||
| use axum::extract::State; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::api::AppState; | ||
|
|
||
| /// GET /metrics — Prometheus text format | ||
| pub async fn metrics(State(state): State<Arc<AppState>>) -> String { | ||
| state.prometheus_handle.render() | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::head::HeadTracker; | ||
| use crate::metrics::Metrics; | ||
| use sqlx::postgres::PgPoolOptions; | ||
| use std::sync::OnceLock; | ||
| use tokio::sync::broadcast; | ||
|
|
||
| fn test_prometheus_handle() -> metrics_exporter_prometheus::PrometheusHandle { | ||
| static PROMETHEUS_HANDLE: OnceLock<metrics_exporter_prometheus::PrometheusHandle> = | ||
| OnceLock::new(); | ||
|
|
||
| PROMETHEUS_HANDLE | ||
| .get_or_init(crate::metrics::install_prometheus_recorder) | ||
| .clone() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn metrics_handler_renders_prometheus_output() { | ||
| let pool = PgPoolOptions::new() | ||
| .connect_lazy("postgres://test@localhost:5432/test") | ||
| .expect("lazy pool"); | ||
| let (block_tx, _) = broadcast::channel(1); | ||
| let (da_tx, _) = broadcast::channel(1); | ||
| let prometheus_handle = test_prometheus_handle(); | ||
| let recorder_metrics = Metrics::new(); | ||
| recorder_metrics.set_indexer_head_block(42); | ||
| let state = Arc::new(AppState { | ||
| pool, | ||
| block_events_tx: block_tx, | ||
| da_events_tx: da_tx, | ||
| head_tracker: Arc::new(HeadTracker::empty(10)), | ||
| rpc_url: String::new(), | ||
| da_tracking_enabled: false, | ||
| faucet: None, | ||
| chain_id: 1, | ||
| chain_name: "Test Chain".to_string(), | ||
| chain_logo_url: None, | ||
| accent_color: None, | ||
| background_color_dark: None, | ||
| background_color_light: None, | ||
| success_color: None, | ||
| error_color: None, | ||
| metrics: recorder_metrics, | ||
| prometheus_handle, | ||
| }); | ||
|
|
||
| let body = super::metrics(State(state)).await; | ||
|
|
||
| assert!(body.contains("atlas_indexer_head_block")); | ||
| } | ||
| } | ||
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.