I have a NodeJS app in which I'm consuming the applicationinsights
NodeJS package (this). According to ApplicationInsights data model described here, it says the property exists, but I am not able to find a code snipped as to how to set this property in the telemetry event I'm sending.
Any snipped describing how to do this would be helpful!
In the Azure Portal, navigate to the Application Insights resource, and click Log Analytics. Log queries help you to fully leverage the value of the data collected in Azure Monitor Logs. Query your custom events by entering “customEvents” in the prompt and click Run.
You can use Application Insights for Node. js services that are hosted in your datacenter, Azure VMs and web apps, and even in other public clouds. To receive, store, and explore your monitoring data, include the SDK in your code.
In Application Insights, a custom event is a data point that you can display in Metrics Explorer as an aggregated count and in Diagnostic Search as individual occurrences. (It isn't related to MVC or other framework "events.") Insert TrackEvent calls in your code to count various events.
Application Insights telemetry model defines a way to correlate telemetry to the operation of which it's a part. For example, a request can make a SQL Database calls and recorded diagnostics info. You can set the correlation context for those telemetry items that tie it back to the request telemetry.
According to your description, I only found the tutorials for ASP.NET about Setting the user context in an ITelemetryInitializer and Authenticated users for JavaScript.
Then I checked the setAuthenticatedUserContext
method under User.ts of Microsoft Application Insights SDK for JavaScript and found the related code snippet under TelemetryContext.ts as follows:
if (typeof userContext.authenticatedId === "string") {
envelope.tags[tagKeys.userAuthUserId] = userContext.authenticatedId;
}
Then checked ContextTagKeys.ts and found the context tags as follows:
this.sessionId = "ai.session.id";
this.userAccountId = "ai.user.accountId";
this.userId = "ai.user.id";
this.userAuthUserId = "ai.user.authUserId";
...
but I am not able to find a code snipped as to how to set this property in the telemetry event I'm sending.
For NodeJS SDK, the context tag keys are under ContextTagKeys.ts. For your requirement, you could leverage the following code snippet:
appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.userAuthUserId] ="xxxx";
For Session id, Account id or other context fields, you just need to choose the relevant context tag key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With