Skip to content

feat(analyze): add --save-analysis flag to save computed results as JSON - #64

Open
yoavkatz wants to merge 2 commits into
mainfrom
feat/save-analysis-json
Open

feat(analyze): add --save-analysis flag to save computed results as JSON#64
yoavkatz wants to merge 2 commits into
mainfrom
feat/save-analysis-json

Conversation

@yoavkatz

@yoavkatz yoavkatz commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR bundles two independent sets of changes:

  1. --save-analysis flag — write the computed analysis (per-trace records + group summaries) to a JSON file alongside the existing text report.
  2. Minor fixes — Prometheus port-forward readiness check in evaluate-benchmark.sh and a temp-file cleanup fix in libsh/keycloak-direct-access.sh.

Changes

analyze_traces.py

  • New functions: record_to_dict, build_group_summaries, build_analysis_json
    • build_group_summaries computes all aggregated stats per group (timing avg/p50/p95/min/max, token counts, infra metrics, per-call latency averages, % time breakdowns) using the existing avg/percentile/std helpers.
    • build_analysis_json returns {"traces": [...], "groups": [...]} — every parsed TraceRecord plus all group summaries.
  • New CLI flag: --json PATH — writes the analysis JSON to PATH; text report still prints to stdout unchanged.
  • Refactored print_report: now reads from build_group_summaries instead of recomputing stats from raw records. The text output and JSON output share a single computation, so numbers are guaranteed to match.

analyze-run.sh

  • Added --save-analysis FILE flag and SAVE_ANALYSIS_FILE env var.
  • PYTHON_ARGS converted from a string to a bash array (safe handling of paths with spaces).
  • --json FILE forwarded to analyze_traces.py in both pipeline paths (with and without --save-traces).
  • Banner and ✓ Saved analysis to FILE confirmation added.

run-ibac-comparison.sh

  • Added --save-analysis FILE flag, forwarded to the analyze-run.sh comparison call at the end of the run.

evaluate-benchmark.sh

  • Added a readiness check for the Prometheus port-forward: wait 3 s, confirm the process is still alive, print a clear error (with kubectl log tail) if it died, and emit a confirmation on success — matching the existing pattern for the OTEL collector port-forward.

libsh/keycloak-direct-access.sh

  • Replace the hardcoded /tmp/kc_put_response.txt scratch file with a mktemp-generated temp file and a RETURN trap that removes it. Avoids collisions under concurrent invocations and ensures cleanup on error paths.

Usage

# save analysis from a live cluster run
./analyze-run.sh --kind --window 3h --save-analysis ./analysis.json

# save analysis from a full ibac comparison
./run-ibac-comparison.sh --save-analysis ./analysis.json

# use analyze_traces.py directly on a saved traces file
python3 analyze_traces.py --json ./analysis.json traces.json

Test plan

  • echo '{"traces":[]}' | python3 analyze_traces.py --json /tmp/out.json → file contains {"traces": [], "groups": []}, text prints "No Agent.Session traces found."
  • Run against a saved raw traces file: python3 analyze_traces.py --json /tmp/out.json traces-*.jsongroups and traces populated, numbers match the printed tables.
  • ./analyze-run.sh --kind --window 3h --save-analysis ./analysis.json → text report prints, ✓ Saved analysis to ./analysis.json appears, jq . analysis.json is valid.
  • ./run-ibac-comparison.sh --dry --save-analysis ./analysis.json → dry-run output shows --save-analysis forwarded to analyze-run.sh.
  • Prometheus port-forward failure in evaluate-benchmark.sh produces a clear error rather than silently continuing.
  • Concurrent enable_direct_access_grants calls no longer collide on /tmp/kc_put_response.txt.

🤖 Generated with Claude Code

Adds a --save-analysis FILE option to analyze-run.sh and
run-ibac-comparison.sh that writes the full analysis output to a JSON
file alongside the existing text report.

## What changed

### analyze_traces.py

- Added `record_to_dict`, `build_group_summaries`, and
  `build_analysis_json` functions that serialize every parsed
  TraceRecord and all aggregated group statistics (timing avg/p50/p95/
  min/max, token counts, infra metrics, per-call latency averages, %
  breakdowns) into a structured dict.
- Added `--json PATH` CLI argument: when given, the computed analysis
  is written to PATH as pretty-printed JSON; the text report continues
  to print to stdout unchanged.
- Refactored `print_report` to consume `build_group_summaries` instead
  of recomputing stats independently — the text output and JSON output
  now share a single computation, so the numbers are guaranteed to
  match.

### analyze-run.sh

- Added `--save-analysis FILE` flag (and `SAVE_ANALYSIS_FILE` env var).
- Converted `PYTHON_ARGS` from a string to a bash array so file paths
  with spaces are handled safely.
- Forwards `--json FILE` to `analyze_traces.py` in both the tee and
  non-tee pipeline paths.
- Prints a `✓ Saved analysis to FILE` confirmation on success.

### run-ibac-comparison.sh

- Added `--save-analysis FILE` flag that is forwarded to the
  `analyze-run.sh` comparison call at the end of the run.

## Usage

    # save analysis from a live cluster run
    ./analyze-run.sh --kind --window 3h --save-analysis ./analysis.json

    # save analysis from a full ibac comparison
    ./run-ibac-comparison.sh --save-analysis ./analysis.json

    # use analyze_traces.py directly on a saved traces file
    python3 analyze_traces.py --json ./analysis.json traces.json

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Yoav Katz <katz@il.ibm.com>
…leanup

Two independent fixes carried over from the refactor/keycloak-direct-access-helper branch:

### evaluate-benchmark.sh

Add a readiness check for the Prometheus port-forward that was already
started but never verified: wait 3 s, confirm the process is still
alive, print a clear error (with kubectl log tail) if it died, and emit
a ✓ confirmation on success — matching the pattern used for the OTEL
collector port-forward directly above.

### libsh/keycloak-direct-access.sh

Replace the hardcoded /tmp/kc_put_response.txt scratch file with a
`mktemp`-generated temp file and a RETURN trap that removes it. This
avoids collisions when multiple invocations run concurrently and ensures
the file is cleaned up even on error paths.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Yoav Katz <katz@il.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New/ToDo

Development

Successfully merging this pull request may close these issues.

2 participants