-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hermes): move metrics endpoint to a separate metrics server
- Loading branch information
1 parent
29dc108
commit 8075d07
Showing
8 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export PYTHNET_HTTP_ADDR=http://pythnet-http-rpc/ | ||
export PYTHNET_WS_ADDR=ws://pythnet-ws-rpc/ | ||
export RPC_LISTEN_ADDR=0.0.0.0:7575 | ||
export METRICS_LISTEN_ADDR=0.0.0.0:7576 | ||
export BENCHMARKS_ENDPOINT=https://benchmarks.pyth.network | ||
export WORMHOLE_SPY_RPC_ADDR=http://spy-or-beacon-rpc | ||
export RUST_LOG=info |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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,17 @@ | ||
use { | ||
clap::Args, | ||
std::net::SocketAddr, | ||
}; | ||
|
||
const DEFAULT_METRICS_SERVER_LISTEN_ADDR: &str = "127.0.0.1:33888"; | ||
|
||
#[derive(Args, Clone, Debug)] | ||
#[command(next_help_heading = "Metrics Options")] | ||
#[group(id = "Metrics")] | ||
pub struct Options { | ||
/// Address and port the RPC server will bind to. | ||
#[arg(long = "metrics-server-listen-addr")] | ||
#[arg(default_value = DEFAULT_METRICS_SERVER_LISTEN_ADDR)] | ||
#[arg(env = "METRICS_LISTEN_ADDR")] | ||
pub server_listen_addr: SocketAddr, | ||
} |
This file contains 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 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,59 @@ | ||
//! Metrics Server | ||
//! | ||
//! This server serves metrics over /metrics in OpenMetrics format. | ||
use { | ||
crate::{ | ||
config::RunOptions, | ||
state::State as AppState, | ||
}, | ||
anyhow::Result, | ||
axum::{ | ||
extract::State, | ||
response::IntoResponse, | ||
routing::get, | ||
Router, | ||
}, | ||
prometheus_client::encoding::text::encode, | ||
std::sync::{ | ||
atomic::Ordering, | ||
Arc, | ||
}, | ||
}; | ||
|
||
|
||
#[tracing::instrument(skip(opts, state))] | ||
pub async fn run(opts: RunOptions, state: Arc<AppState>) -> Result<()> { | ||
tracing::info!(endpoint = %opts.metrics.server_listen_addr, "Starting Metrics Server."); | ||
|
||
let app = Router::new(); | ||
let app = app | ||
.route("/metrics", get(metrics)) | ||
.with_state(state.clone()); | ||
|
||
// Binds the axum's server to the configured address and port. This is a blocking call and will | ||
// not return until the server is shutdown. | ||
axum::Server::try_bind(&opts.metrics.server_listen_addr)? | ||
.serve(app.into_make_service()) | ||
.with_graceful_shutdown(async { | ||
while !crate::SHOULD_EXIT.load(Ordering::Acquire) { | ||
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; | ||
} | ||
|
||
tracing::info!("Shutting down metrics server..."); | ||
}) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn metrics(State(state): State<Arc<AppState>>) -> impl IntoResponse { | ||
let registry = state.metrics_registry.read().await; | ||
let mut buffer = String::new(); | ||
|
||
// Should not fail if the metrics are valid and there is memory available | ||
// to write to the buffer. | ||
encode(&mut buffer, ®istry).unwrap(); | ||
|
||
buffer | ||
} |