Application Insights TrackEvent n'a jamais persisté dans Azure

Dec 17 2020

J'essaie d'implémenter le SDK pour Node pour suivre les événements personnalisés dans notre base de code. Le service que j'ai écrit est appelé à partir d'une chaîne de méthodes asynchrone et s'exécute dans 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")
    }
}

Le service lui-même ressemble à ceci:

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()
    }
}

Cependant, les événements que j'envoie ne sont jamais visibles dans Azure Portal lorsque j'interroge la customEventstable. J'ai attendu plus de 10 minutes car je sais qu'il peut y avoir des retards dans les informations sur les applications.

J'ai essayé de faire l'appel pour vider asynchrone à la place et de l'utiliser awaitlors de l'appel, mais cela n'aide pas non plus:

Promise.resolve(this._instance.flush(true))

La variable d'environnement APPLICATION_INSIGHTS_CONNECTION_STRINGa le format InstrumentationKey=xxx;IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/.

Mon problème semble très similaire à l' échec de C # Application Insight: TrackEvent n'envoie pas à Azure Application Insight , mais ai-je vraiment besoin de mettre en veille / expirer pour tenir compte des horaires?

La sortie du journal lorsque je lance cela ne dit rien, aucune erreur produite et rien ne semble éteint:

[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)

Qu'est-ce que je rate?

Mettre à jour

Donc, apparemment, il y a un JavaScript et un SDK Node pour rendre les choses plus confuses. J'essaierai avec le SDK Node, mais je ne vois pas pourquoi le premier ne devrait pas fonctionner.

Solution

La construction suivante de ApplicationInsightsService fonctionne:

// 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)
    }
}

Pour les applications de nœuds:

Faire :npm install --save applicationinsights

Ne pas :npm install --save @microsoft/applicationinsights-web

Réponses

jokarl Dec 17 2020 at 18:42

La construction suivante des ApplicationInsightsServiceouvrages:

// 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)
    }
}

Pour les applications de nœuds:

Faire :npm install --save applicationinsights

Ne pas :npm install --save @microsoft/applicationinsights-web