SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-825806%22%20slang%3D%22en-US%22%3EHow%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-825806%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20the%20App%20Insights%20UI%2C%20I%20can%20generate%20a%20link%20to%20a%20query%20by%20clicking%20on%20Copy%20-%26gt%3B%20Copy%20Link%20to%20Query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIs%20there%20some%20way%20to%20generate%20such%20a%20URL%20programmatically%2C%20If%20I%20know%20the%20query%20text%20and%20the%20App%20Insights%20endpoint%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-881120%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-881120%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F399303%22%20target%3D%22_blank%22%3E%40joruales%3C%2FA%3E%26nbsp%3Bwould%20be%20great%20to%20know%20a%20bit%20more%20on%20the%20intention%20of%20the%20custom%20URL%20to%20the%20query%2C%20but%20would%20recommend%20to%20use%20Azure%20Monitor%20workbooks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Fusage-workbooks%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Fapp%2Fusage-workbooks%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-883207%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-883207%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9020%22%20target%3D%22_blank%22%3E%40Dave%20Rend%C3%B3n%3C%2FA%3E%26nbsp%3Bwe%20have%20a%20tool%20that%20runs%20queries%20on%20App%20Insights%20DBs%2C%20and%20we%20would%20like%20to%20allow%20users%20to%20continue%20investigating%20by%20providing%20them%20a%20link%20that%20opens%20a%20starter%20query%20directly%20in%20app%20insights.%20I%20don't%20think%20that%20Azure%20Monitor%20workbooks%20fits%20this%20use%20case%20since%20we're%20not%20interested%20in%20a%20dashboard%20UI%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1029140%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1029140%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F399303%22%20target%3D%22_blank%22%3E%40joruales%3C%2FA%3E%26nbsp%3BMaybe%20a%20bit%20late%2C%20but%20as%20you%20probably%20noticed%2C%20a%20URL%20generated%20from%20%22Copy%20Link%20to%20Query%22%20has%20the%20following%20format%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23%40%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fportal.azure.com%2F%23%40%3C%2FA%3E%3CSTRONG%3ETENANT_ID%3C%2FSTRONG%3E%2Fblade%2FMicrosoft_Azure_Monitoring_Logs%2FLogsBlade%2FresourceId%2F%252Fsubscriptions%252F%3CSTRONG%3ESUBSCRIPTION_ID%3C%2FSTRONG%3E%252FresourceGroups%252F%3CSTRONG%3ERESOURCEGROUP%3C%2FSTRONG%3E%252Fproviders%252Fmicrosoft.insights%252Fcomponents%252F%3CSTRONG%3EAPPINSIGHTSINSTANCENAME%3C%2FSTRONG%3E%2Fsource%2FLogsBlade.AnalyticsShareLinkToQuery%2F%3CSTRONG%3Eq%3C%2FSTRONG%3E%2F%3CSTRONG%3EENCODEDSTRING%3C%2FSTRONG%3E%2Ftimespan%2F%3CSTRONG%3ETIMESPAN%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20%3CSTRONG%3EENCODEDSTRING%3C%2FSTRONG%3E%20is%20your%20query%20zipped%20and%20URL%20encoded%2Fescaped.%20You%20must%20use%20this%20approach%20when%20the%20query%20has%20more%20than%201600%20characters.%20Otherwise%2C%20if%20your%20query%20has%20less%20than%201600%20characters%2C%20you%20can%20replace%20the%20%3CSTRONG%3E%3CEM%3Eq%3C%2FEM%3E%3C%2FSTRONG%3E%20parameter%20by%20a%20%3CEM%3E%3CSTRONG%3Equery%3C%2FSTRONG%3E%3C%2FEM%3E%20parameter%20and%20the%20encoded%20string%20will%20simply%20be%20your%20query%20URL%20escaped.%20For%20instance%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23%40%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fportal.azure.com%2F%23%40%3C%2FA%3E%3CSTRONG%3ETENANT_ID%3C%2FSTRONG%3E%2Fblade%2FMicrosoft_Azure_Monitoring_Logs%2FLogsBlade%2FresourceId%2F%252Fsubscriptions%252F%3CSTRONG%3ESUBSCRIPTION_ID%3C%2FSTRONG%3E%252FresourceGroups%252F%3CSTRONG%3ERESOURCEGROUP%3C%2FSTRONG%3E%252Fproviders%252Fmicrosoft.insights%252Fcomponents%252F%3CSTRONG%3EAPPINSIGHTSINSTANCENAME%3C%2FSTRONG%3E%2Fsource%2FLogsBlade.AnalyticsShareLinkToQuery%2F%3CSTRONG%3Equery%3C%2FSTRONG%3E%2F%3CSTRONG%3EavailabilityResults%2520%257C%2520where%2520duration%2520%253E%2520500%2520%257C%2520take%25201%3C%2FSTRONG%3E%2Ftimespan%2F%3CSTRONG%3ETIMESPAN%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewould%20open%20your%20App%20Insights%20instance%20with%20the%20following%20query%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EavailabilityResults%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3E%7C%20%3C%2FSPAN%3E%3CSPAN%3Ewhere%3C%2FSPAN%3E%3CSPAN%3E%20duration%20%26gt%3B%20%3C%2FSPAN%3E%3CSPAN%3E500%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3E%7C%20%3C%2FSPAN%3E%3CSPAN%3Etake%3C%2FSPAN%3E%20%3CSPAN%3E1%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1029290%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1029290%22%20slang%3D%22en-US%22%3EThanks!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1455483%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1455483%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F453722%22%20target%3D%22_blank%22%3E%40hspinto%3C%2FA%3E%26nbsp%3BThanks%20for%20your%20answer%20and%20it%20was%20really%20helpful.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnfortunately%20My%20query%20was%20more%20than%201600%20characters%20and%20I%20have%20tried%20encodeurl%20to%20my%20query%20and%20try%20to%20open%20Application%20Instance%20I%20am%20getting%20'%3CSPAN%3EThe%20query%20provided%20in%20the%20URL%20was%20in%20an%20incorrect%20format'.%20Error.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EMy%20Encoded%20URL%20query%20looks%20below%3A%26nbsp%3Bunion%2520exceptions%252C%2520traces%2520%257C%2520order%2520by%2520timestamp%2520%257C%2520extend%2520EAICode%2520%253D%2520tostring(customDimensions.%255B%2522EAI%2520Code%2522%255D)%2520%257C%2520extend%2520EventId%2520%253D%2520tostring(customDimensions.EventId)%2520%257C%2520extend%2520User%2520%253D%2520tostring(customDimensions.UserId)%2520%257C%2520extend%2520IsError%2520%253D%2520tostring(customDimensions.IsError)%2520%257C%2520extend%2520Exception%2520%253D%2520tostring(details%255B0%255D.type)%2520%257C%2520project%2520TimeStamp%2520%253D%2520timestamp%252C%2520TransactionId%2520%253D%2520operation_Id%252C%2520ParentServiceId%2520%253D%2520operation_ParentId%252C%2520EventId%252C%2520operation_Name%252C%2520cloud_RoleName%252C%2520cloud_RoleInstance%252C%2520message%252C%2520EAICode%252C%2520IsError%252C%2520Exception%2520%257C%2520where%2520EventId%2520%253D%253D%2522578%2522%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ECould%20you%20please%20help%20me%20in%20encoding%20my%20query%20string%3F%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1456040%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1456040%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F695936%22%20target%3D%22_blank%22%3E%40krishnachandar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eif%20your%20query%20has%20more%20than%201600%20chars%2C%20you%20must%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1)%20zip%20your%20query%3B%3C%2FP%3E%0A%3CP%3E2)%20encode%20it%3B%3C%2FP%3E%0A%3CP%3E3)%20use%20%3CSTRONG%3Eq%26nbsp%3B%3C%2FSTRONG%3Einstead%20of%20%3CSTRONG%3Equery%3C%2FSTRONG%3E%26nbsp%3Bin%20the%20URL%20path.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1459215%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1459215%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F453722%22%20target%3D%22_blank%22%3E%40hspinto%3C%2FA%3E%26nbsp%3BThanks%20again%20for%20the%20reply.%20My%20question%20may%20be%20silly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20what%20zip%20means.%20Can%20we%20zip%20from%20client%20side%20i.e.%2C%20from%20javascript%20%2F%20angular%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20encode%20mean%20encodeURI()%20method%20from%20javascript%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20by%20providing%20sample%20code%20to%20zip%20and%20encode%20from%20javascript%20%2F%20angular%20would%20be%20really%20helpful.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20reason%20for%20seeking%20this%20help%20is%20I%20need%20to%20prepare%20query%20dynamically%20and%20open%20Application%20Insights%20Instance%20from%20Angular.%20Please%20help%20me%20in%20this%20regard.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20Advance!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1463972%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1463972%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F695936%22%20target%3D%22_blank%22%3E%40krishnachandar%3C%2FA%3E%2C%20I%20am%20not%20a%20Javascript%20expert%20nor%20I%20have%20any%20code%20sample%20to%20share%20with%20you.%20But%20you%20can%20for%20sure%20zip%20from%20client%20side%2C%20as%20there%20are%20many%20libraries%20out%20there%20to%20perform%20that.%20I%20would%20however%20recommend%20you%20to%20first%20analyze%20the%20Azure%20portal%20Javascript%20code%20itself.%20When%20you%20click%20on%20the%20%22Link%20to%20query%22%20button%2C%20everything%20happens%20on%20the%20client%20side.%20If%20you%20manage%20to%20understand%20how%20that%20button%20click%20event%20is%20captured%20and%20processed%2C%20you'll%20for%20sure%20find%20the%20answer%20to%20your%20question%20in%20the%20event%20processor%20code%20itself.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1463990%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1463990%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F695936%22%20target%3D%22_blank%22%3E%40krishnachandar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'm%20not%20sure%20about%20how%20to%20do%20it%20in%20JS%2C%20but%20this%20is%20how%20I%20do%20it%20in%20C%23%3A%3C%2FP%3E%0A%3CP%3EThe%20trick%20is%20utf8-encode%20the%20query%2C%20then%20gzip%20it%2C%20then%20base64-encode%20it%2C%20then%20url-encode%20it%2C%20and%20finally%20include%20it%20in%20the%20url.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20static%20private%20string%20EncodedQuery(string%20query)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20bytes%20%3D%20Encoding.UTF8.GetBytes(query)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20using%20(MemoryStream%20memoryStream%20%3D%20ExtendedStream.CreateMemoryStream())%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20using%20(GZipStream%20compressionStream%20%3D%20new%20GZipStream(memoryStream%2C%20CompressionMode.Compress%2C%20leaveOpen%3A%20true))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20compressionStream.Write(bytes%2C%200%2C%20bytes.Length)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20memoryStream.Seek(0%2C%20SeekOrigin.Begin)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Byte%5B%5D%20data%20%3D%20memoryStream.ToArray()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20encodedQuery%20%3D%20Convert.ToBase64String(data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20HttpUtility.UrlEncode(encodedQuery)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1469269%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20programmatically%20generate%20a%20link%20to%20open%20an%20App%20Insights%20KQL%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1469269%22%20slang%3D%22en-US%22%3E%3CP%3E%40%20krishnachandar%2C%20adding%20to%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F399303%22%20target%3D%22_blank%22%3E%40joruales%3C%2FA%3E%26nbsp%3Bgreat%20feedback%2C%20I%20recommend%20you%20to%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fpako%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Epako%3C%2FA%3E%20package%20to%20zip%20the%20query.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

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?

12 Replies

@joruales would be great to know a bit more on the intention of the custom URL to the query, but would recommend to use Azure Monitor workbooks https://docs.microsoft.com/en-us/azure/azure-monitor/app/usage-workbooks

@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

best response confirmed by joruales (Microsoft)
Solution

@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 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?

@krishnachandar 

 

if your query has more than 1600 chars, you must:

 

1) zip your query;

2) encode it;

3) use instead of query in the URL path.

@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!

 

@krishnachandar, I am not a Javascript expert nor I have any code sample to share with you. But you can for sure zip from client side, as there are many libraries out there to perform that. I would however recommend you to first analyze the Azure portal Javascript code itself. When you click on the "Link to query" button, everything happens on the client side. If you manage to understand how that button click event is captured and processed, you'll for sure find the answer to your question in the event processor code itself.

@krishnachandar 

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);
            }
        }

 

@ krishnachandar, adding to @joruales great feedback, I recommend you to use the pako package to zip the query.

@krishnachandar Did you ever figure out how to encode and zip the query in javascript? None of the methods below worked for me.

@jorsmi 

Just incase anyone else comes here looking for a complete implementation, I figured it out using @hspinto :

 

import pako from 'pako'

const query = 'some query'
const compressed = pako.gzip(query, {to: "string"}) // gzip the query and set options to output as a string
const base64 = btoa(compressed) // base64 encode
const encodedQuery = encodeURIComponent(base64) // URI encode
console.log(encodedQuery)

 

Reference:

Can you reverse engineer Saved Query link to Application Insights? - Stack Overflow

pako - npm (npmjs.com)

How can you encode a string to Base64 in JavaScript? - Stack Overflow