| Azure-Insights :👈 | 👉:Azure-Log-Analytics |
Kusto Query Language (KQL) Overview |
Kusto Query Language (KQL)
Kusto Query Language (KQL) is a powerful, read-only query language used to explore, analyze, and visualize large volumes of structured, semi-structured, and unstructured data in Azure services such as Log Analytics, Application Insights, and Azure Data Explorer.
βοΈ Key Features
- Optimized for fast data exploration and ad-hoc queries.
- Supports filtering, aggregation, joins, and advanced data transformations.
- Integrates with dashboards, workbooks, and alerts for real-time monitoring.
- Provides time-series analysis and pattern detection.
π Common KQL Operations
- Filtering: `where` clause to narrow down data.
- Projection: `project` to select specific columns.
- Aggregation: `summarize` to group and calculate metrics.
- Joining Data: `join` to combine tables.
- Sorting: `order by` to arrange results.
- Time Series Analysis: `make-series` and `bin()` for temporal aggregation.
π§© Example Query
Logs | where TimeGenerated > ago(1h) | where SeverityLevel == "Error" | summarize ErrorCount = count() by Computer | order by ErrorCount desc
β Why Use KQL
- Quickly identify trends and anomalies in large datasets.
- Supports monitoring, diagnostics, and troubleshooting in Azure.
- Enables custom dashboards and alerting rules.
- Ideal for multi-tenant SaaS telemetry and modular infrastructure analysis.
π§ What is KQL?
Kusto Query Language (KQL) is a read-only, data exploration language used across Azure services like:
- Azure Monitor / Log Analytics
- Microsoft Sentinel
- Azure Data Explorer
- Microsoft Defender for Cloud
Itβs designed for fast, interactive analysis of large datasets β especially telemetry, logs, metrics, and time-series data β without modifying the underlying data.
π Core Concepts
| Concept | Description |
|---|---|
| Read-only | You can query and transform data, but not insert/update/delete. |
| Data-flow model | Queries are built as a sequence of operations connected by a pipe (|) β each step transforms the table before passing it on. |
| Schema hierarchy | Data is organized into databases β tables β columns (similar to SQL). |
| Case-sensitive | Table names, column names, and operators are case-sensitive. |
| Optimized for telemetry | Rich support for text search, parsing, time-series, aggregation, and anomaly detection. |
π Basic Syntax Structure
TableName | where Timestamp > ago(1h) | project ColumnA, ColumnB | summarize Count = count() by ColumnA | order by Count desc
How it works:
- Start with a table (TableName)
- Filter rows (
where) - Select columns (
project) - Aggregate data (
summarize) - Sort results (
order by)
π Common Operators
| Operator | Purpose | Example |
|---|---|---|
| where | Filter rows | where Level == "Error" |
| project | Select columns | project TimeGenerated, Message |
| summarize | Aggregate | summarize avg(DurationMs) by UserId |
| extend | Add calculated columns | extend LoadTimeSec = DurationMs / 1000 |
| join | Combine tables | join kind=inner |
| order by | Sort results | order by Count desc |
| parse | Extract values from text | parse Message with "User:" UserId " logged in" |
β‘ Example in Azure Monitor
Scenario: Find the top 5 IP addresses with the most failed logins in the last 24 hours.
SigninLogs | where ResultType != 0 | where TimeGenerated > ago(24h) | summarize Failures = count() by IPAddress | top 5 by Failures desc
π― Best Practices for Writing KQL
- Filter early β Use
whereas soon as possible to reduce dataset size. - Project only needed columns β Avoid pulling unnecessary data.
- Use
summarizewisely β Aggregate before joins to reduce cost. - Parameterize queries in Workbooks for reusability.
- Test in small time windows before running large queries.
π Why Youβll Love It for Your Work
- Tag data with
TenantIdto filter dashboards per tenant. - Build modular KQL snippets for reuse in Workbooks, Alerts, and APIs.
- Integrate KQL queries into Angular dashboards via the Azure Monitor REST API for live telemetry.
KQL Cheat Sheet - Azure Monitor, Sentinel, Application Insights
π KQL Cheat Sheet
20+ ready-to-use query patterns for Azure Monitor, Microsoft Sentinel, and Application Insights.
1οΈβ£ Azure Monitor & Log Analytics Queries
-
Heartbeat | summarize Count = count() by ComputerList all monitored computers with heartbeat count.
-
Perf | where ObjectName == "Processor" and CounterName == "% Processor Time" | summarize AvgCPU = avg(CounterValue) by bin(TimeGenerated, 5m), ComputerAverage CPU usage per computer in 5-min bins.
-
AzureActivity | where ActivityStatus == "Failed" | project TimeGenerated, Caller, OperationName, ActivityStatusShow failed Azure operations.
-
SecurityEvent | where EventID == 4625 | summarize FailedLogins = count() by Account, bin(TimeGenerated, 1h)Failed login attempts per account per hour.
-
InsightsMetrics | where Name == "Requests" | summarize TotalRequests = sum(Val) by bin(TimeGenerated, 1h), ResourceTotal requests per resource per hour.
2οΈβ£ Microsoft Sentinel Queries
-
SigninLogs | where ResultType != 0 | summarize Failures = count() by UserPrincipalName, IPAddressFailed sign-ins by user and IP.
-
SecurityAlert | summarize Alerts = count() by AlertName, SeverityCount of alerts by name and severity.
-
SecurityIncident | where Status == "Active" | project IncidentNumber, Title, Severity, OwnerList active incidents with details.
-
OfficeActivity | where Operation == "FileAccessed" | summarize AccessCount = count() by UserId, Site_UrlFile access count by user and site.
-
DeviceNetworkEvents | where RemoteUrl contains "malicious.com" | project Timestamp, DeviceName, RemoteUrlDetect connections to malicious domains.
3οΈβ£ Application Insights Queries
-
requests | summarize AvgDuration = avg(duration) by bin(timestamp, 1h)Average request duration per hour.
-
requests | where success == "False" | summarize Failures = count() by nameFailed requests by operation name.
-
dependencies | summarize AvgDuration = avg(duration) by targetAverage dependency call duration by target.
-
traces | where message contains "Exception" | project timestamp, message, severityLevelList exception traces with severity.
-
pageViews | summarize Views = count() by namePage view count by page name.
4οΈβ£ Cross-Service / Advanced Patterns
-
union requests, dependencies | summarize TotalCalls = count() by bin(timestamp, 1h)Combine requests and dependencies for total calls per hour.
-
SigninLogs | join kind=inner (SecurityAlert) on $left.UserPrincipalName == $right.CompromisedEntity | project TimeGenerated, UserPrincipalName, AlertNameCorrelate sign-ins with security alerts.
-
Perf | where CounterName == "% Processor Time" | summarize AvgCPU = avg(CounterValue) by Computer | order by AvgCPU descTop CPU-consuming machines.
-
SecurityEvent | where EventID in (4720, 4722, 4723) | summarize Count = count() by EventID, AccountTrack account creation, enable, and password change events.
-
requests | extend DurationSec = todouble(duration)/1000 | summarize P95 = percentile(DurationSec, 95) by bin(timestamp, 1h)95th percentile request duration per hour.
-
Heartbeat | summarize LastSeen = max(TimeGenerated) by Computer | where LastSeen < ago(1h)Machines not reporting heartbeat in the last hour.
π‘ Tips for Using KQL Efficiently
- Filter early with
whereto reduce dataset size. - Use
projectto select only needed columns. - Aggregate with
summarizebefore joins for performance. - Parameterize queries in Workbooks for reusability.
- Test with small time windows before scaling up.
| Azure-Insights :👈 | 👉:Azure-Log-Analytics |