Skip to content

Commit

Permalink
Compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning committed Oct 31, 2024
1 parent a11b49f commit 350a4e1
Show file tree
Hide file tree
Showing 8 changed files with 5,907 additions and 943 deletions.
6,727 changes: 5,849 additions & 878 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ client = { path = "anchor/client" }
qbft = { path = "anchor/qbft" }
http_api = { path = "anchor/http_api" }
http_metrics = { path = "anchor/http_metrics" }
task_executor = { git = "https://github.com/sigp/lighthouse", branch = "anchor", default-features = false, features = [
"tracing",
] }
metrics = { git = "https://github.com/sigp/lighthouse", branch = "anchor" }
validator_metrics = { git = "https://github.com/sigp/lighthouse", branch = "anchor" }
sensitive_url = { git = "https://github.com/sigp/lighthouse", branch = "anchor" }
slot_clock = { git = "https://github.com/sigp/lighthouse", branch = "anchor" }
task_executor = { git = "https://github.com/sigp/lighthouse", branch = "anchor", default-features = false, features = [ "tracing", ] }
metrics = { git = "https://github.com/agemanning/lighthouse", branch = "modularize-vc" }
validator_metrics = { git = "https://github.com/agemanning/lighthouse", branch = "modularize-vc" }
sensitive_url = { git = "https://github.com/agemanning/lighthouse", branch = "modularize-vc" }
slot_clock = { git = "https://github.com/agemanning/lighthouse", branch = "modularize-vc" }
derive_more = { version = "1.0.0", features = ["full"] }
async-channel = "1.9"
axum = "0.7.7"
clap = { version = "4.5.15", features = ["derive", "wrap_help"]}
dirs = "5.0.1"
futures = "0.3.30"
# dirs = "3"
http = "1.1"
tower-http = "0.6"
tower-http = {version = "0.6", features = ["cors"] }
hyper = "1.4"
parking_lot = "0.12"
serde = { version = "1.0.208", features = ["derive"] }
Expand Down
3 changes: 3 additions & 0 deletions anchor/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ path = "src/lib.rs"
[dependencies]
task_executor = { workspace = true }
http_api = { workspace = true }
http_metrics = { workspace = true }
clap = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
sensitive_url = { workspace = true }
dirs = { workspace = true }
hyper = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
parking_lot = { workspace = true }
# Local dependencies
fdlimit = "0.3"
ethereum_hashing = "0.7.0"
Expand Down
4 changes: 2 additions & 2 deletions anchor/client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ pub struct Anchor {
value_name = "PORT",
help = "Set the listen TCP port for the Prometheus metrics HTTP server.",
display_order = 0,
default_value_if("metrics", ArgPredicate::IsPresent, "5064"),
default_value_if("metrics", ArgPredicate::IsPresent, "5164"),
requires = "metrics"
)]
pub metrics_port: u16,

// TODO: Metrics CORS Origin
#[clap(
long,
global = true,
Expand Down
32 changes: 5 additions & 27 deletions anchor/client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Default for Config {
execution_nodes,
allow_unsynced_beacon_node: false,
http_api: <_>::default(),
// http_metrics: <_>::default(),
http_metrics: <_>::default(),
beacon_nodes_tls_certs: None,
execution_nodes_tls_certs: None,
}
Expand Down Expand Up @@ -154,37 +154,15 @@ pub fn from_cli(cli_args: &Anchor) -> Result<Config, String> {
* Prometheus metrics HTTP server
*/

// TODO:
/*
if cli_args.get_flag("metrics") {
if cli_args.metrics {
config.http_metrics.enabled = true;
}

if let Some(address) = cli_args.get_one::<String>("metrics-address") {
config.http_metrics.listen_addr = address
.parse::<IpAddr>()
.map_err(|_| "metrics-address is not a valid IP address.")?;
}
if let Some(port) = cli_args.get_one::<String>("metrics-port") {
config.http_metrics.listen_port = port
.parse::<u16>()
.map_err(|_| "metrics-port is not a valid u16.")?;
if let Some(address) = cli_args.metrics_address {
config.http_metrics.listen_addr = address;
}

if let Some(allow_origin) = cli_args.get_one::<String>("metrics-allow-origin") {
// Pre-validate the config value to give feedback to the user on node startup, instead of
// as late as when the first API response is produced.
hyper::header::HeaderValue::from_str(allow_origin)
.map_err(|_| "Invalid allow-origin value")?;
config.http_metrics.allow_origin = Some(allow_origin.to_string());
}
if cli_args.get_flag(DISABLE_MALLOC_TUNING_FLAG) {
config.http_metrics.allocator_metrics_enabled = false;
}
*/
config.http_metrics.listen_port = cli_args.metrics_port;

Ok(config)
}
Expand Down
25 changes: 14 additions & 11 deletions anchor/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ mod version;

pub use cli::Anchor;
use config::Config;
use parking_lot::RwLock;
use std::net::SocketAddr;
use std::sync::Arc;
use task_executor::TaskExecutor;
use tokio::net::TcpListener;
use tracing::{debug, error, info};

pub struct Client {}

impl Client {
/// Runs the Anchor Client
pub async fn run(_executor: TaskExecutor, config: Config) -> Result<(), String> {
pub async fn run(executor: TaskExecutor, config: Config) -> Result<(), String> {
// Attempt to raise soft fd limit. The behavior is OS specific:
// `linux` - raise soft fd limit to hard
// `macos` - raise soft fd limit to `min(kernel limit, hard fd limit)`
Expand All @@ -39,24 +43,23 @@ impl Client {
);

// Optionally start the metrics server.
let http_metrics_shared_state = if config.http_metrics.enabled {
let _http_metrics_shared_state = if config.http_metrics.enabled {
let shared_state = Arc::new(RwLock::new(http_metrics::Shared { genesis_time: None }));

let exit = context.executor.exit();
let exit = executor.exit();

// Attempt to bind to the socket
let socket = SocketAddr::new(config.listen_addr, config.listen_port);
let listener = TcpListener::bind(socket).await.map_err(|e| format!("Unable to bind to metrics server port: {}", e.to_string()))?;
let socket = SocketAddr::new(config.http_api.listen_addr, config.http_api.listen_port);
let listener = TcpListener::bind(socket)
.await
.map_err(|e| format!("Unable to bind to metrics server port: {}", e.to_string()))?;

let metrics_future = http_metrics::serve(listener, shared_state.clone(), exit)
let metrics_future = http_metrics::serve(listener, shared_state.clone(), exit);

context
.clone()
.executor
.spawn_without_exit(metrics_future, "metrics-http");
executor.spawn_without_exit(metrics_future, "metrics-http");
Some(shared_state)
} else {
info!(log, "HTTP metrics server is disabled");
info!("HTTP metrics server is disabled");
None
};

Expand Down
3 changes: 2 additions & 1 deletion anchor/http_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ axum = { workspace = true }
parking_lot = { workspace = true }
serde = { workspace = true }
tower-http = { workspace = true }
http = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
validator_metrics = { workspace = true }
metrics = { workspace = true }
# Group dependencies
warp_utils = { git = "https://github.com/agemanning/lighthouse", branch = "modularize-vc" }
41 changes: 26 additions & 15 deletions anchor/http_metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
//! This may be a temporary addition, once the Lighthouse VC moves to axum we may be able to group
//! code.
use axum::{extract::State, routing::get, Router};
use axum::{
body::Body,
extract::State,
http::Method,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Router,
};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::net::SocketAddr;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::net::TcpListener;
use tracing::{error, info};

use http::{header, Method, Request, Response};
use tower_http::cors::{Any, CorsLayer};
// use http_body_util::Full;
use tracing::error;

/// Contains objects which have shared access from inside/outside of the metrics server.
pub struct Shared {
/// If we know genesis, it is entered here.
genesis_time: Option<u64>,
pub genesis_time: Option<u64>,
}

/// Configuration for the HTTP server.
Expand Down Expand Up @@ -58,7 +62,7 @@ fn create_router(shared_state: Arc<RwLock<Shared>>) -> Router {
}

/// Gets the prometheus metrics
async fn metrics_handler(State(state): State<Arc<RwLock<Shared>>>) -> &'static str {
async fn metrics_handler(State(state): State<Arc<RwLock<Shared>>>) -> Response<Body> {
// Use common lighthouse validator metrics
use validator_metrics::*;

Expand Down Expand Up @@ -104,11 +108,16 @@ async fn metrics_handler(State(state): State<Arc<RwLock<Shared>>>) -> &'static s

warp_utils::metrics::scrape_health_metrics();

encoder
.encode(&lighthouse_metrics::gather(), &mut buffer)
.unwrap();
encoder.encode(&metrics::gather(), &mut buffer).unwrap();

String::from_utf8(buffer).map_err(|e| format!("Failed to encode prometheus info: {:?}", e))
match String::from_utf8(buffer) {
Ok(v) => v.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to encode promethus data: {}", e),
)
.into_response(),
}
}

/// Creates a server that will serve requests using information from `ctx`.
Expand All @@ -118,13 +127,15 @@ pub async fn serve(
listener: TcpListener,
shared_state: Arc<RwLock<Shared>>,
shutdown: impl Future<Output = ()> + Send + Sync + 'static,
) -> Result<(), ()> {
) {
// Generate the axum routes
let router = create_router(shared_state);

// Start the http api server
axum::serve(listener, router)
if let Err(e) = axum::serve(listener, router)
.with_graceful_shutdown(shutdown)
.await
.map_err(|e| error!(?e, "HTTP Metrics server failed"))
{
error!(?e, "HTTP Metrics server failed");
}
}

0 comments on commit 350a4e1

Please sign in to comment.