Skip to content

Commit 2ae49b5

Browse files
committed
simple metric.
1 parent 87f14fc commit 2ae49b5

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util/health/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "movement-health"
3+
description = "Health utilities for Movement services"
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
authors.workspace = true
8+
repository.workspace = true
9+
homepage.workspace = true
10+
publish.workspace = true
11+
rust-version.workspace = true
12+
13+
[dependencies]
14+
poem = { workspace = true }
15+
anyhow = { workspace = true }
16+
17+
[lints]
18+
workspace = true

util/health/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use anyhow::Error;
2+
use poem::listener::TcpListener;
3+
use poem::{get, handler, IntoResponse, Response, Route, Server};
4+
5+
pub async fn run_service(hostname: String, port: u16) -> Result<(), Error> {
6+
let route = Route::new().at("/health", get(health));
7+
let url = format!("{}:{}", hostname, port);
8+
tracing::info!("Start health check access on :{url} .");
9+
Server::new(TcpListener::bind(url)).run(route).await.map_err(Into::into)
10+
}
11+
12+
#[handler]
13+
async fn health() -> Response {
14+
"{\"OK\": \"healthy\"}".into_response()
15+
}

util/tracing/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ hex = { workspace = true }
2525
rand = { workspace = true }
2626
ring = "0.17.7"
2727
warp = "0.3"
28+
poem = { workspace = true }
2829
prometheus = "0.13"
2930
clap = { workspace = true }
31+
anyhow = { workspace = true }
3032
lazy_static = "1.4.0"
3133

3234
[lints]

util/tracing/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use tokio::runtime::Runtime;
1414
use tokio::time;
1515
use warp::Filter;
1616

17+
pub mod simple_metrics;
18+
1719
// Create a default NodeConfig for telemetry
1820
static DEFAULT_NODE_CONFIG: Lazy<NodeConfig> = Lazy::new(|| {
1921
let mut config = NodeConfig::default();

util/tracing/src/simple_metrics.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use poem::http::StatusCode;
2+
use poem::{get, handler, listener::TcpListener, IntoResponse, Route, Server};
3+
use prometheus::{gather, Encoder, TextEncoder};
4+
use tokio::task::JoinHandle;
5+
6+
/// Start a simple metrics server on the given hostname and port. This is for the usage other than the node.
7+
pub async fn start_metrics_server(listen_hostname: String, listen_port: u16) -> Result<JoinHandle<()>, anyhow::Error> {
8+
let bind_address = format!("{}:{}", listen_hostname, listen_port);
9+
10+
let metrics_route = Route::new().at("/metrics", get(metrics_handler));
11+
12+
let server_handle = tokio::spawn(async move {
13+
let listener = TcpListener::bind(&bind_address);
14+
aptos_logger::info!("Starting Prometheus metrics server on http://{}/metrics", bind_address);
15+
16+
if let Err(e) = Server::new(listener).run(metrics_route).await {
17+
aptos_logger::error!("Metrics server error: {}", e);
18+
}
19+
});
20+
21+
Ok(server_handle)
22+
}
23+
24+
#[handler]
25+
async fn metrics_handler() -> impl IntoResponse {
26+
let metrics = gather();
27+
let encoder = TextEncoder::new();
28+
let mut buffer = vec![];
29+
30+
match encoder.encode(&metrics, &mut buffer) {
31+
Ok(_) => match String::from_utf8(buffer) {
32+
Ok(metrics_text) => poem::Response::builder()
33+
.status(StatusCode::OK)
34+
.header("content-type", "text/plain")
35+
.body(metrics_text),
36+
Err(_) => poem::Response::builder()
37+
.status(StatusCode::INTERNAL_SERVER_ERROR)
38+
.body("Error encoding metrics"),
39+
},
40+
Err(_) => poem::Response::builder()
41+
.status(StatusCode::INTERNAL_SERVER_ERROR)
42+
.body("Error gathering metrics"),
43+
}
44+
}

0 commit comments

Comments
 (0)