From 1ef24feb711c2336153a4cc3fdf60b1afe8d9c47 Mon Sep 17 00:00:00 2001 From: HongSena Date: Mon, 6 Nov 2023 00:11:18 +0900 Subject: [PATCH 1/2] update gcp collector add cloudwatch logging code --- .../gcp/lambda/lambda_function.py | 21 +++++++++++++++++++ const_config.py | 9 +++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/collector/spot-dataset/gcp/lambda/lambda_function.py b/collector/spot-dataset/gcp/lambda/lambda_function.py index 24e9724..9aa2377 100644 --- a/collector/spot-dataset/gcp/lambda/lambda_function.py +++ b/collector/spot-dataset/gcp/lambda/lambda_function.py @@ -32,6 +32,24 @@ def requests_retry_session( return session +def upload_cloudwatch(df_current, timestamp): + ondemand_count = len(df_current.drop(columns=['Spot Price', 'Savings']).dropna()) + spot_count = len(df_current.drop(columns=['OnDemand Price', 'Savings']).dropna()) + + cw_client = boto3.client('logs') + + log_event = { + 'timestamp': int(timestamp.timestamp()), + 'message': f'GCPONDEMAND: {ondemand_count} GCPSPOT: {spot_count}' + } + + cw_client.put_log_events( + logGroupName=GCP_CONST.LOG_GROUP_NAME, + logStreamName=GCP_CONST.LOG_STREAM_NAME, + logEvents=[log_event] + ) + + def gcp_collect(timestamp): # load pricelist response = requests_retry_session().get(GCP_CONST.API_LINK) @@ -78,6 +96,9 @@ def gcp_collect(timestamp): slack_msg_sender.send_slack_message(e) print(e) + # upload to cloudwatch data count + upload_cloudwatch(df_current, timestamp) + # # get previous latest_data from s3 object = s3.Object(STORAGE_CONST.BUCKET_NAME, GCP_CONST.S3_LATEST_DATA_SAVE_PATH) response = object.get() diff --git a/const_config.py b/const_config.py index 490457b..2a73f10 100644 --- a/const_config.py +++ b/const_config.py @@ -126,4 +126,11 @@ def S3_LATEST_DATA_SAVE_PATH(): @constant def LOCAL_PATH(): return "/tmp" - + + @constant + def LOG_GROUP_NAME(): + return "Collection-Data-Count" + + @constant + def LOG_STREAM_NAME(): + return "GCP-Count" From c4cc06e5569b86005bd7e45689c6b5055fa6cc2f Mon Sep 17 00:00:00 2001 From: HongSena Date: Tue, 7 Nov 2023 03:54:05 +0900 Subject: [PATCH 2/2] update const_config update log group name --- const_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/const_config.py b/const_config.py index 2a73f10..9ac6204 100644 --- a/const_config.py +++ b/const_config.py @@ -128,7 +128,7 @@ def LOCAL_PATH(): return "/tmp" @constant - def LOG_GROUP_NAME(): + def SPOT_DATA_COLLECTION_LOG_GROUP_NAME(): return "Collection-Data-Count" @constant