azureazure-application-insightsazure-gov

Application Insights 'invalid instrumentation key' when using key from Azure Gov Cloud for javascript SDK


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.


Solution

  • 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>