2

Migrating my website to gov cloud, but one of the issues i'm having is that the application insights instrumentation key from the gov cloud doesn't seem to work. The post response back i get is:

{"itemsReceived":7,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"},{"index":1,"statusCode":400,"message":"Invalid instrumentation key"},{"index":2,"statusCode":400,"message":"Invalid instrumentation key"},{"index":3,"statusCode":400,"message":"Invalid instrumentation key"},{"index":4,"statusCode":400,"message":"Invalid instrumentation key"},{"index":5,"statusCode":400,"message":"Invalid instrumentation key"},{"index":6,"statusCode":400,"message":"Invalid instrumentation key"}]}

i'm still tracking SOME data, from linking my web app to application insights directly gives me some information ( like https://learn.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net ), but the javascript SDK ( https://learn.microsoft.com/en-us/azure/azure-monitor/app/javascript ) that i'm using in my application is what is erroring out. It works fine if i give it an instrumentation key from the regular azure cloud, but if i give it one from the gov cloud then it won't work.

I know the key is correct, and i know that my insights are running or else it wouldn't log any activity at all. it just seems like azure gov cloud doesn't like the javascript SDK.

Phil
  • 1,852
  • 2
  • 28
  • 55
  • 2
    I’m wondering if you have configured the endpoint properly for App Insights in US Gov region. – Gaurav Mantri May 22 '20 at 16:54
  • huh, i didn't notice the endpointSuffix before in the app insights configuration. do you know how do i use that suffix for the npm setup in https://github.com/microsoft/ApplicationInsights-JS ? – Phil May 22 '20 at 17:03
  • 1
    Please see node.js section here: https://learn.microsoft.com/en-us/azure/azure-government/documentation-government-services-monitoringandmanagement – Gaurav Mantri May 22 '20 at 17:05
  • 1
    Or the Javascript section. – Gaurav Mantri May 22 '20 at 17:06
  • thank you! I'll work on that, but i think that absolutely looks like the problem. – Phil May 22 '20 at 17:12
  • Let me know if this solves your problem. I will post my comments as answer then. – Gaurav Mantri May 22 '20 at 17:13
  • 1
    yep, it absolutely fixed the problem, thx. the 'connectionstring' property in https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=js was the final piece i needed though – Phil May 22 '20 at 18:38

2 Answers2

5

The right way is to rely on Connection String (it takes care of non public cloud dns suffixes): https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=js#how-to-set-a-connection-string

Its support is available in Javascript v2.3.0.

You can find it in Application Insights overview:

enter image description here

And then pasting it in your snippet:

{
  connectionString:"InstrumentationKey=00000000-0000-0000-0000-000000000000;"
}

(relying on manual overrides of all public endpoint is error-prone since Application Insights can introduce new features requiring new public endpoints which will not work)

ZakiMa
  • 5,637
  • 1
  • 24
  • 48
  • Yes. Connection string in this case will have "applicationinsights.us" as suffix. All AI SDKs will assemble all needed public endpoints based on it. – ZakiMa May 23 '20 at 08:51
  • For non-public cloud connection string looks like this (endpoint suffix depends on a cloud): InstrumentationKey=00000000-0000-0000-0000-000000000000;EndpointSuffix=ai.contoso.com; – ZakiMa May 23 '20 at 08:53
1

App Insights in Azure Gov has different endpoint than Azure General (Commercial) and as mentioned in the comments you will need to use those endpoints instead of using regular endpoints.

You can learn more about App Insights (and Azure Monitoring in general) in Azure Gov here: https://learn.microsoft.com/en-us/azure/azure-government/documentation-government-services-monitoringandmanagement

From the same link:

Configuring your NodeJS Application to target App Insights in Azure Gov region:

var appInsights = require("applicationinsights");
appInsights.setup('INSTRUMENTATION_KEY');
appInsights.defaultClient.config.endpointUrl = "https://dc.applicationinsights.us/v2/track"; // ingestion
appInsights.defaultClient.config.profileQueryEndpoint = "https://dc.applicationinsights.us/api/profiles/{0}/appId"; // appid/profile lookup
appInsights.defaultClient.config.quickPulseHost = "https://quickpulse.applicationinsights.us/QuickPulseService.svc"; //live metrics
appInsights.Configuration.start();

Configuring your JavaScript Application to target App Insights in Azure Gov region:

<script type="text/javascript">
   var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){
      function n(e){t[e]=function(){var n=arguments;t.queue.push(function(){t[e].apply(t,n)})}}var t={config:e};t.initialize=!0;var i=document,a=window;setTimeout(function(){var n=i.createElement("script");n.src=e.url||"https://az416426.vo.msecnd.net/next/ai.2.min.js",i.getElementsByTagName("script")[0].parentNode.appendChild(n)});try{t.cookie=i.cookie}catch(e){}t.queue=[],t.version=2;for(var r=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];r.length;)n("track"+r.pop());n("startTrackPage"),n("stopTrackPage");var s="Track"+r[0];if(n("start"+s),n("stop"+s),n("setAuthenticatedUserContext"),n("clearAuthenticatedUserContext"),n("flush"),!(!0===e.disableExceptionTracking||e.extensionConfig&&e.extensionConfig.ApplicationInsightsAnalytics&&!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){n("_"+(r="onerror"));var o=a[r];a[r]=function(e,n,i,a,s){var c=o&&o(e,n,i,a,s);return!0!==c&&t["_"+r]({message:e,url:n,lineNumber:i,columnNumber:a,error:s}),c},e.autoExceptionInstrumented=!0}return t
   }({
      instrumentationKey:"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
      endpointUrl: "https://dc.applicationinsights.us/v2/track"
   });

   window[aiName]=aisdk,aisdk.queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
</script>
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • The better way is to rely on connection string (which includes dns suffix for a cloud) rather than providing all URLs manually. The latter approach is error prone since Application Insights can introduce more public endpoints (for new features) which will not work without keeping this list in sync. – ZakiMa May 23 '20 at 07:38