使用功能标志配置实验和 A/B 测试

App Lifecycle Manager 功能标志分配可帮助您通过拆分流量百分比来测试应用。这样,您的一些用户可以看到应用的一个变体,而其他用户可以看到另一个变体,从而帮助您确定哪些变体或功能更成功(此过程通常称为 A/B 测试)。功能标志分配可帮助您定义如何拆分流量,并将分配的变体与应用集成。

本指南介绍了如何定义变体、配置流量分配以及将变体与应用集成。

为简化跟踪和分析,您应为变体分配描述性字符串 ID(例如“experimental”和“baseline”),并在代码和分析流水线中直接使用这些名称。

前提条件

开始之前,请确保您满足以下方面的要求:

  1. 已完成“部署功能标志”快速入门或“使用功能标志”(独立快速入门)。
  2. 配置为管理 App Lifecycle Manager 资源的 Google Cloud CLI 环境。

配置功能标志分配

如需配置实验性功能标志,请执行以下操作:

  1. 定义将用于粘性分桶的随机属性(例如 userID),以确保每位用户都能获得一致的体验。

      gcloud beta app-lifecycle-manager flags attributes create "user-id-attr" \
          --key="userID" \
          --attribute-value-type="STRING" \
          --location=global
    
  2. 使用分配来定义拆分(例如 50/50),并引用描述性变体 ID。

    # Create a flag with explicitly named variants for the experiment and a 50/50 allocation referencing the custom IDs
    gcloud beta app-lifecycle-manager flags create "search-algo-test" \
      --key="search-algo-test" \
      --flag-value-type=BOOL \
      --location="global" \
      --unit-kind="demo-test-unitkind" \
      --variants='[
          {
          "id": "experimental",
          "booleanValue": true
          },
          {
          "id": "baseline",
          "booleanValue": false
          }
      ]'\
      --evaluation-spec='{
          "allocations": [{
              "id": "search-split-50-50",
              "randomizedOn": "userID",
              "slots": [
              {"variant": "baseline", "weight": 50},
              {"variant": "experimental", "weight": 50}
              ]
          }],
          "defaultAllocation": "search-split-50-50",
          "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/user-id-attr"]
      }'
    
  3. 创建 FlagRelease,并使用 Rollout 将其部署到您的单元:

    gcloud beta app-lifecycle-manager flags releases create "search-algo-experiment-v1" \
        --location="global" \
        --unit-kind="demo-test-unitkind" \
        --flags="search-algo-test"
    
    gcloud beta app-lifecycle-manager rollout-kinds create "rollout-kind-of-search-algo" \
        --unit-kind="demo-test-unitkind" \
        --rollout-orchestration-strategy="Google.Cloud.Simple.AllAtOnce" \
        --location="global"
    
    gcloud beta app-lifecycle-manager rollouts create "rollout-search-algo-experiment" \
        --flag-release="search-algo-experiment-v1" \
        --rollout-kind="rollout-kind-of-search-algo" \
        --location="global"
    
  4. 在后端服务中,初始化 OpenFeature SDK 并将 userID 注入评估上下文。使用 BooleanValueDetails(或您所用语言的等效方法)方法在应用中检索 variantID(字符串)。这样,您就可以根据描述性名称(而不仅仅是布尔值)切换后端逻辑。

    // 1. Prepare evaluation context
    evalCtx := openfeature.NewEvaluationContext(
        currentUser.ID,
        map[string]any{"userID": currentUser.ID},
    )
    
    // 2. Fetch evaluation details to get the variant name
    details, err := client.BooleanValueDetails(ctx, "search-algo-test", false, evalCtx)
    
    // 3. Execute logic based on the Variant ID (string name)
    if details.Variant == "experimental" {
        results = search.ExperimentalV2(query)
    } else {
        results = search.BaselineV1(query)
    }
    
  5. 使用描述性变体名称,以便您的审核和分析自动生成文档。如需审核评估,请将 details.Variant 字符串与效果指标一起记录:

     startTime := time.Now()
     // ... perform search ...
     duration := time.Since(startTime)
    
     // Audit: Log the descriptive variant name ("experimental" or "baseline")
     logger.Info("Search performed",
        "variant", details.Variant,
        "latency_ms", duration.Milliseconds(),
     )
    

    通过比较“实验”组与“基准”组的指标,您可以手动分析新算法是否提高了后端效率或搜索相关性,然后再继续全面发布。

后续步骤