Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure - App Insights - how to track the logged-in Username in Auth Id?

What is the best-supported approach for tracking logged-in Usernames/Ids in App Insights telemetry?

A User with Username "JonTester1" said some Pages he visited 4 hours ago were really slow. How can I see everything JonTester1 did in App Insights to trouble shoot/know which pages he's referring to?

Seems like User Id in App Insights is some Azure-generated anonymized thing like u7gbh that Azure ties to its own idea of the same user (thru cookie?). It doesn't know about our app's usernames at all.

I've also seen a separate field in App Insights called Auth Id (or user_AuthenticatedId in some spots), which looks to sometimes have the actual username e.g. "JonTester1" filled in - but not always... And while I don't see any mention of this field in the docs, it seems promising. How is our app's code/config supposed to be setting that Auth Id to make sure every App Insights log/telemetry has it set?

Relevant MS docs:

  • https://docs.microsoft.com/en-us/azure/azure-monitor/app/usage-send-user-context This looks to just copy one library Telemtry object's User Id into another... no mention of our custom, helpful Username/Id anyway... and most in-the-wild examples I see don't actually look like this, including MS docs own examples in the 3rd link below; they instead hardcode get a new TelemtryClient()
  • https://docs.microsoft.com/en-us/azure/azure-monitor/app/website-monitoring No mention of consistently tracking a custom Username/Id
  • https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#authenticated-users Shows some different helpful pieces, but still no full example. E.g. it says with only the setAuth... JS function call (still no full example of working client-side JS that tracks User) on the page, you don't need any server-side code for it to track custom User Id across both client-side and server-side telemetry sent to Azure... yet then it also shows explicit code to new up a TelemetryClient() server-side to track User Id (in the Global.asax.cs or where?)... so you do need both?

Similar SO questions, but don't connect the dots/show a full solution:

  • Azure Insights telemetry not showing Auth ID on all transactions
  • Application Insights - Tracking user and session across schemas
  • How is Application insight tracking the User_Id?
  • Display user ID in the metrics of application Insight

I'm hoping this question and answers can get this more ironed out; hopefully do a better job of documentation than the relevant MS docs...

like image 657
Don Cheadle Avatar asked May 05 '19 03:05

Don Cheadle


1 Answers

The first link in your question lists the answer. What it does show you is how to write a custom telemetry initializer. Such an initializer lets you add or overwrite properties that will be send along any telemetry that is being send to App Insights.

Once you add it to the configuration, either in code or the config file (see the docs mentioned earlier in the answer) it will do its work without you needing to create special instances of TelemtryClient. That is why this text of you does not make sense to me:

[…] and most in-the-wild examples I see don't actually look like this, including MS docs own examples in the 3rd link below; they instead hardcode get a new TelemtryClient()

You can either overwrite the value of UserId or overwrite AuthenticatedUserId in your initializer. You can modify the code given in the docs like this:

        if (requestTelemetry != null && !string.IsNullOrEmpty(requestTelemetry.Context.User.Id) &&
            (string.IsNullOrEmpty(telemetry.Context.User.Id) || string.IsNullOrEmpty(telemetry.Context.Session.Id)))
        {
            // Set the user id on the Application Insights telemetry item.
            telemetry.Context.User.AuthenticatedUserId = HttpContext.Current.User.Identity.Name;
        }

You can then see the Auth Id and User Id by going to your AI resource -> Search and click an item. Make sure to press "Show All" first, otherwise the field is not displayed.

Auth Id in the screenshot below is set to the user id from the database in our example:

enter image description here

We access the server from azure functions as well so we set the user id server side as well since there is no client involved in such scenarios.

There is no harm in settting it in both places, javascript and server side via an initializer. That way you cover all scenario's.

like image 130
Peter Bons Avatar answered Sep 19 '22 12:09

Peter Bons