Search code examples
azureazure-application-insightsazure-api-managementkql

App Insights: Fetch Request Count grouped by Request Param


So, I am trying to analyze performance of a system. We have 3 types of Request URLs. Something like this-->

1. https://<base-url>/recent?param1=string1
   https://<base-url>/recent?param2=string2
2. https://<base-url>/all?param1=string3
   https://<base-url>/all?param2=string4
3. https://<base-url>?param3=string5

I was using this query :

let start=datetime("2023-07-31T23:59:59.000Z");
let end=datetime("2023-08-01T23:59:59.000Z");
let timeGrain=5m;
let dataset=requests
    | where timestamp > start and timestamp < end
    | where client_Type != "Browser"
    | where * has "<base-url>?param1"
;
dataset
| summarize count_=sum(itemCount), avg(duration), percentiles(duration, 50, 95, 99) by operation_Name
| union(dataset
    | summarize count_=sum(itemCount), avg(duration), percentiles(duration, 50, 95, 99)
    | extend operation_Name="Overall")

I want to analyze the requests count for these URLs by particular request param. I was able to do so for particular request param. I want to have a modular query which displays the count and duration grouped by the request params.


Solution

  • I have used these URLs to reproduce and got the result by using the below given query-

    1. https://<base-url>/echo/recent?param1=Afreen
       https://<base-url>/echo/recent?param2=Afreen
    2. https://<base-url>/echo/all?param1=Afreen
       https://<base-url>/echo/all?param2=Afreen
    3. https://<base-url>/echo/resource?param1=sample
    

    Query:

    let start=datetime("2023-08-07T11:40:30.8754634Z");
    let end=datetime("2023-08-09T23:59:59.000Z");
    let timeGrain = 5m;
    let base_url = dynamic(["https://<base-url>/echo/resource", "https://<base-url>/echo/recent", "https://<base-url>/echo/all"]);
    let dataset = requests
        | where timestamp > start and timestamp < end
        | where client_Type != "Browser"
        | where url has_any (base_url) and isnotempty(split(url, "?")[1])
        | extend urlParams = tostring(parse_url(url)["Query Parameters"])
    ;
    dataset
    | summarize count_ = sum(itemCount), avg(duration), percentiles(duration, 50, 95, 99) by urlParams
    

    Output:

    enter image description here

    If you want to group them by Param_Name then use split(urlParams, ":")[0], as parse_url(url)["Query Parameters"] returns value in "param" : "value" format.