typescript - Application Insights TrackEvent 从未在 Azure 中保留
问题描述
我正在尝试为 Node 实现 SDK 以跟踪我们代码库中的自定义事件。我编写的服务是从异步方法链中调用的,并在 Azure Functions 中运行:
public async handleEvent(event: Event) {
// Do stuff
// Then report event
this.reportEvent(event);
}
private reportEvent(event: Event) {
if (this._applicationInsightsService) {
this._applicationInsightsService.reportEvent(event)
this._context.log("Successfully sent event to application insights")
} else {
this._context.log("Could not send metrics to application insights, service is not defined")
}
}
服务本身如下所示:
export class ApplicationInsightsService {
private _instance: ApplicationInsights
private constructor(connectionString: string) {
this._instance = new ApplicationInsights({
config: {
connectionString: connectionString
}
})
this._instance.loadAppInsights()
}
public static create() {
if (process.env.APPLICATION_INSIGHTS_CONNECTION_STRING === undefined) {
throw new Error("APPLICATION_INSIGHTS_CONNECTION_STRING undefined, cannot report metrics")
}
return new ApplicationInsightsService(process.env.APPLICATION_INSIGHTS_CONNECTION_STRING)
}
public reportEvent(event: Event) {
this._instance.trackEvent({
name: event.type,
properties: event
})
this._instance.flush()
}
}
customEvents
但是,当我查询表时,我发送的事件在 Azure 门户中永远不可见。我已经等了超过 10 分钟,因为我知道应用程序洞察可能会有延迟。
我尝试过调用异步刷新并await
在调用它时使用,但这也无济于事:
Promise.resolve(this._instance.flush(true))
环境变量APPLICATION_INSIGHTS_CONNECTION_STRING
的格式为InstrumentationKey=xxx;IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/
.
我的问题似乎与C# Application Insight Failure: TrackEvent Does not send to Azure Application Insight非常相似,但我真的需要休眠/超时来考虑时间吗?
我运行它时的日志输出什么也没说,没有产生任何错误,也没有任何异常:
[12/17/2020 11:06:10 AM] Executing 'Functions.EventHandler' (Reason='New ServiceBus message detected on 'integrator-events'.', Id=812ccf8f-3cd3-4c75-8ab4-20614556d597)
[12/17/2020 11:06:10 AM] Trigger Details: MessageId: 125f60d79c5a4b029a417bee68df95d7, DeliveryCount: 1, EnqueuedTime: 12/17/2020 11:06:11 AM, LockedUntil: 12/17/2020 11:07:11 AM, SessionId: (null)
[12/17/2020 11:06:10 AM] Received event
[12/17/2020 11:06:10 AM] Successfully sent event to application insights
[12/17/2020 11:06:10 AM] Executed 'Functions.EventHandler' (Succeeded, Id=812ccf8f-3cd3-4c75-8ab4-20614556d597)
我错过了什么?
更新
所以显然有一个JavaScript 和一个Node SDK 让事情变得更加混乱。我将尝试使用 Node SDK,但我不明白为什么前者不应该工作。
解决方案
ApplicationInsightsService 的以下构造有效:
// Previously:
// import { ApplicationInsights } from "@microsoft/applicationinsights-web"
import { TelemetryClient } from "applicationinsights"
export class ApplicationInsightsService {
private _instance: TelemetryClient
private constructor(connectionString: string) {
this._instance = new TelemetryClient(connectionString)
}
}
对于节点应用程序:
做:npm install --save applicationinsights
不要:npm install --save @microsoft/applicationinsights-web
解决方案
工程建设如下ApplicationInsightsService
:
// Previously:
// import { ApplicationInsights } from "@microsoft/applicationinsights-web"
import { TelemetryClient } from "applicationinsights"
export class ApplicationInsightsService {
private _instance: TelemetryClient
private constructor(connectionString: string) {
this._instance = new TelemetryClient(connectionString)
}
}
对于节点应用程序:
做:npm install --save applicationinsights
不要:npm install --save @microsoft/applicationinsights-web
推荐阅读
- c# - 如何从c#中的输入类型时间检索代码中的值
- javascript - 如何设置全局单击侦听器以从许多按钮中获取值?
- moodle - 升级到 Moodle 3.6 后调用未定义函数 mysql_connect
- javascript - 实际上告诉我可以设置的选项的 jsPDF 文档?
- android - 错误:找不到符号类 DaggerAppComponent
- php - 如何修复应用程序系统中的 PHP Excel 导出错误
- react-native - 如何解决 Windows 中的本地构建问题
- javascript - Vuejs上传多个图像在没有cdn的情况下不起作用
- azure-ad-b2c - 将 ADFS 2.0 配置为 b2c 的 SAML 身份提供者
- user-interface - Windows 7 上的 Chrome 71(设置为英语)中的 Jenkins 2.153 部分显示非英语 - 如何强制英语?