Use Toggle to Roll out a Feature
We will guide you to use the FeatureProbe platform to control a back-end or front-end program, and let the back-end or front-end program display new features by percentage of received user requests.
Create a toggle on the platform
- Log in to the FeatureProbe demo platform. If you log in for the first time, please enter your email address. You can continue to use your email to access your data in the future.
- Click
+Toggle
to create a new toggle. - Set the name and key to
tutorial_rollout
, clickCreate and publish
. - Click
tutorial_rollout
from the toggle list to open the targeting details page. - Change the return variation of the default rule to
a percentage rollout
. - Set 10% to open the toggle (return true), 90% to close the toggle (return false), and set the status to
enabled
. - Click the
Publish
button below andConfirm
the changes.
Till now, the operation on the platform is complete. We have created a toggle to manage gradual rollout. Next, we will use it in the program to see the actual effect.
After the toggle is created, it can be accessed in the back-end program or in the front-end program. Below we will introduce how to use it in Back-end code and Front-end code this toggle, the two are independent of each other, you can choose to read the part you are interested in according to your needs.
Control the backend program
We provide a backend code example, from which you can start to experience how the backend code uses the toggle.
Write code
- According to the language you are familiar with, download and open the corresponding back-end sample code.
git clone https://github.com/FeatureProbe/server-sdk-java.git
cd server-sdk-java
Open src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java
file with an editor.
- Java
- Go
- Rust
- Python
- Node.js
git clone https://github.com/FeatureProbe/server-sdk-java.git
cd server-sdk-java
Open src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java
file with an editor.
git clone https://github.com/FeatureProbe/server-sdk-go.git
cd server-sdk-go
Open the example/main.go
file with an editor.
git clone https://github.com/FeatureProbe/server-sdk-rust.git
cd server-sdk-rust
Open the examples/demo.rs
file with an editor.
git clone https://github.com/FeatureProbe/server-sdk-python.git
cd server-sdk-python
Open the demo.py
file with an editor.
git clone https://github.com/FeatureProbe/server-sdk-node.git
cd server-sdk-node
Open the examples/demo.js
file with an editor.
Open the FeatureProbe platform project list page, you can click
Projects
on the toggle details page to openCopy
Server SDK Key
Fill in
Server SDK Key
andFeatureProbe remote URL
("https://featureprobe.io/server") into the corresponding variables of the backend code
- Java
- Go
- Rust
- Python
- Node.js
private static final String FEATURE_PROBE_SERVER_URL = "https://featureprobe.io/server";
private static final String FEATURE_PROBE_SERVER_SDK_KEY = // Fill in the server SDK key
config := featureprobe.FPConfig{
RemoteUrl: "https://featureprobe.io/server",
ServerSdkKey: // Fill in the server SDK key
RefreshInterval: 5000, // ms
WaitFirstResp: true,
}
let remote_url = "https://featureprobe.io/server";
let server_sdk_key = // Fill in the server SDK key
FEATURE_PROBE_SERVER_URL = 'https://featureprobe.io/server'
FEATURE_PROBE_SERVER_SDK_KEY = # Fill in the server SDK key
const FEATURE_PROBE_SERVER_URL = 'https://featureprobe.io/server';
const FEATURE_PROBE_SERVER_SDK_KEY = // Fill in the server SDK key
- Add the following code to simulate 100 users accessing this toggle.
- Java
- Go
- Rust
- Python
- Node.js
public static void main(String[] args) throws IOException {
Logger root = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.WARN);
final FPConfig config = FPConfig.builder()
.remoteUri(FEATURE_PROBE_SERVER_URL)
.build();
// Init FeatureProbe, share this FeatureProbe instance in your project.
final FeatureProbe fpClient = new FeatureProbe(FEATURE_PROBE_SERVER_SDK_KEY, config);
//
final String YOUR_TOGGLE_KEY = "tutorial_rollout";
for (Integer i = 0; i < 100; i++) {
FPUser user = new FPUser();
Boolean isOpen = fpClient.boolValue(YOUR_TOGGLE_KEY, user, false);
System.out.println("feature for user " + i + " is :" + isOpen);
}
fpClient.close();
}
func main() {
config := featureprobe.FPConfig{
RemoteUrl: "https://featureprobe.io/server",
ServerSdkKey: // Fill in the server SDK key
RefreshInterval: 5000, // ms
WaitFirstResp: true,
}
fp, err := featureprobe.NewFeatureProbe(config)
if err != nil {
fmt.Println(err)
return
}
for i:=0; i<100; i++ {
user := featureprobe.NewUser()
detail := fp.BoolValue("tutorial_rollout", user, false)
fmt.Println("feature for user", i, "is:", detail)
}
fp.Close()
}
#[tokio::main]
async fn main() {
let remote_url = "https://featureprobe.io/server";
let server_sdk_key = // Fill in the server SDK key
let config = FPConfig {
remote_url: remote_url.to_owned(),
server_sdk_key: server_sdk_key.to_owned(),
refresh_interval: Duration::from_millis(2000),
..Default::default()
};
let fp = match FeatureProbe::new(config) {
Ok(fp) => fp,
Err(e) => {
tracing::error!("{:?}", e);
return;
}
};
for n in 1..100 {
let user = FPUser::new();
let enable = fp.bool_value("tutorial_rollout", &user, false);
println!("feature for user {:?} is: {:?}", n, enable);
}
fp.close();
}
logging.basicConfig(level=logging.WARNING)
if __name__ == '__main__':
FEATURE_PROBE_SERVER_URL = 'https://featureprobe.io/server'
FEATURE_PROBE_SERVER_SDK_KEY = # Fill in the server SDK key
config = fp.Config(remote_uri=FEATURE_PROBE_SERVER_URL, # FeatureProbe server URL
sync_mode='polling',
refresh_interval=3)
with fp.Client(FEATURE_PROBE_SERVER_SDK_KEY, config) as client:
for i in range(100):
user = fp.User()
is_open = client.value('tutorial_rollout', user, default=False)
print('feature for user ' + str(i) + ' is: ' + str(is_open))
const fpClient = new featureProbe.FeatureProbe({
remoteUrl: FEATURE_PROBE_SERVER_URL,
serverSdkKey: FEATURE_PROBE_SERVER_SDK_KEY,
refreshInterval: 5000,
});
for(let i = 0; i < 100; i++) {
const user = new featureProbe.FPUser();
const isOpen = fpClient.booleanValue(YOUR_TOGGLE_KEY, user, false);
console.log("feature for user " + i + " is :" + isOpen);
}
- Run the program.
- Java
- Go
- Rust
- Python
- Node.js
mvn package
java -jar ./target/server-sdk-java-1.4.0.jar
go run example/main.go
cargo run --example demo
pip3 install -r requirements.txt
python3 demo.py
node demo.js
Validate result
From the command line log, we can see that about 10% of the users entered the toggle, that is, they got the return variation value true.
log example
feature for user 0 is :false
feature for user 1 is :true
feature for user 2 is :false
feature for user 3 is :false
feature for user 4 is :false
feature for user 5 is :false
feature for user 6 is :false
feature for user 7 is :false
feature for user 8 is :false
feature for user 9 is :false
feature for user 10 is :false
feature for user 11 is :true
feature for user 12 is :false
feature for user 13 is :false
feature for user 14 is :false
feature for user 15 is :false
feature for user 16 is :false
feature for user 17 is :false
feature for user 18 is :false
feature for user 19 is :false
feature for user 20 is :false
Every time the program is run, the user who enters the toggle may be different. If users with the same id always get the same toggle result, you need to use FPUser's stableRollout interface.
You can go back to the toggle targeting page of the platform, adjust the percentage, and then re-run the server program to see if the ratio of entering the toggle in the log has changed.
Control the front-end program
We provide a front-end js code example, and you can start to experience how the front-end code uses toggles.
Write code
- Clone the code.
git clone https://github.com/FeatureProbe/client-sdk-js.git
cd client-sdk-js
- Open platform to get client sdk key.
Click the "Projects" tab to enter the "Projects" list, obtain various SDK keys, and modify service and environment information.
- Open
example/index.html
and fill inClient SDK Key
andFeatureProbe URL
("https://featureprobe.io/server")
const fpClient = new featureProbe.FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});
- Simulate the current user accessing the toggle
tutorial_rollout
and get the toggle status directly
<script>
const user = new featureProbe.FPUser();
const fpClient = new featureProbe.FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});
fpClient.start();
fpClient.on("ready", function() {
const boolValue = fpClient. boolValue("tutorial_rollout", false);
document.getElementById("boolean-result").innerText = boolValue;
});
</script>
Validate result
Open index.html
in the browser, manually refresh the page (simulating multiple visits by the user), and you can see that the page can get the return value of true
or false
according to the configured percentage.
Demo
- True
- False
FeatureProbe JS SDK demo
This is a simple front-end-only application using FeatureProbe JS SDK.
boolean type
FeatureProbe evaluation boolean type toggle result is : true
FeatureProbe JS SDK demo
This is a simple front-end-only application using FeatureProbe JS SDK.
boolean type
FeatureProbe evaluation boolean type toggle result is : false
You can go back to the toggle targeting page of the platform, adjust the percentage, and then refresh the page to see if the obtained true
/false
has changed.
If you want the same user, no matter how he refreshes, he will always get a stable value. You need to Stable Rollout, and pass in the user's unique ID.