-
Notifications
You must be signed in to change notification settings - Fork 123
Adds more detailed telemetry fields #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ef0bfd4
Working allocationId
rossgrambo 27330d7
Adds Base64Url byte extension, adjusts TelemetryEventHandler logic, a…
rossgrambo ba1676c
Update comments
rossgrambo ada8823
Removed allocation id and fixed some tests
rossgrambo 85cdd0a
Removed bytes extension
rossgrambo f9cb776
Merge branch 'preview' into rossgrambo-extra-telemetry-fields
rossgrambo 9406999
Update src/Microsoft.FeatureManagement/Telemetry/TelemetryEventHandle…
rossgrambo e53aeb0
Resolving comments
rossgrambo a904411
Merge branch 'rossgrambo-extra-telemetry-fields' of https://github.co…
rossgrambo 4cb6a17
Fix formatting
rossgrambo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Working allocationId
- Loading branch information
commit ef0bfd48cce2b0c8528e723e761a9c40c2ad1ec6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/Microsoft.FeatureManagement/Telemetry/TelemetryEventHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Web; | ||
|
|
||
| namespace Microsoft.FeatureManagement.Telemetry | ||
| { | ||
| internal static class TelemetryEventHandler | ||
| { | ||
| private static readonly string EvaluationEventVersion = "1.0.0"; | ||
|
|
||
| /// <summary> | ||
| /// Handles an evaluation event by adding it as an activity event to the current Activity. | ||
| /// </summary> | ||
| /// <param name="evaluationEvent">The <see cref="EvaluationEvent"/> to publish as an <see cref="ActivityEvent"/></param> | ||
| /// <param name="logger">Optional logger to log warnings to</param> | ||
| public static void HandleEvaluationEvent(EvaluationEvent evaluationEvent, ILogger logger) | ||
| { | ||
| Debug.Assert(evaluationEvent != null); | ||
| Debug.Assert(evaluationEvent.FeatureDefinition != null); | ||
rossgrambo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| var tags = new ActivityTagsCollection() | ||
| { | ||
| { "FeatureName", evaluationEvent.FeatureDefinition.Name }, | ||
| { "Enabled", evaluationEvent.Enabled }, | ||
| { "VariantAssignmentReason", evaluationEvent.VariantAssignmentReason }, | ||
| { "Version", EvaluationEventVersion } | ||
| }; | ||
|
|
||
| if (!string.IsNullOrEmpty(evaluationEvent.TargetingContext?.UserId)) | ||
| { | ||
| tags["TargetingId"] = evaluationEvent.TargetingContext.UserId; | ||
| } | ||
|
|
||
| if (!string.IsNullOrEmpty(evaluationEvent.Variant?.Name)) | ||
| { | ||
| tags["Variant"] = evaluationEvent.Variant.Name; | ||
| } | ||
|
|
||
| if (evaluationEvent.FeatureDefinition.Telemetry.Metadata != null) | ||
| { | ||
| foreach (KeyValuePair<string, string> kvp in evaluationEvent.FeatureDefinition.Telemetry.Metadata) | ||
| { | ||
| if (tags.ContainsKey(kvp.Key)) | ||
| { | ||
| logger?.LogWarning($"{kvp.Key} from telemetry metadata will be ignored, as it would override an existing key."); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| tags[kvp.Key] = kvp.Value; | ||
samsadsam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| // VariantAllocationPercentage | ||
| if (evaluationEvent.FeatureDefinition.Allocation?.Percentile != null) | ||
| { | ||
| tags["VariantAssignmentPercentage"] = evaluationEvent.FeatureDefinition.Allocation.Percentile | ||
| .Where(p => p.Variant == evaluationEvent.Variant.Name) | ||
| .Sum(p => p.To - p.From); | ||
| } | ||
|
|
||
| // DefaultWhenEnabled | ||
| if (evaluationEvent.FeatureDefinition.Allocation?.DefaultWhenEnabled != null) | ||
| { | ||
| tags["DefaultWhenEnabled"] = evaluationEvent.FeatureDefinition.Allocation.DefaultWhenEnabled; | ||
| } | ||
|
|
||
| // AllocationId | ||
| string allocationId = GenerateAllocationId(evaluationEvent.FeatureDefinition); | ||
|
|
||
| if (allocationId != null) | ||
| { | ||
| tags["AllocationId"] = allocationId; | ||
| } | ||
|
|
||
| var activityEvent = new ActivityEvent("FeatureFlag", DateTimeOffset.UtcNow, tags); | ||
|
|
||
| Activity.Current.AddEvent(activityEvent); | ||
| } | ||
|
|
||
| private static string GenerateAllocationId(FeatureDefinition featureDefinition) | ||
| { | ||
| StringBuilder inputBuilder = new StringBuilder(); | ||
|
|
||
| // Seed | ||
| inputBuilder.Append($"seed={featureDefinition.Allocation?.Seed ?? ""}"); | ||
|
|
||
| var allocatedVariants = new HashSet<string>(); | ||
|
|
||
| // DefaultWhenEnabled | ||
| if (featureDefinition.Allocation?.DefaultWhenEnabled != null) | ||
| { | ||
| allocatedVariants.Add(featureDefinition.Allocation.DefaultWhenEnabled); | ||
| } | ||
|
|
||
| inputBuilder.Append("\n"); | ||
| inputBuilder.Append($"default_when_enabled={featureDefinition.Allocation?.DefaultWhenEnabled ?? ""}"); | ||
|
|
||
| // Percentiles | ||
| inputBuilder.Append("\n"); | ||
| inputBuilder.Append("percentiles="); | ||
|
|
||
| if (featureDefinition.Allocation?.Percentile != null && featureDefinition.Allocation.Percentile.Any()) | ||
| { | ||
| var sortedPercentiles = featureDefinition.Allocation.Percentile | ||
| .Where(p => p.From != p.To) | ||
| .OrderBy(p => p.From) | ||
| .ToList(); | ||
|
|
||
| allocatedVariants.UnionWith(sortedPercentiles.Select(p => p.Variant)); | ||
|
|
||
| inputBuilder.Append(string.Join(";", sortedPercentiles.Select(p => $"{p.From},{p.Variant},{p.To}"))); | ||
| } | ||
|
|
||
| // Variants | ||
| inputBuilder.Append("\n"); | ||
| inputBuilder.Append("variants="); | ||
|
|
||
| if (allocatedVariants.Any() && featureDefinition.Variants != null && featureDefinition.Variants.Any()) | ||
| { | ||
| var sortedVariants = featureDefinition.Variants | ||
| .Where(variant => allocatedVariants.Contains(variant.Name)) | ||
| .OrderBy(variant => variant.Name) | ||
| .ToList(); | ||
|
|
||
| inputBuilder.Append(string.Join(";", sortedVariants.Select(v => $"{v.Name},{v.ConfigurationValue?.Value}"))); | ||
| } | ||
|
|
||
| // If there's not a special seed and no variants allocated, return null | ||
| if (featureDefinition.Allocation?.Seed == null && | ||
| !allocatedVariants.Any()) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Example input string | ||
| // input == "seed=123abc\ndefault_when_enabled=Control\npercentiles=0,Control,20;20,Test,100\nvariants=Control,standard;Test,special" | ||
| string input = inputBuilder.ToString(); | ||
|
|
||
| using (SHA256 sha256 = SHA256.Create()) | ||
| { | ||
| byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); | ||
| byte[] truncatedHash = new byte[15]; | ||
| Array.Copy(hash, truncatedHash, 15); | ||
| return Convert.ToBase64String(truncatedHash); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.