Answers: <p>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.</p>
  <p>Here's an example of how you can set up a log-based metric for this purpose:</p>
  <ol>
  <li><p>Go to the Cloud Logging page in the Google Cloud Console.</p>
  </li>
  <li><p>Select the log group for your GKE-hosted API.</p>
  </li>
  <li><p>Click the &quot;Create Metric&quot; button.</p>
  </li>
  <li><p>In the &quot;Create Metric&quot; dialog, specify a name for your metric (e.g.
  &quot;API Availability&quot;) and a description (optional).</p>
  </li>
  <li><p>In the &quot;Filter&quot; 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 &quot;status_code&quot; field
  in your logs:</p>
  <p>resource.type=&quot;gke_container&quot; AND
  jsonPayload.status_code</p>
  </li>
  <li><p>In the &quot;Aggregation&quot; field, specify how you want to aggregate the
  extracted values. You can use the &quot;COUNT&quot; aggregation to count the
  number of occurrences of each status code.</p>
  </li>
  <li><p>Click the &quot;Create&quot; button to create the metric.</p>
  </li>
  </ol>
  <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-12-22T23:46:56.080Z
Answers: 2022-12-22T23:46:56.080Z
Answers: unixoid
Body: <p>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)</p>
  
Tags: <kubernetes><gcp><sre>
Title: GCP log based availability metric
UserDisplayName: grzegorz_p
ThreadText: # GCP log based availability metric
  
  (Asked by: username_0 on 2022-12-22)
  
  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)
  
  **Tags:** kubernetes, gcp, sre
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-12-22. Score: 2 | Accepted 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:
  
  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.
  
  
  ---
  
  