-
Notifications
You must be signed in to change notification settings - Fork 119
Adds example for Application Insights Publisher #284
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 4 commits into
preview
from
rossgrambo/telemetry-application-insights-example
Nov 15, 2023
Merged
Changes from all commits
Commits
Show all changes
4 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 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> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" /> | ||
| </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 | ||
| TargetingContext targetingContext = new TargetingContext | ||
| { | ||
| UserId = username, | ||
| Groups = groups | ||
| }; | ||
|
|
||
| // | ||
| // Cache for subsequent lookup | ||
| httpContext.Items[TargetingContextLookup] = targetingContext; | ||
|
|
||
| return new ValueTask<TargetingContext>(targetingContext); | ||
| } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
examples/EvaluationDataToApplicationInsights/Middlewares/MyTelemetryInitializer.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,35 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using Microsoft.ApplicationInsights.Channel; | ||
| using Microsoft.ApplicationInsights.Extensibility; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Middlewares | ||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } |
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> | ||
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| public class CheckoutModel : PageModel | ||
| { | ||
| private readonly ILogger<CheckoutModel> _logger; | ||
|
|
||
| public CheckoutModel(ILogger<CheckoutModel> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
| } | ||
| } |
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; } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
| @page | ||
| @model IndexModel | ||
| @{ | ||
| ViewData["Title"] = "Home page"; | ||
| } | ||
|
|
||
| <div class="text-center"> | ||
| <h1 class="display-4">Welcome @Model.Username</h1> | ||
| <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> | ||
| </div> |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
| using Microsoft.AspNetCore.Hosting.Server; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using Microsoft.FeatureManagement; | ||
|
|
||
| namespace EvaluationDataToApplicationInsights.Pages | ||
| { | ||
| public class IndexModel : PageModel | ||
| { | ||
| private readonly ILogger<IndexModel> _logger; | ||
| private readonly IFeatureManager _featureManager; | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
|
||
| public string Username { get; set; } | ||
|
|
||
| public IndexModel(ILogger<IndexModel> logger, IFeatureManager featureManager, IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| _logger = logger; | ||
| _featureManager = featureManager; | ||
| _httpContextAccessor = httpContextAccessor; | ||
| } | ||
|
|
||
| public IActionResult OnGet() | ||
| { | ||
| Username = _httpContextAccessor.HttpContext.Request.Cookies["username"]; | ||
|
|
||
| return Page(); | ||
| } | ||
| } | ||
| } |
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 | ||
| @{ | ||
| } |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using System; | ||
|
|
||
| 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"); | ||
| } | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.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,78 @@ | ||
| @using System.Security.Claims | ||
| @using Microsoft.AspNetCore.Http | ||
|
|
||
| @inject IHttpContextAccessor httpContextAccessor | ||
| @{ | ||
| string username = httpContextAccessor.HttpContext.Request.Cookies["username"]; | ||
| } | ||
|
|
||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>@ViewData["Title"] - EvaluationDataToApplicationInsights</title> | ||
| <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> | ||
| <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> | ||
| <link rel="stylesheet" href="~/EvaluationDataToApplicationInsights.styles.css" asp-append-version="true" /> | ||
| @Html.Raw(JavaScriptSnippet.FullScript) | ||
|
|
||
| <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> | ||
| <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> | ||
| <div class="container"> | ||
| <a class="navbar-brand" asp-area="" asp-page="/Index">EvaluationDataToApplicationInsights</a> | ||
| <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" | ||
| aria-expanded="false" aria-label="Toggle navigation"> | ||
| <span class="navbar-toggler-icon"></span> | ||
| </button> | ||
| <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> | ||
| <ul class="navbar-nav flex-grow-1"> | ||
| <li class="nav-item"> | ||
| <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> | ||
| </li> | ||
| <li class="nav-item"> | ||
| <a class="nav-link text-dark" asp-area="" asp-page="/Checkout">Checkout</a> | ||
| </li> | ||
| <feature name="FeatureA"> | ||
rossgrambo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <li class="nav-item"> | ||
| <span class="nav-link text-muted">Feature A</span> | ||
| </li> | ||
| </feature> | ||
| <feature name="FeatureB"> | ||
| <li class="nav-item"> | ||
| <span class="nav-link text-muted">Feature B</span> | ||
| </li> | ||
| </feature> | ||
| <li style="display: flex;"> | ||
| <form method="get" asp-page="/RandomizeUser"> | ||
| <button style="height:100%" type="submit">Randomize User</button> | ||
| </form> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| </div> | ||
| </nav> | ||
| </header> | ||
| <div class="container"> | ||
| <main role="main" class="pb-3"> | ||
| @RenderBody() | ||
| </main> | ||
| </div> | ||
|
|
||
| <footer class="border-top footer text-muted"> | ||
| <div class="container"> | ||
| © 2023 - EvaluationDataToApplicationInsights - <a asp-area="" asp-page="/Checkout">Checkout</a> | ||
| </div> | ||
| </footer> | ||
|
|
||
| @await RenderSectionAsync("Scripts", required: false) | ||
| </body> | ||
| </html> | ||
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.