Application Insights TrackEvent không bao giờ tồn tại trong Azure

Dec 17 2020

Tôi đang cố gắng triển khai SDK cho Node để theo dõi các sự kiện tùy chỉnh trong cơ sở mã của chúng tôi. Dịch vụ tôi đã viết được gọi từ một chuỗi phương thức không đồng bộ và chạy trong 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")
    }
}

Bản thân dịch vụ trông như thế này:

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

Tuy nhiên, các sự kiện tôi gửi sẽ không bao giờ hiển thị trong Azure Portal khi tôi truy vấn customEventsbảng. Tôi đã đợi hơn 10 phút vì tôi biết có thể có sự chậm trễ trong thông tin chi tiết về ứng dụng.

Thay vào đó, tôi đã thử thực hiện cuộc gọi chuyển sang trạng thái không đồng bộ và sử dụng awaitkhi gọi nó, nhưng điều đó cũng không giúp được gì:

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

Biến môi trường APPLICATION_INSIGHTS_CONNECTION_STRINGcó định dạng InstrumentationKey=xxx;IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/.

Vấn đề của tôi có vẻ rất giống với lỗi C # Application Insight: TrackEvent Không gửi tới Azure Application Insight , nhưng tôi có thực sự cần phải ngủ / hết giờ để tính thời gian không?

Đầu ra nhật ký khi tôi chạy điều này không có gì, không có lỗi nào được tạo ra và dường như không có gì tắt:

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

Tôi đang thiếu cái gì?

Cập nhật

Vì vậy, rõ ràng có một JavaScript một SDK Node để làm cho mọi thứ trở nên khó hiểu hơn. Tôi sẽ thử với Node SDK, nhưng tôi không hiểu tại sao cái cũ không hoạt động.

Giải pháp

Việc xây dựng ApplicationInsightsService sau đây hoạt động:

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

Đối với các ứng dụng nút:

Làm :npm install --save applicationinsights

Đừng :npm install --save @microsoft/applicationinsights-web

Trả lời

jokarl Dec 17 2020 at 18:42

Việc xây dựng các ApplicationInsightsServicecông trình sau:

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

Đối với các ứng dụng nút:

Làm :npm install --save applicationinsights

Đừng :npm install --save @microsoft/applicationinsights-web