pluggit/feature-balancer

这个PHP库允许您在功能之间开启/关闭或按百分比进行平衡

4.0.0 2024-06-12 10:59 UTC

README

这个PHP库允许您在功能之间开启/关闭或按百分比进行平衡

Build Status Scrutinizer Code Quality Code Coverage

TLDR;

$balancer = (new BalancerBuilder())
    ->withLogger($logger, LogLevel::INFO)
    ->withMonitor($monitor, "my_app.feature_balanced")
    ->create([
        "home_banner" => [
            "do_not_show"   => 20,
            "amazing_offer" => 80,
        ],
        "after_update_email" => [
            "normal"        => 60,
            "special_offer" => 30,
            "do_not_send"   => 10,
        ]
    ]);

/**
 * Random Non-deterministic balance
 * - 20% change to get "do_not_show", 80% chance to get "amazing_offer"
 */
$path = $balancer->get("home_banner");

/**
 * Deterministic balance, given a configuration and a valid seed, 
 * you'll always get the same result
 *
 * Valid seeds are non-empty strings and unsigned numbers
 */
$path = $balancer->get("after_update_email", 78549612);
$path = $balancer->get("after_update_email", -4.75);
$path = $balancer->get("after_update_email", "my_user@example.com");

构建平衡器

该库提供了一个构建器,以简化平衡器的创建。

$balancer = (new BalancerBuilder())->create();

添加日志记录

如果您安装了Psr\Log库,您可以为平衡器做出的每个决策添加日志记录。您可以选择用于记录日志条目的日志级别

// Add a logger when building the balancer
$balancer = (new BalancerBuilder())->->withLogger($logger, LogLevel::DEBUG)->create($config);

/** 
 * This will log: "Feature path returned from the balancer: home_banner -> amazing_offer"
 * Plus some context information:
 * - feature
 * - path
 * - seed
 */
$balancer->get("home_banner", 9874562);

添加监控

如果您安装了pluggit\monitoring库,您可以为平衡器做出的每个决策添加监控。

您必须选择要增加的指标名称

// Add a monitor when building the balancer
$balancer = (new BalancerBuilder())->->withMonitor($monitor, "balanced_feature")->create($config);

/** 
 * This will increment the metric: "balanced_feature", plus some tags
 * - feature
 * - path
 * - seed: true/false
 */
$balancer->get("home_banner", 9874562);

隐藏异常

如果您不想功能平衡器抛出异常,您可以使用ExceptionSilencerDecorator来静默它。它将记录错误,并将请求的功能的路径返回为""

// Add a monitor when building the balancer
$balancer = (new BalancerBuilder())->->withoutExceptions()->create($config);

/** 
 * Requesting an unknown feature will return an empty string -> ""
 */
$balancer->get("unknown_feature");

向平衡器添加功能

您始终可以通过add方法向平衡器添加功能。规则如下

  • 每个功能都必须有一个非空的唯一字符串标识符,添加相同的功能将覆盖以前的配置
  • 每个功能必须至少有一个可能的路径
  • 每个功能路径必须有一个为该功能提供的非空唯一字符串标识符
  • 每个路径都分配了一个百分比
  • 每个路径的百分比必须是0到100之间的无符号整数
  • 所有路径的百分比必须正好是100

违反以下任何规则都会使平衡器抛出Cmp\FeatureBalancer\Exception\InvalidArgumentException异常

有效示例

$balancer->add("my_super_feature",  [
    "do_not_show"   => 20,
    "amazing_offer" => 80,
]);

$balancer->add("my_super_feature",  [
    "do_not_show"   => 0,
    "amazing_offer" => 100,
]);

$balancer->add("my_super_feature",  [
    "amazing_offer" => 100,
]);

无效示例

// No paths defined
$balancer->add("my_super_feature",  []);

// Percentages sum 90
$balancer->add("my_super_feature",  [
    "do_not_show"   => 90,
]);

// Percentages sum 140
$balancer->add("my_super_feature",  [
    "do_not_show"   => 60,
    "amazing_offer" => 80,
]);

// Percentages is not a valid unsigned integer
$balancer->add("my_super_feature",  [
    "do_not_show"   => "60",
    "amazing_offer" => 40,
]);

获取路径

一旦您配置了一个功能,您就可以向平衡器请求一个路径

随机非确定性检索

如果您不传递种子,将根据定义的百分比随机选择路径

$path = $balancer->get("home_banner");

基于种子的确定性检索

如果您传递种子,将使用一个简单但可靠的算法来决定路径,这样对于每个给定的配置和种子,它总是会选择相同的路径

注意 这意味着如果配置或种子发生变化,路径可能会改变

您可以将有符号数字(整数和双精度浮点数)或非空字符串作为种子传递

$path = $balancer->get("after_update_email", 78549612);
$path = $balancer->get("after_update_email", -23.54);
$path = $balancer->get("after_update_email", "my_user@example.com");