Our team is currently in the process of integrating the Node SDK to track custom events within our codebase. The service we've developed is invoked from an asynchronous method chain and is executed in Azure Functions:
public async handleEvent(event: Event) {
// Perform operations
// Send event report
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")
}
}
The structure of the service itself is as follows:
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()
}
}
Despite implementing the code, the events I transmit do not appear in the Azure Portal when I check the customEvents
table. I have waited for over 10 minutes knowing that there may be delays with application insights.
I attempted to make the flush call asynchronous and utilize await
, but this did not resolve the issue either:
Promise.resolve(this._instance.flush(true))
The environment variable
APPLICATION_INSIGHTS_CONNECTION_STRING
adheres to the format InstrumentationKey=xxx;IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/
.
The problem mirrors a scenario discussed in C# Application Insight Failure: TrackEvent Does not send to Azure Application Insight. Do I truly need to account for timing by incorporating sleep or timeouts?
Upon running the code, the log does not indicate any errors or anomalies:
[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)
What could be the missing link here?
Update
Acknowledging the existence of both a JavaScript and a Node SDK adds another layer of complexity. While I will experiment with the Node SDK, I fail to see why the former should not function properly.
Solution
The revised implementation of the ApplicationInsightsService appears as below:
// 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)
}
}
For node applications:
Do:
npm install --save applicationinsights
Don't:
npm install --save @microsoft/applicationinsights-web