Google Cloud Monitoring Operators¶
Note
The Stackdriver* classes in the stackdriver modules are deprecated aliases for the
CloudMonitoring* classes documented below, and will be removed after March 31, 2027.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Select or create a Cloud Platform project using the Cloud Console.
Enable billing for your project, as described in the Google Cloud documentation.
Enable the API, as described in the Cloud Console documentation.
Install API libraries via pip.
pip install 'apache-airflow[google]'Detailed information is available for Installation.
CloudMonitoringListAlertPoliciesOperator¶
Use the CloudMonitoringListAlertPoliciesOperator
to fetch all the Alert Policies identified by given filter.
Using the operator¶
You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.
list_alert_policies = CloudMonitoringListAlertPoliciesOperator(
task_id="list-alert-policies",
filter_=f'(displayName="{ALERT_1_NAME}" OR displayName="{ALERT_2_NAME}")',
)
CloudMonitoringEnableAlertPoliciesOperator¶
Use the CloudMonitoringEnableAlertPoliciesOperator
to enable Alert Policies identified by given filter.
Using the operator¶
You can use this operator with or without project id to enable alert policies. If project id is missing it will be retrieved from Google Cloud connection used.
enable_alert_policy = CloudMonitoringEnableAlertPoliciesOperator(
task_id="enable-alert-policies",
filter_=f'(displayName="{ALERT_1_NAME}" OR displayName="{ALERT_2_NAME}")',
)
CloudMonitoringDisableAlertPoliciesOperator¶
Use the CloudMonitoringDisableAlertPoliciesOperator
to disable Alert Policies identified by given filter.
Using the operator¶
You can use this operator with or without project id to disable alert policies. If project id is missing it will be retrieved from Google Cloud connection used.
disable_alert_policy = CloudMonitoringDisableAlertPoliciesOperator(
task_id="disable-alert-policies",
filter_=f'displayName="{ALERT_1_NAME}"',
)
CloudMonitoringUpsertAlertOperator¶
Use the CloudMonitoringUpsertAlertOperator
to upsert Alert Policies identified by given filter JSON string. If the alert with the given name already
exists, then the operator updates the existing policy otherwise creates a new one.
Using the operator¶
You can use this operator with or without project id to create or update alert policies. If project id is missing it will be retrieved from Google Cloud connection used.
create_alert_policy = CloudMonitoringUpsertAlertOperator(
task_id="create-alert-policies",
alerts=json.dumps({"policies": [TEST_ALERT_POLICY_1, TEST_ALERT_POLICY_2]}),
)
CloudMonitoringDeleteAlertOperator¶
Use the CloudMonitoringDeleteAlertOperator
to delete an Alert Policy identified by given name.
Using the operator¶
The name of the alert to be deleted should be given in the format projects/<PROJECT_NAME>/alertPolicies/<ALERT_NAME>
delete_alert_policy = CloudMonitoringDeleteAlertOperator(
task_id="delete-alert-policy",
name="{{ task_instance.xcom_pull('list-alert-policies')[0]['name'] }}",
)
CloudMonitoringListNotificationChannelsOperator¶
Use the CloudMonitoringListNotificationChannelsOperator
to fetch all the Notification Channels identified by given filter.
Using the operator¶
You can use this operator with or without project id to fetch all the notification channels. If project id is missing it will be retrieved from Google Cloud connection used.
list_notification_channel = CloudMonitoringListNotificationChannelsOperator(
task_id="list-notification-channel",
filter_=f'(displayName="{CHANNEL_1_NAME}" OR displayName="{CHANNEL_2_NAME}")',
)
CloudMonitoringEnableNotificationChannelsOperator¶
Use the CloudMonitoringEnableNotificationChannelsOperator
to enable Notification Channels identified by given filter.
Using the operator¶
You can use this operator with or without project id to enable notification channels. If project id is missing it will be retrieved from Google Cloud connection used.
enable_notification_channel = CloudMonitoringEnableNotificationChannelsOperator(
task_id="enable-notification-channel",
filter_=f'(displayName="{CHANNEL_1_NAME}" OR displayName="{CHANNEL_2_NAME}")',
)
CloudMonitoringDisableNotificationChannelsOperator¶
Use the CloudMonitoringDisableNotificationChannelsOperator
to disable Notification Channels identified by given filter.
Using the operator¶
You can use this operator with or without project id to disable notification channels. If project id is missing it will be retrieved from Google Cloud connection used.
disable_notification_channel = CloudMonitoringDisableNotificationChannelsOperator(
task_id="disable-notification-channel", filter_=f'displayName="{CHANNEL_1_NAME}"'
)
CloudMonitoringUpsertNotificationChannelOperator¶
Use the CloudMonitoringUpsertNotificationChannelOperator
to upsert Notification Channels identified by given channel JSON string. If the channel with the given name already
exists, then the operator updates the existing channel otherwise creates a new one.
Using the operator¶
You can use this operator with or without project id to create or update notification channels. If project id is missing it will be retrieved from Google Cloud connection used.
create_notification_channel = CloudMonitoringUpsertNotificationChannelOperator(
task_id="create-notification-channel",
channels=json.dumps({"channels": [TEST_NOTIFICATION_CHANNEL_1, TEST_NOTIFICATION_CHANNEL_2]}),
)
CloudMonitoringDeleteNotificationChannelOperator¶
Use the CloudMonitoringDeleteNotificationChannelOperator
to delete a Notification Channel identified by given name.
Using the operator¶
The name of the notification channel to be deleted should be given in the format projects/<PROJECT_NAME>/notificationChannels/<CHANNEL_NAME>
You can use this operator with or without project id to delete a notification channel. If project id is missing it will be retrieved from Google Cloud connection used.
delete_notification_channel = CloudMonitoringDeleteNotificationChannelOperator(
task_id="delete-notification-channel",
name="{{ task_instance.xcom_pull('list-notification-channel')[0]['name'] }}",
)