How-To: Google Monitoring API Helps Companies Effectively Keep Track

The Google Cloud Monitoring API can help organizations better manage the cloud services they are increasingly leveraging.

Monitoring computing resources in your cloud applications is not an easy task. Many times developers end up writing their own custom services to track various metrics that can help them understand the current health of their applications. Indeed, while monitoring is often associated with the ability to alert support staff in case certain metrics fall outside of normal operating range, many organizations are interested in monitoring to, for example, better understand monthly bills from cloud providers.

Google Cloud Platform has gained some serious traction this year. The platform comprises several different services, including Compute Engine, Google Cloud Storage, App Engine, Cloud SQL and Big Query.

Google understands that monitoring is key to pushing its platform to a wider adoption. Toward this goal, Google has recently acquired Stackdriver, which provides monitoring services for various cloud providers, along with powerful data visualization and analytics. While Stackdriver has yet to emerge into general availability on the Google Cloud Platform, you can start monitoring your computing resources via the recently released Cloud Monitoring API. The Cloud Monitoring API allows you to read monitoring data such as response times, uptime, disk usage, MySQL queries and more for your Google Cloud Platform projects.

What does the Cloud Monitoring API do?

Using the Cloud Monitoring API, metrics are measured every minute and stored as data points over time. Metrics are currently available for three platform services:

  • Google Compute Engine  (13 metrics)
  • Google Cloud SQL (12 metrics)
  • Google Cloud Pub/Sub ( 14 metrics)

I expect that the number of services and metrics will grow in the coming months, as initial usage and feedback are used to do a better rollout moving forward.
You can get a full list of the metrics here. Key metrics include:

  • CPU usage time
  • Delta count of bytes read from, written to disk
  • VM running time
  • Delta count of incoming bytes, packets dropped by the firewall

Business Use Cases

The Cloud Monitoring API metrics will enable companies to address business concerns such as:

  • Building your own dashboard to track metrics at a level that is important to you
  • Building custom alert mechanisms that can easily detect if a certain metric—say, latency--is not within permitted thresholds and sending emails or instant messages to notify support staff of potential problems
  • Recording out-of-threshold metrics data and using that data to better understand and tally your monthly bill
  • If you provide your own monitoring services, doing a clean integration with the Google Cloud Platform for your customers

This article will cover how to get started with the Google Cloud Monitoring API, from enabling it within your projects and integrating with the API, to developing sample metrics for a running instance in Google Compute Engine.

Setting up Cloud Monitoring API

To enable the Cloud Monitoring API  for a sample Cloud Platform project, you must have signed up for the Google Cloud Platform services.

To exercise the API, I had to start up a VM in Google Compute Engine and perform some operations so that some real data would be provided by the API. Launching and running a Compute Engine VM instance does incur costs, as per the pricing.

Following are the steps you need to take to set up Cloud Monitoring API for your project:

1. Create Project

I set up a Google Cloud Platform project by visiting the Cloud Console and clicking on Create Project. I enabled billing for this project to account for spinning up a Compute Engine VM. I named the test project "cloudmonitor123," the same name as the Project Id.:

2. Enable the Cloud Monitoring API

Click on your project (such as cloudmonitor123) in the Cloud Console, and then go to APIs in the APIs and AUTH section in the left menu.

Click on Enable an API. Select Google Cloud Monitoring API from the list and toggle the switch to ON as shown below:

3. Authenticate Application

You have three options for authentication depending on the flow that you want. At its core, OAuth 2.0 is used by the API to authenticate requests made for any particular projects metrics data.

A quick way to integrate the API would be to integrate the Google JavaScript client library in your code. For the authentication process, you will need to ensure that the user who is authenticating is present in the Cloud Project’s team list, as shown below for the project. Go to the Permissions option in the left menu and add the account as a member. In the screen below you can see that, since I am the creator of the project, I have been added to the team as the owner. But you can add other accounts that are permitted to access the application.

Alternatively, you can integrate OAuth 2.0 flow in your server-side application or offline on behalf of the user.

To get a Client ID for web applications, click on Credentials in the left menu, as shown below:

Then, click on the Create Client ID button and provide details, as shown below:

You can choose what kind of client application you will be using the Google Cloud Monitoring API in. I chose the Web Application type. Depending on where you are going to host it, you should provide the correct JavaScript origins and callback so that, on successful authentication and authorization from the user, you can receive a callback and then move forward with the rest of your application functionality.

After successful creation, note your Client ID, as given below:

Finally, you can use a Service Account flow to access the API from your server application. This is best when you want to avoid going through the User Intervention completely. In the Permissions option for the project, you will see various service accounts that you can use.

Sample code for Cloud Metrics

Here is an example of how you can integrate the Cloud Monitoring API within your application. The application is a single web page in which we shall use Google JavaScript API to load the necessary APIs (OAuth and Cloud Monitoring API), and then access one of the metrics to retrieve the JSON-formatted data.
The source code for the file is shown below: <!DOCTYPE html>
    <title>Cloud Monitoring API</title>
    <div id="login">
       <input id="loginButton" type="button" value="Login"/>
    <div id="metrics">
       <input id="metricsButton" type="button" value="Get Metrics"/>
    <div id="response-payload"></div>
    <script type="text/javascript">
        var scopes = '';
        var client_id = 'YOUR_WEBCLIENT_ID';
        function handleAuth(authResult) {
          if (authResult && authResult['access_token']) {
                  // User granted authorization
  <code>         // Set the token for later API calls
                  // Next step, load API and query API
                 document.getElementById('loginButton').style.visibility = 'hidden';
                  document.getElementById('login').innerText = 'Welcome to Cloud Monitoring API Metrics';
                } else {
                       document.getElementById('loginButton').style.visibility = '';
        //This method performs authorization
        function signin(mode, callback) {
                gapi.auth.authorize({response_type : 'token',client_id: client_id,scope: scopes, immediate: mode},callback);
        //This function is invoked when the Google JavaScript library is loaded
        function init() {
              var apisToLoad;
              //This callback function will do an automatic signin, in case they
              var callback = function() {
                 console.log("inside callback");
                 if (--apisToLoad == 0) {
                      //If APIs are loaded, invoke the signin method
                   signin(true, handleAuth);
              //Load the Cloud Monitoring and OAuth2 APIs
              apisToLoad = 2;
              gapi.client.load('cloudmonitoring', 'v2beta1', callback);
        document.getElementById('loginButton').onclick = function() {
        document.getElementById('metricsButton').onclick = function() {
        //Function used to generate Date Time in RFC 3339 format
        function ISODateString(d){
               function pad(n){return n<10 ? '0'+n : n}
               return d.getUTCFullYear()+'-'
                    + pad(d.getUTCMonth()+1)+'-'
                    + pad(d.getUTCDate())+'T'
                    + pad(d.getUTCHours())+':'
                    + pad(d.getUTCMinutes())+':'
                    + pad(d.getUTCSeconds())+'Z'}
              function getTimeSeriesDataPoints() {
                 var youngest = ISODateString(new Date());
                       // Timeseries.list API method allows us to get a series of data points for a particular metric.
                       // You need to provide the project name, the metric to track and the youngest parameter(End of time interval)
                       var request = gapi.client.cloudmonitoring.timeseries.list({
                         'project' : 'cloudmonitor123',
                         'metric' : '',
                         'youngest': youngest
                       // Execute the request and provide a callback function
                       request.execute(function(resp) {
                         // Insert the response into a div for viewing.
                         document.getElementById('response-payload').innerText =
       <!-- Load Google JavaScript Client API -->
    <script src=""></script>
Let us discuss some points for the code shown above:

  • The flow starts when you load the Google JavaScript Client API, which invokes the init method.
  • The init method loads two APIs that are required--OAuth2 and Cloud Monitoring API. Once the loading is done, it will go forward with authentication via the signin method.
  • The Signin method invokes the gapi.auth.authorize method and passes in the OAuth 2.0 scope, WEB_CLIENT_ID and the callback for the Authentication response. If the authentication response is OK, set the token in the gapi.auth object.
  • The Get Metrics button will invoke the getTimeSeriesDataPoints() function. This method will invoke the Cloud Monitoring API to get the metrics (JSON response) for the Compute Engine instance uptime. For this how-to I simply displayed the JSON response, but you can take this information and provide better visualization via a library.


The documentation available for Google Cloud Monitoring API is comprehensive. Some of the useful resources are listed below:


Google Cloud Monitoring API is a good start to monitoring your Google Cloud Projects via various metrics. The API currently is limited to various metrics under three services--Compute, Databases and Pub/Sub--but I expect it will expand out to more services in the near future.
  Google has recently acquired Stackdriver, which is likely to be integrated deeply into each Cloud Platform project. It's possible that Stackdriver will meet the needs of a cross section of users when it comes to monitoring, but for those planning on working with the data and building their own visualization/alert mechanisms--including Monitoring Service Providers--Cloud Monitoring API is an effective and relatively easy-to-use goal.

Romin Irani Romin loves learning about new technologies and teaching it to others. His passion is to help developers succeed.