Search code examples
google-cloud-platformterraformgoogle-cloud-monitoringmonitoring-query-language

GCP MQL: Calculate ratio from 3 metrics


I have 3 metrics created in my GCP 1) success request count 2) redirected request count and 3) failed request count. All the 3 metrics are created from log-based metric on Kubernetes log.

Here's basically a Terraform resource definition of one metric "Success requests.

resource "google_logging_metric" "success_requests" {
  filter      = <<-EOT
    resource.type="k8s_container"
    jsonPayload.message:"Request success"
  EOT
  name        = "success_requests"
  project     = var.gcp_project_id

  metric_descriptor {
    metric_kind = "DELTA"
    unit        = "1"
    value_type  = "INT64"
  }
}

The other 2 basically just filter jsonPayload.message with Request redirected and Request failed.

I want to create a dashboard that shows the success ratio with this formula

success_rate = success_requests / (success_requests + redirected_requests + failed_requests)

I am successful creating a dashboard that shows the three metrics using below MQL.

k8s_container | { 
    t_0:
        metric logging.googleapis.com/user/success_requests;
    t_1: 
        metric logging.googleapis.com/user/redirected_requests;
    t_2: 
        metric logging.googleapis.com/user/failed_requests
} | union

But how do I create a dashboard that display the success rate with formula as mentioned before?


Solution

  • To perform some arithmetic operations using metrics, you need to work with the time series using Arithmetic Computation

    To sum two time series, configure your query to fetch two tables of time series, join those results, and then call the add function. The following example illustrates a query that computes the sum of the number of bytes read from, and written to, Compute Engine instances:

      fetch gce_instance
      | { metric 'compute.googleapis.com/instance/disk/read_bytes_count'
        ; metric 'compute.googleapis.com/instance/disk/write_bytes_count' }
      | outer_join 0
      | add
    

    After you summarize the three time series you can use div function.

    The following query uses time_shift, join, and div to compute the ratio of the mean utilization in each zone between now and one week ago.

    fetch gce_instance::compute.googleapis.com/instance/cpu/utilization
    | group_by [zone], mean(val())
    | {
        ident
      ;
        time_shift 1w
      }
    | join | div
    

    You can review more examples of queries using MQL in this Link.

    You can review more of Functions implementing the arithmetic operators in MQL in this link.