Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a url to a new AppInsights query?

I have a process that generates AppInsights telemetry. I would like to prove a link to a query in AppInsights. However, it is not the same query every time - the parameters change. I know I can share a link to an existing query, but how do I generate such a link to a new query?

like image 321
mark Avatar asked Oct 23 '25 16:10

mark


1 Answers

In your Application Insights Query Editor, we have an option called Copy link to query. In this link we have following details: enter image description here The URL generated from this action has the following format:

https://portal.azure.com/#@ TENANT_ID/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/resourceId/%2Fsubscriptions%2F SUBSCRIPTION_ID %2FresourceGroups%2F< RESOURCEGROUP%2Fproviders%2Fmicrosoft.insights%2Fcomponents%2F APPLICATION INSIGHTS_INSTANCE_NAME /source/LogsBlade.AnalyticsShareLinkToQuery/q/ ENCODED BASE 64_KQL_QUERY /timespan/TIMESPAN

I’ve emphasized in bold here the parameters of the URL. These parameters have the following values:

TENANT_ID: Your Tenant ID

SUBSCRIPTION_ID: Your Azure Subscription ID that contains the Application Insights instance.

RESOURCE_GROUP: Your Resource Group where the Application Insights instance is deployed.

APPINSIGHTS_INSTANCE_NAME: Your Application Insights instance Name.

ENCODED_KQL_QUERY: Base64 encoding of your query text zipped and URL encoded

TIMESPAN: time filter for the query (optional).

If your query has less than 1600 characters, you can also replace the q parameter in the above URL with a query parameter, and the encoded string will simply be your query plain text escaped (without zipping and encoding).

Dynamic URL it’s important to:

  • Take the text of your KQL query

  • Zip it

  • Encode it in Base64

A C# code that does the encoding of the KQL query is the following:

Generate the Query whatever you want and pass that into the below function to get the Encoded base 64 URL and you can add this in a base URL of application insights.

    static string Encodedbase64KQLQuery(string query)
    {
        var bytes = System.Text.Encoding.UTF8.GetBytes(query);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (GZipStream compressedStream = new GZipStream(memoryStream, CompressionMode.Compress, leaveOpen: true))
            {
                compressedStream.Write(bytes, 0, bytes.Length);
            }
        memoryStream.Seek(0, SeekOrigin.Begin);
        Byte[] bytedata = memoryStream.ToArray();
        string encodedBase64Query = Convert.ToBase64String(bytedata);
        return HttpUtility.UrlEncode(encodedBase64Query);
        }
    }

Please visit this blog which helped me a lot.

like image 162
Delliganesh Sevanesan Avatar answered Oct 26 '25 18:10

Delliganesh Sevanesan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!