Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanliang-ms committed Jan 18, 2024
2 parents 176cf78 + 54cb358 commit b28615c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.FeatureManagement.FeatureFilters;

namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights
{
/// <summary>
/// Provides extension methods for tracking events with <see cref="TargetingContext"/>.
/// </summary>
public static class TelemetryClientExtensions
{
/// <summary>
/// Extension method to track an event with <see cref="TargetingContext"/>.
/// </summary>
public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null)
{
ValidateTargetingContext(targetingContext);

if (properties == null)
{
properties = new Dictionary<string, string>();
}

properties["TargetingId"] = targetingContext.UserId;

telemetryClient.TrackEvent(eventName, properties, metrics);
}

/// <summary>
/// Extension method to track an <see cref="EventTelemetry"/> with <see cref="TargetingContext"/>.
/// </summary>
public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext)
{
ValidateTargetingContext(targetingContext);

if (telemetry == null)
{
telemetry = new EventTelemetry();
}

telemetry.Properties["TargetingId"] = targetingContext.UserId;

telemetryClient.TrackEvent(telemetry);
}

private static void ValidateTargetingContext(TargetingContext targetingContext)
{
if (targetingContext == null)
{
throw new ArgumentNullException(nameof(targetingContext));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ public static class FeatureManagementBuilderExtensions
return builder;
}

/// <summary>
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
/// </summary>
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
{
//
// Register the targeting context accessor with the same lifetime as the feature manager
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
{
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
}
else
{
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
}

builder.AddFeatureFilter<TargetingFilter>();

return builder;
}

/// <summary>
/// Adds a telemetry publisher to the feature management system.
/// </summary>
Expand All @@ -69,28 +92,5 @@ private static IFeatureManagementBuilder AddTelemetryPublisher(this IFeatureMana

return builder;
}

/// <summary>
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
/// </summary>
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
{
//
// Register the targeting context accessor with the same lifetime as the feature manager
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
{
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
}
else
{
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
}

builder.AddFeatureFilter<TargetingFilter>();

return builder;
}
}
}

0 comments on commit b28615c

Please sign in to comment.