Skip to content

Commit 11a1c8d

Browse files
committed
lfcli_base.py : Forward LFRequest.last_elapsed_ms through to the API logger
Signed-off-by: Sidartha-CT <neelapu.sidartha@candelatech.com>
1 parent bdfd95b commit 11a1c8d

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

py-json/LANforge/lfcli_base.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ def log_set_filename(filename=None):
220220

221221
def _log_api_call(self, method: str, url: str, data: Optional[Any] = None, response_code: Optional[int] = None,
222222
error: Optional[Exception] = None, diagnostics: Optional[str] = None,
223-
sent_at: Optional[datetime.datetime] = None) -> None:
223+
sent_at: Optional[datetime.datetime] = None,
224+
elapsed_ms: Optional[float] = None) -> None:
224225
"""
225226
Record one json_get/json_post/json_put/json_delete call via lanforge_client.api_logger.
226227
@@ -234,12 +235,15 @@ def _log_api_call(self, method: str, url: str, data: Optional[Any] = None, respo
234235
sent_at: Timestamp captured by LFRequest right before the request was issued
235236
(its last_sent_at attribute); falls back to record_api_call's own call time
236237
when not available.
238+
elapsed_ms: Wall-clock duration of the underlying urlopen() call in milliseconds
239+
(LFRequest's last_elapsed_ms attribute). None when not measured.
237240
238241
Returns:
239242
None.
240243
"""
241244
api_logger.record_api_call(method=method, url=url, data=data, response_code=response_code,
242-
error=error, diagnostics=diagnostics, sent_at=sent_at)
245+
error=error, diagnostics=diagnostics, sent_at=sent_at,
246+
elapsed_ms=elapsed_ms)
243247

244248
def json_post(self, _req_url, _data, debug_=False, suppress_related_commands_=None, response_json_list_=None):
245249
"""
@@ -301,12 +305,14 @@ def json_post(self, _req_url, _data, debug_=False, suppress_related_commands_=No
301305
self._log_api_call("POST", _req_url, data=_data,
302306
response_code=getattr(json_response, 'status', None) or getattr(lf_r, 'last_response_code', None),
303307
error=_api_error, diagnostics=getattr(lf_r, 'last_diagnostics', None),
304-
sent_at=getattr(lf_r, 'last_sent_at', None))
308+
sent_at=getattr(lf_r, 'last_sent_at', None),
309+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
305310
exit(1)
306311
self._log_api_call("POST", _req_url, data=_data,
307312
response_code=getattr(json_response, 'status', None) or getattr(lf_r, 'last_response_code', None),
308313
error=_api_error, diagnostics=getattr(lf_r, 'last_diagnostics', None),
309-
sent_at=getattr(lf_r, 'last_sent_at', None))
314+
sent_at=getattr(lf_r, 'last_sent_at', None),
315+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
310316
return json_response
311317

312318
def json_put(self, _req_url, _data, debug_=False, response_json_list_=None):
@@ -350,12 +356,14 @@ def json_put(self, _req_url, _data, debug_=False, response_json_list_=None):
350356
self._log_api_call("PUT", _req_url, data=_data,
351357
response_code=getattr(json_response, 'status', None) or getattr(lf_r, 'last_response_code', None),
352358
error=_api_error, diagnostics=getattr(lf_r, 'last_diagnostics', None),
353-
sent_at=getattr(lf_r, 'last_sent_at', None))
359+
sent_at=getattr(lf_r, 'last_sent_at', None),
360+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
354361
exit(1)
355362
self._log_api_call("PUT", _req_url, data=_data,
356363
response_code=getattr(json_response, 'status', None) or getattr(lf_r, 'last_response_code', None),
357364
error=_api_error, diagnostics=getattr(lf_r, 'last_diagnostics', None),
358-
sent_at=getattr(lf_r, 'last_sent_at', None))
365+
sent_at=getattr(lf_r, 'last_sent_at', None),
366+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
359367
return json_response
360368

361369
def json_get(self, _req_url, debug_=None):
@@ -385,7 +393,8 @@ def json_get(self, _req_url, debug_=None):
385393
time.sleep(10)
386394
self._log_api_call("GET", _req_url, response_code=getattr(lf_r, 'last_response_code', None),
387395
diagnostics=getattr(lf_r, 'last_diagnostics', None),
388-
sent_at=getattr(lf_r, 'last_sent_at', None))
396+
sent_at=getattr(lf_r, 'last_sent_at', None),
397+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
389398
return None
390399
except ValueError as ve:
391400
_api_error = ve
@@ -396,12 +405,14 @@ def json_get(self, _req_url, debug_=None):
396405
if self.exit_on_error:
397406
self._log_api_call("GET", _req_url, response_code=getattr(lf_r, 'last_response_code', None), error=_api_error,
398407
diagnostics=getattr(lf_r, 'last_diagnostics', None),
399-
sent_at=getattr(lf_r, 'last_sent_at', None))
408+
sent_at=getattr(lf_r, 'last_sent_at', None),
409+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
400410
sys.exit(1)
401411

402412
self._log_api_call("GET", _req_url, response_code=getattr(lf_r, 'last_response_code', None), error=_api_error,
403413
diagnostics=getattr(lf_r, 'last_diagnostics', None),
404-
sent_at=getattr(lf_r, 'last_sent_at', None))
414+
sent_at=getattr(lf_r, 'last_sent_at', None),
415+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
405416
return json_response
406417

407418
def json_delete(self, _req_url, debug_=False):
@@ -425,7 +436,8 @@ def json_delete(self, _req_url, debug_=False):
425436
logger.debug("LFCliBase.json_delete: no entity/response, probabily status 404")
426437
self._log_api_call("DELETE", _req_url, response_code=getattr(lf_r, 'last_response_code', None),
427438
diagnostics=getattr(lf_r, 'last_diagnostics', None),
428-
sent_at=getattr(lf_r, 'last_sent_at', None))
439+
sent_at=getattr(lf_r, 'last_sent_at', None),
440+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
429441
return None
430442
except ValueError as ve:
431443
_api_error = ve
@@ -436,12 +448,14 @@ def json_delete(self, _req_url, debug_=False):
436448
if self.exit_on_error:
437449
self._log_api_call("DELETE", _req_url, response_code=getattr(lf_r, 'last_response_code', None), error=_api_error,
438450
diagnostics=getattr(lf_r, 'last_diagnostics', None),
439-
sent_at=getattr(lf_r, 'last_sent_at', None))
451+
sent_at=getattr(lf_r, 'last_sent_at', None),
452+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
440453
sys.exit(1)
441454
# print("----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ")
442455
self._log_api_call("DELETE", _req_url, response_code=getattr(lf_r, 'last_response_code', None), error=_api_error,
443456
diagnostics=getattr(lf_r, 'last_diagnostics', None),
444-
sent_at=getattr(lf_r, 'last_sent_at', None))
457+
sent_at=getattr(lf_r, 'last_sent_at', None),
458+
elapsed_ms=getattr(lf_r, 'last_elapsed_ms', None))
445459
return json_response
446460

447461
@staticmethod

0 commit comments

Comments
 (0)