LogQL
Video Lecture
Description
Now that we have a loki data source we can query it with the LogQL query language.
In this video, we will try out many LogQL queries in the systemdjournal
stream selector that we just set up.
There are two types of LogQL queries:
 Log queries returning the contents of log lines as streams.
 Metric queries that convert logs into value matrixes.
A LogQL query consists of,
 The log stream selector
 Filter expression
We can use operations on both the log stream selectors and filter expressions to refine them.
Log Stream Selectors
Operators
 = : equals
 != : not equals
 =~ : regex matches
 !~ : regex does not match
Examples
Return all log lines for the job systemdjournal
1 

Return all log lines for the unit ssh.service
1 

Return all log lines for the job systemdjournal
and the unit cron.service
1 

Show all log lines for 2 jobs with different names
1 

Filter Expressions
Operators
Used for testing text within log line streams.
 = : equals
 != : not equals
 ~ : regex matches
 !~ : regex does not match
Examples
Return lines including the text "error"
1 

Return lines not including the text "error"
1 

Return lines including the text "error" or "info" using regex
1 

Return lines not including the text "error" or "info" using regex
1 

Return lines including the text "error" but not including "info"
1 

Return lines including the text "Invalid user" and including ("paulo" or "redis") using regex
1 

Return lines including the text "status 403" or "status 503" using regex
1 

Range and Instance Vectors
The data so far is returned as streams of log lines. We can graph these in visualisations if we convert them to vectors. We can aggregate the lines into numeric values, such as counts, which then become known as instance or range vectors.
 count_over_time : Shows the total count of log lines for time range
 rate : Similar as count_over_time but converted to number of entries per second
 bytes_over_time : Number of bytes in each log stream in the range
 bytes_rate : Similar to bytes_over_time but converted to number of bytes per second
Examples
The count of jobs at 1 minutes time intervals
1 

The rate of logs per minute. Rate is similar to count_over_time
but shows the entries per second.
1 

Info
rate = count_over_time / 60 / range(m)
eg,
1 

The count of errors
at 1h time intervals
1 

Aggregate Functions
An aggregate function converts a range vector result into a single instance vector.
 sum : Calculate the total of all instance vectors in the range at time
 min : Show the minimum value from all instance vectors in the range at time
 max : Show the maximum value from all instance vectors in the range at time
 avg : Calculate the average of the values from all instance vectors in the range at time
 stddev : Calculate the standard deviation of the values from all instance vectors in the range at time
 stdvar : Calculate the standard variance of the values from all instance vectors in the range at time
 count : Count the number of elements all all instance vectors in the range at time
 bottomk : Select lowest k values in all the instance vectors in the range at time
 topk : Select highest k values in all the instance vectors in the range at time
Note
bottomk and topk don't produce an instance vector, but a range vector of containing the k number of instance vectors in the time range.
Examples
Calculate the total of all instance vectors in the range at time
1 

Show the minimum value from all instance vectors in the range at time
1 

Show the maximum value from all instance vectors in the range at time
1 

Show only the top 2 values from all instance vectors in the range at time
1 

Aggregate Group
Convert an instance vector into a range vector organised by unit
Examples
Group a single log stream by unit
1 

Group multiple log streams by job
1 

Group multiple log streams and specific unit by job
1 

Group multiple log streams by job and unit
1 

Comparison Operators
Comparison Operators. Used for testing numeric values present in scalars and vectors.
 == (equality)
 != (inequality)
 > (greater than)
 >= (greater than or equal to)
 < (less than)
 <= (less than or equal to)
Examples
Returns values greater than 4
1 

Returns values less than or equal to 1
1 

Logical Operators
These can be applied to both range and instance vectors
 and : Bother sides must be true
 or : Either side must be true
 unless : Return values unless value
Examples
Returns values greater than 4 or values less then or equal to 1
1 

Return values between 2 and 5
1 

Arithmetic Operators
 + : Add
  : Subtract
 * : Multiply
 / : Divide
 % : Modulus
 ^ : Power/Exponentiation
Examples
1 2 

Operator order
Many Operators can be used at a time. The order follows the PEMDAS construct. PEMDAS is an acronym for the words parenthesis, exponents, multiplication, division, addition, subtraction.
Examples
A nonsensical example
1 2 3 

Proving that count_over_time / 60 / range(m) = rate
1 2 3 
