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
hspinto Thanks again for the reply. My question may be silly.
Not sure what zip means. Can we zip from client side i.e., from javascript / angular?
Is encode mean encodeURI() method from javascript?
Any help by providing sample code to zip and encode from javascript / angular would be really helpful.
The reason for seeking this help is I need to prepare query dynamically and open Application Insights Instance from Angular. Please help me in this regard.
Thank you in Advance!
I'm not sure about how to do it in JS, but this is how I do it in C#:
The trick is utf8-encode the query, then gzip it, then base64-encode it, then url-encode it, and finally include it in the url.
static private string EncodedQuery(string query)
{
var bytes = Encoding.UTF8.GetBytes(query);
using (MemoryStream memoryStream = ExtendedStream.CreateMemoryStream())
{
using (GZipStream compressionStream = new GZipStream(memoryStream, CompressionMode.Compress, leaveOpen: true))
{
compressionStream.Write(bytes, 0, bytes.Length);
}
memoryStream.Seek(0, SeekOrigin.Begin);
Byte[] data = memoryStream.ToArray();
string encodedQuery = Convert.ToBase64String(data);
return HttpUtility.UrlEncode(encodedQuery);
}
}