-
Notifications
You must be signed in to change notification settings - Fork 127
Add example for telemetry publishing. #304
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
jimmyca15
merged 10 commits into
microsoft:preview
from
jimmyca15:user/jimmyca/appInsightsTelemetryExample
Nov 15, 2023
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aa3157e
Adds example library for Application Insights
rossgrambo 9a4370b
Adds readme to example
rossgrambo 886d0a9
Updates readme and persists telemetry metadata by placing it in the c…
rossgrambo 445458d
Simplified javascript and other slight modifications
rossgrambo 2380659
Updates.
jimmyca15 a3d39f3
Merge branch 'preview' into user/jimmyca/appInsightsTelemetryExample
jimmyca15 e71d88a
Updates.
jimmyca15 0f39dda
Improvements.
jimmyca15 b88eb3a
Add missing project in sln file.
jimmyca15 403d370
Remove nullable usage. Other updates.
jimmyca15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="6.1.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Microsoft.FeatureManagement.AspNetCore\Microsoft.FeatureManagement.AspNetCore.csproj" /> | ||
| <ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
51 changes: 51 additions & 0 deletions
51
examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using Microsoft.FeatureManagement.FeatureFilters; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights | ||
| { | ||
| /// <summary> | ||
| /// Provides an implementation of <see cref="ITargetingContextAccessor"/> that creates a targeting context using info from the current HTTP request. | ||
| /// </summary> | ||
| public class HttpContextTargetingContextAccessor : ITargetingContextAccessor | ||
| { | ||
| private const string TargetingContextLookup = "HttpContextTargetingContextAccessor.TargetingContext"; | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
|
||
| public HttpContextTargetingContextAccessor(IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
| } | ||
|
|
||
| public ValueTask<TargetingContext> GetContextAsync() | ||
| { | ||
| HttpContext httpContext = _httpContextAccessor.HttpContext; | ||
|
|
||
| // | ||
| // Try cache lookup | ||
| if (httpContext.Items.TryGetValue(TargetingContextLookup, out object value)) | ||
| { | ||
| return new ValueTask<TargetingContext>((TargetingContext)value); | ||
| } | ||
|
|
||
| string username = httpContext.Request.Cookies["username"]; | ||
|
|
||
| List<string> groups = new List<string>(); | ||
|
|
||
| // | ||
| // Build targeting context based off user info | ||
|
jimmyca15 marked this conversation as resolved.
Outdated
|
||
| TargetingContext targetingContext = new TargetingContext | ||
|
jimmyca15 marked this conversation as resolved.
Outdated
|
||
| { | ||
| UserId = username, | ||
| Groups = groups | ||
| }; | ||
|
|
||
| // | ||
| // Cache for subsequent lookup | ||
| httpContext.Items[TargetingContextLookup] = targetingContext; | ||
|
|
||
| return new ValueTask<TargetingContext>(targetingContext); | ||
| } | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| @page | ||
| @model CheckoutModel | ||
| @{ | ||
| ViewData["Title"] = "Checkout"; | ||
| } | ||
| <h1>@ViewData["Title"]</h1> | ||
|
|
||
| <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!');"> | ||
| Check Out | ||
| </button> |
8 changes: 8 additions & 0 deletions
8
examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| public class CheckoutModel : PageModel | ||
| { | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| @page | ||
| @model ErrorModel | ||
| @{ | ||
| ViewData["Title"] = "Error"; | ||
| } | ||
|
|
||
| <h1 class="text-danger">Error.</h1> | ||
| <h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
|
||
| @if (Model.ShowRequestId) | ||
| { | ||
| <p> | ||
| <strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
| </p> | ||
| } | ||
|
|
||
| <h3>Development Mode</h3> | ||
| <p> | ||
| Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
| </p> | ||
| <p> | ||
| <strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
| It can result in displaying sensitive information from exceptions to end users. | ||
| For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
| and restarting the app. | ||
| </p> |
27 changes: 27 additions & 0 deletions
27
examples/EvaluationDataToApplicationInsights/Pages/Error.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
| [IgnoreAntiforgeryToken] | ||
| public class ErrorModel : PageModel | ||
| { | ||
| public string? RequestId { get; set; } | ||
|
jimmyca15 marked this conversation as resolved.
Outdated
|
||
|
|
||
| public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
|
||
| private readonly ILogger<ErrorModel> _logger; | ||
|
|
||
| public ErrorModel(ILogger<ErrorModel> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public void OnGet() | ||
| { | ||
| RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
| } | ||
| } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| @page | ||
| @model IndexModel | ||
| @{ | ||
| ViewData["Title"] = "Home page"; | ||
| } | ||
|
|
||
| <div class="text-center mb-40"> | ||
| <h1 class="display-4">Welcome, @Model.Username !</h1> | ||
| </div> | ||
|
|
||
| <div class="text-center"> | ||
| <div style="display:inline-block;width:500px;"> | ||
| <img class="mb-40" src="@ViewData["ImageUri"]" /> | ||
| <div class="panel panel-primary"> | ||
| <div class="panel-heading"> | ||
| <h3 class="panel-title"> | ||
| <span class="glyphicon glyphicon-arrow-right"></span>Rate the image on a scale of 1 - 5 !<span class="glyphicon glyphicon-new-window"></span></a> | ||
| </h3> | ||
| </div> | ||
| <form action="/" method="post"> | ||
| <div class="panel-body mb-5"> | ||
| <ul class="list-group"> | ||
| <li class="list-group-item"> | ||
| <div class="radio"> | ||
| <label> | ||
| <input type="radio" name="imageScore" value="5"> | ||
| 5 | ||
| </label> | ||
| </div> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <div class="radio"> | ||
| <label> | ||
| <input type="radio" name="imageScore" value="4"> | ||
| 4 | ||
| </label> | ||
| </div> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <div class="radio"> | ||
| <label> | ||
| <input type="radio" name="imageScore" value="3"> | ||
| 3 | ||
| </label> | ||
| </div> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <div class="radio"> | ||
| <label> | ||
| <input type="radio" name="imageScore" value="2"> | ||
| 2 | ||
| </label> | ||
| </div> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <div class="radio"> | ||
| <label> | ||
| <input type="radio" name="imageScore" value="1"> | ||
| 1 | ||
| </label> | ||
| </div> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| @Html.AntiForgeryToken() | ||
| <div class="panel-footer"> | ||
| <input type="submit" class="btn btn-primary btn-sm" value="Vote" /> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </div> |
67 changes: 67 additions & 0 deletions
67
examples/EvaluationDataToApplicationInsights/Pages/Index.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using Microsoft.ApplicationInsights; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using Microsoft.FeatureManagement; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| public class IndexModel : PageModel | ||
| { | ||
| private readonly IVariantFeatureManager _featureManager; | ||
| private readonly TelemetryClient _telemetry; | ||
|
|
||
| public string? Username { get; set; } | ||
|
jimmyca15 marked this conversation as resolved.
Outdated
|
||
|
|
||
| public IndexModel( | ||
| IVariantFeatureManager featureManager, | ||
| TelemetryClient telemetry) | ||
| { | ||
| _featureManager = featureManager ?? throw new ArgumentNullException(nameof(featureManager)); | ||
| _telemetry = telemetry ?? throw new ArgumentNullException(nameof(telemetry)); | ||
| } | ||
|
|
||
| public async Task<IActionResult> OnGet() | ||
| { | ||
| Username = Request.Cookies["username"]; | ||
|
|
||
| if (string.IsNullOrEmpty(Username)) | ||
| { | ||
| return Redirect("/RandomizeUser"); | ||
| } | ||
|
|
||
| // | ||
| // Use application's feature manager to get assigned variant for current user | ||
| // | ||
|
jimmyca15 marked this conversation as resolved.
Outdated
|
||
| Variant variant = await _featureManager | ||
| .GetVariantAsync("ImageRating", HttpContext.RequestAborted); | ||
|
|
||
| // | ||
| // Set the page's display image based on the assigned variant. | ||
| ViewData["ImageUri"] = variant.Configuration.Value; | ||
|
|
||
| return Page(); | ||
| } | ||
|
|
||
| public IActionResult OnPost() | ||
| { | ||
| if (Request.Form != null) | ||
| { | ||
| string val = Request.Form["imageScore"]; | ||
|
|
||
| if (val != null && | ||
| int.TryParse(val, out int rating)) | ||
| { | ||
| _telemetry.TrackEvent( | ||
| "Vote", | ||
| properties: null, | ||
| metrics: new Dictionary<string, double> | ||
| { | ||
| { "ImageRating", rating } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return Redirect("/RandomizeUser"); | ||
| } | ||
| } | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| @page | ||
| @model EvaluationDataToApplicationInsights.Pages.RandomizeUserModel | ||
| @{ | ||
| } |
18 changes: 18 additions & 0 deletions
18
examples/EvaluationDataToApplicationInsights/Pages/RandomizeUser.cshtml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| public class RandomizeUserModel : PageModel | ||
| { | ||
| public IActionResult OnGet() | ||
| { | ||
| // Clear Application Insights cookies and generate new username | ||
| Response.Cookies.Delete("ai_user"); | ||
| Response.Cookies.Delete("ai_session"); | ||
| Response.Cookies.Append("username", Random.Shared.Next().ToString()); | ||
|
|
||
| return RedirectToPage("/Index"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.