-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add TraceEnricherHookOptions Custom Attributes #526
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
askpt
merged 2 commits into
open-feature:main
from
kylejuliandev:feat/add-trace-hook-custom-attributes
Jul 26, 2025
Merged
Changes from 1 commit
Commits
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
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,91 @@ | ||
| using OpenFeature.Model; | ||
|
|
||
| namespace OpenFeature.Hooks; | ||
|
|
||
| /// <summary> | ||
| /// Configuration options for the <see cref="TraceEnricherHookOptions"/>. | ||
| /// </summary> | ||
| public sealed class TraceEnricherHookOptions | ||
| { | ||
| /// <summary> | ||
| /// The default options for the <see cref="TraceEnricherHookOptions"/>. | ||
| /// </summary> | ||
| public static TraceEnricherHookOptions Default { get; } = new TraceEnricherHookOptions(); | ||
|
|
||
| /// <summary> | ||
| /// Custom dimensions or tags to be associated with current <see cref="System.Diagnostics.Activity"/> in <see cref="TraceEnricherHook"/>. | ||
| /// </summary> | ||
| public IReadOnlyCollection<KeyValuePair<string, object?>> CustomDimensions { get; } | ||
|
|
||
| /// <summary> | ||
| /// Flag metadata callbacks to be associated with current <see cref="System.Diagnostics.Activity"/>. | ||
| /// </summary> | ||
| internal IReadOnlyCollection<KeyValuePair<string, Func<ImmutableMetadata, object?>>> FlagMetadataCallbacks { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TraceEnricherHookOptions"/> class with default values. | ||
| /// </summary> | ||
| private TraceEnricherHookOptions() : this(null, null) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="TraceEnricherHookOptions"/> class. | ||
| /// </summary> | ||
| /// <param name="customDimensions">Optional custom dimensions to tag Counter increments with.</param> | ||
| /// <param name="flagMetadataSelectors">Optional flag metadata callbacks to be associated with current <see cref="System.Diagnostics.Activity"/>.</param> | ||
| internal TraceEnricherHookOptions(IReadOnlyCollection<KeyValuePair<string, object?>>? customDimensions = null, | ||
| IReadOnlyCollection<KeyValuePair<string, Func<ImmutableMetadata, object?>>>? flagMetadataSelectors = null) | ||
| { | ||
| this.CustomDimensions = customDimensions ?? []; | ||
| this.FlagMetadataCallbacks = flagMetadataSelectors ?? []; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new builder for <see cref="TraceEnricherHookOptions"/>. | ||
| /// </summary> | ||
| public static TraceEnricherHookOptionsBuilder CreateBuilder() => new TraceEnricherHookOptionsBuilder(); | ||
|
|
||
| /// <summary> | ||
| /// A builder for constructing <see cref="TraceEnricherHookOptions"/> instances. | ||
| /// </summary> | ||
| public sealed class TraceEnricherHookOptionsBuilder | ||
| { | ||
| private readonly List<KeyValuePair<string, object?>> _customDimensions = new List<KeyValuePair<string, object?>>(); | ||
| private readonly List<KeyValuePair<string, Func<ImmutableMetadata, object?>>> _flagMetadataExpressions = new List<KeyValuePair<string, Func<ImmutableMetadata, object?>>>(); | ||
|
|
||
| /// <summary> | ||
| /// Adds a custom dimension. | ||
| /// </summary> | ||
| /// <param name="key">The key for the custom dimension.</param> | ||
| /// <param name="value">The value for the custom dimension.</param> | ||
| public TraceEnricherHookOptionsBuilder WithCustomDimension(string key, object? value) | ||
askpt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| this._customDimensions.Add(new KeyValuePair<string, object?>(key, value)); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provide a callback to evaluate flag metadata and add it as a custom dimension on the current <see cref="System.Diagnostics.Activity"/>. | ||
| /// </summary> | ||
| /// <param name="key">The key for the custom dimension.</param> | ||
| /// <param name="flagMetadataCallback">The callback to retrieve the value to tag successful flag evaluations.</param> | ||
| /// <returns></returns> | ||
| public TraceEnricherHookOptionsBuilder WithFlagEvaluationMetadata(string key, Func<ImmutableMetadata, object?> flagMetadataCallback) | ||
| { | ||
| var kvp = new KeyValuePair<string, Func<ImmutableMetadata, object?>>(key, flagMetadataCallback); | ||
|
|
||
| this._flagMetadataExpressions.Add(kvp); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds the <see cref="TraceEnricherHookOptions"/> instance. | ||
| /// </summary> | ||
| public TraceEnricherHookOptions Build() | ||
| { | ||
| return new TraceEnricherHookOptions(this._customDimensions.AsReadOnly(), this._flagMetadataExpressions.AsReadOnly()); | ||
| } | ||
| } | ||
| } | ||
84 changes: 84 additions & 0 deletions
84
test/OpenFeature.Tests/Hooks/TraceEnricherHookOptionsTests.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,84 @@ | ||
| using OpenFeature.Hooks; | ||
| using OpenFeature.Model; | ||
|
|
||
| namespace OpenFeature.Tests.Hooks; | ||
|
|
||
| public class TraceEnricherHookOptionsTests | ||
| { | ||
| [Fact] | ||
| public void Default_Options_Should_Be_Initialized_Correctly() | ||
| { | ||
| // Arrange & Act | ||
| var options = TraceEnricherHookOptions.Default; | ||
|
|
||
| // Assert | ||
| Assert.NotNull(options); | ||
| Assert.Empty(options.CustomDimensions); | ||
| Assert.Empty(options.FlagMetadataCallbacks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateBuilder_Should_Return_New_Builder_Instance() | ||
| { | ||
| // Arrange & Act | ||
| var builder = TraceEnricherHookOptions.CreateBuilder(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(builder); | ||
| Assert.IsType<TraceEnricherHookOptions.TraceEnricherHookOptionsBuilder>(builder); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_Should_Return_Options() | ||
| { | ||
| // Arrange | ||
| var builder = TraceEnricherHookOptions.CreateBuilder(); | ||
|
|
||
| // Act | ||
| var options = builder.Build(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(options); | ||
| Assert.IsType<TraceEnricherHookOptions>(options); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("custom_dimension_value")] | ||
| [InlineData(1.0)] | ||
| [InlineData(2025)] | ||
| [InlineData(null)] | ||
| [InlineData(true)] | ||
| public void Builder_Should_Allow_Adding_Custom_Dimensions(object? value) | ||
| { | ||
| // Arrange | ||
| var builder = TraceEnricherHookOptions.CreateBuilder(); | ||
| var key = "custom_dimension_key"; | ||
|
|
||
| // Act | ||
| builder.WithCustomDimension(key, value); | ||
| var options = builder.Build(); | ||
|
|
||
| // Assert | ||
| Assert.Single(options.CustomDimensions); | ||
| Assert.Equal(key, options.CustomDimensions.First().Key); | ||
| Assert.Equal(value, options.CustomDimensions.First().Value); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Builder_Should_Allow_Adding_Flag_Metadata_Expressions() | ||
| { | ||
| // Arrange | ||
| var builder = TraceEnricherHookOptions.CreateBuilder(); | ||
| var key = "flag_metadata_key"; | ||
| static object? expression(ImmutableMetadata m) => m.GetString("flag_metadata_key"); | ||
|
|
||
| // Act | ||
| builder.WithFlagEvaluationMetadata(key, expression); | ||
| var options = builder.Build(); | ||
|
|
||
| // Assert | ||
| Assert.Single(options.FlagMetadataCallbacks); | ||
| Assert.Equal(key, options.FlagMetadataCallbacks.First().Key); | ||
| Assert.Equal(expression, options.FlagMetadataCallbacks.First().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
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.