You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// # Errors////// Will return `Err` if an error occurs when running the `ServerBuilder` start fails.pubasyncfnrun_server(kakarot_rpc_module:RpcModule<()>,rpc_config:RPCConfig,) -> Result<(SocketAddr,ServerHandle),RpcError>{letRPCConfig{ socket_addr } = rpc_config;let cors = CorsLayer::new().allow_methods(Any).allow_origin(Any).allow_headers(Any);let http_middleware =
tower::ServiceBuilder::new().layer(ProxyGetRequestLayer::new("/health","net_health")?).layer(cors);// Creating the prometheus registry to register the metricslet registry = Registry::new();// register the metricslet metrics = RpcMetrics::new(Some(®istry))?.map(|m| MetricsLayer::new(m,"http"));
tokio::spawn(asyncmove{// serve the prometheus metrics on the given port so that it can be readlet _ = init_prometheus(SocketAddr::new(
std::net::IpAddr::V4(Ipv4Addr::new(0,0,0,0)),get_env_or_default("PROMETHEUS_PORT","9615").parse().unwrap(),),
registry,).await;});// add the metrics as a middleware to the RPC so that every new RPC call fires prometheus metrics// upon start, finish etc. we don't need to manually handle each method, it should automatically// work for any new method.let rpc_middleware = RpcServiceBuilder::new().option_layer(metrics);let server = ServerBuilder::default().max_connections(get_env_or_default("RPC_MAX_CONNECTIONS","100").parse().unwrap()).set_http_middleware(http_middleware).set_rpc_middleware(rpc_middleware).build(socket_addr.parse::<SocketAddr>()?).await?;let addr = server.local_addr()?;let handle = server.start(kakarot_rpc_module);Ok((addr, handle))}
And:
/// Initializes the metrics context, and starts an HTTP server/// to serve metrics.pubasyncfninit_prometheus(prometheus_addr:SocketAddr,registry:Registry) -> Result<(),Error>{let listener =
tokio::net::TcpListener::bind(&prometheus_addr).await.map_err(|_| Error::PortInUse(prometheus_addr))?;init_prometheus_with_listener(listener, registry).await}/// Init prometheus using the given listener.asyncfninit_prometheus_with_listener(listener: tokio::net::TcpListener,registry:Registry) -> Result<(),Error>{let listener = hyper::server::conn::AddrIncoming::from_listener(listener)?;
log::info!("〽️ Prometheus exporter started at {}", listener.local_addr());let service = make_service_fn(move |_| {let registry = registry.clone();asyncmove{Ok::<_, hyper::Error>(service_fn(move |req:Request<Body>| request_metrics(req, registry.clone())))}});let(signal, on_exit) = tokio::sync::oneshot::channel::<()>();let server = Server::builder(listener).serve(service).with_graceful_shutdown(async{let _ = on_exit.await;});let result = server.await.map_err(Into::into);// Gracefully shutdown server, otherwise the server does not stop if it has open connectionslet _ = signal.send(());
result
}
I'll probably open a PR to add an example to the examples folder but before, I think it'd be nice to add metrics to a JsonRpsee server in a smoother way. Maybe there exists one already
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
After #1328, I'm asking here if there should be an opinionated way to add metrics to Jsonrpsee.
We currently have this piece of code in Kakarot kkrt-labs/kakarot-rpc#874 ->
And:
Which relies on code from Substrate:
I'll probably open a PR to add an example to the examples folder but before, I think it'd be nice to add metrics to a JsonRpsee server in a smoother way. Maybe there exists one already
Beta Was this translation helpful? Give feedback.
All reactions