[router] add metrics for worker and policy (#8971)
Signed-off-by: Tony Lu <tonyluj@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use super::{CircuitBreaker, CircuitBreakerConfig, WorkerError, WorkerResult};
|
||||
use crate::metrics::RouterMetrics;
|
||||
use async_trait::async_trait;
|
||||
use futures;
|
||||
use serde_json;
|
||||
@@ -259,6 +260,7 @@ impl Worker for BasicWorker {
|
||||
|
||||
fn set_healthy(&self, healthy: bool) {
|
||||
self.healthy.store(healthy, Ordering::Release);
|
||||
RouterMetrics::set_worker_health(self.url(), healthy);
|
||||
}
|
||||
|
||||
async fn check_health_async(&self) -> WorkerResult<()> {
|
||||
|
||||
@@ -181,6 +181,7 @@ impl LoadBalancingPolicy for CacheAwarePolicy {
|
||||
// Increment processed counter
|
||||
workers[min_load_idx].increment_processed();
|
||||
RouterMetrics::record_processed_request(workers[min_load_idx].url());
|
||||
RouterMetrics::record_policy_decision(self.name(), workers[min_load_idx].url());
|
||||
|
||||
return Some(min_load_idx);
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ impl LoadBalancingPolicy for PowerOfTwoPolicy {
|
||||
// Increment processed counter
|
||||
workers[selected_idx].increment_processed();
|
||||
RouterMetrics::record_processed_request(workers[selected_idx].url());
|
||||
RouterMetrics::record_policy_decision(self.name(), workers[selected_idx].url());
|
||||
|
||||
Some(selected_idx)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use super::{get_healthy_worker_indices, LoadBalancingPolicy};
|
||||
use crate::core::Worker;
|
||||
use crate::metrics::RouterMetrics;
|
||||
use rand::Rng;
|
||||
|
||||
/// Random selection policy
|
||||
@@ -30,6 +31,10 @@ impl LoadBalancingPolicy for RandomPolicy {
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let random_idx = rng.gen_range(0..healthy_indices.len());
|
||||
let worker = workers[healthy_indices[random_idx]].url();
|
||||
|
||||
RouterMetrics::record_processed_request(worker);
|
||||
RouterMetrics::record_policy_decision(self.name(), worker);
|
||||
Some(healthy_indices[random_idx])
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use super::{get_healthy_worker_indices, LoadBalancingPolicy};
|
||||
use crate::core::Worker;
|
||||
use crate::metrics::RouterMetrics;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
/// Round-robin selection policy
|
||||
@@ -35,7 +36,10 @@ impl LoadBalancingPolicy for RoundRobinPolicy {
|
||||
// Get and increment counter atomically
|
||||
let count = self.counter.fetch_add(1, Ordering::Relaxed);
|
||||
let selected_idx = count % healthy_indices.len();
|
||||
let worker = workers[healthy_indices[selected_idx]].url();
|
||||
|
||||
RouterMetrics::record_processed_request(worker);
|
||||
RouterMetrics::record_policy_decision(self.name(), worker);
|
||||
Some(healthy_indices[selected_idx])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user