Skip to content

Commit

Permalink
Add ProxyRequestLabels
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Jan 12, 2025
1 parent 5505dff commit 5f41c4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ use prometheus_client::registry::Registry;
#[derive(Debug, Clone)]
pub struct Metrics {
registry: Arc<Registry>,
proxy_requests: Family<HostStateLabels, Counter>,
proxy_requests: Family<ProxyRequestLabels, Counter>,
proxy_response_latency: Family<ResponseLabels, Histogram>,
node_host_current: Family<HostCurrentStateLabels, Gauge>,
node_block_latest: Family<HostStateLabels, Gauge>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
pub struct ProxyRequestLabels {
host: String,
user_agent: String,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
pub struct HostStateLabels {
host: String,
Expand All @@ -35,12 +41,11 @@ struct ResponseLabels {
remote_host: String,
path: String,
status: u16,
user_agent: String,
}

impl Metrics {
pub fn new() -> Self {
let proxy_requests = Family::<HostStateLabels, Counter>::default();
let proxy_requests = Family::<ProxyRequestLabels, Counter>::default();
let proxy_response_latency =
Family::<ResponseLabels, Histogram>::new_with_constructor(|| {
Histogram::new(exponential_buckets(50.0, 1.44, 12))
Expand Down Expand Up @@ -79,10 +84,11 @@ impl Metrics {
}
}

pub fn add_proxy_request(&self, host: &str) {
pub fn add_proxy_request(&self, host: &str, user_agent: &str) {
self.proxy_requests
.get_or_create(&HostStateLabels {
.get_or_create(&ProxyRequestLabels {
host: host.to_string(),
user_agent: user_agent.to_string(),
})
.inc();
}
Expand All @@ -105,7 +111,6 @@ impl Metrics {
host: &str,
path: &str,
remote_host: &str,
user_agent: &str,
status: u16,
latency: u128,
) {
Expand All @@ -116,7 +121,6 @@ impl Metrics {
path,
remote_host: remote_host.to_string(),
status,
user_agent: user_agent.to_string(),
})
.observe(latency as f64);
}
Expand Down
3 changes: 1 addition & 2 deletions src/proxy_request_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Service<Request<IncomingBody>> for ProxyRequestService {
req.uri(),
);

self.metrics.add_proxy_request(host);
self.metrics.add_proxy_request(host, &user_agent);

let metrics = self.metrics.clone();
let host = host.to_string();
Expand All @@ -77,7 +77,6 @@ impl Service<Request<IncomingBody>> for ProxyRequestService {
host.as_str(),
url.uri.path(),
url.uri.host().unwrap_or_default(),
user_agent.as_str(),
response.status().as_u16(),
now.elapsed().as_millis(),
);
Expand Down

0 comments on commit 5f41c4a

Please sign in to comment.