Skip to content

Commit

Permalink
metrics: implement metrics server with axum
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jun 22, 2022
1 parent 7d52d39 commit f2c0742
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/sui-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ publish = false
edition = "2021"

[dependencies]
axum = "0.5.9"
anyhow = { version = "1.0.57", features = ["backtrace"] }
clap = { version = "3.1.17", features = ["derive"] }
multiaddr = "0.14.0"
prometheus_exporter = "0.8.4"
prometheus = "0.13.1"
tokio = { version = "1.18.2", features = ["full"] }
tracing = "0.1.34"
parking_lot = "0.12.1"
Expand Down
12 changes: 12 additions & 0 deletions crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use sui_json_rpc::read_api::FullNodeApi;
use sui_json_rpc::read_api::ReadApi;
use sui_json_rpc_api::EventApiServer;

mod metrics;

pub struct SuiNode {
grpc_server: tokio::task::JoinHandle<Result<()>>,
_json_rpc_service: Option<jsonrpsee::http_server::HttpServerHandle>,
Expand All @@ -39,6 +41,15 @@ pub struct SuiNode {

impl SuiNode {
pub async fn start(config: &NodeConfig) -> Result<SuiNode> {
//
// Start metrics server
//
info!(
"Starting Prometheus HTTP endpoint at {}",
config.metrics_address
);
let prometheus_registry = metrics::start_prometheus_server(config.metrics_address);

info!(node =? config.public_key(),
"Initializing sui-node listening on {}", config.network_address
);
Expand Down Expand Up @@ -79,6 +90,7 @@ impl SuiNode {
checkpoint_store,
genesis,
config.enable_event_processing,
&prometheus_registry,
)
.await,
);
Expand Down
9 changes: 0 additions & 9 deletions crates/sui-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use clap::Parser;
use multiaddr::Multiaddr;
use std::path::PathBuf;
use sui_config::{Config, NodeConfig};
use tracing::info;

#[derive(Parser)]
#[clap(rename_all = "kebab-case")]
Expand All @@ -29,14 +28,6 @@ async fn main() -> Result<()> {

let mut config = NodeConfig::load(&args.config_path)?;

// TODO: Switch from prometheus exporter. See https://github.com/MystenLabs/sui/issues/1907
info!(
"Starting Prometheus HTTP endpoint at {}",
config.metrics_address
);
prometheus_exporter::start(config.metrics_address)
.expect("Failed to start Prometheus exporter");

if let Some(listen_address) = args.listen_address {
config.network_address = listen_address;
}
Expand Down
36 changes: 36 additions & 0 deletions crates/sui-node/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use axum::{extract::Extension, http::StatusCode, routing::get, Router};
use prometheus::{Registry, TextEncoder};
use std::net::SocketAddr;

const METRICS_ROUTE: &str = "/metrics";

pub fn start_prometheus_server(addr: SocketAddr) -> Registry {
let registry = Registry::new();

let app = Router::new()
.route(METRICS_ROUTE, get(metrics))
.layer(Extension(registry.clone()));

tokio::spawn(async move {
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
});

registry
}

async fn metrics(Extension(registry): Extension<Registry>) -> (StatusCode, String) {
let metrics_families = registry.gather();
match TextEncoder.encode_to_string(&metrics_families) {
Ok(metrics) => (StatusCode::OK, metrics),
Err(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("unable to encode metrics: {error}"),
),
}
}

0 comments on commit f2c0742

Please sign in to comment.