Linux Labs

SRE practices: SLOs, incidents, redundancy / Stage 1 of 8

SLI: the share of good requests

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 6 · on a machine · 7 min

A time window

The log keeps three hours, and the complaints are about the last one. An SLI is always counted over a time window.

The command above printed about 97 %: three hours of the shop's work went into the count, and two calm hours diluted the last one. That is why an SLI is named together with its window: "availability over the last hour", "over a day", "over 30 days". The same requests give different SLIs in different windows.

To select a window, you need each request's time. The shop's log is written in the sre format — the log_format directive sets it:

bash
cat /etc/nginx/conf.d/lab-sre-log.conf

It is the familiar combined format plus two fields at the end. $request_time is how many seconds nginx spent on the request. $msec is the moment of the request in unix time: seconds since 1 January 1970 UTC, with milliseconds after the dot. In awk the line's last field is $NF, the one before it is $(NF-1).

Unix time is a single number, so a plain comparison sets the window: a line belongs to the last hour if its $NF is not less than "now minus 3600 seconds". date prints that number: date +%s is the current unix time, date -d '1 hour ago' +%s is an hour ago. The -v option passes the value into the awk program:

bash
from=$(date -d '1 hour ago' +%s); log=/var/log/nginx/access.log
sudo awk -v from="$from" '$NF >= from' "$log" | wc -l
Exercise · do it

Count the availability for an hour

Count the shop's availability for the last hour — the share of good answers in percent — and put one number in ~/sli/availability.txt, for example 99.12.

Sign in and start The machine is available after signing in

Goals · 0 of 1

  • Put the availability for the last hour in ~/sli/availability.txt not done yet

Step goals

0 of 1
  • Put the availability for the last hour in ~/sli/availability.txt — not done yet
Ubuntu 24.04

Step goals

0 of 1
  • Put the availability for the last hour in ~/sli/availability.txt — not done yet
Loading…