forked from AcalaNetwork/subway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
940 additions
and
218 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use super::{Extension, ExtensionRegistry}; | ||
use async_trait::async_trait; | ||
use prometheus_endpoint::init_prometheus; | ||
use prometheus_endpoint::Registry; | ||
use serde::Deserialize; | ||
use std::iter; | ||
use std::net::{Ipv4Addr, SocketAddr}; | ||
use tokio::task::JoinHandle; | ||
|
||
pub struct Prometheus { | ||
pub registry: Registry, | ||
pub exporter_task: JoinHandle<()>, | ||
} | ||
|
||
impl Drop for Prometheus { | ||
fn drop(&mut self) { | ||
self.exporter_task.abort(); | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
pub struct PrometheusConfig { | ||
pub port: u16, | ||
pub prefix: Option<String>, | ||
pub chain_label: Option<String>, | ||
} | ||
|
||
#[async_trait] | ||
impl Extension for Prometheus { | ||
type Config = PrometheusConfig; | ||
|
||
async fn from_config(config: &Self::Config, _registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> { | ||
Ok(Self::new(config.clone())) | ||
} | ||
} | ||
|
||
impl Prometheus { | ||
pub fn new(config: PrometheusConfig) -> Self { | ||
let labels = config | ||
.chain_label | ||
.clone() | ||
.map(|l| iter::once(("chain".to_string(), l.clone())).collect()); | ||
let prefix = match config.prefix { | ||
Some(p) if p.is_empty() => None, | ||
p => p, | ||
}; | ||
let registry = Registry::new_custom(prefix, labels).expect("Can't happen"); | ||
|
||
let exporter_task = start_prometheus_exporter(registry.clone(), config.port); | ||
Self { | ||
registry, | ||
exporter_task, | ||
} | ||
} | ||
|
||
pub fn registry(&self) -> &Registry { | ||
&self.registry | ||
} | ||
} | ||
|
||
fn start_prometheus_exporter(registry: Registry, port: u16) -> JoinHandle<()> { | ||
let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), port); | ||
|
||
tokio::spawn(async move { | ||
init_prometheus(addr, registry).await.unwrap(); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use futures::{future::BoxFuture, FutureExt}; | ||
use jsonrpsee::server::middleware::rpc::RpcServiceT; | ||
use jsonrpsee::types::Request; | ||
use jsonrpsee::MethodResponse; | ||
use prometheus_endpoint::{register, Counter, Histogram, HistogramOpts, Registry, U64}; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
type MetricPair = (Counter<U64>, Histogram); | ||
|
||
#[derive(Clone)] | ||
pub struct PrometheusService<S> { | ||
inner: S, | ||
registry: Registry, | ||
call_metrics: Arc<Mutex<HashMap<String, MetricPair>>>, | ||
} | ||
|
||
impl<S> PrometheusService<S> { | ||
pub fn new(inner: S, registry: &Registry) -> Self { | ||
Self { | ||
inner, | ||
registry: registry.clone(), | ||
call_metrics: Arc::new(Mutex::new(HashMap::new())), | ||
} | ||
} | ||
|
||
fn register_metrics_for(&self, method: String) -> MetricPair { | ||
let counter_name = format!("{}_count", method); | ||
let histogram_name = format!("{}_histogram", method); | ||
|
||
let counter = Counter::new(counter_name, "No help").unwrap(); | ||
let histogram = Histogram::with_opts(HistogramOpts::new(histogram_name, "No help")).unwrap(); | ||
|
||
let counter = register(counter, &self.registry).unwrap(); | ||
let histogram = register(histogram, &self.registry).unwrap(); | ||
|
||
(counter, histogram) | ||
} | ||
|
||
fn metrics_for(&self, method: String) -> MetricPair { | ||
let mut metrics = self.call_metrics.lock().unwrap(); | ||
let (counter, histogram) = metrics | ||
.entry(method.clone()) | ||
.or_insert_with(|| self.register_metrics_for(method)); | ||
|
||
(counter.clone(), histogram.clone()) | ||
} | ||
} | ||
|
||
impl<'a, S> RpcServiceT<'a> for PrometheusService<S> | ||
where | ||
S: RpcServiceT<'a> + Send + Sync + Clone + 'static, | ||
{ | ||
type Future = BoxFuture<'a, MethodResponse>; | ||
|
||
fn call(&self, req: Request<'a>) -> Self::Future { | ||
let (counter, histogram) = self.metrics_for(req.method.to_string()); | ||
|
||
let service = self.inner.clone(); | ||
async move { | ||
counter.inc(); | ||
|
||
let timer = histogram.start_timer(); | ||
let res = service.call(req).await; | ||
timer.stop_and_record(); | ||
|
||
res | ||
} | ||
.boxed() | ||
} | ||
} |
Oops, something went wrong.