OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution.
It requires Elixir 1.14 or greater to run.
The package can be installed by adding open_feature
to your list of dependencies
in mix.exs
:
def deps do
[{:open_feature, "~> 0.1"}]
end
provider = %OpenFeature.Provider.InMemory{
flags: %{
"v2_enabled" => %{
disabled: false,
default_variant: "on",
variants: %{
"on" => true,
"off" => false,
}
}
}
}
{:ok, provider} = OpenFeature.set_provider(provider)
client = OpenFeature.get_client()
v2_enabled = OpenFeature.Client.get_boolean_value(client, "v2_enabled", false)
For details, including API documentation, see the respective Hex docs.
Status | Features | Description |
---|---|---|
Providers | Integrate with a commercial, open source, or in-house feature management tool. | |
β | Targeting | Contextually-aware flag evaluation using evaluation context. |
β | Hooks | Add functionality to various stages of the flag evaluation life-cycle. |
β | Logging | Integrate with popular logging packages. |
β | Domains | Logically bind clients with providers. |
β | Eventing | React to state changes in the provider or flag management system. |
β | Shutdown | Gracefully clean up a provider during application shutdown. |
β | Extending | Extend OpenFeature with custom providers. |
Implemented: β
| In-progress:
Providers are an abstraction between a flag management system and the OpenFeature SDK. Look here for a complete list of available providers. If the provider you're looking for hasn't been created yet, see the develop a provider section to learn how to build it yourself.
Once you've added a provider as a dependency, it can be registered with OpenFeature like this:
provider = %OpenFeature.Provider.InMemory{
flags: %{
"v2_enabled" => %{
disabled: false,
default_variant: "one",
variants: %{
"on" => true,
"off" => false
}
}
}
}
{:ok, provider} = OpenFeature.set_provider(provider)
In some situations, it may be beneficial to register multiple providers in the same application. This is possible using domains, which is covered in more detail below.
Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location. In OpenFeature, we refer to this as targeting. If the flag management system you're using supports targeting, you can provide the input data using the evaluation context.
# set a value to the global context
OpenFeature.set_global_context(%{region: "us-east-1"})
# set a value to the client context
client = OpenFeature.get_client() |> OpenFeature.Client.set_context(%{region: "us-east-1"})
# set a value to the invocation context
flag_value = OpenFeature.Client.get_boolean_value(client, "some-flag", flag, context: %{region: "us-east-1"})
Hooks allow for custom logic to be added at well-defined points of the flag evaluation life-cycle. Look here for a complete list of available hooks.
Once you've added a hook as a dependency, it can be registered at the client or flag invocation level.
## add a hook on this client, to run on all evaluations made by this client
client = OpenFeature.Client.add_hooks(client, [%OpenFeature.Hook{}])
## add a hook for this evaluation only
flag_value = OpenFeature.Client.get_boolean_value(client, flag_key, false, hooks: [%OpenFeature.Hook{}]);
The Elixir SDK uses the default Elixir Logger.
Clients can be assigned to a domain. A domain is a logical identifier which can be used to associate clients with a particular provider. If a domain has no associated provider, the default provider is used.
provider = %OpenFeature.Provider.InMemory{
flags: %{
"v2_enabled" => %{
disabled: false,
default_variant: "default",
variants: %{
"default" => true
}
}
}
}
# registering the default provider
{:ok, _provider} = OpenFeature.set_provider(provider)
# registering a provider to a domain
{:ok, _provider} = OpenFeature.set_provider("my-domain", provider)
# A client bound to the default provider
default_client = OpenFeature.get_client()
# A client bound to the CachedProvider provider
domain_client = OpenFeature.get_client("my-domain")
Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions.
Initialization events (PROVIDER_READY
on success, PROVIDER_ERROR
on failure) are dispatched for every provider.
Some providers support additional events, such as PROVIDER_CONFIGURATION_CHANGED
.
Please refer to the documentation of the provider you're using to see what events are supported.
# add an event handler to a client
OpenFeature.Client.add_event_handler(client, :configuration_changed, fn event_details ->
# do something when the provider's flag settings change
end)
The OpenFeature API provides a close function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down.
OpenFeature.shutdown()
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency.
This can be a new repository or included in the existing contrib repository available under the OpenFeature organization.
Youβll then need to write the provider by implementing the OpenFeature.Provider
behaviour exported by the OpenFeature SDK.
defmodule OpenFeature.Provider.NoOp do
alias OpenFeature.ResolutionDetails
@behaviour OpenFeature.Provider
defstruct name: "NoOp", domain: nil, state: :not_ready, hooks: []
def initialize(provider, domain, _evaluation_context), do: {:ok, %{provider | state: :ready, domain: domain}}
def shutdown(_provider), do: :ok
def resolve_boolean_value(_provider, _key, default, _context), do: {:ok, %ResolutionDetails{value: default}}
def resolve_string_value(_provider, _key, default, _context), do: {:ok, %ResolutionDetails{value: default}}
def resolve_number_value(_provider, _key, default, _context), do: {:ok, %ResolutionDetails{value: default}}
def resolve_map_value(_provider, _key, default, _context), do: {:ok, %ResolutionDetails{value: default}}
end
Built a new provider? Let us know so we can add it to the docs!
- Give this repo a βοΈ!
- Follow us on social media:
- Twitter: @openfeature
- LinkedIn: OpenFeature
- Join us on Slack
- For more, check out our community page
Interested in contributing? Great, we'd love your help! To get started, take a look at the CONTRIBUTING guide.
Made with contrib.rocks.