feat(analyze): add --save-analysis flag to save computed results as JSON - #64
Open
yoavkatz wants to merge 2 commits into
Open
feat(analyze): add --save-analysis flag to save computed results as JSON#64yoavkatz wants to merge 2 commits into
yoavkatz wants to merge 2 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR bundles two independent sets of changes:
--save-analysisflag — write the computed analysis (per-trace records + group summaries) to a JSON file alongside the existing text report.evaluate-benchmark.shand a temp-file cleanup fix inlibsh/keycloak-direct-access.sh.Changes
analyze_traces.pyrecord_to_dict,build_group_summaries,build_analysis_jsonbuild_group_summariescomputes all aggregated stats per group (timing avg/p50/p95/min/max, token counts, infra metrics, per-call latency averages, % time breakdowns) using the existingavg/percentile/stdhelpers.build_analysis_jsonreturns{"traces": [...], "groups": [...]}— every parsedTraceRecordplus all group summaries.--json PATH— writes the analysis JSON toPATH; text report still prints to stdout unchanged.print_report: now reads frombuild_group_summariesinstead 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--save-analysis FILEflag andSAVE_ANALYSIS_FILEenv var.PYTHON_ARGSconverted from a string to a bash array (safe handling of paths with spaces).--json FILEforwarded toanalyze_traces.pyin both pipeline paths (with and without--save-traces).✓ Saved analysis to FILEconfirmation added.run-ibac-comparison.sh--save-analysis FILEflag, forwarded to theanalyze-run.shcomparison call at the end of the run.evaluate-benchmark.sh✓confirmation on success — matching the existing pattern for the OTEL collector port-forward.libsh/keycloak-direct-access.sh/tmp/kc_put_response.txtscratch file with amktemp-generated temp file and aRETURNtrap that removes it. Avoids collisions under concurrent invocations and ensures cleanup on error paths.Usage
Test plan
echo '{"traces":[]}' | python3 analyze_traces.py --json /tmp/out.json→ file contains{"traces": [], "groups": []}, text prints "No Agent.Session traces found."python3 analyze_traces.py --json /tmp/out.json traces-*.json→groupsandtracespopulated, numbers match the printed tables../analyze-run.sh --kind --window 3h --save-analysis ./analysis.json→ text report prints,✓ Saved analysis to ./analysis.jsonappears,jq . analysis.jsonis valid../run-ibac-comparison.sh --dry --save-analysis ./analysis.json→ dry-run output shows--save-analysisforwarded toanalyze-run.sh.evaluate-benchmark.shproduces a clear error rather than silently continuing.enable_direct_access_grantscalls no longer collide on/tmp/kc_put_response.txt.🤖 Generated with Claude Code