Skip to content
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Here are some of the benefits of using this library:
* [Targeting](#targeting)
* [Targeting Exclusion](#targeting-exclusion)
* [Variants](#variants)
* [Variants in Dependency Injection](#variants-in-dependency-injection)
* [Telemetry](#telemetry)
* [Enabling Telemetry](#enabling-telemetry)
* [Custom Telemetry Publishers](#custom-telemetry-publishers)
Expand Down Expand Up @@ -924,7 +925,7 @@ The `Allocation` setting of a feature flag has the following properties:
| `DefaultWhenDisabled` | Specifies which variant should be used when a variant is requested while the feature is considered disabled. |
| `DefaultWhenEnabled` | Specifies which variant should be used when a variant is requested while the feature is considered enabled and no other variant was assigned to the user. |
| `User` | Specifies a variant and a list of users to whom that variant should be assigned. |
| `Group` | Specifies a variant and a list of groups the current user has to be in for that variant to be assigned. |
| `Group` | Specifies a variant and a list of groups. The variant will be assigned if the user is in at least one of the groups. |
| `Percentile` | Specifies a variant and a percentage range the user's calculated percentage has to fit into for that variant to be assigned. |
| `Seed` | The value which percentage calculations for `Percentile` are based on. The percentage calculation for a specific user will be the same across all features if the same `Seed` value is used. If no `Seed` is specified, then a default seed is created based on the feature name. |

Expand All @@ -934,7 +935,9 @@ If the feature is enabled, the feature manager will check the `User`, `Group`, a

Allocation logic is similar to the [Microsoft.Targeting](./README.md#MicrosoftTargeting) feature filter, but there are some parameters that are present in targeting that aren't in allocation, and vice versa. The outcomes of targeting and allocation are not related.

#### Overriding Enabled State with a Variant
**Note:** To allow allocating feature variants, you need to register `ITargetingContextAccessor`. This can be done by calling the `WithTargeting<T>` method.

### Overriding Enabled State with a Variant

You can use variants to override the enabled state of a feature flag. This gives variants an opportunity to extend the evaluation of a feature flag. If a caller is checking whether a flag that has variants is enabled, the feature manager will check if the variant assigned to the current user is set up to override the result. This is done using the optional variant property `StatusOverride`. By default, this property is set to `None`, which means the variant doesn't affect whether the flag is considered enabled or disabled. Setting `StatusOverride` to `Enabled` allows the variant, when chosen, to override a flag to be enabled. Setting `StatusOverride` to `Disabled` provides the opposite functionality, therefore disabling the flag when the variant is chosen. A feature with a `Status` of `Disabled` cannot be overridden.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a "why?" here, because there's only a narrow reason they would want to use the overrides. It's if a customer wants to allocate via variants without changing their existing IsEnabled code. Meaning they want to return bool results but have more than 2 "buckets".

@zhiyuanliang-ms zhiyuanliang-ms Mar 19, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today, feature filters can be used to conditionally enable feature flags. Variants should be able to extend this functionality by also playing a role in the decision to enable a feature flag. A property can be included in a feature flag’s variants giving them the opportunity to override the flag’s enabled state. This is important considering the enabled state of a flag is used in our integration points with ASP.NET Core such as the FeatureGateAttribute

quoted from the design doc

This is the original purpose for this design.

This gives variants an opportunity to extend the evaluation of a feature flag.

I think this sentence is a solid reason but we may come up a good story or an attracting use case of statusOverride.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important considering the enabled state of a flag is used in our integration points with ASP.NET Core such as the FeatureGateAttribute

This part is valid, but it's still not a big use case. Essentially, customers can always use a normal flag with filters to achieve whatever boolean targeting they want to achieve. Variants don't offer an advantage to boolean targeting over the standard TargetingFilter.

One reason someone would use Variants in the bool world is to adding more telemetry "buckets" so they can split their traffic (although this is also not necessary as the split traffic of "true" results should be equal).

Another reason is if a customer's plan is to call both GetVariant and IsEnabled(like using FeatureGateAttribute) on a single flag, then they might use it, but this is likely overloading what a flag should be in charge of.

My fear is that users will see this section in the readme and think that they should be using it- instead of using it to address a specific problem.


Expand Down Expand Up @@ -970,6 +973,63 @@ If you are using a feature flag with binary variants, the `StatusOverride` prope

In the above example, the feature is enabled by the `AlwaysOn` filter. If the current user is in the calculated percentile range of 10 to 20, then the `On` variant is returned. Otherwise, the `Off` variant is returned and because `StatusOverride` is equal to `Disabled`, the feature will now be considered disabled.

### Variants in Dependency Injection

Variant feature flags can be used in conjunction with dependency injection to surface different implementations of a service for different users. This is accomplished through the use of the `IVariantServiceProvider<TService>` interface.

``` C#
IVariantServiceProvider<IAlgorithm> algorithmServiceProvider;
...

IAlgorithm forecastAlgorithm = await algorithmServiceProvider.GetServiceAsync(cancellationToken);
```

In the snippet above, the `IVariantServiceProvider<IAlgorithm>` will retrieve an implementation of `IAlgorithm` from the dependency injection container. The chosen implementation is dependent upon:
* The feature that the `IAlgorithm` service was registered with.
* The allocated variant for that feature.

The `IVariantServiceProvider<T>` is made available to the application by calling `IFeatureManagementBuilder.WithVariantService<T>(string featureName)`. See below for an example.

``` C#
services.AddFeatureManagement()
.WithVariantService<IAlgorithm>("ForecastAlgorithm");
```

With the call above, `IAlgorithm` will be wired up with the variant feature flag `ForecastAlgorithm`. The `GetServiceAsync` method of `IVariantService<IAlgorithm>` will retrieve the variant service of `IAlgorithm` which matches the name of allocated variant of the `ForecastAlgorithm` flag, from the dependency injection container. If there is no appropriate variant service found, the `GetServiceAsync` method will return null.

``` javascript
{
// The example variant feature flag
"ForecastAlgorithm": {
"Variants": [
{
"Name": "AlgorithmBeta"
},
...
]
}
}
```

**Note:** The `WithVariantService<TService>` method will not register any implementation(s) of `TService`. You need to register variant services as `TService` to allow `IVariantServiceProvider<TService>` to retrieve them from the dependency injection container. Which extension method to use for registration depends on your needs. Here is an example.

``` C#
// Adds an implementation for `IAlgorithm`, which is consumed as a variant service above.
services.AddSingleton<IAlgorithm, AlgorithmBeta>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
services.AddSingleton<IAlgorithm, AlgorithmBeta>();
// Adds an implementation for `IAlgorithm`, which is consumed as a variant service above.
services.AddSingleton<IAlgorithm, AlgorithmBeta>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we forget to register implementation for the service?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will return null if there is no matched implementation found in the DI container.

@zhiyuanliang-ms zhiyuanliang-ms Mar 19, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding an sentence to mention this after the section
"With the call above, ..."

Something like:

services.AddFeatureManagement() 
        .WithVariantService<IAlgorithm>("ForecastAlgorithm");

With the call above, IAlgorithm will be wired up with the variant feature flag ForecastAlgorithm. The GetServiceAsync method of IVariantService<IAlgorithm> will retrieve the variant service of IAlgorithm which matches the name of allocated variant of the ForecastAlgorithm flag, from the dependency injection container. If there is no appropriate variant service found, the GetServiceAsync method will return null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

The call above makes IVariantServiceProvider<IAlgorithm> available in the service collection. Implementation(s) of IAlgorithm must be added separately via an add method such as services.AddSingleton<IAlgorithm, SomeImplementation>(). The implementation of IAlgorithm that the IVariantServiceProvider uses depends on the ForecastAlgorithm feature. If no implementation of IAlgorithm is added to the service collection, then the IVariantServiceProvider<IAlgorithm>.GetServiceAsync() will return a task with a null result.

```

#### Variant Service Alias Attribute

``` C#
[VariantServiceAlias("Beta")]
public class AlgorithmBeta : IAlgorithm
{
...
}
```

The variant service provider will use the type names of implementations to match the allocated variant. If a variant service is decorated with the `VariantServiceAliasAttribute`, the name declared in this attribute should be used in configuration to reference this variant service.

## Telemetry

When a feature flag change is deployed, it is often important to analyze its effect on an application. For example, here are a few questions that may arise:
Expand Down