-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Add trace when evaluating feature flags #755
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
Open
kylejuliandev
wants to merge
13
commits into
open-feature:main
Choose a base branch
from
kylejuliandev:kylej/improve-tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b609d9
Add OpenTelemetry log exporting and ensure traces reliably sample
kylejuliandev 703798f
Add code to start activity and initial unit tests
kylejuliandev 73ee185
Add additional unit tests and ensure tags are added
kylejuliandev 24ea6b6
Fix build issue
kylejuliandev 3dce87e
Address gemini code review comments
kylejuliandev a6b8ec3
Add tests to improve code coverage of OpenFeatureActivitySource
kylejuliandev 7e99375
Set activity kind to internal
kylejuliandev 3b8eac5
Ensure major.minor.patch version is included in trace
kylejuliandev 6e943fa
Move tag keys to string constants in separate class
kylejuliandev 6d64f4f
Remove duplicate activity disposal
kylejuliandev 21c9a1c
Add internal extension method for adding tags to activities
kylejuliandev 23d0839
Merge branch 'main' into kylej/improve-tracing
kylejuliandev 06fc54b
Remove null check on Activity and create constant for error.type
kylejuliandev 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
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,70 @@ | ||
| using System.Diagnostics; | ||
| using OpenFeature.Constant; | ||
|
|
||
| namespace OpenFeature; | ||
|
|
||
| static class OpenFeatureActivitySource | ||
| { | ||
| static readonly ActivitySource Source = new("OpenFeature", GetLibraryVersion()); | ||
|
|
||
| internal static Activity? StartActivity(string name) | ||
| => Source.StartActivity(name, ActivityKind.Internal); | ||
|
|
||
| internal const string EvaluationActivityName = "feature_flag.evaluation"; | ||
| internal const string FeatureFlagKeyName = "feature_flag.key"; | ||
| internal const string FeatureFlagProviderName = "feature_flag.provider.name"; | ||
| internal const string FeatureFlagReasonName = "feature_flag.result.reason"; | ||
| internal const string FeatureFlagValueName = "feature_flag.result.value"; | ||
| internal const string FeatureFlagVariantName = "feature_flag.result.variant"; | ||
|
kylejuliandev marked this conversation as resolved.
|
||
| internal const string FeatureFlagErrorMessageName = "feature_flag.error.message"; | ||
|
|
||
| internal const string ErrorTypeName = "error.type"; | ||
|
|
||
| // Mapped to standard `error.types` https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-events/#evaluation-event | ||
| internal static string GetFlagEvaluationErrorDescription(this ErrorType errorType) => | ||
| errorType switch | ||
| { | ||
| ErrorType.ProviderNotReady => "provider_not_ready", | ||
| ErrorType.FlagNotFound => "flag_not_found", | ||
| ErrorType.ParseError => "parse_error", | ||
| ErrorType.TypeMismatch => "type_mismatch", | ||
| ErrorType.General => "general", | ||
| ErrorType.InvalidContext => "invalid_context", | ||
| ErrorType.TargetingKeyMissing => "targeting_key_missing", | ||
| ErrorType.ProviderFatal => "provider_fatal", | ||
| _ => "_OTHER" | ||
| }; | ||
|
|
||
| // Mapped to standard `feature_flag.result.reason` https://opentelemetry.io/docs/specs/semconv/feature-flags/feature-flags-events/#evaluation-event | ||
| internal static string? GetFlagEvaluationReasonDescription(string? reason) => | ||
| reason switch | ||
| { | ||
| Reason.TargetingMatch => "targeting_match", | ||
| Reason.Split => "split", | ||
| Reason.Disabled => "disabled", | ||
| Reason.Default => "default", | ||
| Reason.Static => "static", | ||
| Reason.Cached => "cached", | ||
| Reason.Unknown => "unknown", | ||
| Reason.Error => "error", | ||
| _ => reason | ||
|
kylejuliandev marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| static string GetLibraryVersion() | ||
| { | ||
| var version = typeof(OpenFeatureActivitySource).Assembly | ||
| .GetName() | ||
| .Version; | ||
|
|
||
| // "3" = major.minor.patch only | ||
| return version?.ToString(3) ?? "UNKNOWN"; | ||
| } | ||
|
|
||
| internal static void AddTagIfRequested(this Activity activity, string tagName, object? value) | ||
| { | ||
| if (!activity.IsAllDataRequested) | ||
| return; | ||
|
|
||
| activity.AddTag(tagName, value); | ||
| } | ||
| } | ||
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
111 changes: 111 additions & 0 deletions
111
test/OpenFeature.Tests/OpenFeatureActivitySourceTests.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,111 @@ | ||
| using System.Diagnostics; | ||
| using OpenFeature.Constant; | ||
|
|
||
| namespace OpenFeature.Tests; | ||
|
|
||
| public class OpenFeatureActivitySourceTests | ||
| { | ||
| [Fact] | ||
| public void StartActivity_ReturnsActivityWithCorrectName() | ||
| { | ||
| using var activityListener = new ActivityListener() | ||
| { | ||
| ShouldListenTo = source => true, | ||
| Sample = (ref _) => ActivitySamplingResult.AllDataAndRecorded | ||
| }; | ||
|
|
||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| var activity = OpenFeatureActivitySource.StartActivity("test_activity"); | ||
|
|
||
| Assert.NotNull(activity); | ||
| Assert.Equal("test_activity", activity.OperationName); | ||
| Assert.Equal("OpenFeature", activity.Source.Name); | ||
| Assert.NotNull(activity.Source.Version); | ||
| Assert.NotEmpty(activity.Source.Version); | ||
| Assert.Equal(ActivityKind.Internal, activity.Kind); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ErrorType.ProviderNotReady, "provider_not_ready")] | ||
| [InlineData(ErrorType.FlagNotFound, "flag_not_found")] | ||
| [InlineData(ErrorType.ParseError, "parse_error")] | ||
| [InlineData(ErrorType.TypeMismatch, "type_mismatch")] | ||
| [InlineData(ErrorType.General, "general")] | ||
| [InlineData(ErrorType.InvalidContext, "invalid_context")] | ||
| [InlineData(ErrorType.TargetingKeyMissing, "targeting_key_missing")] | ||
| [InlineData(ErrorType.ProviderFatal, "provider_fatal")] | ||
| [InlineData((ErrorType)999, "_OTHER")] | ||
| public void GetFlagEvaluationErrorDescription_ReturnsCorrectDescription(ErrorType errorType, string expectedDescription) | ||
| { | ||
| var actual = errorType.GetFlagEvaluationErrorDescription(); | ||
|
|
||
| Assert.Equal(expectedDescription, actual); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("TARGETING_MATCH", "targeting_match")] | ||
| [InlineData("SPLIT", "split")] | ||
| [InlineData("DISABLED", "disabled")] | ||
| [InlineData("DEFAULT", "default")] | ||
| [InlineData("STATIC", "static")] | ||
| [InlineData("CACHED", "cached")] | ||
| [InlineData("UNKNOWN", "unknown")] | ||
| [InlineData("ERROR", "error")] | ||
| [InlineData("OTHER", "OTHER")] | ||
| public void GetFlagEvaluationReasonDescription(string? reason, string expectedDescription) | ||
| { | ||
| var actual = OpenFeatureActivitySource.GetFlagEvaluationReasonDescription(reason); | ||
|
|
||
| Assert.Equal(expectedDescription, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTagIfRequested_AddsTag() | ||
| { | ||
| var exportedActivities = new List<Activity>(); | ||
| using var activityListener = new ActivityListener() | ||
| { | ||
| ShouldListenTo = source => source.Name == "OpenFeature", | ||
| Sample = (ref _) => ActivitySamplingResult.AllDataAndRecorded, | ||
| ActivityStopped = activity => exportedActivities.Add(activity) | ||
| }; | ||
|
|
||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| using (var activity = OpenFeatureActivitySource.StartActivity("set_tag_if_requested")) | ||
| { | ||
| activity?.AddTagIfRequested("custom_tag_name", true); | ||
| } | ||
|
|
||
| Assert.Single(exportedActivities); | ||
|
|
||
| var actualActivity = exportedActivities.First(); | ||
| Assert.Equal("custom_tag_name", actualActivity.TagObjects.First().Key); | ||
| Assert.Equal(true, actualActivity.TagObjects.First().Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTagIfRequested_WhenDataNotRequested_DoesNotAddTag() | ||
| { | ||
| var exportedActivities = new List<Activity>(); | ||
| using var activityListener = new ActivityListener() | ||
| { | ||
| ShouldListenTo = source => source.Name == "OpenFeature", | ||
| Sample = (ref _) => ActivitySamplingResult.PropagationData, | ||
| ActivityStopped = activity => exportedActivities.Add(activity) | ||
| }; | ||
|
|
||
| ActivitySource.AddActivityListener(activityListener); | ||
|
|
||
| using (var activity = OpenFeatureActivitySource.StartActivity("set_tag_if_requested")) | ||
| { | ||
| activity?.AddTagIfRequested("custom_tag_name", true); | ||
| } | ||
|
|
||
| Assert.Single(exportedActivities); | ||
|
|
||
| var actualActivity = exportedActivities.First(); | ||
| Assert.Empty(actualActivity.TagObjects); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.