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.
- C++17 or newer (due to the usage of
std::optional,std::any,std::variant, andstd::string_view) - Bazel build system
To use the OpenFeature C++ SDK in your Bazel project, add the dependency to your MODULE.bazel file or your WORKSPACE (depending on your Bazel configuration).
void example() {
// flags defined in memory
std::unordered_map<std::string, std::any> my_flags = {
{"v2_enabled", true}
};
// configure a provider
openfeature::OpenFeatureAPI& api = openfeature::OpenFeatureAPI::GetInstance();
api.SetProviderAndWait(std::make_shared<openfeature::InMemoryProvider>(my_flags));
// create a client
std::shared_ptr<openfeature::Client> client = api.GetClient();
// get a bool flag value
bool flag_value = client->GetBooleanValue("v2_enabled", false);
}| 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. |
| ❌ | Tracking | Associate user-actions with flag evaluations for the purposes of experimentation. |
| ❌ | Transaction Context Propagation | Set a specific evaluation context for a transaction (e.g. an HTTP request or a thread) |
| ✅ | Shutdown | Gracefully clean up a provider during application shutdown. |
| ✅ | Extending | Extend OpenFeature with custom providers and hooks. |
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:
To register a provider in a blocking manner to ensure it is ready before further actions are taken, use the SetProviderAndWait method:
openfeature::OpenFeatureAPI& api = openfeature::OpenFeatureAPI::GetInstance();
api.SetProviderAndWait(std::make_shared<MyProvider>());To register a provider in a non-blocking manner, use the SetProvider method:
openfeature::OpenFeatureAPI::GetInstance().SetProvider(std::make_shared<MyProvider>());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.
openfeature::OpenFeatureAPI& api = openfeature::OpenFeatureAPI::GetInstance();
// Set a global evaluation context
openfeature::EvaluationContext global_ctx = openfeature::EvaluationContext::Builder()
.WithAttribute("region", "us-east-1")
.build();
api.SetEvaluationContext(global_ctx);
// Set a client-level evaluation context
std::shared_ptr<openfeature::Client> client = api.GetClient();
openfeature::EvaluationContext client_ctx = openfeature::EvaluationContext::Builder()
.WithAttribute("app_version", "1.0.5")
.build();
client->SetEvaluationContext(client_ctx);
// Provide an invocation-level evaluation context
openfeature::EvaluationContext req_ctx = openfeature::EvaluationContext::Builder()
.WithTargetingKey("session-id-12345")
.WithAttribute("email", "user@example.com")
.build();
bool flag_value = client->GetBooleanValue("some-flag", false, req_ctx);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. If the hook you're looking for hasn't been created yet, see the develop a hook section to learn how to build it yourself.
Once you've added a hook as a dependency, it can be registered at the global, client, or flag invocation level.
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.
openfeature::OpenFeatureAPI& api = openfeature::OpenFeatureAPI::GetInstance();
// registering the default provider
api.SetProvider(std::make_shared<LocalProvider>());
// registering a provider to a domain
api.SetProvider("my-domain", std::make_shared<CachedProvider>());
// A client bound to the default provider
std::shared_ptr<openfeature::Client> client_default = api.GetClient();
// A client bound to the CachedProvider provider
std::shared_ptr<openfeature::Client> domain_scoped_client = api.GetClient("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.
The tracking API allows you to use OpenFeature abstractions and objects to associate user actions with feature flag evaluations.
This is essential for robust experimentation powered by feature flags.
For example, a flag enhancing the appearance of a UI component might drive user engagement to a new feature; to test this hypothesis, telemetry collected by a hook or provider can be associated with telemetry reported in the client's track function.
Note that some providers may not support tracking; check the documentation for your provider for more information.
Transaction context is a container for transaction-specific evaluation context (e.g. user id, user agent, IP). Transaction context can be set where specific data is available (e.g. an auth service or request handler) and by using the transaction context propagator it will automatically be applied to all flag evaluations within a transaction (e.g. a request or thread).
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.
// shut down all providers
openfeature::OpenFeatureAPI::GetInstance().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 FeatureProvider interface exported by the OpenFeature SDK.
#include "openfeature/provider.h"
class MyProvider : public openfeature::FeatureProvider {
public:
openfeature::Metadata GetMetadata() const override {
return {"My Provider"};
}
absl::Status Init(const openfeature::EvaluationContext& ctx) override {
// start up your provider
return absl::OkStatus();
}
absl::Status Shutdown() override {
// shut down your provider
return absl::OkStatus();
}
std::unique_ptr<openfeature::BoolResolutionDetails> GetBooleanEvaluation(
std::string_view key, bool default_value,
const openfeature::EvaluationContext& ctx) override {
// resolve a boolean flag value
}
std::unique_ptr<openfeature::StringResolutionDetails> GetStringEvaluation(
std::string_view flag, std::string_view default_value,
const openfeature::EvaluationContext& ctx) override {
// resolve a string flag value
}
std::unique_ptr<openfeature::IntResolutionDetails> GetIntegerEvaluation(
std::string_view flag, int64_t default_value,
const openfeature::EvaluationContext& ctx) override {
// resolve a int flag value
}
std::unique_ptr<openfeature::DoubleResolutionDetails> GetDoubleEvaluation(
std::string_view flag, double default_value,
const openfeature::EvaluationContext& ctx) override {
// resolve a double flag value
}
std::unique_ptr<openfeature::ObjectResolutionDetails> GetObjectEvaluation(
std::string_view flag, Value default_value,
const openfeature::EvaluationContext& ctx) override {
// resolve a object flag value
}
};Built a new provider? Let us know so we can add it to the docs!
To develop a hook, 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.
Implement your own hook by conforming to the Hook interface.
To satisfy the interface, all methods (Before/After/Finally/Error) need to be defined.
To avoid defining empty functions, make use of the UnimplementedHook struct (which already implements all the empty functions).
Built a new hook? 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.