Skip to content

Commit

Permalink
feat(metrics): added metrics and peer_id endpoints (#1699)
Browse files Browse the repository at this point in the history
* feat(metrics): added metrics and peer_id endpoints

* fix fmt

* Update nox/src/node.rs

Co-authored-by: Aleksey Proshutisnkiy <justprosh@users.noreply.github.com>

---------

Co-authored-by: Aleksey Proshutisnkiy <justprosh@users.noreply.github.com>
  • Loading branch information
gurinderu and justprosh committed Jul 12, 2023
1 parent bc15bdc commit c42b919
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 104 deletions.
40 changes: 39 additions & 1 deletion Cargo.lock

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

6 changes: 2 additions & 4 deletions crates/created-swarm/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ where
let connectivity = node.connectivity.clone();
let aquamarine_api = node.aquamarine_api.clone();
async move {
let outlet = node
.start(Some(peer_id.to_string()))
.await
.expect("node start");
let outlet = node.start(peer_id).await.expect("node start");

CreatedSwarm {
peer_id,
Expand Down Expand Up @@ -312,6 +309,7 @@ pub fn create_swarm_with_runtime<RT: AquaRuntime>(
"builtins_base_dir": config.builtins_dir,
"external_multiaddresses": [config.listen_on],
"spell_base_dir": Some(config.spell_base_dir.clone().unwrap_or(to_abs_path(PathBuf::from("spell")))),
"http_port": null
});

let node_config: UnresolvedConfig =
Expand Down
2 changes: 1 addition & 1 deletion crates/server-config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn default_websocket_port() -> u16 {
9999
}

pub fn default_metrics_port() -> u16 {
pub fn default_http_port() -> u16 {
18080
}

Expand Down
17 changes: 13 additions & 4 deletions crates/server-config/src/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub struct UnresolvedNodeConfig {
#[serde(flatten)]
pub metrics_config: MetricsConfig,

#[serde(flatten)]
pub http_config: Option<HttpConfig>,

#[serde(default)]
pub bootstrap_config: BootstrapConfig,

Expand Down Expand Up @@ -199,6 +202,7 @@ impl UnresolvedNodeConfig {
listen_config: self.listen_config,
allowed_binaries: self.allowed_binaries,
system_services: self.system_services,
http_config: self.http_config,
};

Ok(result)
Expand Down Expand Up @@ -342,6 +346,8 @@ pub struct NodeConfig {
pub allowed_binaries: Vec<String>,

pub system_services: SystemServicesConfig,

pub http_config: Option<HttpConfig>,
}

#[derive(Clone, Deserialize, Serialize, Derivative, Copy)]
Expand Down Expand Up @@ -369,16 +375,19 @@ pub struct TransportConfig {
pub max_established: Option<u32>,
}

#[derive(Clone, Deserialize, Serialize, Derivative, Copy)]
#[derivative(Debug)]
pub struct HttpConfig {
#[serde(default = "default_http_port")]
pub http_port: u16,
}

#[derive(Clone, Deserialize, Serialize, Derivative)]
#[derivative(Debug)]
pub struct MetricsConfig {
#[serde(default = "default_metrics_enabled")]
pub metrics_enabled: bool,

/// Metrics port
#[serde(default = "default_metrics_port")]
pub metrics_port: u16,

#[serde(default = "default_services_metrics_timer_resolution")]
#[serde(with = "humantime_serde")]
pub metrics_timer_resolution: Duration,
Expand Down
8 changes: 3 additions & 5 deletions crates/server-config/src/resolved_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ impl ResolvedConfig {
addrs
}

pub fn metrics_listen_addr(&self) -> SocketAddr {
SocketAddr::new(
self.listen_config.listen_ip,
self.metrics_config.metrics_port,
)
pub fn http_listen_addr(&self) -> Option<SocketAddr> {
self.http_config
.map(|config| SocketAddr::new(self.listen_config.listen_ip, config.http_port))
}

pub fn listen_multiaddrs(&self) -> Vec<Multiaddr> {
Expand Down
2 changes: 1 addition & 1 deletion nox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ humantime-serde = { workspace = true }
log = { workspace = true }
tracing-log = { version = "0.1.3" }
console-subscriber = { version = "0.1.10", features = ["parking_lot"] }
hyper = "0.14.27"
axum = { version = "0.6.18", features = ["macros"] }
itertools = { workspace = true }
eyre = { workspace = true }
base64 = { workspace = true }
Expand Down
73 changes: 73 additions & 0 deletions nox/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use axum::body::Body;
use axum::http::header::CONTENT_TYPE;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Json, Router,
};
use libp2p::PeerId;
use prometheus_client::encoding::text::encode;
use prometheus_client::registry::Registry;
use serde_json::json;
use std::net::SocketAddr;
use std::sync::Arc;

async fn handler_404() -> impl IntoResponse {
(StatusCode::NOT_FOUND, "nothing to see here")
}

async fn handle_metrics(State(state): State<RouteState>) -> Response<Body> {
let mut buf = String::new();
let registry = state
.0
.registry
.as_ref()
.expect("Registry is not initialized");
encode(&mut buf, registry).unwrap(); //TODO: fix unwrap

let body = Body::from(buf);
Response::builder()
.header(
CONTENT_TYPE,
"application/openmetrics-text; version=1.0.0; charset=utf-8",
)
.body(body)
.unwrap() //TODO: fix unwrap
}

async fn handle_peer_id(State(state): State<RouteState>) -> Response {
let peer_id = state.0.peer_id;
Json(json!({
"peer_id": peer_id.to_string(),
}))
.into_response()
}

#[derive(Debug, Clone)]
struct RouteState(Arc<Inner>);

#[derive(Debug)]
struct Inner {
registry: Option<Registry>,
peer_id: PeerId,
}

pub async fn start_http_endpoint(
listen_addr: SocketAddr,
registry: Option<Registry>,
peer_id: PeerId,
) {
let state = RouteState(Arc::new(Inner { registry, peer_id }));
let app: Router = Router::new()
.route("/metrics", get(handle_metrics))
.route("/peer_id", get(handle_peer_id))
.fallback(handler_404)
.with_state(state);

axum::Server::bind(&listen_addr)
.serve(app.into_make_service())
.await
.expect("Could not make http endpoint")
}
2 changes: 1 addition & 1 deletion nox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod builtins;
mod connectivity;
mod dispatcher;
mod effectors;
mod http;
mod layers;
mod metrics;
mod node;
mod tasks;

Expand Down
5 changes: 1 addition & 4 deletions nox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ async fn start_fluence(config: ResolvedConfig) -> eyre::Result<impl Stoppable> {
Node::new(config, vm_config, VERSION).wrap_err("error create node instance")?;
node.listen(listen_addrs).wrap_err("error on listen")?;

let node_exit_outlet = node
.start(Some(peer_id.to_string()))
.await
.wrap_err("node failed to start")?;
let node_exit_outlet = node.start(peer_id).await.wrap_err("node failed to start")?;

struct Fluence {
node_exit_outlet: oneshot::Sender<()>,
Expand Down
64 changes: 0 additions & 64 deletions nox/src/metrics.rs

This file was deleted.

Loading

0 comments on commit c42b919

Please sign in to comment.