Crates.io | open-feature-flagd |
lib.rs | open-feature-flagd |
version | |
source | src |
created_at | 2025-02-20 22:58:45.813437+00 |
updated_at | 2025-04-13 18:41:58.102407+00 |
description | The official flagd provider for OpenFeature. |
homepage | https://openfeature.dev/ |
repository | https://github.com/open-feature/rust-sdk-contrib |
max_upload_size | |
id | 1563291 |
Cargo.toml error: | TOML parse error at line 24, column 1 | 24 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A Rust implementation of the OpenFeature provider for flagd, enabling dynamic feature flag evaluation in your applications.
This provider supports multiple evaluation modes, advanced targeting rules, caching strategies, and connection management. It is designed to work seamlessly with the OpenFeature SDK and the flagd service.
Multiple Evaluation Modes
Advanced Targeting
Caching Strategies
Connection Management
Add the dependency in your Cargo.toml
:
cargo add open-feature-flagd
cargo add open-feature
Then integrate it into your application:
use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
use open_feature::provider::FeatureProvider;
use open_feature::EvaluationContext;
#[tokio::main]
async fn main() {
// Example using the REST resolver mode.
let provider = FlagdProvider::new(FlagdOptions {
host: "localhost".to_string(),
port: 8016,
resolver_type: ResolverType::Rest,
..Default::default()
}).await.unwrap();
let context = EvaluationContext::default().with_targeting_key("user-123");
let result = provider.resolve_bool_value("bool-flag", &context).await.unwrap();
println!("Flag value: {}", result.value);
}
In RPC mode, the provider communicates with flagd via gRPC. It supports features like streaming updates, retry mechanisms, and name resolution (including Envoy).
use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
use open_feature::provider::FeatureProvider;
use open_feature::EvaluationContext;
#[tokio::main]
async fn main() {
let provider = FlagdProvider::new(FlagdOptions {
host: "localhost".to_string(),
port: 8013,
resolver_type: ResolverType::Rpc,
..Default::default()
}).await.unwrap();
let context = EvaluationContext::default().with_targeting_key("user-123");
let bool_result = provider.resolve_bool_value("feature-enabled", &context).await.unwrap();
println!("Feature enabled: {}", bool_result.value);
}
In REST mode the provider uses the OpenFeature Remote Evaluation Protocol (OFREP) over HTTP. It is useful when gRPC is not an option.
use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
use open_feature::provider::FeatureProvider;
use open_feature::EvaluationContext;
#[tokio::main]
async fn main() {
let provider = FlagdProvider::new(FlagdOptions {
host: "localhost".to_string(),
port: 8016,
resolver_type: ResolverType::Rest,
..Default::default()
}).await.unwrap();
let context = EvaluationContext::default().with_targeting_key("user-456");
let result = provider.resolve_string_value("feature-variant", &context).await.unwrap();
println!("Variant: {}", result.value);
}
In-process evaluation is performed locally. Flag configurations are sourced via gRPC sync stream. This mode supports advanced targeting operators (fractional, semver, string comparisons) using the built-in evaluation engine.
use open_feature_flagd::{CacheSettings, FlagdOptions, FlagdProvider, ResolverType};
use open_feature::provider::FeatureProvider;
use open_feature::EvaluationContext;
#[tokio::main]
async fn main() {
let provider = FlagdProvider::new(FlagdOptions {
host: "localhost".to_string(),
port: 8015,
resolver_type: ResolverType::InProcess,
selector: Some("my-service".to_string()),
cache_settings: Some(CacheSettings::default()),
..Default::default()
}).await.unwrap();
let context = EvaluationContext::default()
.with_targeting_key("user-abc")
.with_custom_field("environment", "production")
.with_custom_field("semver", "2.1.0");
let dark_mode = provider.resolve_bool_value("dark-mode", &context).await.unwrap();
println!("Dark mode enabled: {}", dark_mode.value);
}
File mode is an in-process variant where flag configurations are read from a file. This is useful for development or environments without network access.
use open_feature_flagd::{FlagdOptions, FlagdProvider, ResolverType};
use open_feature::provider::FeatureProvider;
use open_feature::EvaluationContext;
#[tokio::main]
async fn main() {
let file_path = "./path/to/flagd-config.json".to_string();
let provider = FlagdProvider::new(FlagdOptions {
host: "localhost".to_string(),
resolver_type: ResolverType::File,
source_configuration: Some(file_path),
..Default::default()
}).await.unwrap();
let context = EvaluationContext::default();
let result = provider.resolve_int_value("rollout-percentage", &context).await.unwrap();
println!("Rollout percentage: {}", result.value);
}
Configurations can be provided as constructor options or via environment variables (with constructor options taking priority). The following options are supported:
Option | Env Variable | Type / Supported Value | Default | Compatible Resolver |
---|---|---|---|---|
Host | FLAGD_HOST | string | "localhost" | RPC, REST, In-Process, File |
Port | FLAGD_PORT | number | 8013 (RPC), 8016 (REST) | RPC, REST, In-Process, File |
Target URI | FLAGD_TARGET_URI | string | "" | RPC, In-Process |
TLS | FLAGD_TLS | boolean | false | RPC, In-Process |
Socket Path | FLAGD_SOCKET_PATH | string | "" | RPC |
Certificate Path | FLAGD_SERVER_CERT_PATH | string | "" | RPC, In-Process |
Cache Type (LRU / In-Memory / Disabled) | FLAGD_CACHE | string ("lru", "mem", "disabled") | lru | RPC, In-Process, File |
Cache TTL (Seconds) | FLAGD_CACHE_TTL | number | 60 | RPC, In-Process, File |
Max Cache Size | FLAGD_MAX_CACHE_SIZE | number | 1000 | RPC, In-Process, File |
Offline File Path | FLAGD_OFFLINE_FLAG_SOURCE_PATH | string | "" | File |
Retry Backoff (ms) | FLAGD_RETRY_BACKOFF_MS | number | 1000 | RPC, In-Process |
Retry Backoff Maximum (ms) | FLAGD_RETRY_BACKOFF_MAX_MS | number | 120000 | RPC, In-Process |
Retry Grace Period | FLAGD_RETRY_GRACE_PERIOD | number | 5 | RPC, In-Process |
Event Stream Deadline (ms) | FLAGD_STREAM_DEADLINE_MS | number | 600000 | RPC |
Offline Poll Interval (ms) | FLAGD_OFFLINE_POLL_MS | number | 5000 | File |
Source Selector | FLAGD_SOURCE_SELECTOR | string | "" | In-Process |
Apache 2.0 - See LICENSE for more information.