From 0a42c2c09ddef5b01ad11ae84f59b924d63e3019 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Mon, 8 Jan 2024 17:50:11 -0800 Subject: [PATCH 1/5] Adds extension methods for TrackEvent and TrackMetric --- .../ApplicationInsightsTelemetryExtensions.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs new file mode 100644 index 00000000..3dbc81c1 --- /dev/null +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs @@ -0,0 +1,72 @@ +using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.FeatureManagement.FeatureFilters; + +namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights +{ + /// + /// Provides extension methods for tracking events with TargetingContext. + /// + public static class ApplicationInsightsTelemetryExtensions + { + /// + /// Extension method to track an event with TargetingContext. + /// + public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary properties = null, IDictionary metrics = null) + { + if (properties == null) + { + properties = new Dictionary(); + } + + properties.Add("TargetingId", targetingContext.UserId); + + telemetryClient.TrackEvent(eventName, properties, metrics); + } + + /// + /// Extension method to track an EventTelemetry with TargetingContext. + /// + public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext) + { + if (telemetry == null) + { + telemetry = new EventTelemetry(); + } + + telemetry.Properties["TargetingId"] = targetingContext.UserId; + + telemetryClient.TrackEvent(telemetry); + } + + /// + /// Extension method to track a metric with TargetingContext. + /// + public static void TrackMetric(this TelemetryClient telemetryClient, string name, double value, TargetingContext targetingContext, IDictionary properties = null) + { + if (properties == null) + { + properties = new Dictionary(); + } + + properties.Add("TargetingId", targetingContext.UserId); + + telemetryClient.TrackMetric(name, value, properties); + } + + /// + /// Extension method to track a MetricTelemetry with TargetingContext. + /// + public static void TrackMetric(this TelemetryClient telemetryClient, MetricTelemetry telemetry, TargetingContext targetingContext) + { + if (telemetry == null) + { + telemetry = new MetricTelemetry(); + } + + telemetry.Properties["TargetingId"] = targetingContext.UserId; + + telemetryClient.TrackMetric(telemetry); + } + } +} From f9c30645dfa203d2f2b891947504f1d8a4679bf2 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Tue, 9 Jan 2024 15:01:55 -0800 Subject: [PATCH 2/5] Adds copyright, crefs, and uses []= instead of .Add for properties --- .../ApplicationInsightsTelemetryExtensions.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs index 3dbc81c1..2a4ef172 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs @@ -1,4 +1,7 @@ -using Microsoft.ApplicationInsights; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. +// +using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.FeatureManagement.FeatureFilters; @@ -10,7 +13,7 @@ namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights public static class ApplicationInsightsTelemetryExtensions { /// - /// Extension method to track an event with TargetingContext. + /// Extension method to track an event with . /// public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary properties = null, IDictionary metrics = null) { @@ -19,13 +22,13 @@ public static void TrackEvent(this TelemetryClient telemetryClient, string event properties = new Dictionary(); } - properties.Add("TargetingId", targetingContext.UserId); + properties["TargetingId"] = targetingContext.UserId; telemetryClient.TrackEvent(eventName, properties, metrics); } /// - /// Extension method to track an EventTelemetry with TargetingContext. + /// Extension method to track an with . /// public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext) { @@ -40,7 +43,7 @@ public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemet } /// - /// Extension method to track a metric with TargetingContext. + /// Extension method to track a metric with . /// public static void TrackMetric(this TelemetryClient telemetryClient, string name, double value, TargetingContext targetingContext, IDictionary properties = null) { @@ -49,13 +52,13 @@ public static void TrackMetric(this TelemetryClient telemetryClient, string name properties = new Dictionary(); } - properties.Add("TargetingId", targetingContext.UserId); + properties["TargetingId"] = targetingContext.UserId; telemetryClient.TrackMetric(name, value, properties); } /// - /// Extension method to track a MetricTelemetry with TargetingContext. + /// Extension method to track a with . /// public static void TrackMetric(this TelemetryClient telemetryClient, MetricTelemetry telemetry, TargetingContext targetingContext) { From c76dba59cedc01e263d49d3af14079bce6f904c2 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Tue, 9 Jan 2024 15:23:36 -0800 Subject: [PATCH 3/5] Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs Co-authored-by: Jimmy Campbell --- .../ApplicationInsightsTelemetryExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs index 2a4ef172..d174e79f 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs @@ -10,7 +10,7 @@ namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights /// /// Provides extension methods for tracking events with TargetingContext. /// - public static class ApplicationInsightsTelemetryExtensions + public static class TelemetryClientExtensions { /// /// Extension method to track an event with . From 8560fb955d25053530e9b480f0a224ad3604d660 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Tue, 9 Jan 2024 15:49:52 -0800 Subject: [PATCH 4/5] Added null check, moved shared logic to helper functions --- ...nsions.cs => TelemetryClientExtensions.cs} | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) rename src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/{ApplicationInsightsTelemetryExtensions.cs => TelemetryClientExtensions.cs} (73%) diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs similarity index 73% rename from src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs rename to src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs index d174e79f..ee17d85d 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs @@ -17,12 +17,14 @@ public static class TelemetryClientExtensions /// public static void TrackEvent(this TelemetryClient telemetryClient, string eventName, TargetingContext targetingContext, IDictionary properties = null, IDictionary metrics = null) { + ValidateTargetingContext(targetingContext); + if (properties == null) { properties = new Dictionary(); } - properties["TargetingId"] = targetingContext.UserId; + AddTargetingProperties(properties, targetingContext); telemetryClient.TrackEvent(eventName, properties, metrics); } @@ -32,12 +34,14 @@ public static void TrackEvent(this TelemetryClient telemetryClient, string event /// public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemetry telemetry, TargetingContext targetingContext) { + ValidateTargetingContext(targetingContext); + if (telemetry == null) { telemetry = new EventTelemetry(); } - telemetry.Properties["TargetingId"] = targetingContext.UserId; + AddTargetingProperties(telemetry.Properties, targetingContext); telemetryClient.TrackEvent(telemetry); } @@ -47,12 +51,14 @@ public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemet /// public static void TrackMetric(this TelemetryClient telemetryClient, string name, double value, TargetingContext targetingContext, IDictionary properties = null) { + ValidateTargetingContext(targetingContext); + if (properties == null) { properties = new Dictionary(); } - properties["TargetingId"] = targetingContext.UserId; + AddTargetingProperties(properties, targetingContext); telemetryClient.TrackMetric(name, value, properties); } @@ -62,14 +68,29 @@ public static void TrackMetric(this TelemetryClient telemetryClient, string name /// public static void TrackMetric(this TelemetryClient telemetryClient, MetricTelemetry telemetry, TargetingContext targetingContext) { + ValidateTargetingContext(targetingContext); + if (telemetry == null) { telemetry = new MetricTelemetry(); } - telemetry.Properties["TargetingId"] = targetingContext.UserId; + AddTargetingProperties(telemetry.Properties, targetingContext); telemetryClient.TrackMetric(telemetry); } + + private static void ValidateTargetingContext(TargetingContext targetingContext) + { + if (targetingContext == null) + { + throw new ArgumentNullException(nameof(targetingContext)); + } + } + + private static void AddTargetingProperties(IDictionary properties, TargetingContext targetingContext) + { + properties["TargetingId"] = targetingContext.UserId; + } } } From 0168adf77ef7f7cbc129da3fd67b7c68e0a8b28a Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Thu, 11 Jan 2024 13:12:17 -0800 Subject: [PATCH 5/5] Removed TrackMetric extension for now --- .../TelemetryClientExtensions.cs | 43 +------------------ 1 file changed, 2 insertions(+), 41 deletions(-) diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs index ee17d85d..44d68ddb 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/TelemetryClientExtensions.cs @@ -24,7 +24,7 @@ public static void TrackEvent(this TelemetryClient telemetryClient, string event properties = new Dictionary(); } - AddTargetingProperties(properties, targetingContext); + properties["TargetingId"] = targetingContext.UserId; telemetryClient.TrackEvent(eventName, properties, metrics); } @@ -41,45 +41,11 @@ public static void TrackEvent(this TelemetryClient telemetryClient, EventTelemet telemetry = new EventTelemetry(); } - AddTargetingProperties(telemetry.Properties, targetingContext); + telemetry.Properties["TargetingId"] = targetingContext.UserId; telemetryClient.TrackEvent(telemetry); } - /// - /// Extension method to track a metric with . - /// - public static void TrackMetric(this TelemetryClient telemetryClient, string name, double value, TargetingContext targetingContext, IDictionary properties = null) - { - ValidateTargetingContext(targetingContext); - - if (properties == null) - { - properties = new Dictionary(); - } - - AddTargetingProperties(properties, targetingContext); - - telemetryClient.TrackMetric(name, value, properties); - } - - /// - /// Extension method to track a with . - /// - public static void TrackMetric(this TelemetryClient telemetryClient, MetricTelemetry telemetry, TargetingContext targetingContext) - { - ValidateTargetingContext(targetingContext); - - if (telemetry == null) - { - telemetry = new MetricTelemetry(); - } - - AddTargetingProperties(telemetry.Properties, targetingContext); - - telemetryClient.TrackMetric(telemetry); - } - private static void ValidateTargetingContext(TargetingContext targetingContext) { if (targetingContext == null) @@ -87,10 +53,5 @@ private static void ValidateTargetingContext(TargetingContext targetingContext) throw new ArgumentNullException(nameof(targetingContext)); } } - - private static void AddTargetingProperties(IDictionary properties, TargetingContext targetingContext) - { - properties["TargetingId"] = targetingContext.UserId; - } } }