microsoft / FeatureManagement-Dotnet

Microsoft.FeatureManagement provides standardized APIs for enabling feature flags within applications. Utilize this library to secure a consistent experience when developing applications that use patterns such as beta access, rollout, dark deployments, and more.
MIT License
1.02k stars 111 forks source link

Telemetry Publishing via Activity & Activity Event #455

Closed rossgrambo closed 2 months ago

rossgrambo commented 2 months ago

Why this PR?

412

We now have our own ActivitySource and add an ActivityEvent on it. The event is essentially the EvaluationEvent that was previously passed to the Publisher.

Why our own ActivitySource instead of using the parent Activity?

We could emit the ActivityEvent onto whatever parent Activity is running. Sadly this approach has a few drawbacks:

  1. Listeners who want this event now have to listen to all activities.
  2. The parent code may not have an activity
  3. If a customer implements slow async logic (like reading targeting context from a db), it’s no longer grouped within FeatureManagement.

Visible Changes

  1. Adding publishing uses a different method:
    builder.Services.AddFeatureManagement()
    .WithTargeting<HttpContextTargetingContextAccessor>()
    .AddTelemetryPublisher<ApplicationInsightsTelemetryPublisher>();

Becomes

builder.Services.AddFeatureManagement()
    .WithTargeting<HttpContextTargetingContextAccessor>()
    .AddAppInsightsTelemetryPublisher();
  1. Public types ApplicationInsightsTelemetryPublisher & ITelemetryPublisher have been removed entirely
  2. An Activity and Activity Event are available to listen to. Here's the schemas at a glance:
    Activity: {
    Name: "Microsoft.FeatureManagement",
    Version: "1.0.0"
    }
ActivityEvent: {
  Tags: {
    "TargetingId", evaluationEvent.TargetingContext?.UserId,
    "FeatureName", evaluationEvent.FeatureDefinition.Name,
    "Variant", evaluationEvent.Variant?.Name,
    "Enabled", evaluationEvent.Enabled,
    "VariantAssignmentReason", evaluationEvent.VariantAssignmentReason,
    "Version", ActivitySource.Version,
    ... // (Any items added to TelemetryMetadata)
  }
}
jimmyca15 commented 2 months ago

Thanks for the thorough review on this @julealgon !