@@ -22,22 +22,29 @@ def send_log_to_firehose(log_data: dict) -> None:
2222
2323
2424def generate_and_send_logs (
25- start_time , base_log_data : dict , additional_log_data : dict , is_error_log : bool = False
25+ start_time : float ,
26+ base_log_data : dict ,
27+ additional_log_data : dict ,
28+ use_ms_precision : bool = False ,
29+ is_error_log : bool = False
2630) -> None :
2731 """Generates log data which includes the base_log_data, additional_log_data, and time taken (calculated using the
2832 current time and given start_time) and sends them to Cloudwatch and Firehose."""
29- log_data = {** base_log_data , "time_taken" : f"{ round (time .time () - start_time , 5 )} s" , ** additional_log_data }
33+ seconds_elapsed = time .time () - start_time
34+ formatted_time_elapsed = f"{ round (seconds_elapsed * 1000 , 5 )} ms" if use_ms_precision else \
35+ f"{ round (seconds_elapsed , 5 )} s"
36+
37+ log_data = {** base_log_data , "time_taken" : formatted_time_elapsed , ** additional_log_data }
3038 log_function = logger .error if is_error_log else logger .info
3139 log_function (json .dumps (log_data ))
3240 send_log_to_firehose (log_data )
3341
3442
35- def convert_messsage_to_ack_row_logging_decorator (func ):
43+ def convert_message_to_ack_row_logging_decorator (func ):
3644 """This decorator logs the information on the conversion of a single message to an ack data row"""
3745
3846 @wraps (func )
3947 def wrapper (message , created_at_formatted_string ):
40-
4148 base_log_data = {"function_name" : f"ack_processor_{ func .__name__ } " , "date_time" : str (datetime .now ())}
4249 start_time = time .time ()
4350
@@ -57,13 +64,14 @@ def wrapper(message, created_at_formatted_string):
5764 "operation_requested" : message .get ("operation_requested" , "unknown" ),
5865 ** process_diagnostics (diagnostics , file_key , message_id ),
5966 }
60- generate_and_send_logs (start_time , base_log_data , additional_log_data )
67+ generate_and_send_logs (start_time , base_log_data , additional_log_data , use_ms_precision = True )
6168
6269 return result
6370
6471 except Exception as error :
6572 additional_log_data = {"status" : "fail" , "statusCode" : 500 , "diagnostics" : str (error )}
66- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = True )
73+ generate_and_send_logs (start_time , base_log_data , additional_log_data , use_ms_precision = True ,
74+ is_error_log = True )
6775 raise
6876
6977 return wrapper
0 commit comments