Application InsightsTrackEventがAzureで永続化されることはありません

Dec 17 2020

コードベースでカスタムイベントを追跡するために、SDK forNodeを実装しようとしています。私が作成したサービスは、非同期メソッドチェーンから呼び出され、AzureFunctionsで実行されます。

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テーブルにクエリを実行すると、送信したイベントがAzurePortalに表示されることはありません。アプリケーションの洞察に遅れが生じる可能性があることを知っているので、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の失敗と非常に似ているようです:TrackEventは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 ノードの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

回答

jokarl Dec 17 2020 at 18:42

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