88import time
99from datetime import datetime
1010from functools import wraps
11- from clients import firehose_client , logger , STREAM_NAME
11+ from clients import logger , firehose_client
1212
1313
14- def send_log_to_firehose (log_data : dict ) -> None :
14+ def send_log_to_firehose (stream_name , log_data : dict ) -> None :
1515 """Sends the log_message to Firehose"""
1616 try :
1717 record = {"Data" : json .dumps ({"event" : log_data }).encode ("utf-8" )}
18- response = firehose_client .put_record (DeliveryStreamName = STREAM_NAME , Record = record )
18+ response = firehose_client .put_record (DeliveryStreamName = stream_name , Record = record )
1919 logger .info ("Log sent to Firehose: %s" , response )
2020 except Exception as error : # pylint:disable = broad-exception-caught
2121 logger .exception ("Error sending log to Firehose: %s" , error )
2222
2323
24- def generate_and_send_logs (
25- start_time , base_log_data : dict , additional_log_data : dict , is_error_log : bool = False
26- ) -> None :
24+ def generate_and_send_logs (stream_name ,
25+ start_time , base_log_data : dict , additional_log_data : dict , is_error_log : bool = False
26+ ) -> None :
2727 """Generates log data which includes the base_log_data, additional_log_data, and time taken (calculated using the
2828 current time and given start_time) and sends them to Cloudwatch and Firehose."""
2929 log_data = {** base_log_data , "time_taken" : f"{ round (time .time () - start_time , 5 )} s" , ** additional_log_data }
3030 log_function = logger .error if is_error_log else logger .info
3131 log_function (json .dumps (log_data ))
32- send_log_to_firehose (log_data )
32+ send_log_to_firehose (stream_name , log_data )
3333
3434
35- def logging_decorator (prefix = "id_sync" ):
35+ def logging_decorator (prefix : str , stream_name ):
3636 def decorator (func ):
3737 @wraps (func )
3838 def wrapper (* args , ** kwargs ):
@@ -43,11 +43,13 @@ def wrapper(*args, **kwargs):
4343 start_time = time .time ()
4444 try :
4545 result = func (* args , ** kwargs )
46- generate_and_send_logs (start_time , base_log_data , additional_log_data = result )
46+ generate_and_send_logs (stream_name ,
47+ start_time , base_log_data , additional_log_data = result )
4748 return result
4849 except Exception as e :
4950 additional_log_data = {"statusCode" : 500 , "error" : str (e )}
50- generate_and_send_logs (start_time , base_log_data , additional_log_data , is_error_log = True )
51+ generate_and_send_logs (stream_name ,
52+ start_time , base_log_data , additional_log_data , is_error_log = True )
5153 raise
5254 return wrapper
5355 return decorator
0 commit comments