Skip to content

Commit

Permalink
Add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Oct 25, 2024
1 parent d012d39 commit 5bf95a1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
48 changes: 36 additions & 12 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ serde_json = "1.0.96"
reth-rpc-layer = { git = "https://github.com/paradigmxyz/reth.git", tag = "v1.0.7" }
reth-optimism-payload-builder = { git = "https://github.com/paradigmxyz/reth.git", tag = "v1.0.7", features = ["optimism"] }
futures = "0.3.31"
metrics-derive = "0.1"
metrics = "0.24.0"

[dev-dependencies]
anyhow = "1.0"
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use jsonrpsee::http_client::transport::HttpBackend;
use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::server::Server;
use jsonrpsee::RpcModule;
use metrics::ServerMetrics;
use proxy::ProxyLayer;
use reth_rpc_layer::{AuthClientLayer, AuthClientService};
use server::{EngineApiServer, EthEngineApi};
Expand All @@ -16,6 +17,7 @@ use tracing::{info, Level};
use tracing_subscriber::EnvFilter;

mod error;
mod metrics;
mod proxy;
mod selector;
mod server;
Expand Down Expand Up @@ -82,6 +84,8 @@ async fn main() -> Result<()> {
.with_env_filter(EnvFilter::new(args.log_level.to_string())) // Set the log level
.init();

let metrics = ServerMetrics::default();

// Handle JWT secret
let jwt_secret = match (args.jwt_path, args.jwt_token) {
(Some(file), None) => {
Expand Down Expand Up @@ -127,6 +131,7 @@ async fn main() -> Result<()> {
.map(|c| Arc::new(c.clone()))
.collect(),
args.boost_sync,
metrics,
);
let mut module: RpcModule<()> = RpcModule::new(());
module
Expand Down
18 changes: 18 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use metrics::Counter;
use metrics_derive::Metrics;

#[derive(Metrics)]
#[metrics(scope = "metrics_custom")]
pub struct ServerMetrics {
#[metric(describe = "Count of fork_choice_updated_v3 calls proxied to the builder")]
pub fcu_count: Counter,

#[metric(describe = "Count of new_payload_v3 calls proxied to the builder")]
pub new_payload_count: Counter,

#[metric(describe = "Count of get_payload_v3 calls proxied to the builder")]
pub get_payload_count: Counter,

#[metric(describe = "Count of send_raw_transaction calls proxied to the builder")]
pub send_raw_tx_count: Counter,
}
22 changes: 16 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use reth_rpc_layer::AuthClientService;
use std::sync::Arc;
use tracing::{debug, error, info};

use crate::metrics::ServerMetrics;
use crate::selector::{DefaultPayloadSelector, PayloadSelector};

#[rpc(server, client, namespace = "engine")]
Expand Down Expand Up @@ -55,18 +56,21 @@ pub struct EthEngineApi<S = AuthClientService<HttpBackend>> {
builder_clients: Vec<Arc<HttpClient<S>>>,
payload_selector: Arc<dyn PayloadSelector + Send + Sync>,
boost_sync: bool,
metrics: ServerMetrics,
}

impl<S> EthEngineApi<S> {
pub fn new(
l2_client: Arc<HttpClient<S>>,
builder_clients: Vec<Arc<HttpClient<S>>>,
boost_sync: bool,
metrics: ServerMetrics,
) -> Self {
Self {
l2_client,
builder_clients,
boost_sync,
metrics,
payload_selector: Arc::new(DefaultPayloadSelector),
}
}
Expand All @@ -79,13 +83,16 @@ impl EthApiServer for EthEngineApi {
message = "received send_raw_transaction",
"bytes_len" = bytes.len()
);
let builder = self.builder_client.clone();
let tx_bytes = bytes.clone();
tokio::spawn(async move {
builder.send_raw_transaction(tx_bytes).await.map_err(|e| {
for builder in self.builder_clients.iter() {
self.metrics.send_raw_tx_count.increment(1);
let builder = builder.clone();
let tx_bytes = bytes.clone();
tokio::spawn(async move {
builder.send_raw_transaction(tx_bytes).await.map_err(|e| {
error!(message = "error calling send_raw_transaction for builder", "error" = %e);
})
});
})
});
}
self.l2_client
.send_raw_transaction(bytes)
.await
Expand Down Expand Up @@ -129,6 +136,7 @@ impl EngineApiServer for EthEngineApi {
if should_send_to_builder {
// async call to each builder to trigger payload building and sync
for builder in self.builder_clients.iter() {
self.metrics.fcu_count.increment(1);
let builder = builder.clone();
let attr = payload_attributes.clone();
tokio::spawn(async move {
Expand Down Expand Up @@ -173,6 +181,7 @@ impl EngineApiServer for EthEngineApi {
let builder_client_futures = self.builder_clients.iter().map(|builder| {
let builder = builder.clone();
Box::pin(async move {
self.metrics.get_payload_count.increment(1);
let payload = builder.get_payload_v3(payload_id).await.map_err(|e| {
error!(message = "error calling get_payload_v3 from builder", "error" = %e, "payload_id" = %payload_id);
e
Expand Down Expand Up @@ -231,6 +240,7 @@ impl EngineApiServer for EthEngineApi {
// async call to builder to sync the builder node
if self.boost_sync {
for builder in self.builder_clients.iter() {
self.metrics.new_payload_count.increment(1);
let builder = builder.clone();
let builder_payload = payload.clone();
let builder_versioned_hashes = versioned_hashes.clone();
Expand Down

0 comments on commit 5bf95a1

Please sign in to comment.