Skip to content

refactor(metrics): reduce per-hook boilerplate with declarative macros#806

Open
Molter73 wants to merge 1 commit into
mainfrom
mauro/feat/refactor-metrics
Open

refactor(metrics): reduce per-hook boilerplate with declarative macros#806
Molter73 wants to merge 1 commit into
mainfrom
mauro/feat/refactor-metrics

Conversation

@Molter73

@Molter73 Molter73 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Description

Use macro_rules! to generate repetitive per-hook code from a single list of hook names, reducing the number of places that must be updated when adding a new LSM hook.

  • fact-ebpf/src/lib.rs: impl_metrics_t! macro generates metrics_t::accumulate() from the hook list.
  • fact/src/metrics/kernel_metrics.rs: define_kernel_metrics! macro generates the KernelMetrics struct, constructor, registration, and collect() method. Also refactors refresh_labels to iterate over (label, value) tuples instead of 5 separate blocks.
  • fact/src/metrics/mod.rs: Extract inc_label/inc_label_by helpers in EventCounter to deduplicate the label lookup pattern. Widen visibility of counter field and MetricEvents to pub(crate).
  • fact-ebpf/src/bpf/types.h: Reorder metrics_t fields to group all path_* hooks together.

Assisted-by: claude-opus-4-6@default noreply@opencode.ai

Checklist

  • Patch has a change log entry OR does not need one.
  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
  • Added integration tests
  • Added regression tests

If any of these don't apply, please comment below.

Testing Performed

Run locally and checked metrics were increasing.

Summary by CodeRabbit

  • Refactor
    • Internal metrics handling consolidated to improve code maintainability and reduce duplication across implementations. Metrics accumulation and collection logic refactored through macro-based helper patterns. Event counter increment methods unified to use shared internal helpers for consistency. Label management patterns streamlined. All public APIs remain fully backward compatible.

Use macro_rules! to generate repetitive per-hook code from a single
list of hook names, reducing the number of places that must be updated
when adding a new LSM hook.

- fact-ebpf/src/lib.rs: impl_metrics_t! macro generates
  metrics_t::accumulate() from the hook list.
- fact/src/metrics/kernel_metrics.rs: define_kernel_metrics! macro
  generates the KernelMetrics struct, constructor, registration, and
  collect() method. Also refactors refresh_labels to iterate over
  (label, value) tuples instead of 5 separate blocks.
- fact/src/metrics/mod.rs: Extract inc_label/inc_label_by helpers in
  EventCounter to deduplicate the label lookup pattern. Widen
  visibility of counter field and MetricEvents to pub(crate).
- fact-ebpf/src/bpf/types.h: Reorder metrics_t fields to group all
  path_* hooks together.

Assisted-by: claude-opus-4-6@default <noreply@opencode.ai>
@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Enterprise

Run ID: 013264d6-609e-4eae-8872-2a40fff76ab2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mauro/feat/refactor-metrics

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

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 28.72%. Comparing base (26b1c8b) to head (1a06dc8).

Files with missing lines Patch % Lines
fact/src/metrics/kernel_metrics.rs 0.00% 32 Missing ⚠️
fact/src/metrics/mod.rs 0.00% 16 Missing ⚠️
fact-ebpf/src/lib.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #806      +/-   ##
==========================================
+ Coverage   27.96%   28.72%   +0.76%     
==========================================
  Files          21       21              
  Lines        2596     2527      -69     
  Branches     2596     2527      -69     
==========================================
  Hits          726      726              
+ Misses       1867     1798      -69     
  Partials        3        3              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Molter73 Molter73 marked this pull request as ready for review June 16, 2026 13:48
@Molter73 Molter73 requested a review from a team as a code owner June 16, 2026 13:48

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 `@fact-ebpf/src/lib.rs`:
- Around line 149-158: The impl_metrics_t! macro invocation has two fields in
the wrong order compared to the C metrics_t struct definition in
fact-ebpf/src/bpf/types.h. The fields d_instantiate and path_rmdir are currently
swapped at the end of the macro argument list. Reorder the fields in the
impl_metrics_t! macro invocation to match the C struct field order exactly:
file_open, path_unlink, path_chmod, path_chown, path_rename, path_mkdir,
d_instantiate, path_rmdir. This ensures correct metric accumulation between Rust
and C code.
🪄 Autofix (Beta)

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: Path: .coderabbit.yml

Review profile: CHILL

Plan: Enterprise

Run ID: 8421f968-219e-4e3c-a235-06d0f88ba51a

📥 Commits

Reviewing files that changed from the base of the PR and between 26b1c8b and 1a06dc8.

📒 Files selected for processing (3)
  • fact-ebpf/src/lib.rs
  • fact/src/metrics/kernel_metrics.rs
  • fact/src/metrics/mod.rs

Comment thread fact-ebpf/src/lib.rs
Comment on lines +149 to +158
impl_metrics_t!(
file_open,
path_unlink,
path_chmod,
path_chown,
path_rename,
path_mkdir,
path_rmdir,
d_instantiate,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check that metrics_t fields match the macro invocation list
# First, find the metrics_t definition in the generated bindings or C header
rg -A 20 "struct metrics_t" fact-ebpf/src/bpf/types.h

# Also verify the hook lists match between lib.rs and kernel_metrics.rs
echo "=== lib.rs hook list ==="
rg -A 10 "impl_metrics_t!" fact-ebpf/src/lib.rs

echo "=== kernel_metrics.rs hook list ==="
rg -A 10 "define_kernel_metrics!" fact/src/metrics/kernel_metrics.rs

Repository: stackrox/fact

Length of output: 768


Reorder fields in the macro invocation to match the C metrics_t struct definition.

All fields are present, but their order does not match the C struct. The C struct defines (in order): file_open, path_unlink, path_chmod, path_chown, path_rename, path_mkdir, d_instantiate, path_rmdir. The Rust macro has path_rmdir and d_instantiate swapped at the end. Update lines 149-158 to match the exact field order from the C definition to ensure correct metric accumulation.

C struct field order (fact-ebpf/src/bpf/types.h)
struct metrics_t {
  struct metrics_by_hook_t file_open;
  struct metrics_by_hook_t path_unlink;
  struct metrics_by_hook_t path_chmod;
  struct metrics_by_hook_t path_chown;
  struct metrics_by_hook_t path_rename;
  struct metrics_by_hook_t path_mkdir;
  struct metrics_by_hook_t d_instantiate;
  struct metrics_by_hook_t path_rmdir;
};
🤖 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 `@fact-ebpf/src/lib.rs` around lines 149 - 158, The impl_metrics_t! macro
invocation has two fields in the wrong order compared to the C metrics_t struct
definition in fact-ebpf/src/bpf/types.h. The fields d_instantiate and path_rmdir
are currently swapped at the end of the macro argument list. Reorder the fields
in the impl_metrics_t! macro invocation to match the C struct field order
exactly: file_open, path_unlink, path_chmod, path_chown, path_rename,
path_mkdir, d_instantiate, path_rmdir. This ensures correct metric accumulation
between Rust and C code.

Source: Coding guidelines

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.

2 participants