-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add Metric Hook Custom Attributes #512
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 7 commits into
open-feature:main
from
kylejuliandev:feat/add-metric-hook-custom-attributes
Jul 19, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6307180
Add initial API for adding Hook custom dimensions / tags
kylejuliandev 74b0ea2
Refactor to add callback to retrieve Flag Metadata keys
kylejuliandev 6aa2a8c
Fix README with usage of WithCustomDimension
kylejuliandev 65bb704
Align AspNetCore sample with README example
kylejuliandev e269b69
Add unit tests to cover new MetricsHookOptions
kylejuliandev e0fcf27
Add Custom Dimensions to all Metrics in Metrics hook
kylejuliandev a278501
Tweak README after Hook update
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
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="MetricsHook"/>. | ||
| /// </summary> | ||
| public sealed class MetricsHookOptions | ||
| { | ||
| /// <summary> | ||
| /// The default options for the <see cref="MetricsHook"/>. | ||
| /// </summary> | ||
| public static MetricsHookOptions Default { get; } = new MetricsHookOptions(); | ||
|
|
||
| /// <summary> | ||
| /// Custom dimensions or tags to be associated with Meters in <see cref="MetricsHook"/>. | ||
| /// </summary> | ||
| public IReadOnlyCollection<KeyValuePair<string, object?>> CustomDimensions { get; } | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| internal IReadOnlyCollection<KeyValuePair<string, Func<ImmutableMetadata, object?>>> FlagMetadataCallbacks { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MetricsHookOptions"/> class with default values. | ||
| /// </summary> | ||
| private MetricsHookOptions() : this(null, null) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MetricsHookOptions"/> class. | ||
| /// </summary> | ||
| /// <param name="customDimensions">Optional custom dimensions to tag Counter increments with.</param> | ||
| /// <param name="flagMetadataSelectors"></param> | ||
| internal MetricsHookOptions(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="MetricsHookOptions"/>. | ||
| /// </summary> | ||
| public static MetricsHookOptionsBuilder CreateBuilder() => new MetricsHookOptionsBuilder(); | ||
|
|
||
| /// <summary> | ||
| /// A builder for constructing <see cref="MetricsHookOptions"/> instances. | ||
| /// </summary> | ||
| public sealed class MetricsHookOptionsBuilder | ||
| { | ||
| 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 MetricsHookOptionsBuilder WithCustomDimension(string key, object? value) | ||
| { | ||
| this._customDimensions.Add(new KeyValuePair<string, object?>(key, value)); | ||
askpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provide a callback to evaluate flag metadata for a specific flag key. | ||
| /// </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 MetricsHookOptionsBuilder 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="MetricsHookOptions"/> instance. | ||
| /// </summary> | ||
| public MetricsHookOptions Build() | ||
| { | ||
| return new MetricsHookOptions(this._customDimensions.AsReadOnly(), this._flagMetadataExpressions.AsReadOnly()); | ||
| } | ||
| } | ||
| } | ||
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 MetricsHookOptionsTests | ||
| { | ||
| [Fact] | ||
| public void Default_Options_Should_Be_Initialized_Correctly() | ||
| { | ||
| // Arrange & Act | ||
| var options = MetricsHookOptions.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 = MetricsHookOptions.CreateBuilder(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(builder); | ||
| Assert.IsType<MetricsHookOptions.MetricsHookOptionsBuilder>(builder); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Build_Should_Return_Options() | ||
| { | ||
| // Arrange | ||
| var builder = MetricsHookOptions.CreateBuilder(); | ||
|
|
||
| // Act | ||
| var options = builder.Build(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(options); | ||
| Assert.IsType<MetricsHookOptions>(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 = MetricsHookOptions.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 = MetricsHookOptions.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); | ||
| } | ||
| } |
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.