App Lifecycle Manager 功能旗標分配功能可讓您分配流量百分比,藉此測試應用程式。這樣一來,部分使用者會看到應用程式的某個變化版本,其他使用者則會看到另一個變化版本,有助於您判斷哪些變化版本或功能更成功 (這項程序通常稱為 A/B 測試)。功能旗標分配可協助您定義如何分配流量,並將分配的變化版本與應用程式整合。
本指南說明如何定義變化版本、設定流量拆分,以及將變化版本與應用程式整合。
為簡化追蹤和分析作業,建議您為變化版本指派描述性字串 ID (例如「experimental」和「baseline」),並直接在程式碼和分析管道中使用這些名稱。
必要條件
開始前,請先確認下列事項:
- 完成部署功能旗標快速入門導覽課程或使用功能旗標 (獨立快速入門導覽課程)。
- 已設定為管理 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(), )比較「實驗」組和「基準」組的指標,即可手動分析新演算法是否能提高後端效率或搜尋關聯性,再決定是否全面推出。
後續步驟
- 瞭解如何排解問題。