Skip to content

Commit

Permalink
Improve metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Jun 26, 2024
1 parent 60dfd80 commit 4eaa3ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 45 deletions.
41 changes: 9 additions & 32 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ use std::sync::Arc;

use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::EncodeLabelSet;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::registry::Registry;

#[derive(Debug, Clone)]
pub struct Metrics {
registry: Arc<Registry>,
proxy_request_counter: Counter<u64>,
proxy_requests: Family<HostStateLabels, Gauge>,
proxy_requests_response: Family<ProxyStateLabels, Gauge>,
proxy_responses: Family<ProxyStateLabels, Gauge>,
node_block_latest: Family<HostStateLabels, Gauge>,
}

Expand All @@ -30,26 +28,20 @@ pub(crate) struct ProxyStateLabels {

impl Metrics {
pub fn new() -> Self {
let proxy_request_counter: Counter<u64> = Default::default();
let proxy_requests = Family::<HostStateLabels, Gauge>::default();
let proxy_requests_response = Family::<ProxyStateLabels, Gauge>::default();
let proxy_responses = Family::<ProxyStateLabels, Gauge>::default();
let node_block_latest = Family::<HostStateLabels, Gauge>::default();

let mut registry = <Registry>::with_prefix("dynode");
registry.register(
"proxy_requests_count",
"How many requests the application has received",
proxy_request_counter.clone(),
);
registry.register(
"proxy_requests",
"proxy_requests_total",
"Proxy requests by host",
proxy_requests.clone(),
);
registry.register(
"proxy_requests_response",
"Proxy requests served by host",
proxy_requests_response.clone(),
"proxy_responses_total",
"Proxy requests served a response by host",
proxy_responses.clone(),
);
registry.register(
"node_block_latest",
Expand All @@ -59,17 +51,12 @@ impl Metrics {

Self {
registry: Arc::new(registry),
proxy_request_counter,
proxy_requests,
proxy_requests_response,
proxy_responses,
node_block_latest,
}
}

pub fn add_total_requests(&self) {
self.proxy_request_counter.inc();
}

pub fn add_proxy_request(&self, host: &str) {
self.proxy_requests
.get_or_create(&HostStateLabels {
Expand All @@ -78,8 +65,8 @@ impl Metrics {
.inc();
}

pub fn add_proxy_request_response(&self, host: &str, status: u64, latency: u64) {
self.proxy_requests_response
pub fn add_proxy_response(&self, host: &str, status: u64, latency: u64) {
self.proxy_responses
.get_or_create(&ProxyStateLabels {
host: host.to_string(),
status,
Expand All @@ -101,14 +88,4 @@ impl Metrics {
encode(&mut buffer, &self.registry).unwrap();
buffer
}

// fn metrics_logger(&self) -> MetricsLogger {
// MetricsLogger {
// request_counter: &self.request_counter,
// }
// }
}

// pub struct MetricsLogger<'a> {
// pub request_counter: &'a Counter<u64>,
// }
23 changes: 10 additions & 13 deletions src/proxy_request_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ impl Service<Request<IncomingBody>> for ProxyRequestService {
.to_str()
.unwrap_or_default();

self.metrics.add_total_requests();

log_incoming_request(&req);

match self.domains.get(host) {
Expand All @@ -59,21 +57,20 @@ impl Service<Request<IncomingBody>> for ProxyRequestService {

async move { proxy_pass(req, url).await }.boxed()
}
_ => async move { Self::unsupported_chain(req).await }.boxed(), //async move { handle_request(req).await }.boxed(), //Ok(Response::new(Full::from("unsupported domain")))},
_ => {
self.metrics.add_proxy_request("");

async move {
Ok(Response::builder()
.body(Full::new(Bytes::from("unsupported domain")))
.unwrap())
}
.boxed()
}
}
}
}

impl ProxyRequestService {
async fn unsupported_chain(
_req: Request<IncomingBody>,
) -> Result<Response<Full<Bytes>>, Box<dyn std::error::Error + Send + Sync>> {
Ok(Response::builder()
.body(Full::new(Bytes::from("unsupported domain")))
.unwrap())
}
}

async fn proxy_pass(
original_request: Request<IncomingBody>,
url: RequestUrl,
Expand Down

0 comments on commit 4eaa3ab

Please sign in to comment.