Skip to content

Commit b2ae62a

Browse files
vid277cdxker
authored andcommitted
feature: setup basic actix metrics server
1 parent 33f340e commit b2ae62a

6 files changed

Lines changed: 65 additions & 5 deletions

File tree

crates/grpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tokio = { workspace = true, features = ["full"] }
1818
tonic = { workspace = true }
1919
types = { path = "../types" }
2020
tracing = { workspace = true }
21+
metrics = "0.24"
2122

2223
[lints]
2324
workspace = true

crates/grpc/src/grpc_handler.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ impl NodeControl for NodeControlService {
9191
&self,
9292
request: Request<CheckBalanceRequest>,
9393
) -> Result<Response<CheckBalanceResponse>, Status> {
94-
let request = request.into_inner();
95-
let response = grpc_operator::check_balance(&self.network, request).await?;
96-
Ok(Response::new(response))
94+
let result = {
95+
let request = request.into_inner();
96+
grpc_operator::check_balance(&self.network, request).await
97+
};
98+
99+
match result {
100+
Ok(resp) => Ok(Response::new(resp)),
101+
Err(status) => Err(status),
102+
}
97103
}
98104

99105
async fn get_chain_info(

crates/node/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ num-traits = "0.2.19"
6161
abci = { path = "../abci" }
6262
messenger = { path = "../../messenger" }
6363
grpc = { path = "../grpc" }
64+
metrics = "0.24"
65+
metrics-exporter-prometheus = { version = "0.17", features = ["http-listener"] }
66+
actix-web = "4"
6467

6568
[dev-dependencies]
6669
tempfile = "3.8"

crates/node/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub struct PeerData {
3636

3737
pub struct NodeState<N: Network, W: Wallet> {
3838
pub handlers: Vec<Box<dyn Handler<N, W>>>,
39-
4039
pub peer_id: PeerId,
4140
pub peers: HashSet<PeerId>,
4241

crates/node/src/start_node.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ use crate::{
66
NodeConfig, NodeState, key_manager::load_and_decrypt_keypair, swarm_manager::build_swarm,
77
wallet::TaprootWallet,
88
};
9+
use actix_web::{App, HttpResponse, HttpServer, web};
910
use bitcoin::Network;
1011
use grpc::grpc_handler::NodeControlService;
12+
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
1113
use std::path::{Path, PathBuf};
14+
use std::sync::Arc;
1215
use tokio::sync::broadcast;
1316
use tonic::transport::Server;
1417
use tracing_appender::rolling::{RollingFileAppender, Rotation};
1518
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
1619

17-
use std::sync::Arc;
20+
type PrometheusHandler = Arc<PrometheusHandle>;
1821

1922
pub async fn start_node(
2023
config: NodeConfig,
@@ -76,6 +79,43 @@ pub async fn start_node(
7679
tracing::info!("Logging initialized with console output only");
7780
}
7881

82+
let prometheus_handle: Arc<PrometheusHandle> = {
83+
let builder = PrometheusBuilder::new();
84+
Arc::new(
85+
builder
86+
.install_recorder()
87+
.expect("failed to install Prometheus recorder"),
88+
)
89+
};
90+
91+
let metrics_server_handle = tokio::spawn(async move {
92+
async fn metrics_endpoint(handler: web::Data<PrometheusHandler>) -> HttpResponse {
93+
metrics::counter!("metrics_scrape_requests_total").increment(1);
94+
HttpResponse::Ok()
95+
.content_type("text/plain")
96+
.body(handler.render())
97+
}
98+
99+
async fn health_endpoint() -> HttpResponse {
100+
HttpResponse::Ok().content_type("text/plain").body("OK")
101+
}
102+
103+
tracing::info!("Starting metrics server on 0.0.0.0:8080");
104+
105+
let server = HttpServer::new(move || {
106+
App::new()
107+
.app_data(web::Data::new(prometheus_handle.clone()))
108+
.route("/metrics", web::get().to(metrics_endpoint))
109+
.route("/health", web::get().to(health_endpoint))
110+
})
111+
.bind(("0.0.0.0", 8080))
112+
.expect("Failed to bind metrics endpoint");
113+
114+
tracing::info!("Metrics server bound successfully, starting to serve");
115+
116+
server.run().await.expect("Metrics server failed");
117+
});
118+
79119
let keypair = match load_and_decrypt_keypair(&config) {
80120
Ok(kp) => kp,
81121
Err(e) => {
@@ -218,6 +258,12 @@ pub async fn start_node(
218258
Err(e) => tracing::error!("Chain interface error: {}", e),
219259
}
220260
}
261+
result = metrics_server_handle => {
262+
match result {
263+
Ok(()) => tracing::info!("Metrics server stopped"),
264+
Err(e) => tracing::error!("Metrics server error: {}", e),
265+
}
266+
}
221267
}
222268

223269
Ok(())

docker-compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
entrypoint: "/app/cli run --key-file-path /app/configs/node_1.json --config-file-path /app/configs/node_1.yaml --use-mock-oracle"
1212
ports:
1313
- "50051:50051"
14+
- "8081:8080"
1415
volumes:
1516
- ./keys/node_1/node_1.json:/app/configs/node_1.json
1617
- ./keys/node_1/node_1.yaml:/app/configs/node_1.yaml
@@ -29,6 +30,7 @@ services:
2930
entrypoint: "/app/cli run --key-file-path /app/configs/node_2.json --config-file-path /app/configs/node_2.yaml --use-mock-oracle"
3031
ports:
3132
- "50052:50051"
33+
- "8082:8080"
3234
volumes:
3335
- ./keys/node_2/node_2.json:/app/configs/node_2.json
3436
- ./keys/node_2/node_2.yaml:/app/configs/node_2.yaml
@@ -47,6 +49,7 @@ services:
4749
entrypoint: "/app/cli run --key-file-path /app/configs/node_3.json --config-file-path /app/configs/node_3.yaml --use-mock-oracle"
4850
ports:
4951
- "50053:50051"
52+
- "8083:8080"
5053
volumes:
5154
- ./keys/node_3/node_3.json:/app/configs/node_3.json
5255
- ./keys/node_3/node_3.yaml:/app/configs/node_3.yaml
@@ -65,6 +68,7 @@ services:
6568
entrypoint: "/app/cli run --key-file-path /app/configs/node_4.json --config-file-path /app/configs/node_4.yaml --use-mock-oracle"
6669
ports:
6770
- "50054:50051"
71+
- "8084:8080"
6872
volumes:
6973
- ./keys/node_4/node_4.json:/app/configs/node_4.json
7074
- ./keys/node_4/node_4.yaml:/app/configs/node_4.yaml
@@ -83,6 +87,7 @@ services:
8387
entrypoint: "/app/cli run --key-file-path /app/configs/node_5.json --config-file-path /app/configs/node_5.yaml --use-mock-oracle"
8488
ports:
8589
- "50055:50051"
90+
- "8085:8080"
8691
volumes:
8792
- ./keys/node_5/node_5.json:/app/configs/node_5.json
8893
- ./keys/node_5/node_5.yaml:/app/configs/node_5.yaml

0 commit comments

Comments
 (0)