-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogging_decorators.py
More file actions
161 lines (133 loc) · 5.32 KB
/
logging_decorators.py
File metadata and controls
161 lines (133 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Decorators for logging and sending logs to Firehose"""
import os
import time
from datetime import datetime
from functools import wraps
from common.log_decorator import generate_and_send_logs
PREFIX = "ack_processor"
STREAM_NAME = os.getenv("SPLUNK_FIREHOSE_NAME", "immunisation-fhir-api-internal-dev-splunk-firehose")
def convert_message_to_ack_row_logging_decorator(func):
"""This decorator logs the information on the conversion of a single message to an ack data row"""
@wraps(func)
def wrapper(message, created_at_formatted_string):
base_log_data = {
"function_name": f"{PREFIX}_{func.__name__}",
"date_time": str(datetime.now()),
}
start_time = time.time()
try:
result = func(message, created_at_formatted_string)
file_key = message.get("file_key", "file_key_missing")
message_id = message.get("row_id", "unknown")
diagnostics = message.get("diagnostics")
additional_log_data = {
"file_key": file_key,
"message_id": message_id,
"operation_start_time": message.get("operation_start_time", "unknown"),
"operation_end_time": message.get("operation_end_time", "unknown"),
"vaccine_type": message.get("vaccine_type", "unknown"),
"supplier": message.get("supplier", "unknown"),
"local_id": message.get("local_id", "unknown"),
"operation_requested": message.get("operation_requested", "unknown"),
**process_diagnostics(diagnostics, file_key, message_id),
}
generate_and_send_logs(
STREAM_NAME,
start_time,
base_log_data,
additional_log_data,
use_ms_precision=True,
)
return result
except Exception as error:
additional_log_data = {
"status": "fail",
"statusCode": 500,
"diagnostics": str(error),
}
generate_and_send_logs(
STREAM_NAME,
start_time,
base_log_data,
additional_log_data,
use_ms_precision=True,
is_error_log=True,
)
raise
return wrapper
def complete_batch_file_process_logging_decorator(func):
"""This decorator logs when record processing is complete."""
@wraps(func)
def wrapper(*args, **kwargs):
base_log_data = {
"function_name": f"{PREFIX}_{func.__name__}",
"date_time": str(datetime.now()),
}
start_time = time.time()
# NB this doesn't require a try-catch block as the wrapped function never throws an exception
result = func(*args, **kwargs)
if result is not None:
message_for_logs = "Record processing complete"
base_log_data.update(result)
additional_log_data = {
"status": "success",
"statusCode": 200,
"message": message_for_logs,
}
generate_and_send_logs(STREAM_NAME, start_time, base_log_data, additional_log_data)
return result
return wrapper
def ack_lambda_handler_logging_decorator(func):
"""This decorator logs the execution info for the ack lambda handler."""
@wraps(func)
def wrapper(event, context, *args, **kwargs):
base_log_data = {
"function_name": f"{PREFIX}_{func.__name__}",
"date_time": str(datetime.now()),
}
start_time = time.time()
try:
result = func(event, context, *args, **kwargs)
message_for_logs = "Lambda function executed successfully!"
additional_log_data = {
"status": "success",
"statusCode": 200,
"message": message_for_logs,
}
generate_and_send_logs(STREAM_NAME, start_time, base_log_data, additional_log_data)
return result
except Exception as error:
additional_log_data = {
"status": "fail",
"statusCode": 500,
"diagnostics": str(error),
}
generate_and_send_logs(
STREAM_NAME,
start_time,
base_log_data,
additional_log_data,
is_error_log=True,
)
raise
return wrapper
def process_diagnostics(diagnostics, file_key, message_id):
"""Returns a dictionary containing the status, statusCode and diagnostics"""
if diagnostics is not None:
return {
"status": "fail",
"statusCode": (diagnostics.get("statusCode") if isinstance(diagnostics, dict) else 500),
"diagnostics": (
diagnostics.get("error_message")
if isinstance(diagnostics, dict)
else "Unable to determine diagnostics issue"
),
}
if file_key == "file_key_missing" or message_id == "unknown":
diagnostics = "An unhandled error occurred during batch processing"
return {"status": "fail", "statusCode": 500, "diagnostics": diagnostics}
return {
"status": "success",
"statusCode": 200,
"diagnostics": "Operation completed successfully",
}