Zenmanage Rust SDK
Add feature flags to your Rust services. Rules are fetched once and evaluated in your process, so reading a flag doesn't call the API for an answer.
- Async, on
tokio - Targeting by context, segment and attributes
- Percentage rollouts that put the same user in the same bucket every time
- Your defaults when a flag or the API can't be reached
- Memory, filesystem, no-op or your own cache
- State helpers for Axum, Actix Web and Rocket
Requirements
- Rust 1.88 or later
- A
tokioruntime - A server key (
srv_...). This SDK is for servers, so client (cli_) and mobile (mob_) keys are rejected.
Installation
With a web framework integration:
Get started
use ;
async
Create one Zenmanage when your application starts and share it. flags() is cheap: every manager it returns uses the same loaded rules.
Targeting with a context
A context describes who or what you're evaluating for. Rules target its type, identifier and attributes.
# use ;
# async
with_context returns a manager scoped to that context. It doesn't change any other manager, so build one per request.
Attributes can hold several values. A condition such as "equals" or "contains" matches when any of them does, and its opposite matches when none does.
Percentage rollouts
There's nothing to configure in code. Start a rollout in Zenmanage and pass a context with an identifier. The SDK hashes the rollout's salt with the identifier (CRC32B), so a user stays in or out on every server and across restarts. A context without an identifier is never in a rollout.
Defaults
single looks for a value in this order:
- The flag from Zenmanage
- The default you pass to
single - The
DefaultsCollectionentry - If there is none of these, an error
# use ;
# async
If the rules can't be loaded, single logs a warning and uses your default, so an outage doesn't turn into a failed request. Give every flag a default.
Flag values
A flag is a boolean, string, number or json flag, with an accessor for each: as_bool(), as_string(), as_number() and as_json(). Use is_enabled() to ask whether a boolean flag is on.
# use Zenmanage;
# async
Calling the wrong accessor never panics and never converts. It returns an empty value:
| Flag type | as_bool() |
as_string() |
as_number() |
as_json() |
|---|---|---|---|---|
boolean |
the value | "" |
0 |
{} |
string |
true |
the value | 0 |
{} |
number |
true |
"" |
the value | {} |
json |
true |
"" |
0 |
the object or array |
as_bool() is true for every flag that isn't boolean, even a number flag set to 0. is_enabled() is false for all of them.
A default takes its type from the Rust value: bool, text, i32 or f64, or a serde_json::Value. A JSON object, array or null becomes a json default. A JSON scalar becomes the matching plain type.
All flags
# use Zenmanage;
# async
all() evaluates every flag for the manager's context. It returns an error if the rules have never loaded, and it doesn't report usage.
Caching and refreshing
Rules are loaded the first time you need them and used for the cache TTL (one hour by default), counted from when they were fetched, even if they come from a shared cache. The next read after that reloads them. If the reload fails, the SDK keeps using the rules it has and tries again within 30 seconds.
To pick up a change right away:
# use Zenmanage;
# async
Choose where the rules are cached:
# use ;
#
Use a separate cache directory for each environment. To cache somewhere else, such as Redis, implement CacheBackend and pass it to with_cache. A cache that fails is logged and skipped; the SDK fetches from the API instead.
Usage reporting
Each single call reports the flag key, your context and your default, so Zenmanage can show which flags are in use. Reports are sent in the background and never delay or fail an evaluation.
A report that's still in flight is lost when the tokio runtime shuts down. In a command-line tool, job or serverless function, call flush_usage before you exit:
# use Zenmanage;
# async
report_usage records a flag you read some other way, such as from all(). Turn reporting off with with_usage_reporting(false).
Errors and logging
Methods return zenmanage::Result, with ZenmanageError saying what failed. Requests that fail to connect, time out or get a 5xx response are tried three times.
The SDK logs through the log crate. Add a logger such as env_logger to see its warnings.
Web frameworks
Each integration puts a shared SDK in your framework's state.
| Framework | Feature | Helper | Example |
|---|---|---|---|
| Axum 0.8 | axum |
integrations::axum::extension_layer |
axum_integration.rs |
| Actix Web 4 | actix |
integrations::actix::app_data |
actix_integration.rs |
| Rocket 0.5 | rocket |
integrations::rocket::manage_sdk |
rocket_integration.rs |
async
Configuration
| Builder method | Environment variable | Default |
|---|---|---|
with_environment_token |
ZENMANAGE_ENVIRONMENT_TOKEN |
required |
with_cache_ttl (seconds) |
ZENMANAGE_CACHE_TTL |
3600 |
with_cache_backend |
ZENMANAGE_CACHE_BACKEND (memory, filesystem, null) |
memory |
with_cache_directory |
ZENMANAGE_CACHE_DIR |
required for filesystem |
with_usage_reporting |
ZENMANAGE_ENABLE_USAGE_REPORTING |
on |
with_api_endpoint |
ZENMANAGE_API_ENDPOINT |
https://api.zenmanage.com |
with_cache |
none |
ConfigBuilder::from_environment() reads the variables.
Examples
The examples folder covers contexts, defaults, caching, A/B tests, percentage rollouts and each web framework.
Versioning
The crate follows semantic versioning.
Contributing
See CONTRIBUTING.md. To report a security problem, see SECURITY.md.
License
MIT. See LICENSE.