Skip to content

Commit b28615c

Browse files
resolve conflict
2 parents 176cf78 + 54cb358 commit b28615c

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Microsoft.ApplicationInsights;
5+
using Microsoft.ApplicationInsights.DataContracts;
6+
using Microsoft.FeatureManagement.FeatureFilters;
7+
8+
namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights
9+
{
10+
/// <summary>
11+
/// Provides extension methods for tracking events with <see cref="TargetingContext"/>.
12+
/// </summary>
13+
public static class TelemetryClientExtensions
14+
{
15+
/// <summary>
16+
/// Extension method to track an event with <see cref="TargetingContext"/>.
17+
/// </summary>
18+
public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null)
19+
{
20+
ValidateTargetingContext(targetingContext);
21+
22+
if (properties == null)
23+
{
24+
properties = new Dictionary<string, string>();
25+
}
26+
27+
properties["TargetingId"] = targetingContext.UserId;
28+
29+
telemetryClient.TrackEvent(eventName, properties, metrics);
30+
}
31+
32+
/// <summary>
33+
/// Extension method to track an <see cref="EventTelemetry"/> with <see cref="TargetingContext"/>.
34+
/// </summary>
35+
public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext)
36+
{
37+
ValidateTargetingContext(targetingContext);
38+
39+
if (telemetry == null)
40+
{
41+
telemetry = new EventTelemetry();
42+
}
43+
44+
telemetry.Properties["TargetingId"] = targetingContext.UserId;
45+
46+
telemetryClient.TrackEvent(telemetry);
47+
}
48+
49+
private static void ValidateTargetingContext(TargetingContext targetingContext)
50+
{
51+
if (targetingContext == null)
52+
{
53+
throw new ArgumentNullException(nameof(targetingContext));
54+
}
55+
}
56+
}
57+
}

src/Microsoft.FeatureManagement/FeatureManagementBuilderExtensions.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ public static IFeatureManagementBuilder AddFeaturedService<TService>(this IFeatu
4343
return builder;
4444
}
4545

46+
/// <summary>
47+
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
48+
/// </summary>
49+
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
50+
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
51+
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
52+
{
53+
//
54+
// Register the targeting context accessor with the same lifetime as the feature manager
55+
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
56+
{
57+
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
58+
}
59+
else
60+
{
61+
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
62+
}
63+
64+
builder.AddFeatureFilter<TargetingFilter>();
65+
66+
return builder;
67+
}
68+
4669
/// <summary>
4770
/// Adds a telemetry publisher to the feature management system.
4871
/// </summary>
@@ -69,28 +92,5 @@ private static IFeatureManagementBuilder AddTelemetryPublisher(this IFeatureMana
6992

7093
return builder;
7194
}
72-
73-
/// <summary>
74-
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
75-
/// </summary>
76-
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
77-
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
78-
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
79-
{
80-
//
81-
// Register the targeting context accessor with the same lifetime as the feature manager
82-
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
83-
{
84-
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
85-
}
86-
else
87-
{
88-
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
89-
}
90-
91-
builder.AddFeatureFilter<TargetingFilter>();
92-
93-
return builder;
94-
}
9595
}
9696
}

0 commit comments

Comments
 (0)