-
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 all commits
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
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
98 changes: 98 additions & 0 deletions
98
src/Microsoft.FeatureManagement/Telemetry/FeatureEvaluationTelemetry.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,98 @@ | ||
| using Microsoft.Extensions.Logging; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
|
|
||
| namespace Microsoft.FeatureManagement.Telemetry | ||
| { | ||
| internal static class FeatureEvaluationTelemetry | ||
| { | ||
| 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 Publish(EvaluationEvent evaluationEvent, ILogger logger) | ||
| { | ||
| if (Activity.Current == null) | ||
| { | ||
| throw new InvalidOperationException("An Activity must be created before calling this method."); | ||
| } | ||
|
|
||
| if (evaluationEvent == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(evaluationEvent)); | ||
| } | ||
|
|
||
| if (evaluationEvent.FeatureDefinition == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(evaluationEvent.FeatureDefinition)); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
|
|
||
| // VariantAssignmentPercentage | ||
| if (evaluationEvent.VariantAssignmentReason == VariantAssignmentReason.DefaultWhenEnabled) | ||
| { | ||
| // If the variant was assigned due to DefaultWhenEnabled, the percentage reflects the unallocated percentiles | ||
| double allocatedPercentage = evaluationEvent.FeatureDefinition.Allocation?.Percentile?.Sum(p => p.To - p.From) ?? 0; | ||
|
|
||
| tags["VariantAssignmentPercentage"] = 100 - allocatedPercentage; | ||
| } | ||
| else if (evaluationEvent.VariantAssignmentReason == VariantAssignmentReason.Percentile) | ||
| { | ||
| // If the variant was assigned due to Percentile, the percentage is the sum of the allocated percentiles for the given variant | ||
| 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; | ||
| } | ||
|
|
||
| var activityEvent = new ActivityEvent("FeatureFlag", DateTimeOffset.UtcNow, tags); | ||
|
|
||
| Activity.Current.AddEvent(activityEvent); | ||
| } | ||
| } | ||
| } |
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
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
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
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.