Skip to content

Commit 05c321c

Browse files
update telemetry
1 parent d175ca2 commit 05c321c

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

articles/azure-app-configuration/feature-management-dotnet-reference.md

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ When a feature flag change is deployed, it's often important to analyze its effe
13291329
* Which variant is a particular user seeing?
13301330
13311331
1332-
These types of questions can be answered through the emission and analysis of feature flag evaluation events. This library supports emitting these events through telemetry publishers. One or many telemetry publishers can be registered to publish events whenever feature flags are evaluated.
1332+
These types of questions can be answered through the emission and analysis of feature flag evaluation events. This library uses [`System.Diagnostics.Activity`](https://learn.microsoft.com/dotnet/api/system.diagnostics.activity) API to produce tracing telemetry during feature flag evaluation.
13331333
13341334
### Enabling Telemetry
13351335
@@ -1360,41 +1360,54 @@ The `telemetry` section of a feature flag has the following properties:
13601360
| Property | Description |
13611361
| ---------------- | ---------------- |
13621362
| `enabled` | Specifies whether telemetry should be published for the feature flag. |
1363-
| `metadata` | A collection of key-value pairs, modeled as a dictionary, that can be used to attach custom metadata about the feature flag to evaluation events. |
1363+
| `metadata` | A collection of key-value pairs, modeled as a dictionary, that can be used to attach custom metadata about the feature flag to evaluation events.
13641364

1365-
### Custom Telemetry Publishers
1365+
### Custom Telemetry Publishing
13661366

1367-
Custom handling of feature flag telemetry is made possible by implementing an `ITelemetryPublisher` and registering it in the feature manager. Whenever a feature flag that has telemetry enabled is evaluated, the registered telemetry publisher gets a chance to publish the corresponding evaluation event.
1367+
The feature manager has its own `ActivitySource` with name "Microsoft.FeatureManagement". If `telemetry` is enabled for a feature flag, whenever the evaluation of this feature flag is started, the feature manager will start an `Activity`. When the feature flag evaluation is finished, the feature manager will add an `ActivityEvent` called "FeatureFlag" to the `Activity.Current`. The "FeatureFlag" event will have tags which include the information about the feature flag evaluation.
1368+
1369+
To enable custom telemetry publishing, you should create an [`ActivityListener`](https://learn.microsoft.com/dotnet/api/system.diagnostics.activitylistener) and listen to `Microsoft.FeatureManagement` activity source. Here is an example showing how to listen to the feature management activity source and add a callback when feature evaluation is done.
13681370

13691371
``` C#
1370-
public interface ITelemetryPublisher
1372+
ActivitySource.AddActivityListener(new ActivityListener()
13711373
{
1372-
ValueTask PublishEvent(EvaluationEvent evaluationEvent, CancellationToken cancellationToken);
1373-
}
1374+
ShouldListenTo = (activitySource) => activitySource.Name == "Microsoft.FeatureManagement",
1375+
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
1376+
ActivityStopped = (activity) =>
1377+
{
1378+
ActivityEvent? evaluationEvent = activity.Events.FirstOrDefault((activityEvent) => activityEvent.Name == "FeatureFlag");
1379+
1380+
if (evaluationEvent.HasValue && evaluationEvent.Value.Tags.Any())
1381+
{
1382+
// Do something.
1383+
}
1384+
}
1385+
});
13741386
```
13751387

1376-
The `EvaluationEvent` type can be found [here](https://github.com/microsoft/FeatureManagement-Dotnet/blob/preview/src/Microsoft.FeatureManagement/Telemetry/EvaluationEvent.cs) for reference.
1388+
For more information, please go to [Collect a distributed trace](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing-collection-walkthroughs).
1389+
1390+
### Application Insights Telemetry Publisher
13771391

1378-
Registering telemetry publishers is done when calling `AddFeatureManagement()`. Here's an example setting up feature management to emit telemetry with an implementation of `ITelemetryPublisher` called `MyTelemetryPublisher`.
1392+
The `Microsoft.FeatureManagement.Telemetry.ApplicationInsights` package provides a built-in telemetry publisher that sends feature flag evaluation data to [Application Insights](/azure/azure-monitor/app/app-insights-overview). To take advantage of this, add a reference to the package and register the Application Insights telemetry publisher as shown below.
13791393

13801394
``` C#
13811395
builder.services
13821396
.AddFeatureManagement()
1383-
.AddTelemetryPublisher<MyTelemetryPublisher>();
1397+
.AddApplicationInsightsTelemetryPublisher();
13841398
```
13851399

1386-
### Application Insights Telemetry Publisher
1387-
1388-
The `Microsoft.FeatureManagement.Telemetry.ApplicationInsights` package provides a built-in telemetry publisher implementation that sends feature flag evaluation data to [Application Insights](/azure/azure-monitor/app/app-insights-overview). To take advantage of this, add a reference to the package and register the Application Insights telemetry publisher as shown below.
1400+
The `Microsoft.FeatureManagement.Telemetry.ApplicationInsights` package provides the [`TargetingTelemetryInitializer`](https://github.com/microsoft/FeatureManagement-Dotnet/blob/preview/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TargetingTelemetryInitializer.cs) which implements the [ITelemetryInitializer](https://learn.microsoft.com/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer). The `TargetingTelemetryInitializer` will extract targeting information from current activity's baggage and add it to Application Insights telemetry properties.
13891401

13901402
``` C#
1391-
builder.services
1392-
.AddFeatureManagement()
1393-
.AddTelemetryPublisher<ApplicationInsightsTelemetryPublisher>();
1403+
builder.Services.AddSingleton<ITelemetryInitializer, TargetingTelemetryInitializer>();
13941404
```
13951405

1396-
> [!NOTE]
1397-
> The base `Microsoft.FeatureManagement` package doesn't include this telemetry publisher.
1406+
To enable persistance of targeting context in the current activity, you can use the [`TargetingHttpContextMiddleware`](https://github.com/microsoft/FeatureManagement-Dotnet/blob/preview/src/Microsoft.FeatureManagement.AspNetCore/TargetingHttpContextMiddleware.cs).
1407+
1408+
``` C#
1409+
app.UseMiddleware<TargetingHttpContextMiddleware>();
1410+
```
13981411

13991412
An example of its usage can be found in the [EvaluationDataToApplicationInsights](https://github.com/microsoft/FeatureManagement-Dotnet/tree/preview/examples/EvaluationDataToApplicationInsights) example.
14001413

0 commit comments

Comments
 (0)