Skip to content

Add JSON logging format support with environment variable control#2120

Merged
moshemorad merged 5 commits into
masterfrom
claude/json-log-format-runner-vqbzst
Jul 14, 2026
Merged

Add JSON logging format support with environment variable control#2120
moshemorad merged 5 commits into
masterfrom
claude/json-log-format-runner-vqbzst

Conversation

@moshemorad

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for JSON-formatted logging across Robusta components, controlled by the ENABLE_JSON_LOGS_FORMAT environment 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

  • New environment variable: Added ENABLE_JSON_LOGS_FORMAT to env_vars.py with a default value of false
  • Logging initialization: Updated log_init.py to conditionally use JsonFormatter from python-json-logger when JSON logging is enabled
    • JSON logs rename levelname to severity to match conventions used in other Robusta services (relay, holmes)
    • Colored text logging remains the default when the flag is disabled
  • Helm configuration: Added global.enableJsonLogsFormat value to values.yaml and wired it to the runner pod via the ENABLE_JSON_LOGS_FORMAT environment variable in runner.yaml
  • KRR integration: Updated the KRR scan job to inherit the JSON logging setting from the runner, but only when KRR_PUSH_SCAN is enabled (to avoid corrupting log parsing when results are extracted from job logs)
  • Dependencies: Added python-json-logger to pyproject.toml
  • Test coverage: Added comprehensive tests in test_log_init.py to verify JSON logging produces valid JSON and plain logging remains unaffected

Implementation Details

  • The JSON formatter uses ISO 8601 date format (%Y-%m-%dT%H:%M:%S) for consistency with other services
  • The logging configuration uses force=True when setting up JSON logging to override any existing handlers
  • The KRR job only inherits the JSON logging setting when results are pushed back via API, preventing log corruption when results are parsed from captured logs

https://claude.ai/code/session_01AozEpN8uwPRQuF8FPRX4Jy

claude added 2 commits July 12, 2026 14:46
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>
@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown

Docker image ready for 5bdad56 (built in 46s)

⚠️ Warning: does not support ARM (ARM images are built on release only - not on every PR)

Use this tag to pull the image for testing.

📋 Copy commands

⚠️ Temporary images are deleted after 30 days. Copy to a permanent registry before using them:

gcloud 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:5bdad56

Patch 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

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 22e7a1bb-2e16-47ad-a1d8-eeee40baa8f7

📥 Commits

Reviewing files that changed from the base of the PR and between bcfa496 and ede94ce.

📒 Files selected for processing (2)
  • src/robusta/runner/log_init.py
  • tests/test_log_init.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_log_init.py

Walkthrough

The 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.

Changes

JSON logging configuration

Layer / File(s) Summary
Configuration propagation
helm/robusta/values.yaml, helm/robusta/templates/runner.yaml, src/robusta/core/model/env_vars.py, playbooks/robusta_playbooks/krr.py
Adds global.enableJsonLogsFormat, maps it to ENABLE_JSON_LOGS_FORMAT, and passes it to KRR scan pods when KRR_PUSH_SCAN is enabled.
Logging initialization and validation
pyproject.toml, src/robusta/runner/log_init.py, tests/test_log_init.py
Adds python-json-logger, supports JSON-per-line or colored logging, and tests both modes including the severity field.

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
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding JSON logging support controlled by an environment variable.
Description check ✅ Passed The description is directly related to the changeset and accurately describes the logging, Helm, dependency, and test updates.
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 claude/json-log-format-runner-vqbzst

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

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 390f262 and d393913.

📒 Files selected for processing (7)
  • helm/robusta/templates/runner.yaml
  • helm/robusta/values.yaml
  • playbooks/robusta_playbooks/krr.py
  • pyproject.toml
  • src/robusta/core/model/env_vars.py
  • src/robusta/runner/log_init.py
  • tests/test_log_init.py

Comment thread src/robusta/runner/log_init.py Outdated
Comment thread src/robusta/runner/log_init.py Outdated
Comment thread tests/test_log_init.py
claude and others added 2 commits July 13, 2026 11:50
- 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>
@moshemorad moshemorad merged commit f81d93e into master Jul 14, 2026
5 checks passed
@moshemorad moshemorad deleted the claude/json-log-format-runner-vqbzst branch July 14, 2026 11:30
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.

3 participants