英文:
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
const express = require('express');
const querystring = require('querystring');
const url = require('url');
const appInsights = require('./appInsightsSetup'); // 从单独的文件导入appInsights设置
const app = express();
const port = 3000;
app.get('/', (req, res) => {
console.log("appInsights.defaultClient", appInsights.defaultClient);
// 跟踪传入的请求
appInsights.defaultClient.trackRequest({
name: req.path,
url: req.url,
duration: Date.now() - req.startTime,
resultCode: res.statusCode,
success: true
});
res.send('Hello World!');
});
app.get('/stdout', (req, res) => {
let logStr = querystring.parse(url.parse(req.url).query).log;
// logStr = "logging stdout"
process.stdout.write(logStr + '\n');
console.log('console: ' + logStr + '\n');
// 跟踪自定义事件
appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
res.send('Received\n');
});
app.get('/stderr', (req, res) => {
let errStr = querystring.parse(url.parse(req.url).query).log;
process.stderr.write(errStr + '\n');
console.log('console: ' + errStr + '\n');
// 跟踪自定义事件
appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
res.send('Received\n');
});
// 记录请求开始时间的中间件
app.use((req, res, next) => {
req.startTime = Date.now();
next();
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
appInsightsSetup.js
const appInsights = require('applicationinsights');
appInsights.setup("InstrumentationKey=;IngestionEndpoint=")
.setAutoDependencyCorrelation(false)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoCollectConsole(true, true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(false)
.setInternalLogging(true)
.start();
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
appInsights.defaultClient NodeClient {
_telemetryProcessors: [],
config: Config {
_endpointBase: 'https://dc.services.visualstudio.com',
_connectionString: undefined,
_instrumentationKey: '3e9532',
correlationHeaderExcludedDomains: [
'*.core.windows.net',
'*.core.chinacloudapi.cn',
'*.core.cloudapi.de',
'*.core.usgovcloudapi.net',
'*.core.microsoft.scloud',
'*.core.eaglex.ic.gov'
],
correlationIdRetryIntervalMs: 30000,
disableAllExtendedMetrics: false,
disableAppInsights: false,
disableStatsbeat: false,
distributedTracingMode: undefined,
enableAutoCollectConsole: undefined,
enableLoggerErrorToTrace: undefined,
enableAutoCollectDependencies: undefined,
enableAutoCollectIncomingRequestAzureFunctions: undefined,
enableAutoCollectExceptions: undefined,
enableAutoCollectExtendedMetrics: undefined,
enableAutoCollectExternalLoggers: undefined,
enableAutoCollectHeartbeat: undefined,
enableAutoCollectPerformance: undefined,
enableAutoCollectPreAggregatedMetrics: undefined,
enableAutoCollectRequests: undefined,
enableAutoDependencyCorrelation: undefined,
enableInternalDebugLogging: undefined,
enableInternalWarningLogging: undefined,
enableResendInterval: undefined,
enableMaxBytesOnDisk: undefined,
enableSendLiveMetrics: undefined,
enableUseAsyncHooks: undefined,
enableUseDiskRetryCaching: undefined,
endpointUrl: 'https://centralindia-0.in.applicationinsights.azure.com/v2.1/track',
extendedMetricDisablers: undefined,
ignoreLegacyHeaders: false,
maxBatchIntervalMs: 15000,
maxBatchSize: 250,
proxyHttpUrl: undefined,
proxyHttpsUrl: undefined,
quickPulseHost: 'centralindia.livediagnostics.monitor.azure.com',
samplingPercentage: 100,
enableWebInstrumentation: false,
_webInstrumentationConnectionString: '',
webInstrumentationConfig: null,
webInstrumentationSrc: '',
enableAutoWebSnippetInjection: false,
_profileQueryEndpoint: 'https://centralindia-0.in.applicationinsights.azure.com/',
correlationId: 'cid-v1:'
},
Code:
index.js
const express = require('express');
const querystring = require('querystring');
const url = require('url');
const appInsights = require('./appInsightsSetup'); // Import the appInsights setup from the separate file
const app = express();
const port = 3000;
app.get('/', (req, res) => {
console.log("appInsights.defaultClient", appInsights.defaultClient)
// Track the incoming request
appInsights.defaultClient.trackRequest({
name: req.path,
url: req.url,
duration: Date.now() - req.startTime,
resultCode: res.statusCode,
success: true
});
res.send('Hello World!');
});
app.get('/stdout', (req, res) => {
let logStr = querystring.parse(url.parse(req.url).query).log;
// logStr = "logging stdout"
process.stdout.write(logStr + '\n');
console.log('console: ' + logStr + '\n');
// Track the custom event
appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
res.send('Received\n');
});
app.get('/stderr', (req, res) => {
let errStr = querystring.parse(url.parse(req.url).query).log;
process.stderr.write(errStr + '\n');
console.log('console: ' + errStr + '\n');
// Track the custom event
appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
res.send('Received\n');
});
// Middleware to record request start time
app.use((req, res, next) => {
req.startTime = Date.now();
next();
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
appInsightsSetup.js
const appInsights = require('applicationinsights');
appInsights.setup("InstrumentationKey=;IngestionEndpoint=")
.setAutoDependencyCorrelation(false)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoCollectConsole(true, true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(false)
.setInternalLogging(true)
.start();
module.exports = appInsights;
答案1
得分: 1
- 配置 Azure AppInsights 用于 Node.js 应用程序。
appInsightsSetup.js:
const appInsights = require('applicationinsights');
// 将 'YOUR_INSTRUMENTATION_KEY' 替换为你的实际 Application Insights 仪表板密钥
const instrumentationKey = '886e0430-1007-4bc';
appInsights.setup(instrumentationKey)
.setAutoDependencyCorrelation(false)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoCollectConsole(true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(false)
.setInternalLogging(true)
.start();
module.exports = appInsights;
index.js / main.js:
const express = require('express');
const querystring = require('querystring');
const url = require('url');
const appInsights = require('./appInsightsSetup');
const app = express();
const port = 3000;
// 记录请求开始时间的中间件
app.use((req, res, next) => {
req.startTime = Date.now();
next();
});
app.get('/', (req, res) => {
// 追踪传入的请求
appInsights.defaultClient.trackRequest({
name: req.path,
url: req.url,
duration: Date.now() - req.startTime,
resultCode: res.statusCode,
success: true
});
res.send('Hello World!');
});
app.get('/stdout', (req, res) => {
let logStr = querystring.parse(url.parse(req.url).query).log;
process.stdout.write(logStr + '\n');
console.log('console: ' + logStr + '\n');
// 追踪自定义事件
appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
res.send('Received\n');
});
app.get('/stderr', (req, res) => {
let errStr = querystring.parse(url.parse(req.url).query).log;
process.stderr.write(errStr + '\n');
console.log('console: ' + errStr + '\n');
// 追踪自定义事件
appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
res.send('Received\n');
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
-
安装
npm install applicationinsights express querystring url
。 -
你正在经历的行为,例如
enableAutoCollectConsole
和enableAutoDependencyCorrelation
这些属性在NodeClient
对象中显示为undefined
,这是由于applicationinsights
包在内部处理这些配置的方式所致。 -
遥测跟踪按预期工作,你可以放心地在 Azure 门户中看到收集的数据,可以确保你的设置正常运行,尽管在
NodeClient
对象中这些属性显示为undefined
。 -
你观察到的行为不是问题,你可以放心地继续,你的 Azure AppInsights 设置按预期工作。在 Azure 门户中看到收集的遥测数据,并且你的应用程序正常运行,不必担心
NodeClient
对象中这些属性的undefined
值。
输出:
英文:
- Configure the Azure AppInsights for Node.js application.
appInsightsSetup.js:
const appInsights = require('applicationinsights');
// Replace 'YOUR_INSTRUMENTATION_KEY' with your actual Application Insights instrumentation key
const instrumentationKey = '886e0430-1007-4bc';
appInsights.setup(instrumentationKey)
.setAutoDependencyCorrelation(false)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false)
.setAutoCollectDependencies(false)
.setAutoCollectConsole(true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(false)
.setInternalLogging(true)
.start();
module.exports = appInsights;
index.js /main.js:
const express = require('express');
const querystring = require('querystring');
const url = require('url');
const appInsights = require('./appInsightsSetup');
const app = express();
const port = 3000;
// Middleware to record request start time
app.use((req, res, next) => {
req.startTime = Date.now();
next();
});
app.get('/', (req, res) => {
// Track the incoming request
appInsights.defaultClient.trackRequest({
name: req.path,
url: req.url,
duration: Date.now() - req.startTime,
resultCode: res.statusCode,
success: true
});
res.send('Hello World!');
});
app.get('/stdout', (req, res) => {
let logStr = querystring.parse(url.parse(req.url).query).log;
process.stdout.write(logStr + '\n');
console.log('console: ' + logStr + '\n');
// Track the custom event
appInsights.defaultClient.trackEvent({ name: 'stdout', properties: { log: logStr } });
res.send('Received\n');
});
app.get('/stderr', (req, res) => {
let errStr = querystring.parse(url.parse(req.url).query).log;
process.stderr.write(errStr + '\n');
console.log('console: ' + errStr + '\n');
// Track the custom event
appInsights.defaultClient.trackEvent({ name: 'stderr', properties: { log: errStr } });
res.send('Received\n');
});
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
andenableAutoDependencyCorrelation
are showing asundefined
in theNodeClient
object, is expected due to the way theapplicationinsights
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 theNodeClient
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 theNodeClient
object.
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论