upI*_*oud 2 azure node.js telemetry azure-application-insights
我有一个NodeJS应用程序,其中正在使用applicationinsightsNodeJS包(此)。根据此处描述的ApplicationInsights数据模型,它说该属性存在,但是我找不到能在我发送的遥测事件中设置该属性的代码。
任何描述如何执行此操作的信息都会有所帮助!
根据您的描述,我只找到了ASP.NET的教程,有关在ITelemetryInitializer中设置用户上下文和JavaScript的身份验证用户。
然后我查了setAuthenticatedUserContext下方法User.ts的Microsoft应用程序洞察SDK为JavaScript,发现下面的有关代码段TelemetryContext.ts如下:
if (typeof userContext.authenticatedId === "string") {
envelope.tags[tagKeys.userAuthUserId] = userContext.authenticatedId;
}
Run Code Online (Sandbox Code Playgroud)
然后检查ContextTagKeys.ts并找到上下文标记,如下所示:
this.sessionId = "ai.session.id";
this.userAccountId = "ai.user.accountId";
this.userId = "ai.user.id";
this.userAuthUserId = "ai.user.authUserId";
...
Run Code Online (Sandbox Code Playgroud)
但是我找不到在我发送的遥测事件中如何设置此属性的代码。
对于NodeJS SDK,上下文标记键位于ContextTagKeys.ts下。根据您的要求,您可以利用以下代码段:
appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.userAuthUserId] ="xxxx";
Run Code Online (Sandbox Code Playgroud)
对于会话ID,帐户ID或其他上下文字段,您只需要选择相关的上下文标记键即可。
以防万一有人仍在努力根据每个请求将用户信息添加到遥测事件,这就是我所做的:
const appInsights = require("applicationinsights");
appInsights.setup().start()
appInsights.defaultClient.addTelemetryProcessor(
(envelope, context) => {
// context keys from the `ContextTagKeys` contract
const contextKeys = {
userId: "userId",
sessionId: "sessionId",
}
const getContextKey = (key) => {
return appInsights.defaultClient.context.keys[key]
}
const setContextKey = (key, value) => {
envelope.tags[key] = value;
}
// custom context that I set on per-request basis
const requestContext = appInsights.getCorrelationContext().requestContext
const data = envelope.data.baseData;
for (const [key, value] of Object.entries(requestContext)) {
switch (key) {
case "userId":
setContextKey(
getContextKey("userId"), // ai.user.id
value // bob@example.com
)
break
case "sessionId":
setContextKey(
getContextKey("userId"), // ai.session.id
value // 507f191e810c19729de860ea
)
break
default:
// if it's a custom property that doesn't belong in the
// `ContextTagKeys` contract, such as browser information, add
// it as a custom property on the `envelope.data.baseData` object
data.properties[key] = value
}
}
return true
}
)
Run Code Online (Sandbox Code Playgroud)
然后,由于我使用的是Express,所以我创建了一个中间件函数,用于在上下文对象上设置每个请求的信息:
const express = require('express')
const Bowser = require("bowser");
const app = express();
// ...
app.use((req, res, next) => {
const session = req.session;
const userAgent = req.get('User-Agent')
const sessionId = session.id
const userId = session.userId
const browser = Bowser.getParser(userAgent)
const currentRequestContext =
appInsights.getCorrelationContext().requestContext || {}
const nextRequestContext = {
...currentRequestContext,
sessionId, // 507f191e810c19729de860ea
userId, // bob@example.com
browser: // custom property, e.g. Firefox 83
browser.getBrowserName() + " " + browser.getBrowserVersion()
}
appInsights.getCorrelationContext().requestContext = nextRequestContext
next()
})
Run Code Online (Sandbox Code Playgroud)
要获取所有可用合同的列表ContextTagKeys,请查看此处:
ContextTagKeys客户端库中的一些内容也可以在服务器端库中设置,这就是我在填充浏览器信息client_Browser后使属性显示在 Azure 门户中的方法ai.device.browserVersion以下是文档中有关如何创建您自己的自定义遥测处理器的示例:
以下是 GitHub 上的问题,帮助我找到了正确的解决方案:
applicationinsights最后,这是我正在使用的版本:
applicationinsights v1.8.8| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |