【发布时间】:2020-12-17 11:08:47
【问题描述】:
我正在尝试为 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
【问题讨论】:
标签: typescript azure-application-insights