Add JSON logging format support with environment variable control#2120
Conversation
Add an opt-in ENABLE_JSON_LOGS_FORMAT toggle so the runner emits logs as JSON (one object per line) instead of colored text, matching the format already used by relay. This makes logs easier to index and filter with scrapers like Filebeat. - env_vars.py: ENABLE_JSON_LOGS_FORMAT bool (default false) - log_init.py: JSON branch using pythonjsonlogger.JsonFormatter with levelname -> severity, keeping colored logging as the default - krr.py: propagate the toggle to the KRR scan Job, but only when KRR_PUSH_SCAN is enabled (the non-push path parses the result out of the job's combined stdout/stderr logs, which JSON lines would corrupt) - Helm: global.enableJsonLogsFormat fans out to the runner (and the Holmes subchart automatically) - add python-json-logger dependency + unit test Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy
Re-lock with the same Poetry version that generated the committed lock (1.8.5) so the change is limited to adding python-json-logger instead of rewriting the whole file in the 2.x lock format. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy Signed-off-by: Claude <noreply@anthropic.com>
|
✅ Docker image ready for
Use this tag to pull the image for testing. 📋 Copy commandsgcloud auth configure-docker us-central1-docker.pkg.dev
docker pull us-central1-docker.pkg.dev/robusta-development/temporary-builds/robusta-runner:5bdad56
docker tag us-central1-docker.pkg.dev/robusta-development/temporary-builds/robusta-runner:5bdad56 me-west1-docker.pkg.dev/robusta-development/development/robusta-runner-dev:5bdad56
docker push me-west1-docker.pkg.dev/robusta-development/development/robusta-runner-dev:5bdad56Patch Helm values in one line: helm upgrade --install robusta robusta/robusta \
--reuse-values \
--set runner.image=me-west1-docker.pkg.dev/robusta-development/development/robusta-runner-dev:5bdad56 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThe change adds a global JSON logging setting, propagates it to runner and conditional KRR scan pods, configures JSON or colored logging accordingly, adds the JSON logger dependency, and tests both output modes. ChangesJSON logging configuration
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant HelmValues
participant RunnerDeployment
participant init_logging
participant JsonFormatter
HelmValues->>RunnerDeployment: provide ENABLE_JSON_LOGS_FORMAT
RunnerDeployment->>init_logging: initialize logging
init_logging->>JsonFormatter: configure JSON formatting when enabled
JsonFormatter-->>RunnerDeployment: emit line-delimited JSON logs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
- Restore python-json-logger in poetry.lock: the previous lock commit accidentally staged the pre-change lock (via git checkout) so the dependency was dropped, leaving the lock inconsistent with pyproject.toml and failing the poetry-lock pre-commit check - Remove the debug print() in the JSON logging branch of init_logging(); a plain-text line on stdout before the JSON handler would corrupt the JSON log stream Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy Signed-off-by: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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/robusta/runner/log_init.py`:
- Line 20: Remove the raw print statement in the JSON logging setup flow so
stdout contains only JSON log objects; if the startup message must be retained,
emit it through the configured logger after initialization.
- Around line 29-33: Update the plain logging branch in init_logging to use the
same root-handler replacement strategy as the JSON branch, ensuring prior JSON
handlers are removed when reconfiguring. Preserve the existing colored format
and logging settings while applying equivalent forced reconfiguration through
colorlog.basicConfig.
In `@tests/test_log_init.py`:
- Around line 21-27: Update _capture_root_output so the temporary root logger
handler is removed after each test, using fixture teardown or returning the
handler for explicit cleanup after assertions. Preserve the existing buffer
capture and formatter behavior while preventing handlers and StringIO buffers
from accumulating across tests.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 67b3f426-43b0-493f-82d6-a0d78939b70b
📒 Files selected for processing (7)
helm/robusta/templates/runner.yamlhelm/robusta/values.yamlplaybooks/robusta_playbooks/krr.pypyproject.tomlsrc/robusta/core/model/env_vars.pysrc/robusta/runner/log_init.pytests/test_log_init.py
- init_logging(): use force=True in the plain (colorlog) branch too, so a prior JSON handler is replaced on reconfiguration, matching the JSON branch; also drop the leftover debug print - test_log_init: add an autouse fixture that snapshots/restores the root logger so handlers don't leak between tests Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy Signed-off-by: Claude <noreply@anthropic.com>
Summary
This PR adds support for JSON-formatted logging across Robusta components, controlled by the
ENABLE_JSON_LOGS_FORMATenvironment variable. When enabled, logs are emitted as JSON objects (one per line) instead of colored text, making them easier to parse and index with log scrapers like Filebeat.Key Changes
ENABLE_JSON_LOGS_FORMATtoenv_vars.pywith a default value offalselog_init.pyto conditionally useJsonFormatterfrompython-json-loggerwhen JSON logging is enabledlevelnametoseverityto match conventions used in other Robusta services (relay, holmes)global.enableJsonLogsFormatvalue tovalues.yamland wired it to the runner pod via theENABLE_JSON_LOGS_FORMATenvironment variable inrunner.yamlKRR_PUSH_SCANis enabled (to avoid corrupting log parsing when results are extracted from job logs)python-json-loggertopyproject.tomltest_log_init.pyto verify JSON logging produces valid JSON and plain logging remains unaffectedImplementation Details
%Y-%m-%dT%H:%M:%S) for consistency with other servicesforce=Truewhen setting up JSON logging to override any existing handlershttps://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy