Using MQL from the Monitoring API

This page describes how to provide Monitoring Query Language (MQL) queries to the Cloud Monitoring API.

This page doesn't cover creating the MQL queries. For an introduction to creating queries with the Query Editor, see Using the Query Editor. The editor provides both error correction and autocompletion help.

Using MQL from the API

You can provide MQL queries at the following places in the Monitoring API:

Retrieving data with timeSeries.query

To retrieve time-series data from the API with a MQL query, use the timeSeries.query method.

The timeSeries.query method takes a minimal structure that looks like this in JSON):

{
  "query": string
}

For the value of the query field, specify a string in MQL:

{

  "query": "fetch gce_instance::compute.googleapis.com/instance/disk/read_bytes_count
            | within 5m"
}

To experiment with the API, you can use the API Explorer tool on the timeSeries.query reference page. For an introduction to API Explorer tool, see API Explorer.

Another way to try the API this is to put the query into a text file and then execute the query using curl. The following example passes the query in the file query.json to the timeSeries.query method:

curl -d @query.json -H "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" -X POST \
https://monitoring.googleapis.com/v3/projects/${PROJECT_ID}/timeSeries:query

For more information on using curl, see Invoking curl.

If successful, the query returns a table containing the time series requested. The table is divided into two components:

  • The timeSeriesDescriptor describes the label keys, label values, and data points in the table. It doesn't contain any data; it simply describes the data.

  • The timeSeriesData contains the data described in the time-series descriptor. This data is presented as an array of pairs.

    • The first item in the pair, labelValues, records a set of values for the labels listed in the time-series descriptor.
    • The second, pointData, is an embedded array of value/timestamp pairs, which represent the data collected with the specified set of label values.

The response, slightly reformatted, looks like this:

[{
  "timeSeriesTable": {
    "timeSeriesDescriptor": {
      "labelDescriptors": [
        { "key": "resource.project_id" },
        { "key": "resource.zone" },
        { "key": "resource.instance_id" },
        { "key": "metric.instance_name" }
      ],
      "valueDescriptors": [
        {
          "key": "value.utilization",
          "valueType": "DOUBLE",
          "metricKind": "GAUGE"
        }
      ],
      "pointDescriptors": [
        {
          "key": "value.utilization",
          "valueType": "DOUBLE",
          "metricKind": "GAUGE"
        }
      ]
    },
    "timeSeriesData": [
      {
        "labelValues": [
          { "stringValue": "632526624816" },
          { "stringValue": "us-central1-a" },
          { "stringValue": "5106847938297466291" },
          { "stringValue": "gke-kuber-cluster-default-pool-6fe301a0-n8r9" }
        ],
        "pointData": [
          {
            "values": [
              {
                "doubleValue": 0.063896992710942874
              }
            ],
            "timeInterval": {
              "startTime": "1969-12-31T23:59:59.999999Z",
              "endTime": "2020-03-02T20:17:00Z"
            }
          },
          { ... additional value/timestamp pairs ...}
        ]
      },
      { ... additional labelValue/pointData pairs ...},
    ]
  }

Invoking curl

Each curl invocation includes a set of arguments, followed by the URL of an API resource. The common arguments include a Google Cloud project ID and an authentication token. These values are represented here by the PROJECT_ID and TOKEN environment variables.

You might also have to specify other arguments, for example, to specify the type of the HTTP request (for example, -X DELETE). The default request is GET, so the examples don't specify it.

Each curl invocation has this general structure:

curl --http1.1 --header "Authorization: Bearer ${TOKEN}" <other_args> https://monitoring.googleapis.com/v3/projects/${PROJECT_ID}/<request>

To use curl, you must specify your project ID and an access token. To reduce typing and errors, you can put these into environment variables as pass them to curl that way.

To set these variables, do the following:

  1. Create an environment variable to hold the ID of your Cloud Monitoring Workspace. These steps call the variable PROJECT_ID:

    PROJECT_ID=a-sample-project
    
  2. Authenticate to the Cloud SDK:

    gcloud auth login
    
  3. Optional. To avoid having to specify your project ID with each gcloud command, set your project ID as the default by using Cloud SDK:

    gcloud config set project ${PROJECT_ID}
    
  4. Create an authorization token and capture it in an environment variable. These steps call the variable TOKEN:

    TOKEN=`gcloud auth print-access-token`
    

    You have to periodically refresh the access token. If commands that worked suddenly report that you are unauthenticated, reissue this command.

  5. To verify that you got an access token, echo the TOKEN variable:

    echo ${TOKEN}
    ya29.GluiBj8o....