From 772cbbe58207f8060112b33f23765d01dc612519 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Tue, 23 Jan 2024 11:22:54 -0800 Subject: [PATCH 1/2] Adjusts example to use middleware and initializer for targeting id --- ...EvaluationDataToApplicationInsights.csproj | 1 + .../HttpContextTargetingContextAccessor.cs | 2 ++ .../Pages/Checkout.cshtml | 12 +++++-- .../Pages/Shared/_Layout.cshtml | 3 -- .../Program.cs | 10 ++++-- .../README.md | 19 ++-------- .../Telemetry/MyTelemetryInitializer.cs | 35 ------------------- 7 files changed, 23 insertions(+), 59 deletions(-) delete mode 100644 examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs diff --git a/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj b/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj index f4f7bbc6..d3a84c9d 100644 --- a/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj +++ b/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj @@ -12,6 +12,7 @@ + diff --git a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs b/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs index 4f79e466..eeaa16b1 100644 --- a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs +++ b/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs @@ -29,6 +29,8 @@ public ValueTask GetContextAsync() return new ValueTask((TargetingContext)value); } + // + // Grab username from cookie string username = httpContext.Request.Cookies["username"]; var groups = new List(); diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml b/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml index 28c323e2..0d750ee0 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml +++ b/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml @@ -7,6 +7,14 @@

Click Below To Check Out!

- \ No newline at end of file + diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml b/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml index 30a07f01..1ad8ffe4 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml +++ b/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml @@ -20,9 +20,6 @@ -
diff --git a/examples/EvaluationDataToApplicationInsights/Program.cs b/examples/EvaluationDataToApplicationInsights/Program.cs index 41b25026..4b57a4d1 100644 --- a/examples/EvaluationDataToApplicationInsights/Program.cs +++ b/examples/EvaluationDataToApplicationInsights/Program.cs @@ -2,10 +2,10 @@ // Licensed under the MIT license. // using Microsoft.FeatureManagement.Telemetry.ApplicationInsights; -using EvaluationDataToApplicationInsights.Telemetry; using Microsoft.FeatureManagement; using EvaluationDataToApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore; var builder = WebApplication.CreateBuilder(args); @@ -20,8 +20,8 @@ builder.Services.AddApplicationInsightsTelemetry(); // -// App Insights User Tagging -builder.Services.AddSingleton(); +// App Insights TargetingId Tagging +builder.Services.AddSingleton(); // // Enter feature management @@ -55,4 +55,8 @@ app.MapRazorPages(); +// +// Adds Targeting Id to HttpContext +app.UseMiddleware(); + app.Run(); diff --git a/examples/EvaluationDataToApplicationInsights/README.md b/examples/EvaluationDataToApplicationInsights/README.md index aa8ee83c..0458ba93 100644 --- a/examples/EvaluationDataToApplicationInsights/README.md +++ b/examples/EvaluationDataToApplicationInsights/README.md @@ -46,23 +46,10 @@ These cookies are used to correlate telemetry from the browser with telemetry fr *The Javascript SDK is not required, but is useful for collecting browser telemetry and generating these cookies out of the box.* -### Authenticated User ID -In order to connect metrics for the user between multiple services, a Authenticated User Id needs to be emitted. When the application is loaded, a login is simulated by setting a "username" cookie to a random integer. Additionally, the "ai_user" and "ai_session" cookies are expired, to simulate a new browser. +### Targeting Id +In order to connect evaluation events with other metrics from the user, a targeting id needs to be emitted. This can be done multiple ways, but the recommended way is to define a telemetry initializer. This initializer allows the app to modify all telemetry going to Application Insights before it's sent. -To include the authenticated user id on metrics emitted from the Javascript SDK, this app adds the following to _Layout.cshtml: -```html -appInsights.setAuthenticatedUserContext(getCookie("username")); -``` - -To include it on metrics emitted from the ASP.NET SDK, this app uses a TelemetryInitializer named `MyTelemetryInitializer`: -```csharp -builder.Services.AddSingleton(); -``` - -The initializer sets the Authenticated User on the context object for all telemetry emitted from the server: -```csharp -telemetry.Context.User.AuthenticatedUserId = username; -``` +This example uses the provided `TargetingHttpContextMiddleware` and `TargetingTelemetryInitializer`. The middleware adds `TargetingId` (using the targeting context accessor) to the HTTP Context as a request comes in- while the initializer checks for the `TargetingId` on the HTTP Context, and if it exists, adds `TargetingId` to all outgoing Application Insights Telemetry. ## Sample App Usage Sample steps to try out the app: diff --git a/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs b/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs deleted file mode 100644 index c6ed711b..00000000 --- a/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. -// -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.Extensibility; - -namespace EvaluationDataToApplicationInsights.Telemetry -{ - public class MyTelemetryInitializer : ITelemetryInitializer - { - private readonly IHttpContextAccessor _httpContextAccessor; - - public MyTelemetryInitializer(IHttpContextAccessor httpContextAccessor) - { - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); - } - - public void Initialize(ITelemetry telemetry) - { - HttpContext httpContext = _httpContextAccessor.HttpContext; - - if (httpContext == null) - { - return; - } - - string username = httpContext.Request.Cookies["username"]; - - if (username != null) - { - telemetry.Context.User.AuthenticatedUserId = username; - } - } - } -} From 10854a47e04eb65435b93b8049cfb32c3256e1a6 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Fri, 26 Jan 2024 14:40:41 -0800 Subject: [PATCH 2/2] Update examples/EvaluationDataToApplicationInsights/README.md --- examples/EvaluationDataToApplicationInsights/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/EvaluationDataToApplicationInsights/README.md b/examples/EvaluationDataToApplicationInsights/README.md index 0458ba93..5c64276d 100644 --- a/examples/EvaluationDataToApplicationInsights/README.md +++ b/examples/EvaluationDataToApplicationInsights/README.md @@ -49,7 +49,7 @@ These cookies are used to correlate telemetry from the browser with telemetry fr ### Targeting Id In order to connect evaluation events with other metrics from the user, a targeting id needs to be emitted. This can be done multiple ways, but the recommended way is to define a telemetry initializer. This initializer allows the app to modify all telemetry going to Application Insights before it's sent. -This example uses the provided `TargetingHttpContextMiddleware` and `TargetingTelemetryInitializer`. The middleware adds `TargetingId` (using the targeting context accessor) to the HTTP Context as a request comes in- while the initializer checks for the `TargetingId` on the HTTP Context, and if it exists, adds `TargetingId` to all outgoing Application Insights Telemetry. +This example uses the provided `TargetingHttpContextMiddleware` and `TargetingTelemetryInitializer`. The middleware adds `TargetingId` (using the targeting context accessor) to the HTTP Context as a request comes in. The initializer checks for the `TargetingId` on the HTTP Context, and if it exists, adds `TargetingId` to all outgoing Application Insights Telemetry. ## Sample App Usage Sample steps to try out the app: