Skip to content

Commit fca464f

Browse files
committed
fix(guard): add more tokio runtime metrics, remove labels from metrics
1 parent fcd1624 commit fca464f

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

packages/common/runtime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ fn build_tokio_runtime_builder() -> tokio::runtime::Builder {
6767
metrics::TOKIO_THREAD_COUNT.dec();
6868
});
6969

70+
rt_builder.on_task_spawn(move |_| {
71+
metrics::TOKIO_TASK_TOTAL.inc();
72+
});
73+
7074
if env::var("TOKIO_RUNTIME_METRICS").is_ok() {
7175
rt_builder.on_before_task_poll(|_| {
7276
let metrics = tokio::runtime::Handle::current().metrics();
7377
let buckets = metrics.poll_time_histogram_num_buckets();
7478

7579
metrics::TOKIO_GLOBAL_QUEUE_DEPTH.set(metrics.global_queue_depth() as i64);
80+
metrics::TOKIO_ACTIVE_TASK_COUNT.set(metrics.num_alive_tasks() as i64);
7681

7782
for worker in 0..metrics.num_workers() {
7883
metrics::TOKIO_WORKER_OVERFLOW_COUNT

packages/common/runtime/src/metrics.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ lazy_static::lazy_static! {
1414
"Number of pending tasks in the global queue.",
1515
*REGISTRY
1616
).unwrap();
17+
pub static ref TOKIO_TASK_TOTAL: IntCounter =
18+
register_int_counter_with_registry!(
19+
"tokio_task_total",
20+
"Total number of spawned tasks.",
21+
*REGISTRY
22+
).unwrap();
23+
pub static ref TOKIO_ACTIVE_TASK_COUNT: IntGauge =
24+
register_int_gauge_with_registry!(
25+
"tokio_active_task_count",
26+
"Total number of active (running or sleeping) tasks.",
27+
*REGISTRY
28+
).unwrap();
1729
pub static ref TOKIO_WORKER_OVERFLOW_COUNT: IntGaugeVec = register_int_gauge_vec_with_registry!(
1830
"tokio_worker_overflow_count",
1931
"Number of times the given worker thread saturated its local queue.",

packages/edge/infra/guard/core/src/metrics.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,29 @@ lazy_static! {
4949
.unwrap();
5050

5151
// MARK: Proxy requests
52-
pub static ref PROXY_REQUEST_TOTAL: IntCounterVec = register_int_counter_vec_with_registry!(
52+
pub static ref PROXY_REQUEST_TOTAL: IntCounter = register_int_counter_with_registry!(
5353
"guard_proxy_request_total",
5454
"Total number of requests to actor",
55-
&["actor_id", "server_id", "method", "path"],
5655
*REGISTRY,
5756
)
5857
.unwrap();
59-
pub static ref PROXY_REQUEST_PENDING: IntGaugeVec = register_int_gauge_vec_with_registry!(
58+
pub static ref PROXY_REQUEST_PENDING: IntGauge = register_int_gauge_with_registry!(
6059
"guard_proxy_request_pending",
6160
"Number of pending requests to actor",
62-
&["actor_id", "server_id", "method", "path"],
6361
*REGISTRY,
6462
)
6563
.unwrap();
6664
pub static ref PROXY_REQUEST_DURATION: HistogramVec = register_histogram_vec_with_registry!(
6765
"guard_proxy_request_duration",
6866
"Request duration in seconds",
69-
&["actor_id", "server_id", "status"],
67+
&["status"],
7068
*REGISTRY,
7169
)
7270
.unwrap();
7371
pub static ref PROXY_REQUEST_ERROR: IntCounterVec = register_int_counter_vec_with_registry!(
7472
"guard_proxy_request_errors_total",
7573
"Total number of errors when proxying requests to actor",
76-
&["actor_id", "server_id", "error_type"],
74+
&["error_type"],
7775
*REGISTRY,
7876
)
7977
.unwrap();

packages/edge/infra/guard/core/src/proxy_service.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ impl ProxyService {
613613
.path_and_query()
614614
.map(|x| x.to_string())
615615
.unwrap_or_else(|| req.uri().path().to_string());
616-
let method = req.method().clone();
617-
let method_str = method.as_str();
618616

619617
let start_time = Instant::now();
620618

@@ -642,11 +640,6 @@ impl ProxyService {
642640
};
643641

644642
let actor_id = target.actor_id;
645-
let server_id = target.server_id;
646-
647-
// Convert UUIDs to strings for metrics, handling Optional fields
648-
let actor_id_str = actor_id.map_or_else(|| "none".to_string(), |id| id.to_string());
649-
let server_id_str = server_id.map_or_else(|| "none".to_string(), |id| id.to_string());
650643

651644
// Extract IP address from remote_addr
652645
let client_ip = self.remote_addr.ip();
@@ -666,13 +659,9 @@ impl ProxyService {
666659
.map_err(Into::into)
667660
} else {
668661
// Increment metrics
669-
metrics::PROXY_REQUEST_PENDING
670-
.with_label_values(&[&actor_id_str, &server_id_str, method_str, &path])
671-
.inc();
662+
metrics::PROXY_REQUEST_PENDING.inc();
672663

673-
metrics::PROXY_REQUEST_TOTAL
674-
.with_label_values(&[&actor_id_str, &server_id_str, method_str, &path])
675-
.inc();
664+
metrics::PROXY_REQUEST_TOTAL.inc();
676665

677666
// Prepare to release in-flight counter when done
678667
let state_clone = self.state.clone();
@@ -695,18 +684,22 @@ impl ProxyService {
695684

696685
let status = match &res {
697686
Ok(resp) => resp.status().as_u16().to_string(),
698-
Err(_) => "error".to_string(),
687+
Err(err) => {
688+
metrics::PROXY_REQUEST_ERROR
689+
.with_label_values(&[&err.to_string()])
690+
.inc();
691+
692+
"error".to_string()
693+
}
699694
};
700695

701696
// Record metrics
702697
let duration = start_time.elapsed();
703698
metrics::PROXY_REQUEST_DURATION
704-
.with_label_values(&[&actor_id_str, &server_id_str, &status])
699+
.with_label_values(&[&status])
705700
.observe(duration.as_secs_f64());
706701

707-
metrics::PROXY_REQUEST_PENDING
708-
.with_label_values(&[&actor_id_str, &server_id_str, method_str, &path])
709-
.dec();
702+
metrics::PROXY_REQUEST_PENDING.dec();
710703

711704
res
712705
}

0 commit comments

Comments
 (0)