Skip to content

Add patch to send crowdsec stats - #16

Merged
guimard merged 1 commit into
masterfrom
crowdsec-stats
May 8, 2026
Merged

guimard merged 1 commit into
masterfrom
crowdsec-stats

Conversation

@guimard

@guimard guimard commented May 8, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features
    • Added CrowdSec usage metrics collection and reporting capabilities
    • New configuration options in the manager for enabling metrics and setting flush intervals
    • Platform now tracks and periodically reports request and security event metrics to CrowdSec

@guimard
guimard requested a review from Copilot May 8, 2026 09:59
@guimard guimard self-assigned this May 8, 2026
@coderabbitai

coderabbitai Bot commented May 8, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Rate limit exceeded

@guimard has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 36 minutes and 31 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0deb2116-a277-4a90-a666-8c1761c0765e

📥 Commits

Reviewing files that changed from the base of the PR and between 7ed2774 and 9b20546.

📒 Files selected for processing (9)
  • Changes.md
  • full/Dockerfile
  • full/crowdsec-stats.patch
  • manager/Dockerfile
  • manager/crowdsec-stats.patch
  • portal/Dockerfile
  • portal/crowdsec-stats.patch
  • uwsgi-portal/Dockerfile
  • uwsgi-portal/crowdsec-stats.patch
📝 Walkthrough

Walkthrough

This pull request adds CrowdSec usage-metrics support to LemonLDAP::NG across manager configuration and portal request processing. The changes introduce two new configuration attributes (enablement toggle and flush interval), implement metrics collection and periodic HTTP POST delivery to CrowdSec LAPI, and extend the bouncer method's return signature to pass ban decision context.

Changes

CrowdSec Metrics Integration

Layer / File(s) Summary
Changelog & Configuration Schema
Changes.md, full/crowdsec-stats.patch, manager/crowdsec-stats.patch
Changelog entry added; manager attribute schema extended with crowdsecMetricsEnabled (boolean, default 0) and crowdsecMetricsInterval (integer, default 60 seconds); both attributes registered in manager tree UI structure.
Manager UI Translations
full/crowdsec-stats.patch, manager/crowdsec-stats.patch
Localized labels for metrics configuration added across 14 language files (Arabic, English, Spanish, French, Hebrew, Italian, Polish, Portuguese, Brazilian Portuguese, Russian, Turkish, Vietnamese, Chinese, Traditional Chinese).
Portal Metrics State & Initialization
portal/crowdsec-stats.patch, uwsgi-portal/crowdsec-stats.patch
CrowdSec library introduces three internal attributes to track processed request counts, dropped bans grouped by origin and IP type, and service startup timestamp; _init records startup time for metrics payloads.
Bouncer Return Signature Extension
portal/crowdsec-stats.patch, uwsgi-portal/crowdsec-stats.patch
bouncer($ip) method now returns a third value ($decision, the ban decision object) in addition to existing ($ok, $err) tuple.
Metrics Recording & HTTP Flushing
portal/crowdsec-stats.patch, uwsgi-portal/crowdsec-stats.patch
_recordRequest($ip, $banDecision) increments processed/dropped counters by decision origin and IP type; triggers _flushMetrics() on interval; _flushMetrics builds usage-metrics JSON payload and POSTs to /v1/usage-metrics with agent metadata, window size, and metric items, then resets counters.
Plugin Integration
portal/crowdsec-stats.patch, uwsgi-portal/crowdsec-stats.patch
Plugin checkCrowdsec method captures bouncer's third return value and calls _recordRequest for each request, passing the decision when a ban is triggered or undef otherwise.
Docker Build Configuration
full/Dockerfile, manager/Dockerfile, portal/Dockerfile, uwsgi-portal/Dockerfile
Patch application loops in all four image Dockerfiles extended to include and apply crowdsec-stats.patch during build.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A bouncer returns decisions three,
With metrics flowing wild and free,
To CrowdSec's LAPI they race,
Usage-stats find their rightful place!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding a patch to enable CrowdSec statistics reporting across multiple Docker services.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch crowdsec-stats

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copilot AI 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.

Pull request overview

This PR introduces a new crowdsec-stats.patch to extend the LemonLDAP::NG CrowdSec integration so the portal can push usage-metrics to the CrowdSec Local API, and wires the patch into the various Docker images (portal/uwsgi-portal/manager/full). It also adds Manager configuration knobs to control whether metrics are enabled and how frequently they flush.

Changes:

  • Apply a new crowdsec-stats.patch in the portal and uwsgi-portal images to record request counters and POST usage-metrics to CrowdSec LAPI.
  • Add new Manager configuration attributes (crowdsecMetricsEnabled, crowdsecMetricsInterval) and expose them in the configuration tree + UI strings.
  • Update Dockerfiles and Changes.md to include the new patch.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
uwsgi-portal/Dockerfile Adds crowdsec-stats.patch to the patch-apply sequence.
uwsgi-portal/crowdsec-stats.patch Adds request metrics recording + periodic usage-metrics POST to CrowdSec LAPI for uwsgi portal build.
portal/Dockerfile Adds crowdsec-stats.patch to the patch-apply sequence.
portal/crowdsec-stats.patch Adds request metrics recording + periodic usage-metrics POST to CrowdSec LAPI for portal build.
manager/Dockerfile Adds crowdsec-stats.patch to the patch-apply sequence.
manager/crowdsec-stats.patch Introduces Manager-side config attributes + tree entries + language strings for metrics settings.
full/Dockerfile Adds crowdsec-stats.patch to the patch-apply sequence.
full/crowdsec-stats.patch Same Manager-side config/strings patch applied in the full image.
Changes.md Notes addition of the CrowdSec statistics patch in the changelog.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread portal/crowdsec-stats.patch Outdated
Comment thread uwsgi-portal/crowdsec-stats.patch Outdated
Comment thread portal/crowdsec-stats.patch
Comment thread uwsgi-portal/crowdsec-stats.patch
Comment thread portal/crowdsec-stats.patch
Comment thread uwsgi-portal/crowdsec-stats.patch

@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: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
portal/Dockerfile (1)

67-82: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use a safe glob when deleting patch files.

ShellCheck flags rm -f *.patch with SC2035: file names starting with dashes could be misinterpreted as options. Use rm -f -- ./*.patch to safely handle any filename.

🧹 Proposed fix
-    rm -f *.patch && \
+    rm -f -- ./*.patch && \
🤖 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 `@portal/Dockerfile` around lines 67 - 82, The RUN step in the Dockerfile uses
a bare glob removal "rm -f *.patch" which ShellCheck flags (SC2035) because
filenames beginning with dashes could be treated as options; update the removal
in the RUN command that contains the patch loop and subsequent file operations
(the line with "rm -f *.patch") to use a safe glob style by passing the option
terminator and an explicit relative glob (e.g., use "-- ./*.patch") so that
patch files are removed safely even if their names begin with "-" or contain
special characters.
🤖 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 `@portal/crowdsec-stats.patch`:
- Around line 104-109: The current code resets the in-memory window counters
unconditionally after attempting the POST, which drops metrics on transient
failures; change the logic in the push path so that $self->_metricsCounters(...)
and $self->_droppedByOrigin(...) are only reset when $resp->is_success is true
(i.e., move the counter reset into the success branch) and on failure leave the
counters intact so the next flush will retry sending the same window; keep the
existing warn using $resp->status_line and return/exit the function on failure
without clearing the window.
- Around line 39-42: The guard in _recordRequest is inverted so missing configs
enable metrics; update the condition to only proceed when crowdsecMetricsEnabled
is explicitly true. In function _recordRequest check
$self->conf->{crowdsecMetricsEnabled} (or use exists + truth check) and return
early when it is undefined or false, then increment
_metricsCounters->{processed} and perform posting; reference the _recordRequest
function and conf->{crowdsecMetricsEnabled} setting to locate and correct the
logic.

---

Outside diff comments:
In `@portal/Dockerfile`:
- Around line 67-82: The RUN step in the Dockerfile uses a bare glob removal "rm
-f *.patch" which ShellCheck flags (SC2035) because filenames beginning with
dashes could be treated as options; update the removal in the RUN command that
contains the patch loop and subsequent file operations (the line with "rm -f
*.patch") to use a safe glob style by passing the option terminator and an
explicit relative glob (e.g., use "-- ./*.patch") so that patch files are
removed safely even if their names begin with "-" or contain special characters.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 38ea55d4-6a3d-4bb5-ba4b-c97e92e230dd

📥 Commits

Reviewing files that changed from the base of the PR and between 18062bf and 7ed2774.

📒 Files selected for processing (9)
  • Changes.md
  • full/Dockerfile
  • full/crowdsec-stats.patch
  • manager/Dockerfile
  • manager/crowdsec-stats.patch
  • portal/Dockerfile
  • portal/crowdsec-stats.patch
  • uwsgi-portal/Dockerfile
  • uwsgi-portal/crowdsec-stats.patch

Comment on lines +39 to +42
+sub _recordRequest {
+ my ( $self, $ip, $banDecision ) = @_;
+ return unless defined $self->conf->{crowdsecMetricsEnabled} and !$self->conf->{crowdsecMetricsEnabled};
+ $self->_metricsCounters->{processed}++;

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 | ⚡ Quick win

The metrics feature is effectively enabled when the key is missing.

Line 41 only returns when crowdsecMetricsEnabled is both defined and false. On older configs where the key is absent, _recordRequest() still counts requests and will eventually post usage metrics, which breaks the advertised opt-in behavior.

🔒 Proposed fix
 sub _recordRequest {
     my ( $self, $ip, $banDecision ) = `@_`;
-    return unless defined $self->conf->{crowdsecMetricsEnabled} and !$self->conf->{crowdsecMetricsEnabled};
+    return unless $self->conf->{crowdsecMetricsEnabled};
     $self->_metricsCounters->{processed}++;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
+sub _recordRequest {
+ my ( $self, $ip, $banDecision ) = @_;
+ return unless defined $self->conf->{crowdsecMetricsEnabled} and !$self->conf->{crowdsecMetricsEnabled};
+ $self->_metricsCounters->{processed}++;
sub _recordRequest {
my ( $self, $ip, $banDecision ) = `@_`;
return unless $self->conf->{crowdsecMetricsEnabled};
$self->_metricsCounters->{processed}++;
🤖 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 `@portal/crowdsec-stats.patch` around lines 39 - 42, The guard in
_recordRequest is inverted so missing configs enable metrics; update the
condition to only proceed when crowdsecMetricsEnabled is explicitly true. In
function _recordRequest check $self->conf->{crowdsecMetricsEnabled} (or use
exists + truth check) and return early when it is undefined or false, then
increment _metricsCounters->{processed} and perform posting; reference the
_recordRequest function and conf->{crowdsecMetricsEnabled} setting to locate and
correct the logic.

Comment thread portal/crowdsec-stats.patch
@guimard
guimard merged commit b57a0ab into master May 8, 2026
5 checks passed
@guimard
guimard deleted the crowdsec-stats branch May 16, 2026 15:47
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