we have a GKE hosted API served by Go/Gin service. It logs every request with some structured metadata (including status_code). We'd like to create an availability SLI (which is defined as a proportion of good events / all events). So, we'd have to go through the logs and categorize the good/bad event by status code. Technically, my first bet would be to define log-based metric for that, however I am not sure whether such logic is achieavable with log based metric syntax alone. For some services ie. App Engine app or services hosted with Istio availability metric comes out of the box, but I have not found any predefined metric that could be used in this case (we do not have any api gateway in front of it)

1 Answer 1


You can use a metric filter to extract the status codes from your logs and use those values to calculate the proportion of good events to all events.

Here's an example of how you can set up a log-based metric for this purpose:

  1. Go to the Cloud Logging page in the Google Cloud Console.

  2. Select the log group for your GKE-hosted API.

  3. Click the "Create Metric" button.

  4. In the "Create Metric" dialog, specify a name for your metric (e.g. "API Availability") and a description (optional).

  5. In the "Filter" field, enter a filter expression to extract the status codes from your logs. For example, you can use the following expression to extract the status codes from the "status_code" field in your logs:

    resource.type="gke_container" AND jsonPayload.status_code

  6. In the "Aggregation" field, specify how you want to aggregate the extracted values. You can use the "COUNT" aggregation to count the number of occurrences of each status code.

  7. Click the "Create" button to create the metric.

Once the metric is created, you can use the Cloud Monitoring UI or API to view the metric and track the availability of your API over time. You can also set up alerts to be notified when the availability falls below a certain threshold.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.