Skip to content

Reduce allocations when JSON audit-log serialization - #3783

Open
JakubOnderka wants to merge 2 commits into
owasp-modsecurity:v3/masterfrom
JakubOnderka:json-log-perf
Open

JakubOnderka wants to merge 2 commits into
owasp-modsecurity:v3/masterfrom
JakubOnderka:json-log-perf

Conversation

@JakubOnderka

@JakubOnderka JakubOnderka commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

what

  • Replace the LOGFY_ADD macros with type-safe local helper lambdas using std::string_view.
  • Avoid the intermediate YAJL output buffer and the final copy into std::string.
  • Use const references when iterating over rule messages and tags to avoid unnecessary copies.

why

  • Reduce allocations and data copying when generating JSON audit logs.
  • Improve performance on the audit-log serialization path.

Summary by CodeRabbit

  • Bug Fixes
    • Transaction JSON output now preserves complete request and response header values, including values with embedded NUL characters that could previously be truncated.
    • The output retains its trailing newline, keeping the existing end-of-output formatting consistent.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes

Transaction::toJSON now builds its output with string_view-based helpers and writes YAJL output directly into the result string. Request and response headers are passed as strings, and rule metadata iteration uses const references.

Transaction JSON serialization

Layer / File(s) Summary
JSON output helpers
headers/modsecurity/transaction.h, src/transaction.cc
The JSON-emission macros were removed. Transaction::toJSON adds string_view-based helpers and a YAJL callback that appends output directly to the result string.
Transaction field emission
src/transaction.cc
Transaction, request, response, producer, and rule-message fields use the new helpers. Header names and values are passed as strings, and iteration over components, messages, and tags uses const references. The trailing newline is appended directly to the result string.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: zimmerle, airween, meirdev

Merge Risk: Minimal · up to eb07a

This change reduces allocations when serializing the JSON audit log and keeps the same output structure. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Reduce allocations when JSON audit-log serialization" matches the main change. The summary shows Transaction::toJSON now writes JSON through std::string_view helpers and streams YAJL output…
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.
Full details: Title check

Explanation

The title "Reduce allocations when JSON audit-log serialization" matches the main change. The summary shows Transaction::toJSON now writes JSON through std::string_view helpers and streams YAJL output directly into the result string, which avoids intermediate buffers and copies. The PR objectives state the same goal of reducing allocations and copying in JSON audit-log serialization. The title is short, specific, and free of noise. Minor gap: it does not mention the removal of the LOGFY_ADD macros, but that detail is secondary to the title's purpose.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 89f57003-85bd-4136-9adc-d7d457d60b0f
Commits

Reviewing files that changed from the base of the PR and between 475aece and eb07aec.

Files selected for processing: 2

  • headers/modsecurity/transaction.h
  • src/transaction.cc
Files with no reviewable changes: 1

  • headers/modsecurity/transaction.h

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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