-
Notifications
You must be signed in to change notification settings - Fork 127
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 1 commit
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 <script>document.write(getCookie("username"));</script></h1> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given this is a razor page, I'd recommend putting the username into the view data and using razor syntax to inject it into the DOM. |
||
| <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> | ||
| </div> | ||
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
| 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; | ||
|
|
||
| public IndexModel(ILogger<IndexModel> logger, IFeatureManager featureManager) | ||
| { | ||
| _logger = logger; | ||
| _featureManager = featureManager; | ||
| } | ||
|
|
||
| public IActionResult OnGet() | ||
| { | ||
| return Page(); | ||
| } | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
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,95 @@ | ||
| <!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> | ||
| function getCookie(cname) { | ||
| let name = cname + "="; | ||
| let decodedCookie = decodeURIComponent(document.cookie); | ||
| let ca = decodedCookie.split(';'); | ||
| for (let i = 0; i < ca.length; i++) { | ||
| let c = ca[i]; | ||
| while (c.charAt(0) == ' ') { | ||
| c = c.substring(1); | ||
| } | ||
| if (c.indexOf(name) == 0) { | ||
| return c.substring(name.length, c.length); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| function deleteCookie(name, path, domain) { | ||
| if (getCookie(name)) { | ||
| document.cookie = name + "=" + | ||
| ((path) ? ";path=" + path : "") + | ||
| ((domain) ? ";domain=" + domain : "") + | ||
| ";expires=Thu, 01 Jan 1970 00:00:01 GMT"; | ||
| } | ||
| } | ||
|
|
||
| appInsights.setAuthenticatedUserContext(getCookie("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.
|
||
| <li class="nav-item"> | ||
| <a class="nav-link text-dark" asp-area="" asp-page="/Home">Feature A</a> | ||
| </li> | ||
| </feature> | ||
| <feature name="FeatureB"> | ||
| <li class="nav-item"> | ||
| <a class="nav-link text-dark" asp-area="" asp-page="/Home">Feature B</a> | ||
| </li> | ||
| </feature> | ||
| <li style="display: flex;"> | ||
| <button onclick="document.cookie = ' username=' + Math.floor(Math.random() * 100000);deleteCookie('ai_user');deleteCookie('ai_session');window.location=window.location.origin;"> | ||
|
rossgrambo marked this conversation as resolved.
Outdated
|
||
| Randomize User | ||
| </button> | ||
| </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.