Skip to content

Api logs rex - #84

Open
Sidartha-CT wants to merge 7 commits into
masterfrom
api_logs_rex
Open

Api logs rex#84
Sidartha-CT wants to merge 7 commits into
masterfrom
api_logs_rex

Conversation

@Sidartha-CT

Copy link
Copy Markdown
Collaborator

Add opt-in API call logging for LANforge JSON requests

Summary

Adds a lightweight, opt-in CSV logger for every json_get / json_post / json_put / json_delete call made through either of LANforge's two client paths:

  • py-json/LANforge/lfcli_base.pyRealm
  • lanforge_client/lanforge_api.pyLFJsonCommand / LFJsonQuery

When enabled via:

lf_webpage.py --save_api

each call's method, URL, payload, HTTP status, and error/diagnostics get appended to a CSV (default ~/lf_api_calls.csv), which is copied into the run's report folder.

This is meant for debugging a specific test run, not for permanent instrumentation.


Why a new lanforge_client/api_call_log.py module

We want to record specific fields per call (method, url, payload, status, error) and post-process them later as CSV, so the recording logic needed to live inside json_get() / json_post() / json_put() / json_delete() themselves rather than being bolted on from outside.

There are two independent call paths that implement those four methods:

  • LFCliBase (py-json, used by Realm and most scripts)
  • BaseLFJsonRequest (lanforge_client/lanforge_api.py, not currently used by interop)

Rather than duplicating logging logic in both, the common pieces (CSV schema, enable/disable state, the actual record() call) live in one new module in lanforge_client/, and each call path just calls into it.

lanforge_client is the natural home since it's already a shared dependency of both.


Why global (module-level) state instead of a constructor parameter

The enable flag is deliberately process-wide global state in api_call_log.py, not a parameter threaded through Realm / LFCliBase / profile constructors.

Reason:

Realm gets instantiated directly all over the py-scripts tree, and profile classes build their own nested instances.

For example:

  • lf_webpage.py holds a Realm
  • It also imports l4_cxprofile, which itself instantiates its own Realm

To pass an "enable logging" flag through the constructor, it would need to be threaded through every one of those call sites, and there's no guarantee the import/instantiation chain doesn't go a level or two deeper in other scripts.

A module-level configure() called once near the top of a script's main() avoids that — every LFCliBase / Realm or LFJsonCommand / LFJsonQuery created afterward in the same process just picks it up.


What's covered / not covered

Covered

  • LFRequest.py now captures last_response_code and a one-line last_diagnostics summary (reason + X-Error-* headers) on every request, win or fail, so callers have something worth logging.
  • lfcli_base.py (LFCliBase, used by Realm) and lanforge_api.py (BaseLFJsonRequest) both call api_call_log.record(...) after every GET / POST / PUT / DELETE, using those diagnostics.

Not covered

  • A small number of scripts don't go through json_get() / etc. at all — those aren't covered yet and will be migrated separately.

Pause / resume

monitor_for_runtime_csv() in lf_webpage.py polls in a tight loop and would otherwise flood the CSV with repetitive, rarely-useful entries.

Added:

  • api_call_log.pause()
  • api_call_log.resume()

so state (enabled + filename) is preserved but recording is suppressed during that loop by default.

--log_monitor_api_calls opts back in if someone actually wants those calls logged too.


Usage

lf_webpage.py ... \
    --save_api \
    [--api_log_file_name /path/to/log.csv] \
    [--log_monitor_api_calls] (this is somthing change wrt script ) 

Commits

  • LFRequest.py — capture response code + diagnostics summary per request
  • api_call_log.py — new process-wide CSV logger (configure / record / is_enabled)
  • lfcli_base.py — wire _log_api_call into json_get / json_post / json_put / json_delete
  • lanforge_api.py — same, for the lanforge_client request path
  • lf_webpage.py — add --save_api / --api_log_file_name flags, configure logger, copy CSV into report dir
  • api_call_log.py — add pause() / resume()
  • lf_webpage.py — pause logging during the runtime-CSV monitor loop by default, add --log_monitor_api_calls override

Follow-ups (out of scope here)

  • Migrate the remaining scripts that don't go through json_get() / etc.

Here is the log csv That generated out of this when we ran lf_webpage.py :
lf_api_calls.csv

… request

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
…call logging

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
…default

Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
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