Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding authenticated user ID information in server-side NodeJS Application Insights events

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!

like image 398
upInCloud Avatar asked Jun 08 '18 17:06

upInCloud


People also ask

How do I log custom events in application Insights?

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.

Can you use app Insights to monitor your node JS application?

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.

What are events in application Insights?

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.

What is application Insights telemetry?

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.


1 Answers

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.

like image 130
Bruce Chen Avatar answered Sep 28 '22 09:09

Bruce Chen