首页 > 解决方案 > 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

标签: typescriptazure-application-insights

解决方案


工程建设如下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


推荐阅读