配置 Azure AppInsights 用于 Node.js 应用程序

huangapple go评论124阅读模式
英文:

Configure the Azure AppInsights for Node.js application

问题

即使在为Node.js应用程序配置Azure AppInsights之后,我得到的NodeClient的属性(如enableAutoCollectConsole、enableAutoDependencyCorrelation等)都是未定义的。请告诉我出了什么问题?
参考链接:
https://github.com/microsoft/ApplicationInsights-node.js/issues/560

代码:
index.js

  1. const express = require('express');
  2. const querystring = require('querystring');
  3. const url = require('url');
  4. const appInsights = require('./appInsightsSetup'); // 从单独的文件导入appInsights设置
  5. const app = express();
  6. const port = 3000;
  7. app.get('/', (req, res) => {
  8. console.log("appInsights.defaultClient", appInsights.defaultClient);
  9. // 跟踪传入的请求
  10. appInsights.defaultClient.trackRequest({
  11. name: req.path,
  12. url: req.url,
  13. duration: Date.now() - req.startTime,
  14. resultCode: res.statusCode,
  15. success: true
  16. });
  17. res.send('Hello World!');
  18. });
  19. app.get('/stdout', (req, res) => {
  20. let logStr = querystring.parse(url.parse(req.url).query).log;
  21. // logStr = "logging stdout"
  22. process.stdout.write(logStr + '\n');
  23. console.log('console: ' + logStr + '\n');
  24. // 跟踪自定义事件
  25. appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
  26. res.send('Received\n');
  27. });
  28. app.get('/stderr', (req, res) => {
  29. let errStr = querystring.parse(url.parse(req.url).query).log;
  30. process.stderr.write(errStr + '\n');
  31. console.log('console: ' + errStr + '\n');
  32. // 跟踪自定义事件
  33. appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
  34. res.send('Received\n');
  35. });
  36. // 记录请求开始时间的中间件
  37. app.use((req, res, next) => {
  38. req.startTime = Date.now();
  39. next();
  40. });
  41. app.listen(port, () => console.log(`Example app listening on port ${port}!`));

appInsightsSetup.js

  1. const appInsights = require('applicationinsights');
  2. appInsights.setup("InstrumentationKey=;IngestionEndpoint=")
  3. .setAutoDependencyCorrelation(false)
  4. .setAutoCollectRequests(true)
  5. .setAutoCollectPerformance(false)
  6. .setAutoCollectExceptions(false)
  7. .setAutoCollectDependencies(false)
  8. .setAutoCollectConsole(true, true)
  9. .setUseDiskRetryCaching(true)
  10. .setSendLiveMetrics(false)
  11. .setInternalLogging(true)
  12. .start();
  13. module.exports = appInsights;
英文:

Even after configuring the Azure AppInsights for Node.js application, I get the NodeClient with properties like enableAutoCollectConsole,enableAutoDependencyCorrelation, etc as undefined. Please tell me what is wrong?
Reference link:
https://github.com/microsoft/ApplicationInsights-node.js/issues/560

  1. appInsights.defaultClient NodeClient {
  2. _telemetryProcessors: [],
  3. config: Config {
  4. _endpointBase: 'https://dc.services.visualstudio.com',
  5. _connectionString: undefined,
  6. _instrumentationKey: '3e9532',
  7. correlationHeaderExcludedDomains: [
  8. '*.core.windows.net',
  9. '*.core.chinacloudapi.cn',
  10. '*.core.cloudapi.de',
  11. '*.core.usgovcloudapi.net',
  12. '*.core.microsoft.scloud',
  13. '*.core.eaglex.ic.gov'
  14. ],
  15. correlationIdRetryIntervalMs: 30000,
  16. disableAllExtendedMetrics: false,
  17. disableAppInsights: false,
  18. disableStatsbeat: false,
  19. distributedTracingMode: undefined,
  20. enableAutoCollectConsole: undefined,
  21. enableLoggerErrorToTrace: undefined,
  22. enableAutoCollectDependencies: undefined,
  23. enableAutoCollectIncomingRequestAzureFunctions: undefined,
  24. enableAutoCollectExceptions: undefined,
  25. enableAutoCollectExtendedMetrics: undefined,
  26. enableAutoCollectExternalLoggers: undefined,
  27. enableAutoCollectHeartbeat: undefined,
  28. enableAutoCollectPerformance: undefined,
  29. enableAutoCollectPreAggregatedMetrics: undefined,
  30. enableAutoCollectRequests: undefined,
  31. enableAutoDependencyCorrelation: undefined,
  32. enableInternalDebugLogging: undefined,
  33. enableInternalWarningLogging: undefined,
  34. enableResendInterval: undefined,
  35. enableMaxBytesOnDisk: undefined,
  36. enableSendLiveMetrics: undefined,
  37. enableUseAsyncHooks: undefined,
  38. enableUseDiskRetryCaching: undefined,
  39. endpointUrl: 'https://centralindia-0.in.applicationinsights.azure.com/v2.1/track',
  40. extendedMetricDisablers: undefined,
  41. ignoreLegacyHeaders: false,
  42. maxBatchIntervalMs: 15000,
  43. maxBatchSize: 250,
  44. proxyHttpUrl: undefined,
  45. proxyHttpsUrl: undefined,
  46. quickPulseHost: 'centralindia.livediagnostics.monitor.azure.com',
  47. samplingPercentage: 100,
  48. enableWebInstrumentation: false,
  49. _webInstrumentationConnectionString: '',
  50. webInstrumentationConfig: null,
  51. webInstrumentationSrc: '',
  52. enableAutoWebSnippetInjection: false,
  53. _profileQueryEndpoint: 'https://centralindia-0.in.applicationinsights.azure.com/',
  54. correlationId: 'cid-v1:'
  55. },

Code:
index.js

  1. const express = require('express');
  2. const querystring = require('querystring');
  3. const url = require('url');
  4. const appInsights = require('./appInsightsSetup'); // Import the appInsights setup from the separate file
  5. const app = express();
  6. const port = 3000;
  7. app.get('/', (req, res) => {
  8. console.log("appInsights.defaultClient", appInsights.defaultClient)
  9. // Track the incoming request
  10. appInsights.defaultClient.trackRequest({
  11. name: req.path,
  12. url: req.url,
  13. duration: Date.now() - req.startTime,
  14. resultCode: res.statusCode,
  15. success: true
  16. });
  17. res.send('Hello World!');
  18. });
  19. app.get('/stdout', (req, res) => {
  20. let logStr = querystring.parse(url.parse(req.url).query).log;
  21. // logStr = "logging stdout"
  22. process.stdout.write(logStr + '\n');
  23. console.log('console: ' + logStr + '\n');
  24. // Track the custom event
  25. appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
  26. res.send('Received\n');
  27. });
  28. app.get('/stderr', (req, res) => {
  29. let errStr = querystring.parse(url.parse(req.url).query).log;
  30. process.stderr.write(errStr + '\n');
  31. console.log('console: ' + errStr + '\n');
  32. // Track the custom event
  33. appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
  34. res.send('Received\n');
  35. });
  36. // Middleware to record request start time
  37. app.use((req, res, next) => {
  38. req.startTime = Date.now();
  39. next();
  40. });
  41. app.listen(port, () => console.log(`Example app listening on port ${port}!`));

appInsightsSetup.js

  1. const appInsights = require('applicationinsights');
  2. appInsights.setup("InstrumentationKey=;IngestionEndpoint=")
  3. .setAutoDependencyCorrelation(false)
  4. .setAutoCollectRequests(true)
  5. .setAutoCollectPerformance(false)
  6. .setAutoCollectExceptions(false)
  7. .setAutoCollectDependencies(false)
  8. .setAutoCollectConsole(true, true)
  9. .setUseDiskRetryCaching(true)
  10. .setSendLiveMetrics(false)
  11. .setInternalLogging(true)
  12. .start();
  13. module.exports = appInsights;

答案1

得分: 1

  • 配置 Azure AppInsights 用于 Node.js 应用程序。

appInsightsSetup.js:

  1. const appInsights = require('applicationinsights');
  2. // 将 'YOUR_INSTRUMENTATION_KEY' 替换为你的实际 Application Insights 仪表板密钥
  3. const instrumentationKey = '886e0430-1007-4bc';
  4. appInsights.setup(instrumentationKey)
  5. .setAutoDependencyCorrelation(false)
  6. .setAutoCollectRequests(true)
  7. .setAutoCollectPerformance(false)
  8. .setAutoCollectExceptions(false)
  9. .setAutoCollectDependencies(false)
  10. .setAutoCollectConsole(true)
  11. .setUseDiskRetryCaching(true)
  12. .setSendLiveMetrics(false)
  13. .setInternalLogging(true)
  14. .start();
  15. module.exports = appInsights;

index.js / main.js:

  1. const express = require('express');
  2. const querystring = require('querystring');
  3. const url = require('url');
  4. const appInsights = require('./appInsightsSetup');
  5. const app = express();
  6. const port = 3000;
  7. // 记录请求开始时间的中间件
  8. app.use((req, res, next) => {
  9. req.startTime = Date.now();
  10. next();
  11. });
  12. app.get('/', (req, res) => {
  13. // 追踪传入的请求
  14. appInsights.defaultClient.trackRequest({
  15. name: req.path,
  16. url: req.url,
  17. duration: Date.now() - req.startTime,
  18. resultCode: res.statusCode,
  19. success: true
  20. });
  21. res.send('Hello World!');
  22. });
  23. app.get('/stdout', (req, res) => {
  24. let logStr = querystring.parse(url.parse(req.url).query).log;
  25. process.stdout.write(logStr + '\n');
  26. console.log('console: ' + logStr + '\n');
  27. // 追踪自定义事件
  28. appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
  29. res.send('Received\n');
  30. });
  31. app.get('/stderr', (req, res) => {
  32. let errStr = querystring.parse(url.parse(req.url).query).log;
  33. process.stderr.write(errStr + '\n');
  34. console.log('console: ' + errStr + '\n');
  35. // 追踪自定义事件
  36. appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
  37. res.send('Received\n');
  38. });
  39. app.listen(port, () => console.log(`Example app listening on port ${port}!`));
  • 安装 npm install applicationinsights express querystring url

  • 你正在经历的行为,例如 enableAutoCollectConsoleenableAutoDependencyCorrelation 这些属性在 NodeClient 对象中显示为 undefined,这是由于 applicationinsights 包在内部处理这些配置的方式所致。

  • 遥测跟踪按预期工作,你可以放心地在 Azure 门户中看到收集的数据,可以确保你的设置正常运行,尽管在 NodeClient 对象中这些属性显示为 undefined

  • 你观察到的行为不是问题,你可以放心地继续,你的 Azure AppInsights 设置按预期工作。在 Azure 门户中看到收集的遥测数据,并且你的应用程序正常运行,不必担心 NodeClient 对象中这些属性的 undefined 值。

输出:

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

英文:
  • Configure the Azure AppInsights for Node.js application.

appInsightsSetup.js:

  1. const appInsights = require('applicationinsights');
  2. // Replace 'YOUR_INSTRUMENTATION_KEY' with your actual Application Insights instrumentation key
  3. const instrumentationKey = '886e0430-1007-4bc';
  4. appInsights.setup(instrumentationKey)
  5. .setAutoDependencyCorrelation(false)
  6. .setAutoCollectRequests(true)
  7. .setAutoCollectPerformance(false)
  8. .setAutoCollectExceptions(false)
  9. .setAutoCollectDependencies(false)
  10. .setAutoCollectConsole(true)
  11. .setUseDiskRetryCaching(true)
  12. .setSendLiveMetrics(false)
  13. .setInternalLogging(true)
  14. .start();
  15. module.exports = appInsights;

index.js /main.js:

  1. const express = require('express');
  2. const querystring = require('querystring');
  3. const url = require('url');
  4. const appInsights = require('./appInsightsSetup');
  5. const app = express();
  6. const port = 3000;
  7. // Middleware to record request start time
  8. app.use((req, res, next) => {
  9. req.startTime = Date.now();
  10. next();
  11. });
  12. app.get('/', (req, res) => {
  13. // Track the incoming request
  14. appInsights.defaultClient.trackRequest({
  15. name: req.path,
  16. url: req.url,
  17. duration: Date.now() - req.startTime,
  18. resultCode: res.statusCode,
  19. success: true
  20. });
  21. res.send('Hello World!');
  22. });
  23. app.get('/stdout', (req, res) => {
  24. let logStr = querystring.parse(url.parse(req.url).query).log;
  25. process.stdout.write(logStr + '\n');
  26. console.log('console: ' + logStr + '\n');
  27. // Track the custom event
  28. appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
  29. res.send('Received\n');
  30. });
  31. app.get('/stderr', (req, res) => {
  32. let errStr = querystring.parse(url.parse(req.url).query).log;
  33. process.stderr.write(errStr + '\n');
  34. console.log('console: ' + errStr + '\n');
  35. // Track the custom event
  36. appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
  37. res.send('Received\n');
  38. });
  39. app.listen(port, () => console.log(`Example app listening on port ${port}!`));
  • Install npm install applicationinsights express querystring url .

  • The behavior you're experiencing, where certain properties like enableAutoCollectConsole and enableAutoDependencyCorrelation are showing as undefined in the NodeClient object, is expected due to the way the applicationinsights package handles these configurations internally.

  • The telemetry tracking is working as expected and you are seeing data being collected in the Azure portal, you can rest assured that your setup is functioning correctly, despite the properties appearing as undefined in the NodeClient object.

  • The behavior you are observing is not an issue, and you can proceed with confidence that your Azure AppInsights setup is working as intended. Seeing telemetry data being collected in the Azure portal and your application is functioning correctly, there is no need to be concerned about the undefined values for these properties in the NodeClient object.

Output:

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

配置 Azure AppInsights 用于 Node.js 应用程序

huangapple
  • 本文由 发表于 2023年8月8日 20:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定