Lesson 4 of 4 Analyzing Instrumentation

Interrogating Logs with Basic KQL Queries

This lesson teaches the basic KQL operators through examples.

Learning outcomes

After completing this lesson, you will be able to:

  • Read a KQL pipeline.
  • Use filtering and aggregation operators.
  • Build a time series with bin().
  • Complete a query in an exam snippet.

How a KQL query works

A KQL query starts with a table, and each pipe character passes the results to the next operator. Reading a query from top to bottom therefore describes a sequence of steps. Time filters should come early, as this reduces the data that later steps must process.

AppRequests
| where TimeGenerated > ago(1h)
| where Success == false
| summarize Failures = count() by Name
| order by Failures desc
| take 10

This query starts with the requests table, keeps the last hour, keeps failed requests, counts them per operation name, sorts them in descending order and returns the top 10. Note that KQL is case-sensitive for table names, column names and operators.

Put in order

Building a KQL query

Exam skills 5.2.4

Arrange the operators to list the 10 operations with the most failures in the last hour.

Log in to try this activity Interactive activities are free with an account.

Essential operators

The following table lists the operators that the exam expects you to know.

OperatorPurposeExample
whereFilter rowswhere ResultCode == "500"
projectChoose or rename columnsproject TimeGenerated, Name, DurationMs
extendAdd a calculated columnextend Seconds = DurationMs / 1000
summarizeAggregatesummarize count() by Name
order bySortorder by DurationMs desc
takeReturn any N rowstake 20
topReturn the first N by a columntop 5 by DurationMs desc
distinctReturn unique valuesdistinct ClientCity
joinCombine two tablesjoin kind=inner (AppExceptions) on OperationId
unionCombine rows from tablesunion AppRequests, AppDependencies
renderDraw a chartrender timechart

Filtering

== is a case-sensitive comparison, and =~ ignores case. has matches a whole term and is fast, contains matches any part of a string, and startswith matches the start. in tests a list of values. ago(1h) gives a time relative to now, with units such as d, h, m and s.

Aggregating

summarize is the most important operator. Functions include count(), sum(), avg(), max(), min(), dcount() for a distinct count, countif(), and percentile(). by sets the grouping, and bin() groups a time column into fixed periods.

Match the pairs

KQL operators

Exam skills 5.2.4

Match each KQL operator with its purpose.

Log in to try this activity Interactive activities are free with an account.

Common query patterns

A failure rate over time

AppRequests
| where TimeGenerated > ago(24h)
| summarize Total = count(), Failed = countif(Success == false) by bin(TimeGenerated, 15m)
| extend FailureRate = 100.0 * Failed / Total
| project TimeGenerated, FailureRate
| render timechart

Latency percentiles per operation

AppRequests
| where TimeGenerated > ago(1h)
| summarize P50 = percentile(DurationMs, 50), P95 = percentile(DurationMs, 95) by Name
| top 10 by P95 desc

Failed requests with their exceptions

AppRequests
| where TimeGenerated > ago(1h) and Success == false
| join kind=inner (AppExceptions | project OperationId, ExceptionType) on OperationId
| summarize count() by Name, ExceptionType

High CPU on virtual machines

Perf
| where TimeGenerated > ago(30m)
| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName == "_Total"
| summarize AvgCpu = avg(CounterValue) by Computer, bin(TimeGenerated, 5m)
| where AvgCpu > 80

With VM insights, the InsightsMetrics table holds similar data. Application Insights data in a workspace uses tables such as AppRequests, AppDependencies, AppExceptions, AppTraces and AppPageViews, whereas the Logs view inside a classic Application Insights experience may use requests, dependencies and exceptions, with timestamp in place of TimeGenerated. Recognizing both forms helps with exam snippets. Azure Monitor Logs also has the AzureActivity table for the activity log, and the AzureDiagnostics table for many resource logs.

The following diagram annotates each line of a KQL query that counts failed requests per hour.

The KQL query for failed requests per hour over the last day, line by line. where filters the time and the result, summarize with bin makes one row per hour, and render timechart draws the line. project would only pick columns, extend adds a column per row, and take returns arbitrary rows.
The scenario is illustrative.
Spot the bug

A failure-rate query that misleads

Exam skills 5.2.4

Find the lines that make this KQL query return the wrong result.

Log in to try this activity Interactive activities are free with an account.

From a query to an alert

A saved query can be turned into a log search alert, which runs it on a schedule, and fires according to the number of rows or a measured value. A query can also be pinned to a dashboard or used in a workbook. Efficient alert queries filter on time and on specific columns, as a broad query costs more and takes longer.

True or false

Queries as alerts

Exam skills 5.2.4 · 5.1.1

Check how a saved KQL query becomes an alert or a visual.

Log in to try this activity Interactive activities are free with an account.
Worked example

Completing a query

You need a chart of the number of failed requests per hour for the last day. Which operators complete the query?

AppRequests
| where TimeGenerated > ago(1d) and Success == false
| summarize Failures = count() by bin(TimeGenerated, 1h)
| render timechart

where filters the time and result, summarize with bin creates one row per hour, and render timechart draws a line. project would only pick columns, without aggregating. extend would add a column for each row, and would not reduce them. take would return arbitrary rows.

Check yourself

What is the difference between project and extend?

Show answer

project chooses or renames columns, and drops the rest. extend adds a calculated column, and keeps the others.

Exam Tips

Remember for the exam

  • A query starts from a table, and each pipe passes results to the next operator.
  • summarize aggregates; bin() groups by time.
  • project chooses columns; extend adds them; take returns any N rows.
  • Filter by time first for efficient queries.
  • Workspace tables use AppRequests and TimeGenerated; classic use requests and timestamp.