This sample shows how to send feature flag evaluation data to an OpenTelemetry-compatible
backend using Microsoft.FeatureManagement.Telemetry.OpenTelemetry, exported to Azure Monitor
via the Azure Monitor OpenTelemetry Distro
(Azure.Monitor.OpenTelemetry.AspNetCore). Evaluation data is emitted each time a feature or
variant is evaluated with telemetry enabled.
This package is published alongside Microsoft.FeatureManagement.Telemetry.ApplicationInsights
(see the VariantAndTelemetryDemo sample) so that applications not
yet ready to move off the Application Insights SDK aren't forced to. New applications are
encouraged to use Microsoft.FeatureManagement.Telemetry.OpenTelemetry, the industry-standard,
vendor-neutral observability framework.
To run this sample, follow these steps:
- Set up a new Application Insights resource in Azure,
and from
Overviewcopy theConnection String. - Place the connection string in
appsettings.jsonatAPPLICATIONINSIGHTS_CONNECTION_STRING(or set it as an environment variable of the same name instead). - Set the project as the startup project (or
cdinto this directory). - Run the project (
dotnet run) and browse to the app. - Head to the Application Insights resource in the Azure Portal to see the emitted telemetry (logs, traces, and metrics).
A connection string is required for telemetry to be exported. Without one, the app still runs and requests succeed, but
UseAzureMonitor()is skipped and nothing is exported.
This app uses Microsoft.FeatureManagement.Telemetry.OpenTelemetry alongside the
OpenTelemetry .NET SDK and the Azure
Monitor OpenTelemetry Distro to export logs, traces, and metrics. See Program.cs for how
tracing/logging/metrics and Azure Monitor are wired up via UseAzureMonitor().
builder.Services.AddFeatureManagement()
.WithTargeting();
var telemetry = builder.Services.AddOpenTelemetry();
telemetry.AddFeatureManagementProcessors();
if (!string.IsNullOrEmpty(connectionString))
{
telemetry.UseAzureMonitor(o => o.ConnectionString = connectionString);
}Register targeting processors before exporters: call AddFeatureManagementProcessors()
before UseAzureMonitor(). This ensures TargetingId is added to spans and logs before
they are exported.
In order to connect evaluation events with other telemetry from the user, a targeting id needs
to be emitted. This sample uses the provided TargetingHttpContextMiddleware, which reads the
targeting context and adds TargetingId to both the HttpContext and the current Activity's
baggage as a request comes in. TargetingActivityProcessor and TargetingLogProcessor
(automatically registered by AddFeatureManagementProcessors()) then read that baggage
to enrich spans and logs respectively.
Sample steps to try out the app:
- Run the app. When the app is first started a user id will be generated and stored in an
authentication cookie (see
RandomizeUser). - When the page is loaded, the
ImageRatingfeature is evaluated, which defines three variants, emitting aFeatureEvaluationcustom event and trace. - Select a rating for the loaded image and click vote. A
Votecustom event and anImageRatingmetric will be emitted. - Go to Checkout and click "Check Out", which emits a
checkoutcustom event and acheckoutAmountmetric. - Head to the Application Insights resource in the Azure Portal.
- Try going to Logs > New Query and run the query
customEvents. This should show the custom events emitted, each carrying aTargetingIdproperty. - Try going to Metrics. Under Metric find Custom >
ImageRatingandcheckoutAmount. - From the Metrics window, out-of-the-box metrics like Page Views and Server Requests can be
viewed thanks to the ASP.NET Core request instrumentation
UseAzureMonitor()adds automatically.
- Try going to Logs > New Query and run the query