Simple monitoring with Stackdriver for GAE Cron Jobs
If you write Cron Jobs in GAE you may want to add monitoring for them outside of the jobs themselves. Stackdriver offers various monitoring capabilities as part of the Free Basic Tier that takes seconds to setup.
This short post details how to add monitoring for Cron Jobs in GAE with Stackdriver for free.
The first step is to enable Stackdriver if you haven’t already for your Google Compute Platform (GCP) project. Head over to https://app.google.stackdriver.com/ to start. The wizard will walk you through selecting your project(s) that you would like to enable.
Two quick notes to consider:
- If you have multiple projects I would recommend that you create a new GCP project that is named in a sensible way to be able to group your projects in Stackdriver. Stackdriver doesn’t allow for arbitrary parent naming and re-uses the project you start with (c’mon Stackdriver!). This is a little annoying but it takes a few clicks or one gcloud command to create a new GCP project.
- Stackdriver automatically signs you up for the Premium Tier for 30 days and then downgrades you automatically to the Basic Tier after 30 days. You don’t have to do anything here it is just a FYI. I haven’t seen any way to disable this uselessness.
Complete the walkthrough, skipping all the connecting to AWS, daily/weekly reports, et cetera. You can change all of this later if you would like and doesn’t matter for adding monitoring.
Create Alert Policy
On the left menu, select the Alerting tab and Create a Policy.
Start at the top by select Add Condition. The easiest for the type of Cron Jobs that I write is to select Metric Absence. All of the jobs that I typically write will return an HTTP 200 response if all went as expected and this seems to be fairly common.
Under Target > Resource Type, select App Engine. You can choose to apply this alert policy to a Single Project or to All Projects that are in this Stackdriver account. Personally, I have chosen each Project individually for more fine-grained control.
The absence of a 2XX response in my case is considered an alert-able condition. Selection of the absence time period also needs to be configured. In my case, the job runs hourly for this particular use case and at 2 hours I would like to be alerted (hourly + runtime).
Multiple Cron Jobs: At this time it appears you cannot differentiate if you have multiple cron jobs scheduled under a single project. Maybe someone from Google Cloud will read this and file a feature request. Pretty please. :)
You can add multiple conditions for multiple jobs/projects to a single Policy if the Notification will be the same (to save you time in configuration). After adding each condition you will need to choose a Policy Trigger, set a Notification method (there are a lot of options!), add Documentation text and Save the Policy.
Once you save each Policy you will will be able to view (and edit) each on the Alerting > Policies page.
That’s it! Quick and easy (and free) monitoring with Stackdriver for Google App Engine Cron Jobs on Google Compute Platform. Say that 10 times fast!