Skip to content

Commit c531545

Browse files
committed
coderabbit comments
1 parent 7a47329 commit c531545

5 files changed

Lines changed: 51 additions & 25 deletions

File tree

‎src/handlers/http/modal/ingest_server.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ impl IngestServer {
237237
.route(
238238
web::post()
239239
.to(ingest::post_event)
240-
.authorize_for_resource(Action::Ingest),
240+
.authorize_for_resource(Action::Ingest)
241+
.wrap(from_fn(
242+
resource_check::check_resource_utilization_middleware,
243+
)),
241244
),
242245
)
243246
.service(

‎src/handlers/http/resource_check.rs‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ use actix_web::{
2525
error::ErrorServiceUnavailable,
2626
middleware::Next,
2727
};
28+
use sysinfo::{MemoryRefreshKind, RefreshKind, System};
2829
use tokio::{
2930
select,
3031
time::{Duration, interval},
3132
};
3233
use tracing::{info, trace, warn};
3334

35+
use crate::analytics::{SYS_INFO, refresh_sys_info};
3436
use crate::metrics::record_process_metrics_sample;
3537
use crate::parseable::PARSEABLE;
36-
use crate::{
37-
analytics::{SYS_INFO, refresh_sys_info},
38-
metrics::PROCESS_METRICS_ACCUMULATOR,
39-
};
4038

4139
const PROCESS_METRICS_SAMPLE_INTERVAL: Duration = Duration::from_secs(5);
4240

43-
static RESOURCE_CHECK_ENABLED: LazyLock<Arc<AtomicBool>> =
44-
LazyLock::new(|| Arc::new(AtomicBool::new(false)));
41+
static SERVER_OK: LazyLock<Arc<AtomicBool>> = LazyLock::new(|| Arc::new(AtomicBool::new(true)));
4542

4643
/// Spawn a background task to monitor system resources
4744
pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
@@ -51,7 +48,7 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
5148
let mut process_metrics_interval = interval(PROCESS_METRICS_SAMPLE_INTERVAL);
5249
let mut shutdown_rx = shutdown_rx;
5350

54-
let memory_threshold = PARSEABLE.options.memory_utilization_threshold;
51+
let memory_threshold = (PARSEABLE.options.memory_utilization_threshold / 100.0) as f64;
5552

5653
loop {
5754
select! {
@@ -62,19 +59,22 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
6259
refresh_sys_info();
6360

6461
let mut resource_ok = true;
65-
let process_mem = PROCESS_METRICS_ACCUMULATOR.get_mem();
66-
let total_mem = PROCESS_METRICS_ACCUMULATOR.get_total_mem();
67-
let memory_usage = process_mem / total_mem;
6862

69-
// Check memory utilization
70-
if memory_usage > memory_threshold as f64 {
71-
warn!("High memory usage detected: {:.1}% (threshold: {:.1}%)",
72-
memory_usage, memory_threshold);
73-
resource_ok = false;
63+
let mut s = System::new_with_specifics(
64+
RefreshKind::nothing().with_memory(MemoryRefreshKind::everything()),
65+
);
66+
if let Some(cgroup) = s.cgroup_limits() {
67+
if (cgroup.rss as f64) > memory_threshold * (cgroup.total_memory as f64) {
68+
resource_ok = false;
69+
}
70+
} else {
71+
s.refresh_memory();
72+
if (s.used_memory() as f64) > memory_threshold * (s.total_memory() as f64) {
73+
resource_ok = false;
74+
}
7475
}
75-
76-
let previous_state = RESOURCE_CHECK_ENABLED.load(std::sync::atomic::Ordering::SeqCst);
77-
RESOURCE_CHECK_ENABLED.store(resource_ok, std::sync::atomic::Ordering::SeqCst);
76+
let previous_state = SERVER_OK.load(std::sync::atomic::Ordering::SeqCst);
77+
SERVER_OK.store(resource_ok, std::sync::atomic::Ordering::SeqCst);
7878

7979
// Log state changes
8080
if previous_state != resource_ok {
@@ -118,7 +118,7 @@ pub async fn check_resource_utilization_middleware(
118118
req: ServiceRequest,
119119
next: Next<impl MessageBody>,
120120
) -> Result<ServiceResponse<impl MessageBody>, Error> {
121-
let resource_ok = RESOURCE_CHECK_ENABLED.load(std::sync::atomic::Ordering::SeqCst);
121+
let resource_ok = SERVER_OK.load(std::sync::atomic::Ordering::SeqCst);
122122

123123
if !resource_ok {
124124
let error_msg = "Server resources over-utilized";

‎src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ pub use openid;
7474
use parseable::PARSEABLE;
7575
use reqwest::{Client, ClientBuilder};
7676
pub use rustls;
77+
pub use sysinfo;
7778
pub use utils as parseable_utils;
7879
pub use {clap, tracing_actix_web, tracing_opentelemetry, tracing_subscriber};
7980
pub use {opentelemetry, opentelemetry_otlp, opentelemetry_proto, opentelemetry_sdk};
80-
8181
// It is very unlikely that panic will occur when dealing with locks.
8282
pub const LOCK_EXPECT: &str = "Thread shouldn't panic while holding a lock";
8383

‎src/main.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::process::exit;
2121
use parseable::connectors;
2222
use parseable::{
2323
IngestServer, ParseableServer, QueryServer, Server, banner, metrics, option::Mode,
24-
parseable::PARSEABLE, rbac, storage,
24+
parseable::PARSEABLE, rbac, storage,analytics::{SYS_INFO, refresh_sys_info}
2525
};
2626
use tokio::signal::ctrl_c;
2727
use tokio::sync::oneshot;
@@ -31,6 +31,8 @@ use tracing_subscriber::layer::SubscriberExt;
3131
use tracing_subscriber::util::SubscriberInitExt;
3232
use tracing_subscriber::{EnvFilter, Registry, fmt};
3333

34+
use crate::metrics::record_process_metrics_sample;
35+
3436
#[actix_web::main]
3537
#[cfg_attr(feature = "hotpath", hotpath::main)]
3638
async fn main() -> anyhow::Result<()> {
@@ -89,6 +91,27 @@ async fn main() -> anyhow::Result<()> {
8991
});
9092

9193
let prometheus = metrics::build_metrics_handler();
94+
// init process metrics
95+
refresh_sys_info();
96+
let process_metrics = tokio::task::spawn_blocking(|| {
97+
let sys = SYS_INFO.lock().unwrap();
98+
let total_mem = if let Some(cgroup) = sys.cgroup_limits() {
99+
cgroup.total_memory
100+
} else {
101+
sys.total_memory()
102+
};
103+
sysinfo::get_current_pid()
104+
.ok()
105+
.and_then(|pid| sys.process(pid))
106+
.map(|process| (process.cpu_usage() as f64, process.memory(), total_mem))
107+
})
108+
.await
109+
.unwrap();
110+
// first measurement
111+
if let Some((cpu_usage, memory_bytes, total_mem)) = process_metrics {
112+
record_process_metrics_sample(cpu_usage, memory_bytes, total_mem);
113+
}
114+
92115
// Start servers
93116
#[cfg(feature = "kafka")]
94117
{

‎src/metrics/mod.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ impl ProcessMetricsAccumulator {
242242
}
243243

244244
pub fn get_cpu(&self) -> f64 {
245-
self.cpu_usage_avg.get() as f64
245+
self.cpu_usage_avg.get()
246246
}
247247

248248
pub fn get_mem(&self) -> f64 {
249-
self.memory_bytes_avg.get() as f64
249+
self.memory_bytes_avg.get()
250250
}
251251

252252
pub fn get_total_mem(&self) -> f64 {
253-
self.total_memory.get() as f64
253+
self.total_memory.get()
254254
}
255255
}
256256

0 commit comments

Comments
 (0)