Skip to content

Adjusts example to use middleware and initializer for targeting id #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.AspNetCore\Microsoft.FeatureManagement.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public ValueTask<TargetingContext> GetContextAsync()
return new ValueTask<TargetingContext>((TargetingContext)value);
}

//
// Grab username from cookie
string username = httpContext.Request.Cookies["username"];

var groups = new List<string>();
Expand Down
12 changes: 10 additions & 2 deletions examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

<p>Click Below To Check Out!</p>

<button id="checkout" name="checkout" onclick="appInsights.trackEvent({ name: 'checkout', properties: { 'success' : 'yes' } }); appInsights.trackMetric({ name: 'checkoutAmount', average: Math.floor(Math.random() * 100) }); alert('Checked Out!');">
<script>
function checkOut() {
appInsights.trackEvent({ name: 'checkout', properties: { 'success': 'yes' } });
appInsights.trackMetric({ name: 'checkoutAmount', average: Math.floor(Math.random() * 100) });
alert('Checked Out!');
}
</script>

<button id="checkout" name="checkout" onclick="checkOut()">
Check Out
</button>
</button>
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script>
appInsights.setAuthenticatedUserContext(@username);
</script>
</head>
<body>
<header>
Expand Down
10 changes: 7 additions & 3 deletions examples/EvaluationDataToApplicationInsights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -20,8 +20,8 @@
builder.Services.AddApplicationInsightsTelemetry();

//
// App Insights User Tagging
builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
// App Insights TargetingId Tagging
builder.Services.AddSingleton<ITelemetryInitializer, TargetingTelemetryInitializer>();

//
// Enter feature management
Expand Down Expand Up @@ -55,4 +55,8 @@

app.MapRazorPages();

//
// Adds Targeting Id to HttpContext
app.UseMiddleware<TargetingHttpContextMiddleware>();

app.Run();
19 changes: 3 additions & 16 deletions examples/EvaluationDataToApplicationInsights/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITelemetryInitializer, MyTelemetryInitializer>();
```

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. 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:
Expand Down

This file was deleted.