App Lifecycle Manager 功能旗標分配功能可讓您將流量分配給不同版本,以便執行 A/B 測試,並評估功能對使用者同類群組的影響。您可以設定百分比權重和隨機屬性 (例如 userID),提供一致的體驗並評估結果。
如要簡化應用程式程式碼和數據分析管道中的追蹤作業,請為每個變化版本指派描述性字串 ID,例如 experimental 和 baseline。
必要條件
開始前,請先確認下列事項:
- 已完成「部署功能旗標」、「使用 gcloud CLI 部署」或「單獨使用功能旗標」快速入門導覽課程。
- 已設定為管理 App Lifecycle Manager 資源的 Google Cloud CLI 環境。
設定功能旗標分配
如要設定實驗功能旗標,請按照下列步驟操作:
定義隨機屬性 (例如
userID),用於固定分組,確保每位使用者都能獲得一致的體驗。gcloud beta app-lifecycle-manager flags attributes create "user-id-attr" \ --key="userID" \ --attribute-value-type="STRING" \ --location=global使用分配方式定義分割 (例如 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"] }'建立
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"在後端服務中,初始化 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) }請使用描述性變化版本名稱,讓稽核和分析作業自動產生說明文件。如要稽核評估結果,請將
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(), )比較「實驗」組和「基準」組的指標,即可手動分析新演算法是否能提高後端效率或搜尋關聯性,再決定是否全面推出。
後續步驟
- 瞭解如何排解問題。