-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to skip toggling CloudWatch alarms (#3682)
Co-authored-by: Staci Mullins <[email protected]> Co-authored-by: Madison Swain-Bowden <[email protected]>
- Loading branch information
1 parent
b4b0cc9
commit 002066f
Showing
5 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
CloudwatchWrapper extracted partially from | ||
https://github.com/awsdocs/aws-doc-sdk-examples/blob/54c3b82d8f9a12a862f9fcec44909829bda849af/python/example_code/cloudwatch/cloudwatch_basics.py | ||
The CloudwatchWrapper requires the AWS_CLOUDWATCH_CONN_ID, or the `aws_default` | ||
connection, to be set in the Airflow Connections. | ||
Modifying alarms can be skipped by setting the `TOGGLE_CLOUDWATCH_ALARMS` to `False` | ||
in the Airflow Variables, which is particularly the desired behavior when running | ||
the Data Refresh DAGs locally or in a development environment. | ||
""" | ||
import logging | ||
|
||
from airflow.exceptions import AirflowSkipException | ||
from airflow.models import Variable | ||
from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook | ||
from botocore.exceptions import ClientError | ||
|
||
from common.constants import AWS_CLOUDWATCH_CONN_ID | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CloudWatchWrapper: | ||
"""Encapsulates Amazon CloudWatch functions""" | ||
|
||
def __init__(self, cloudwatch_resource): | ||
""":param cloudwatch_resource: A Boto3 CloudWatch resource.""" | ||
self.cloudwatch_resource = cloudwatch_resource | ||
|
||
def enable_alarm_actions(self, alarm_name, enable): | ||
""" | ||
Enable or disable actions on the specified alarm. Alarm actions can be | ||
used to send notifications or automate responses when an alarm enters a | ||
particular state. | ||
:param alarm_name: The name of the alarm. | ||
:param enable: When True, actions are enabled for the alarm. Otherwise, they | ||
disabled. | ||
""" | ||
try: | ||
alarm = self.cloudwatch_resource.Alarm(alarm_name) | ||
if enable: | ||
alarm.enable_actions() | ||
else: | ||
alarm.disable_actions() | ||
logger.info( | ||
"%s actions for alarm %s.", | ||
"Enabled" if enable else "Disabled", | ||
alarm_name, | ||
) | ||
except ClientError: | ||
logger.exception( | ||
"Couldn't %s actions alarm %s.", | ||
"enable" if enable else "disable", | ||
alarm_name, | ||
) | ||
raise | ||
|
||
|
||
def enable_or_disable_alarms(enable): | ||
toggle = Variable.get("TOGGLE_CLOUDWATCH_ALARMS", True, deserialize_json=True) | ||
if not toggle: | ||
raise AirflowSkipException("TOGGLE_CLOUDWATCH_ALARMS is set to False.") | ||
|
||
cloudwatch = AwsBaseHook( | ||
aws_conn_id=AWS_CLOUDWATCH_CONN_ID, | ||
resource_type="cloudwatch", | ||
) | ||
cw_wrapper = CloudWatchWrapper(cloudwatch.get_conn()) | ||
cw_wrapper.enable_alarm_actions("ES Production CPU utilization above 50%", enable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,9 @@ TEST_CONN_ID=postgres_openledger_testing | |
AIRFLOW_CONN_ELASTICSEARCH_HTTP_PRODUCTION=http://es:9200 | ||
AIRFLOW_CONN_ELASTICSEARCH_HTTP_STAGING=http://es:9200 | ||
|
||
# AWS CloudWatch connection. Change the following line to toggle alarms during a Data Refresh. | ||
# AIRFLOW_CONN_AWS_CLOUDWATCH=aws://<key>:<secret>@?region_name=us-east-1 | ||
|
||
# API DB connection. Change the following line in prod to use the appropriate DB | ||
AIRFLOW_CONN_POSTGRES_OPENLEDGER_API=postgres://deploy:deploy@db:5432/openledger | ||
|
||
|
@@ -92,6 +95,7 @@ [email protected] | |
# AWS/S3 configuration - does not need to be changed for development | ||
AWS_ACCESS_KEY=test_key | ||
AWS_SECRET_KEY=test_secret | ||
AWS_DEFAULT_REGION=us-east-1 | ||
# General bucket used for TSV->DB ingestion and logging | ||
OPENVERSE_BUCKET=openverse-storage | ||
# Seconds to wait before poking for availability of the data refresh pool when running a data_refresh | ||
|
@@ -117,3 +121,7 @@ SQLALCHEMY_SILENCE_UBER_WARNING=1 | |
|
||
AIRFLOW_VAR_AIRFLOW_RDS_ARN=unset | ||
AIRFLOW_VAR_AIRFLOW_RDS_SNAPSHOTS_TO_RETAIN=7 | ||
|
||
# Whether to toggle production CloudWatch alarms when running a data refresh DAG. | ||
# Used to prevent requiring AWS credentials when running locally. | ||
AIRFLOW_VAR_TOGGLE_CLOUDWATCH_ALARMS=false |