Because of Cloud Composer's microservice architecture, the end-to-end liveness monitoring of your Airflow setup involves multiple metrics. This tutorial shows one approach to using Google Cloud's operations suite's logs based metrics to set up monitoring and alerting based on a customized metric.
Objectives
- Deploy a simple DAG that runs every few minutes in the Cloud Composer environment
- Leverage the logs that Google Cloud's operations suite produces for Cloud Composer by using the logs-based metrics to create a customized metric
- Set up monitoring and alerting based on the customized metric
Costs
In this document, you use the following billable components of Google Cloud:
- Cloud Composer
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
- Create a Cloud Composer environment.
- Ensure appropriate permissions. The following permissions are required to access the logs and metrics for your Cloud Composer environment:
- Read-only logging and monitoring:
logging.viewer
andmonitoring.viewer
- Read-only logging including private logs:
logging.privateLogViewer
- Read-write monitoring:
monitoring.editor
For more information, see Access control.
- Read-only logging and monitoring:
Adding the DAG to your environment
Upload the following DAG to the Cloud Storage bucket for the environment:
import airflow from airflow import DAG from airflow.operators.bash_operator import BashOperator from datetime import timedelta default_args = { 'retries': 1, 'retry_delay': timedelta(minutes=5), 'start_date': airflow.utils.dates.days_ago(0) } dag = DAG( 'liveness', default_args=default_args, description='liveness monitoring dag', schedule_interval=timedelta(minutes=1)) t1 = BashOperator( task_id='echo', bash_command='echo test', dag=dag, depends_on_past=False)
Wait until Airflow schedules the DAG. You can check the status in the Airflow web interface.
Creating the logs-based counter metric in Google Cloud's operations suite
Go to the Logs Explorer.
Make sure the correct project is selected at the top of the page, or use the drop-down menu at the top of the page to select a project.
In the resource field, select Cloud Composer Environment and then select the location and environment name.
In the log type field, select airflow-worker.
In the filter bar, create a filter to show only logs that the liveness DAG emits by entering the following label: label:workflow:liveness.
At the top of the page, click Create Metric. The Metric Editor displays on the right side of the page, and the viewer panel showing your logs displays on the left side.
In the Metric Editor panel, set the following fields:
- Name: Choose a name that is unique among logs-based metrics in your project, for example liveness-logs-cnt.
- Description: Describe the metric.
- Type: Counter.
Click Create Metric. The Logs-based metrics page displays.
Creating the alerting policy
In the Logs-based metrics page, check the metric you want to create a policy for, and in the menu at the right side of the metric's listing, select Create alert from metric. The Create New Alerting Policy page displays.
On the Create New Alerting Policy page, in the Conditions section, click Edit. A pre-populated Create METRIC window displays.
(Optional) Enter a name for the condition.
In the Target section, for Aggregator, choose count.
In the Configuration section, for Threshold, set the value, as needed.
Click Save. The Create new alerting policy panel displays with your completed condition.
Fill in the Notifications, Documentation, and Name this policy sections of the alerting policy.
Click Save.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
To learn about using a custom dashboard to view metrics for your Cloud Composer environment, see Monitoring environments and workflows in Google Cloud's operations suite.