Skip to content

Commit 0d9d40f

Browse files
committed
Fix for per row ack logging timer
1 parent 3010c2a commit 0d9d40f

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

ack_backend/src/convert_message_to_ack_row.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Functions for converting the incoming message body into a row of ack data"""
22

33
from typing import Union
4-
from logging_decorators import convert_messsage_to_ack_row_logging_decorator
4+
from logging_decorators import convert_message_to_ack_row_logging_decorator
55
from update_ack_file import create_ack_data
66

77

@@ -19,7 +19,7 @@ def get_error_message_for_ack_file(message_diagnostics) -> Union[None, str]:
1919
return message_diagnostics.get("error_message", "Unable to determine diagnostics issue")
2020

2121

22-
@convert_messsage_to_ack_row_logging_decorator
22+
@convert_message_to_ack_row_logging_decorator
2323
def convert_message_to_ack_row(message, created_at_formatted_string):
2424
"""
2525
Takes a single message and returns the ack data row for that message.

ack_backend/src/logging_decorators.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,28 @@ def send_log_to_firehose(log_data: dict) -> None:
2222

2323

2424
def 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+
time_elapsed = f"{round(seconds_elapsed * 1000, 5)}ms" if use_ms_precision else f"{round(seconds_elapsed, 5)}s"
35+
36+
log_data = {**base_log_data, "time_taken": time_elapsed, **additional_log_data}
3037
log_function = logger.error if is_error_log else logger.info
3138
log_function(json.dumps(log_data))
3239
send_log_to_firehose(log_data)
3340

3441

35-
def convert_messsage_to_ack_row_logging_decorator(func):
42+
def convert_message_to_ack_row_logging_decorator(func):
3643
"""This decorator logs the information on the conversion of a single message to an ack data row"""
3744

3845
@wraps(func)
3946
def wrapper(message, created_at_formatted_string):
40-
4147
base_log_data = {"function_name": f"ack_processor_{func.__name__}", "date_time": str(datetime.now())}
4248
start_time = time.time()
4349

@@ -57,13 +63,14 @@ def wrapper(message, created_at_formatted_string):
5763
"operation_requested": message.get("operation_requested", "unknown"),
5864
**process_diagnostics(diagnostics, file_key, message_id),
5965
}
60-
generate_and_send_logs(start_time, base_log_data, additional_log_data)
66+
generate_and_send_logs(start_time, base_log_data, additional_log_data, use_ms_precision=True)
6167

6268
return result
6369

6470
except Exception as error:
6571
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)
72+
generate_and_send_logs(start_time, base_log_data, additional_log_data, use_ms_precision=True,
73+
is_error_log=True)
6774
raise
6875

6976
return wrapper

ack_backend/tests/utils/values_for_ack_backend_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class ValidValues:
157157
"vaccine_type": MOCK_MESSAGE_DETAILS.vaccine_type,
158158
"message_id": MOCK_MESSAGE_DETAILS.row_id,
159159
"operation_requested": "CREATE",
160-
"time_taken": "1.0s",
160+
"time_taken": "1000.0ms",
161161
"local_id": MOCK_MESSAGE_DETAILS.local_id,
162162
"statusCode": 200,
163163
"diagnostics": "Operation completed successfully",
@@ -234,7 +234,7 @@ class InvalidValues:
234234
"vaccine_type": "unknown",
235235
"message_id": "unknown",
236236
"operation_requested": "unknown",
237-
"time_taken": "1.0s",
237+
"time_taken": "1000.0ms",
238238
"local_id": "unknown",
239239
"statusCode": 500,
240240
"diagnostics": "An unhandled error occurred during batch processing",

0 commit comments

Comments
 (0)