All queries in this tutorial use the Log Analytics demo environment. You can use your own environment, but you might not have some of the tables that are used here. Because the data in the demo environment isn't static, the results of your queries might vary slightly from the results shown here.
Count rows
The InsightsMetrics table contains performance data that's collected by insights such as Azure Monitor for VMs and Azure Monitor for containers. To find out how large the table is, we'll pipe its content into an operator that counts rows.
A query is a data source (usually a table name), optionally followed by one or more pairs of the pipe character and some tabular operator. In this case, all records from the InsightsMetrics table are returned and then sent to the count operator. The count operator displays the results because the operator is the last command in the query.
InsightsMetrics | count
Here's the output:
Count
1,263,191
Filter by Boolean expression: where
The AzureActivity table has entries from the Azure activity log, which provides insight into subscription-level or management group-level events occuring in Azure. Let's see only Critical entries during a specific week.
The where operator is common in the Kusto Query Language. where filters a table to rows that match specific criteria. The following example uses multiple commands. First, the query retrieves all records for the table. Then, it filters the data for only records that are in the time range. Finally, it filters those results for only records that have a Critical level.
AzureActivity| where TimeGenerated > datetime(10-01-2020) and TimeGenerated < datetime(10-07-2020)| where Level == 'Critical'
Select a subset of columns: project
Use project to include only the columns you want. Building on the preceding example, let's limit the output to certain columns:
AzureActivity| where TimeGenerated > datetime(10-01-2020) and TimeGenerated < datetime(10-07-2020)| where Level == 'Critical'| project TimeGenerated, Level, OperationNameValue, ResourceGroup, _ResourceId
Show n rows: take
NetworkMonitoring contains monitoring data for Azure virtual networks. Let's use the take operator to look at 10 random sample rows in that table. The take shows some rows from a table in no particular order:
The extend operator is similar to project, but it adds to the set of columns instead of replacing them. You can use both operators to create a new column based on a computation on each row.
The Perf table has performance data that's collected from virtual machines that run the Log Analytics agent.
The summarize operator groups together rows that have the same values in the by clause. Then, it uses an aggregation function like count to combine each group in a single row. A range of aggregation functions are available. You can use several aggregation functions in one summarize operator to produce several computed columns.
The SecurityEvent table contains security events like logons and processes that started on monitored computers. You can count how many events of each level occurred on each computer. In this example, a row is produced for each computer and level combination. A column contains the count of events.
SecurityEvent| summarizecount()by Computer, Level
Summarize by scalar values
You can aggregate by scalar values like numbers and time values, but you should use the bin() function to group rows into distinct sets of data. For example, if you aggregate by TimeGenerated, you'll get a row for most time values. Use bin() to consolidate values per hour or day.
The InsightsMetrics table contains performance data that's organized according to insights from Azure Monitor for VMs and Azure Monitor for containers. The following query shows the hourly average processor utilization for multiple computers:
InsightsMetrics| where Computer startswith"DC"| where Namespace == "Processor"and Name == "UtilizationPercentage"| summarizeavg(Val) by Computer, bin(TimeGenerated, 1h)
Display a chart or table: render
The render operator specifies how the output of the query is rendered. Log Analytics renders output as a table by default. You can select different chart types after you run the query. The render operator is useful to include in queries in which a specific chart type usually is preferred.
The following example shows the hourly average processor utilization for a single computer. It renders the output as a timechart.
InsightsMetrics| where Computer == "DC00.NA.contosohotels.com"| where Namespace == "Processor"and Name == "UtilizationPercentage"| summarizeavg(Val) by Computer, bin(TimeGenerated, 1h)| rendertimechart
Work with multiple series
If you use multiple values in a summarize by clause, the chart displays a separate series for each set of values:
InsightsMetrics| where Computer startswith"DC"| where Namespace == "Processor"and Name == "UtilizationPercentage"| summarizeavg(Val) by Computer, bin(TimeGenerated, 1h)| rendertimechart
Join data from two tables
What if you need to retrieve data from two tables in a single query? You can use the join operator to combine rows from multiple tables in a single result set. Each table must have a column that has a matching value so that the join understands which rows to match.
VMComputer is a table that Azure Monitor uses for VMs to store details about virtual machines that it monitors. InsightsMetrics contains performance data that's collected from those virtual machines. One value collected in InsightsMetrics is available memory, but not the percentage memory that's available. To calculate the percentage, we need the physical memory for each virtual machine. That value is in VMComputer.
The following example query uses a join to perform this calculation. The distinct operator is used with VMComputer because details are regularly collected from each computer. As result, the table contains multiple rows for each computer. The two tables are joined using the Computer column. A row is created in the resulting set that includes columns from both tables for each row in InsightsMetrics, where the value in Computer has the same value in the Computer column in VMComputer.
Use let to make queries easier to read and manage. You can use this operator to assign the results of a query to a variable that you can use later. By using the let statement, the query in the preceding example can be rewritten as:
let PhysicalComputer = VMComputer | distinct Computer, PhysicalMemoryMB;let AvailableMemory = InsightsMetrics | where Namespace == "Memory"and Name == "AvailableMB" | project TimeGenerated, Computer, AvailableMemoryMB = Val;PhysicalComputer| joinkind=inner (AvailableMemory) on Computer| project TimeGenerated, Computer, PercentMemory = AvailableMemoryMB / PhysicalMemoryMB * 100
The following sections give examples of how to work with charts when using the Kusto Query Language.
Chart the results
Begin by reviewing the number of computers per operating system during the past hour:
Heartbeat| where TimeGenerated > ago(1h)| summarizecount(Computer) by OSType
By default, the results display as a table:
For a more useful view, select Chart, and then select the Pie option to visualize the results:
Timecharts
Show the average and the 50th and 95th percentiles of processor time in bins of one hour.
The following query generates multiple series. In the results, you can choose which series to show in the timechart.
Perf| where TimeGenerated > ago(1d) | where CounterName == "% Processor Time"| summarizeavg(CounterValue), percentiles(CounterValue, 50, 95) bybin(TimeGenerated, 1h)
Select the Line chart display option:
Reference line
A reference line can help you easily identify whether the metric exceeded a specific threshold. To add a line to a chart, extend the dataset by adding a constant column:
Multiple expressions in the by clause of summarize create multiple rows in the results. One row is created for each combination of values.
SecurityEvent| where TimeGenerated > ago(1d)| summarizecount()bytostring(EventID), AccountType, bin(TimeGenerated, 1h)
When you view the results as a chart, the chart uses the first column from the by clause. The following example shows a stacked column chart that's created by using the EventID value. Dimensions must be of the string type. In this example, the EventID value is cast to string:
You can switch between columns by selecting the drop-down arrow for the column name:
Smart analytics
This section includes examples that use smart analytics functions in Azure Log Analytics to analyze user activity. You can use these examples to analyze your own applications that are monitored by Azure Application Insights, or use the concepts in these queries for similar analysis on other data.
Cohorts analytics
Cohort analysis tracks the activity of specific groups of users, known as cohorts. Cohort analytics attempts to measure how appealing a service is by measuring the rate of returning users. Users are grouped by the time they first used the service. When analyzing cohorts, we expect to find a decrease in activity over the first tracked periods. Each cohort is titled by the week its members were observed for the first time.
The following example analyzes the number of activities users completed during five weeks after their first use of the service:
let startDate = startofweek(bin(datetime(2017-01-20T00:00:00Z), 1d));let week = range Cohort from startDate todatetime(2017-03-01T00:00:00Z) step7d;// For each user, we find the first and last timestamp of activitylet FirstAndLastUserActivity = (end:datetime) { customEvents | where customDimensions["sourceapp"]=="ai-loganalyticsui-prod"// Check 30 days back to see first time activity. | where timestamp > startDate - 30d | where timestamp < end | summarizemin=min(timestamp), max=max(timestamp) by user_AuthenticatedId};let DistinctUsers = (cohortPeriod:datetime, evaluatePeriod:datetime) {toscalar ( FirstAndLastUserActivity(evaluatePeriod)// Find members of the cohort: only users that were observed in this period for the first time. | wheremin >= cohortPeriod andmin < cohortPeriod + 7d// Pick only the members that were active during the evaluated period or after. | wheremax > evaluatePeriod - 7d | summarizedcount(user_AuthenticatedId)) };week | where Cohort == startDate// Finally, calculate the desired metric for each cohort. In this sample, we calculate distinct users but you can change
// this to any other metric that would measure the engagement of the cohort members.| extend r0 = DistinctUsers(startDate, startDate+7d), r1 = DistinctUsers(startDate, startDate+14d), r2 = DistinctUsers(startDate, startDate+21d), r3 = DistinctUsers(startDate, startDate+28d), r4 = DistinctUsers(startDate, startDate+35d)| union (week | where Cohort == startDate + 7d| extend r0 = DistinctUsers(startDate+7d, startDate+14d), r1 = DistinctUsers(startDate+7d, startDate+21d), r2 = DistinctUsers(startDate+7d, startDate+28d), r3 = DistinctUsers(startDate+7d, startDate+35d) )| union (week | where Cohort == startDate + 14d| extend r0 = DistinctUsers(startDate+14d, startDate+21d), r1 = DistinctUsers(startDate+14d, startDate+28d), r2 = DistinctUsers(startDate+14d, startDate+35d) )| union (week | where Cohort == startDate + 21d| extend r0 = DistinctUsers(startDate+21d, startDate+28d), r1 = DistinctUsers(startDate+21d, startDate+35d) ) | union (week | where Cohort == startDate + 28d| extend r0 = DistinctUsers (startDate+28d, startDate+35d) )// Calculate the retention percentage for each cohort by weeks| project Cohort, r0, r1, r2, r3, r4, p0 = r0/r0*100, p1 = todouble(r1)/todouble (r0)*100, p2 = todouble(r2)/todouble(r0)*100, p3 = todouble(r3)/todouble(r0)*100, p4 = todouble(r4)/todouble(r0)*100| sortby Cohort asc
Here's the output:
Rolling monthly active users and user stickiness
The following example uses time-series analysis with the series_fir function. You can use the series_fir function for sliding window computations. The sample application being monitored is an online store that tracks users' activity through custom events. The query tracks two types of user activities: AddToCart and Checkout. It defines an active user as a user who completed a checkout at least once on a specific day.
let endtime = endofday(datetime(2017-03-01T00:00:00Z));let window = 60d;let starttime = endtime-window;let interval = 1d;let user_bins_to_analyze = 28;// Create an array of filters coefficients for series_fir(). A list of '1' in our case will produce a simple sum.let moving_sum_filter = toscalar(range x from1to user_bins_to_analyze step1 | extend v=1 | summarize makelist(v)); // Level of engagement. Users will be counted as engaged if they completed at least this number of activities.let min_activity = 1;customEvents| where timestamp > starttime | where customDimensions["sourceapp"] == "ai-loganalyticsui-prod"// We want to analyze users who actually checked out in our website.| where (name == "Checkout") and user_AuthenticatedId <> ""// Create a series of activities per user.| make-series UserClicks=count() default=0on timestamp in range(starttime, endtime-1s, interval) by user_AuthenticatedId// Create a new column that contains a sliding sum. // Passing 'false' as the last parameter to series_fir() prevents normalization of the calculation by the size of the window.
// For each time bin in the *RollingUserClicks* column, the value is the aggregation of the user activities in the // 28 days that preceded the bin. For example, if a user was active once on 2016-12-31 and then inactive throughout // January, then the value will be 1 between 2016-12-31 -> 2017-01-28 and then 0s. | extend RollingUserClicks=series_fir(UserClicks, moving_sum_filter, false)// Use the zip() operator to pack the timestamp with the user activities per time bin.| project User_AuthenticatedId=user_AuthenticatedId , RollingUserClicksByDay=zip(timestamp, RollingUserClicks)// Transpose the table and create a separate row for each combination of user and time bin (1 day).| mv-expand RollingUserClicksByDay| extend Timestamp=todatetime(RollingUserClicksByDay[0])// Mark the users that qualify according to min_activity.| extend RollingActiveUsersByDay=iff(toint(RollingUserClicksByDay[1]) >= min_activity, 1, 0)// And finally, count the number of users per time bin.| summarizesum(RollingActiveUsersByDay) by Timestamp// First 28 days contain partial data, so we filter them out.| where Timestamp > starttime + 28d// Render as timechart.| rendertimechart
Here's the output:
The following example turns the preceding query into a reusable function. The example then uses the query to calculate rolling user stickiness. An active user in this query is defined as a user who completed a checkout at least once on a specific day.
let rollingDcount = (sliding_window_size: int, event_name:string){let endtime = endofday(datetime(2017-03-01T00:00:00Z));let window = 90d;let starttime = endtime-window;let interval = 1d; let moving_sum_filter = toscalar(range x from 1 to sliding_window_size step 1 | extend v=1| summarize makelist(v));
let min_activity = 1; customEvents | where timestamp > starttime | where customDimensions["sourceapp"]=="ai-loganalyticsui-prod" | where (name == event_name) | where user_AuthenticatedId <> "" | make-series UserClicks=count() default=0on timestamp in range(starttime, endtime-1s, interval) by user_AuthenticatedId | extend RollingUserClicks=fir(UserClicks, moving_sum_filter, false) | project User_AuthenticatedId=user_AuthenticatedId , RollingUserClicksByDay=zip(timestamp, RollingUserClicks) | mv-expand RollingUserClicksByDay | extend Timestamp=todatetime(RollingUserClicksByDay[0]) | extend RollingActiveUsersByDay=iff(toint(RollingUserClicksByDay[1]) >= min_activity, 1, 0) | summarizesum(RollingActiveUsersByDay) by Timestamp | where Timestamp > starttime + 28d};// Use the moving_sum_filter with bin size of 28 to count MAU.rollingDcount(28, "Checkout")| join(// Use the moving_sum_filter with bin size of 1 to count DAU. rollingDcount(1, "Checkout"))on Timestamp| project sum_RollingActiveUsersByDay1 *1.0 / sum_RollingActiveUsersByDay, Timestamp| rendertimechart
Here's the output:
Regression analysis
This example demonstrates how to create an automated detector for service disruptions based exclusively on an application's trace logs. The detector seeks abnormal, sudden increases in the relative amount of error and warning traces in the application.
Two techniques are used to evaluate the service status based on trace logs data:
Use make-series to convert semi-structured textual trace logs into a metric that represents the ratio between positive and negative trace lines.
Use series_fit_2lines and series_fit_line for advanced step-jump detection by using time-series analysis with a two-line linear regression.
let startDate = startofday(datetime("2017-02-01"));let endDate = startofday(datetime("2017-02-07"));let minRsquare = 0.8; // Tune the sensitivity of the detection sensor. Values close to 1 indicate very low sensitivity.
// Count all Good (Verbose + Info) and Bad (Error + Fatal + Warning) traces, per day.traces| where timestamp > startDate and timestamp < endDate| summarize Verbose = countif(severityLevel == 0), Info = countif(severityLevel == 1), Warning = countif(severityLevel == 2), Error = countif(severityLevel == 3), Fatal = countif(severityLevel == 4) bybin(timestamp, 1d)| extend Bad = (Error + Fatal + Warning), Good = (Verbose + Info)// Determine the ratio of bad traces, from the total.| extend Ratio = (todouble(Bad) / todouble(Good + Bad))*10000| project timestamp , Ratio// Create a time series.| make-series RatioSeries=any(Ratio) default=0on timestamp in range(startDate , endDate -1d, 1d) by'TraceSeverity'// Apply a 2-line regression to the time series.| extend (RSquare2, SplitIdx, Variance2,RVariance2,LineFit2)=series_fit_2lines(RatioSeries)// Find out if our 2-line is trending up or down.| extend (Slope,Interception,RSquare,Variance,RVariance,LineFit)=series_fit_line(LineFit2)// Check whether the line fit reaches the threshold, and if the spike represents an increase (rather than a decrease).| project PatternMatch = iff(RSquare2 > minRsquare and Slope>0, "Spike detected", "No Match")