microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NextJS and AI always connects to West Europe

djpiper28 opened this issue · comments

Replication:

// loadAppInsights.js
/**
 * This is part of a work around to get Azure App Insights working, see below for more information
 * https://medium.com/microsoftazure/enabling-the-node-js-application-insights-sdk-in-next-js-746762d92507
 * https://github.com/microsoft/ApplicationInsights-node.js/issues/808
 */

const appInsights = require('applicationinsights');
const SecretClient = require('@azure/keyvault-secrets').SecretClient;
const DefaultAzureCredential = require('@azure/identity').DefaultAzureCredential;

const isEnabled = !!process.env.KEY_VAULT_URL;

if (isEnabled) {
  console.log('Initialising app insights - getting connection string from key vault.');
  const keyVaultUrl = process.env.KEY_VAULT_URL;
  const keyVaultConnectionStringKey = 'AppInsightsConnectionString';

  const keyVault = new SecretClient(keyVaultUrl, new DefaultAzureCredential());
  keyVault
    .getSecret(keyVaultConnectionStringKey)
    .then(appInsightsConnectionString => {
      appInsights
        .setup(appInsightsConnectionString.value)
        .setAutoCollectConsole(true, true)
        .setAutoCollectDependencies(true)
        .setAutoCollectExceptions(true)
        .setAutoCollectRequests(true)
        .setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
      appInsights.defaultClient.setAutoPopulateAzureProperties()
      appInsights.defaultClient.config.endpointUrl = 'https://uksouth-1.in.applicationinsights.azure.com'
      appInsights.start();

      console.log('Initialised app insights');
    })
    .catch(e => {
      throw new Error('Cannot initialise app insights, reason: ' + e);
    });
} else {
  console.log('App Insights logging disabled');
}

Started using node --require ./loadappinsights.js node_modules/next/dist/bin/next dev

Dependencies:

  "dependencies": {
    "@azure/identity": "^4.0.1",
    "@azure/keyvault-secrets": "^4.7.0",
    "@azure/logger": "^1.0.4",
    "apisauce": "^3.0.1",
    "applicationinsights": "3.0.0-beta.11",
    "autoprefixer": "10.4.14",
    "dayjs": "^1.11.10",
    "dayjs-plugin-utc": "^0.1.2",
    "follow-redirects": "^1.15.5",
    "govuk-frontend": "4.7.0",
    "next": "^14.1.0",
    "react": "^18.2.0",
    "react-dom": "18.2.0",
    "react-json-tree": "^0.18.0",
    "sass": "^1.70.0",
    "swr": "^2.2.4",
    "tailwindcss": "3.3.3",
    "uuid": "^9.0.1",
    "zustand": "^4.5.0"
  },

Versions tested:

  • 2.9.2, beta, 3.0.0-beta.11

Environment:

  • AKS Cluster
  • App Insights
  • All on uk south

@ZanyLeonic was able to replicate this issue as well.

Error messages seen:
image
image

It is hard-coded by the looks of it
image

@djpiper28 You should be able to avoid this error by setting the environment variable APPLICATION_INSIGHTS_NO_STATSBEAT. Does this error occur in both AKS and in local testing of the application?

@djpiper28 You should be able to avoid this error by setting the environment variable APPLICATION_INSIGHTS_NO_STATSBEAT. Does this error occur in both AKS and in local testing of the application?

It occurs on the aks and locally. I will try that env var on Monday

@djpiper28 Also, regarding the @azure/core-tracing loading issue, this was addressed here: #1107

that env var seemed to work 👍🏼

this appears to have broken the logs

@djpiper28 Can you provide more information. What are you seeing with your logs?

I am no longer getting this issue and I'm not sure what changes have been made to the AKS. Codewise nothing was done

@djpiper28 I'm still not quite sure what your issue with the logs entailed. If you need more help diagnosing the issue, I can try to help with more detail, but otherwise if everything is working as expected I'm happy to resolve this issue.

Issue is the app insights library talks to west Europe servers for internal telemetry or something, and on our aks cluster there is a firewall that blocks it. (the firewall is for gdpr reasons)

@djpiper28 Sounds like that was resolved by disabling statsbeat via the environment variable. Is there a new issue occurring?