Skip to content

feat: expose averaged process CPU and memory per cluster node - #1743

Open
praveen5959 wants to merge 1 commit into
mainfrom
cluster-metrics
Open

feat: expose averaged process CPU and memory per cluster node#1743
praveen5959 wants to merge 1 commit into
mainfrom
cluster-metrics

Conversation

@praveen5959

@praveen5959 praveen5959 commented Aug 7, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features
    • Added background monitoring of the application’s CPU and memory usage.
    • Added Prometheus metrics for process CPU usage and resident memory.
    • Resource metrics are averaged over collected samples for more stable reporting.
    • CPU and memory values are now included in exported metrics.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The resource monitor now samples process CPU and memory usage every 10 seconds. The metrics module averages samples and exposes Prometheus gauges. Prometheus metric conversion includes the new process metrics.

Changes

Process resource metrics

Layer / File(s) Summary
Metric aggregation and registration
src/metrics/mod.rs
Adds process CPU and resident-memory gauges. Records running averages with atomic state. Registers both gauges and tests averaging behavior.
Periodic process sampling
src/handlers/http/resource_check.rs
Adds a 10-second sampling interval. Refreshes system information, resolves the current process, and records CPU and memory samples.
Prometheus metric conversion
src/metrics/prom_utils.rs
Adds process CPU and memory fields to Metrics. Initializes them to zero and populates them from Prometheus gauges.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ResourceMonitor
  participant SYS_INFO
  participant CurrentProcess
  participant ProcessMetrics
  participant PrometheusRegistry
  ResourceMonitor->>SYS_INFO: Refresh system information
  ResourceMonitor->>CurrentProcess: Resolve current process
  CurrentProcess-->>ResourceMonitor: CPU usage and memory
  ResourceMonitor->>ProcessMetrics: Record sample
  ProcessMetrics->>PrometheusRegistry: Update averaged gauges
Loading

Suggested reviewers: parmesant

Poem

A rabbit checks the gauges bright,
Samples CPU by moonlit light.
Memory hops into the stream,
Averages form a tidy dream.
Prometheus records the sight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning No pull request description was provided, so the required description, testing status, comments, and documentation sections are missing. Add a description that explains the goal, solution, key changes, issue reference if applicable, and the required testing, comments, and documentation checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: exposing averaged process CPU and memory metrics for each cluster node.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cluster-metrics

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/metrics/mod.rs`:
- Around line 179-237: Rename PROCESS_CPU_USAGE_PERCENT and PROCESS_MEMORY_BYTES
to clearly indicate lifetime averages, updating both metric names and help
strings to use “average” terminology. Apply the same renamed metric identifiers
in the matching definitions or references in prom_utils.rs, while leaving the
accumulator and recording behavior unchanged.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 654732fb-7118-4d92-a09b-0c5654821b6e

📥 Commits

Reviewing files that changed from the base of the PR and between fc36117 and a045da8.

📒 Files selected for processing (3)
  • src/handlers/http/resource_check.rs
  • src/metrics/mod.rs
  • src/metrics/prom_utils.rs

Comment thread src/metrics/mod.rs
Comment on lines +179 to +237
pub static PROCESS_CPU_USAGE_PERCENT: Lazy<Gauge> = Lazy::new(|| {
Gauge::with_opts(
Opts::new(
"process_cpu_usage_percent",
"Current CPU usage percent for this Parseable process",
)
.namespace(METRICS_NAMESPACE),
)
.expect("metric can be created")
});

pub static PROCESS_MEMORY_BYTES: Lazy<Gauge> = Lazy::new(|| {
Gauge::with_opts(
Opts::new(
"process_memory_bytes",
"Current resident memory used by this Parseable process in bytes",
)
.namespace(METRICS_NAMESPACE),
)
.expect("metric can be created")
});

const CPU_USAGE_PRECISION: f64 = 1_000.0;

#[derive(Default)]
struct ProcessMetricsAccumulator {
cpu_usage_sum: AtomicU64,
memory_bytes_sum: AtomicU64,
sample_count: AtomicU64,
}

impl ProcessMetricsAccumulator {
fn record(&self, cpu_usage_percent: f64, memory_bytes: u64) -> (f64, f64) {
self.cpu_usage_sum.fetch_add(
(cpu_usage_percent * CPU_USAGE_PRECISION).round() as u64,
Ordering::Relaxed,
);
self.memory_bytes_sum
.fetch_add(memory_bytes, Ordering::Relaxed);
let sample_count = self.sample_count.fetch_add(1, Ordering::Relaxed) + 1;

(
self.cpu_usage_sum.load(Ordering::Relaxed) as f64
/ sample_count as f64
/ CPU_USAGE_PRECISION,
self.memory_bytes_sum.load(Ordering::Relaxed) as f64 / sample_count as f64,
)
}
}

static PROCESS_METRICS_ACCUMULATOR: Lazy<ProcessMetricsAccumulator> =
Lazy::new(ProcessMetricsAccumulator::default);

pub fn record_process_metrics_sample(cpu_usage_percent: f64, memory_bytes: u64) {
let (average_cpu_usage, average_memory_bytes) =
PROCESS_METRICS_ACCUMULATOR.record(cpu_usage_percent, memory_bytes);
PROCESS_CPU_USAGE_PERCENT.set(average_cpu_usage);
PROCESS_MEMORY_BYTES.set(average_memory_bytes);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Name these gauges as averages.

record_process_metrics_sample publishes a lifetime running average. The metric names and help text describe a current value. Dashboards and alerts can interpret these values as 10-second samples.

Rename the gauges to include average, or state Lifetime average in both help strings. Update the matching metric names in src/metrics/prom_utils.rs in the same change.

Proposed fix
- "process_cpu_usage_percent",
- "Current CPU usage percent for this Parseable process",
+ "process_cpu_usage_percent_average",
+ "Lifetime average CPU usage percent for this Parseable process",

- "process_memory_bytes",
- "Current resident memory used by this Parseable process in bytes",
+ "process_memory_bytes_average",
+ "Lifetime average resident memory used by this Parseable process in bytes",
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/metrics/mod.rs` around lines 179 - 237, Rename PROCESS_CPU_USAGE_PERCENT
and PROCESS_MEMORY_BYTES to clearly indicate lifetime averages, updating both
metric names and help strings to use “average” terminology. Apply the same
renamed metric identifiers in the matching definitions or references in
prom_utils.rs, while leaving the accumulator and recording behavior unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant