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
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:
Go to the Cloud Logging page in the Google Cloud Console.
Select the log group for your GKE-hosted API.
Click the "Create Metric" button.
In the "Create Metric" dialog, specify a name for your metric (e.g. "API Availability") and a description (optional).
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
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.
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.