Forum Discussion
How to programmatically generate a link to open an App Insights KQL query?
- Nov 24, 2019
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
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:
hspinto Thanks for your answer and it was really helpful.
Unfortunately My query was more than 1600 characters and I have tried encodeurl to my query and try to open Application Instance I am getting 'The query provided in the URL was in an incorrect format'. Error.
My Encoded URL query looks below: union%20exceptions%2C%20traces%20%7C%20order%20by%20timestamp%20%7C%20extend%20EAICode%20%3D%20tostring(customDimensions.%5B%22EAI%20Code%22%5D)%20%7C%20extend%20EventId%20%3D%20tostring(customDimensions.EventId)%20%7C%20extend%20User%20%3D%20tostring(customDimensions.UserId)%20%7C%20extend%20IsError%20%3D%20tostring(customDimensions.IsError)%20%7C%20extend%20Exception%20%3D%20tostring(details%5B0%5D.type)%20%7C%20project%20TimeStamp%20%3D%20timestamp%2C%20TransactionId%20%3D%20operation_Id%2C%20ParentServiceId%20%3D%20operation_ParentId%2C%20EventId%2C%20operation_Name%2C%20cloud_RoleName%2C%20cloud_RoleInstance%2C%20message%2C%20EAICode%2C%20IsError%2C%20Exception%20%7C%20where%20EventId%20%3D%3D%22578%22
Could you please help me in encoding my query string?