Breyta

Deila með


Enable telemetry for feature flags in an ASP.NET Core application

In this tutorial, you use telemetry in your ASP.NET Core application to track feature flag evaluations and custom events. Telemetry allows you to make informed decisions about your feature management strategy. You utilize the feature flag with telemetry enabled created in the overview for enabling telemetry for feature flags. Before proceeding, ensure that you create a feature flag named Greeting in your Configuration store with telemetry enabled. This tutorial builds on top of the tutorial for using variant feature flags in an ASP.NET Core application.

Prerequisites

Add telemetry to your ASP.NET Core application

  1. Add the latest version of the Application Insights package.

    dotnet add package Microsoft.ApplicationInsights.AspNetCore
    dotnet add package Microsoft.FeatureManagement.Telemetry.ApplicationInsights
    
  2. Add Application Insights telemetry services to the container. Add the following code after the line that adds Azure App Configuration.

    // Add Application Insights telemetry
    builder.Services.AddApplicationInsightsTelemetry(options =>
    {
        options.ConnectionString = builder.Configuration.GetConnectionString("ApplicationInsights");
    });
    
  3. Update the feature management services to enable telemetry publishing. Modify the existing feature management configuration to include the AddApplicationInsightsTelemetry() call.

    // Add Azure App Configuration and feature management services to the container.
    builder.Services.AddAzureAppConfiguration()
        .AddFeatureManagement()
        .WithTargeting()
        .AddApplicationInsightsTelemetry();
    
  4. Add the targeting middleware to associate outgoing events with the targeting ID. Add the following code after the line app.UseAzureAppConfiguration();.

    // Use targeting middleware to associate events with the targeting ID
    app.UseMiddleware<TargetingHttpContextMiddleware>();
    

Track custom events

Now you'll add a custom event to track when users like a quote. You'll create a backend endpoint to handle the like action and track it using Application Insights.

  1. In Index.cshtml.cs, add a using statement for Application Insights.

    using Microsoft.ApplicationInsights;
    
  2. Adjust the IndexModel to get a TelemetryClient.

    public class IndexModel(
            TelemetryClient telemetry,
            ILogger<IndexModel> logger,
            IVariantFeatureManagerSnapshot featureManager
        ) : PageModel
    {
        private readonly TelemetryClient _telemetry = telemetry;
    
  3. Additionally, add a OnPostLike() function, tracking an event called "Liked" when the endpoint is used.

    public IActionResult OnPostLike()
    {
        _telemetry.TrackEvent("Liked");
        return new JsonResult(new { success = true });
    }
    
  4. In Index.cshtml, add an antiforgerytoken before the script tag.

    <form method="post" style="display:none;">
        @Html.AntiForgeryToken()
    </form>
    
    <script>
        ...
    
  5. Adjust the heartClicked function with the following content.

    async function heartClicked(button) {
        const response = await fetch(`?handler=Like`, { method: "POST" });
        if (response.ok) {
            const data = await response.json();
            if (data.success) {
                var icon = button.querySelector('i');
                icon.classList.toggle('far');
                icon.classList.toggle('fas');
            }
        }
    }
    

    The OnPostLike method handles the like action. It uses the TelemetryClient to track a custom "Liked" event when a user clicks the heart button.

Build and run the app

  1. Application Insights requires a connection string to connect to your Application Insights resource. Set the ConnectionStrings:ApplicationInsights as a user secret.

    dotnet user-secrets set ConnectionStrings:ApplicationInsights "<your-Application-Insights-Connection-String>"
    
  2. After setting the environment variable, restart your terminal and rebuild and run the application.

    dotnet build
    dotnet run
    

Collect telemetry

Deploy your application to begin collecting telemetry from your users. To test its functionality, you can simulate user activity by creating many test users. Each user will experience one of the greeting message variants, and they can interact with the application by clicking the heart button to like a quote. As your user base grows, you can monitor the increasing volume of telemetry data collected in Azure App Configuration. Additionally, you can drill down into the data to analyze how each variant of the feature flag influences user behavior.

Additional resources