Forum Discussion

joruales's avatar
joruales
Icon for Microsoft rankMicrosoft
Aug 27, 2019
Solved

How to programmatically generate a link to open an App Insights KQL query?

In the App Insights UI, I can generate a link to a query by clicking on Copy -> Copy Link to Query.

 

Is there some way to generate such a URL programmatically, If I know the query text and the App Insights endpoint?

  • joruales Maybe a bit late, but as you probably noticed, a URL generated from "Copy Link to Query" has the following format:

     

    https://portal.azure.com/#@TENANT_ID/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/resourceId/%2Fsubscriptions%2FSUBSCRIPTION_ID%2FresourceGroups%2FRESOURCEGROUP%2Fproviders%2Fmicrosoft.insights%2Fcomponents%2FAPPINSIGHTSINSTANCENAME/source/LogsBlade.AnalyticsShareLinkToQuery/q/ENCODEDSTRING/timespan/TIMESPAN

     

    The ENCODEDSTRING is your query zipped and URL encoded/escaped. You must use this approach when the query has more than 1600 characters. Otherwise, if your query has less than 1600 characters, you can replace the q parameter by a query parameter and the encoded string will simply be your query URL escaped. For instance:

     

    https://portal.azure.com/#@TENANT_ID/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/resourceId/%2Fsubscriptions%2FSUBSCRIPTION_ID%2FresourceGroups%2FRESOURCEGROUP%2Fproviders%2Fmicrosoft.insights%2Fcomponents%2FAPPINSIGHTSINSTANCENAME/source/LogsBlade.AnalyticsShareLinkToQuery/query/availabilityResults%20%7C%20where%20duration%20%3E%20500%20%7C%20take%201/timespan/TIMESPAN

     

    would open your App Insights instance with the following query:

     

    availabilityResults
    | where duration > 500
    | take 1

12 Replies

    • joruales's avatar
      joruales
      Icon for Microsoft rankMicrosoft

      Dave Rendón we have a tool that runs queries on App Insights DBs, and we would like to allow users to continue investigating by providing them a link that opens a starter query directly in app insights. I don't think that Azure Monitor workbooks fits this use case since we're not interested in a dashboard UI

      • hspinto's avatar
        hspinto
        Icon for Microsoft rankMicrosoft

        joruales Maybe a bit late, but as you probably noticed, a URL generated from "Copy Link to Query" has the following format:

         

        https://portal.azure.com/#@TENANT_ID/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/resourceId/%2Fsubscriptions%2FSUBSCRIPTION_ID%2FresourceGroups%2FRESOURCEGROUP%2Fproviders%2Fmicrosoft.insights%2Fcomponents%2FAPPINSIGHTSINSTANCENAME/source/LogsBlade.AnalyticsShareLinkToQuery/q/ENCODEDSTRING/timespan/TIMESPAN

         

        The ENCODEDSTRING is your query zipped and URL encoded/escaped. You must use this approach when the query has more than 1600 characters. Otherwise, if your query has less than 1600 characters, you can replace the q parameter by a query parameter and the encoded string will simply be your query URL escaped. For instance:

         

        https://portal.azure.com/#@TENANT_ID/blade/Microsoft_Azure_Monitoring_Logs/LogsBlade/resourceId/%2Fsubscriptions%2FSUBSCRIPTION_ID%2FresourceGroups%2FRESOURCEGROUP%2Fproviders%2Fmicrosoft.insights%2Fcomponents%2FAPPINSIGHTSINSTANCENAME/source/LogsBlade.AnalyticsShareLinkToQuery/query/availabilityResults%20%7C%20where%20duration%20%3E%20500%20%7C%20take%201/timespan/TIMESPAN

         

        would open your App Insights instance with the following query:

         

        availabilityResults
        | where duration > 500
        | take 1

Resources