microsoft / FeatureManagement-Dotnet

Microsoft.FeatureManagement provides standardized APIs for enabling feature flags within applications. Utilize this library to secure a consistent experience when developing applications that use patterns such as beta access, rollout, dark deployments, and more.
MIT License
1.05k stars 115 forks source link

Consider the ability to allocate variants based on custom filters #460

Open CuddleBunny opened 5 months ago

CuddleBunny commented 5 months ago

Variants are going to be a huge boon to this library and I'm excited to leverage them. I often find myself creating custom filters that evaluate based on the application state rather than the traditional user centric targeting scenarios. It would be great if we could allocate variants based on these filters. Perhaps something like:

  "FeatureManagement":
  {
      "MyVariantFeatureFlag":
      {   
          "Allocation": {
              "DefaultWhenEnabled": "Small",
              "AllocatedFor": [
                  {
                      "Name": "CustomFilter",
                      "Parameters": {
                          "SomeParam": "SomeValue"
                      },
                      "Variant": "Big"
                  }
              ]
          },
          "Variants": [
              { 
                  "Name": "Big"
              },  
              { 
                  "Name": "Small"
              } 
          ]
      }
  }
jimmyca15 commented 5 months ago

It's an interesting idea. With your suggestion, what would you expect the interface of "CustomFilter" to look like?

CuddleBunny commented 5 months ago

@jimmyca15 - I haven't gone too deep on the allocation code after I saw it was all private and I couldn't extend it myself. Could it be as simple as:

public interface IContextualVariantFeatureFilter<TContext> : IContextualFeatureFilter<TContext> {
    string Variant { get; set; }
}
zhiyuanliang-ms commented 5 months ago

Hi, @CuddleBunny Thank you for your suggestion. This is an interesting idea.

But, there is one thing I want to call out here. For the code snippet you provided here, I found that you were using .NET Feature Management schema to declare a variant feature flag. We had a PR to remove the variant & telemetry support from the .NET FM schema. The reason is that, starting from v4.0.0, we are going to embrace the Microsoft Feature Management schema which will replace .NET FM schema to be the primary schema for the .NET feature management library. Please see here and #408.

Now, our latest releases have fully supported the Microsoft FM schema. The latest preview release 4.0.0-preview3 can still recognize the variant feature flag specified in .NET schema but our next preview release will only recognize variant feature flag specified in Microsoft FM schema.

Besides, all the example projects in preview branch and example code snippets in our public doc for preview version have been changed to Microsoft FM schema. You can have a look at them. Thank you so much!

CuddleBunny commented 5 months ago

@zhiyuanliang-ms - I saw the PR for the schema changes but hadn't looked into it yet. The docs I found for variants was in the release/v4 branch. I see that they were changed in the preview branch. If I understand the new schema correctly, the result would be something like the following, but I suspect the underlying code wouldn't change much?

{
    "feature_management": {
        "feature_flags": [
            {
                "id": "MyVariantFeatureFlag",
                "enabled": true,
                "allocation": {
                    "default_when_enabled": "Small",
                    "client_filters": [
                        {
                            "name": "CustomFilter",
                            "parameters": {
                                "SomeParam": "SomeValue"
                            },
                            "variant": "Big"
                        }
                    ]
                },
                "variants": [
                    {
                        "name": "Big"
                    },
                    {
                        "name": "Small"
                    }
                ]
            }
        ]
    }
}
zhiyuanliang-ms commented 5 months ago

@CuddleBunny The new variant feature flag configuration you provided is correct.

I suspect the underlying code wouldn't change much

You are right. The code didn't change much.