{"id":11313,"date":"2025-02-15T13:37:12","date_gmt":"2025-02-15T13:37:12","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11313"},"modified":"2025-02-15T19:59:06","modified_gmt":"2025-02-15T19:59:06","slug":"basics-of-kql","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/basics-of-kql\/","title":{"rendered":"Basics of KQL"},"content":{"rendered":"<p>In the previous two post we&#8217;ve looked at logging and using the TelemetryClient to send information to Application Insights. Application Insights offers a powerful query language (Kusto Query Language &#8211; KQL) for filtering logs.<\/p>\n<p>We cannot possibly cover all options of KQL, so this post will just cover some of the basics and useful queries.<\/p>\n<p><strong>Tables<\/strong><\/p>\n<p>Application Insights supplied several tables as ways tracking events, information and various logging data. <\/p>\n<p>The main tables are as follows<\/p>\n<ul>\n<li><strong>requests<\/strong>: Logs information regarding HTTP requests in your application.<\/li>\n<li><strong>dependencies<\/strong>: Tracks calls made to external services or databases.<\/li>\n<li><strong>exceptions<\/strong>: Logs exceptions within your application.<\/li>\n<li><strong>traces<\/strong>: Diagnostic log messages and traces from your application.<\/li>\n<li><strong>pageViews<\/strong>: Page views and user interactions within your web application.<\/li>\n<li><strong>customEvents<\/strong>: Custom events that are defined to track specific actions or user interactions.<\/li>\n<li><strong>metrics<\/strong>: Tracks performance metrics and custom metrics.<\/li>\n<li><strong>availabilityResults<\/strong>: Availability tests that check uptime and responsiveness of your application\/<\/li>\n<li><strong>appExceptions<\/strong>: Like exceptions, specifically for application exceptions.<\/li>\n<li><strong>appMetrics<\/strong>: Like metrics, specifically for application metrics.<\/li>\n<li><strong>appPageViews<\/strong>: Like pageViews, specifically for application page views.<\/li>\n<li><strong>appPerformanceCounters<\/strong>: Performance counters for your application.<\/li>\n<li><strong>appSystemEvents<\/strong>: System level events within your application.<\/li>\n<li><strong>appTraces<\/strong>: Like traces, specifically for your application traces.<\/li>\n<li><strong>azureActivity<\/strong>: Azure activity logs.<\/li>\n<li><strong>browserTimings<\/strong>: Captures detailed timings information about the browser&#8217;s performance when loading web pages.<\/li>\n<\/ul>\n<p>We can combine timespan, so 1d6h30m means 1 day, 6 hours and 30 minutes.<\/p>\n<p><strong>Get everything<\/strong><\/p>\n<p>We can get everything across all tables (see below for information on the tables) using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsearch *\r\n<\/pre>\n<p><strong>Tables<\/strong><\/p>\n<p>We can just get everything from a table by running the query against a table, so for example for the traces table<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntraces\r\n<\/pre>\n<p><strong>Filtering<\/strong><\/p>\n<p>Obviously returning all <em>traces<\/em> for example, is probably returning more rows than we want, so we can filter using the <em>where<\/em> keyword<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntraces\r\n| where severityLevel == 3\r\n<\/pre>\n<p><strong>Projections<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntraces\r\n| where severityLevel == 3\r\n| project timestamp, message\r\n<\/pre>\n<p><strong>Aggregation<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntraces\r\n| where severityLevel == 3\r\n| summarize count() by bin(timestamp, 1h)\r\n<\/pre>\n<p><strong>Ordering<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nrequests\r\n| where success == &quot;false&quot;\r\n| summarize count() by bin(timestamp, 1h)\r\n| order by bin(timestamp, 1h) desc\r\n<\/pre>\n<p><strong>Samples<\/strong><\/p>\n<p>Get all the tables that have data <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsearch * \r\n| distinct $table\r\n<\/pre>\n<p>Get all records within a table for the last 10 minutes<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntraces\r\n| where timestamp &gt; ago(1m)\r\n<\/pre>\n<p>The <em>ago<\/em> function allows us to use a timespan which includes<\/p>\n<ul>\n<li><strong>d<\/strong>: Days, for example 3d for three days<\/li>\n<li><strong>h<\/strong>: Hours, for example 2h for two hours<\/li>\n<li><strong>m<\/strong>: Minutes, for example 30m for thirty minutes<\/li>\n<li><strong>s<\/strong>: Seconds, for example 10s for ten seconds<\/li>\n<li><strong>ms<\/strong>: Milliseconds, for example 100ms for a hundred milliseconds<\/li>\n<li><strong>microsecond<\/strong>: Microseconds, for example 20microsecond for 20 microseconds<\/li>\n<li><strong>tick<\/strong>: Nanoseconds, for example 1tick for 100 nanoseconds<\/li>\n<\/ul>\n<p>Summarizing each day&#8217;s request count into a timechart (a line chart). We also have options got a bar chart (barchart), pie chart (piechart), area chart (areachart) and scatter chart (scatterchart)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nrequests\r\n| summarize request_count = count() by bin(timestamp, 1d)\r\n| render timechart \r\n<\/pre>\n<p>For some of the other chart types we need to supply difference information, so let&#8217;s look at a pie chart of the different requests<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nrequests\r\n| summarize request_count = count() by name\r\n| render piechart    \r\n<\/pre>\n<p>We can get requests between two dates, including using of the now() function<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nrequests\r\n| where timestamp between (datetime(2025-02-14T00:00:00Z) .. now())\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/kusto\/query\/?view=azure-data-explorer&#038;preserve-view=true&#038;form=MG0AV3\" target=\"_blank\">Kusto Query Language (KQL) overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous two post we&#8217;ve looked at logging and using the TelemetryClient to send information to Application Insights. Application Insights offers a powerful query language (Kusto Query Language &#8211; KQL) for filtering logs. We cannot possibly cover all options of KQL, so this post will just cover some of the basics and useful queries. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[739],"tags":[],"class_list":["post-11313","post","type-post","status-publish","format-standard","hentry","category-application-insights"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11313"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11313\/revisions"}],"predecessor-version":[{"id":11333,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11313\/revisions\/11333"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}