microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
321 stars 138 forks source link

NextJS and AI always connects to West Europe #1265

Closed djpiper28 closed 3 months ago

djpiper28 commented 5 months ago

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:

Environment:

@ZanyLeonic was able to replicate this issue as well.

Error messages seen: image image

djpiper28 commented 5 months ago

It is hard-coded by the looks of it image

JacksonWeber commented 5 months ago

@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 commented 5 months ago

@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

JacksonWeber commented 5 months ago

@djpiper28 Also, regarding the @azure/core-tracing loading issue, this was addressed here: https://github.com/microsoft/ApplicationInsights-node.js/issues/1107

djpiper28 commented 5 months ago

that env var seemed to work 👍🏼

djpiper28 commented 5 months ago

this appears to have broken the logs

JacksonWeber commented 5 months ago

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

djpiper28 commented 4 months ago

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

JacksonWeber commented 4 months ago

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

djpiper28 commented 4 months ago

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)

JacksonWeber commented 4 months ago

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

JacksonWeber commented 3 months ago

Closing as there have not been updates on this issue.