您可以透過 App Lifecycle Manager 功能旗標分配,依流量百分比分割流量,測試應用程式。部分使用者會看到應用程式的某個版本,其他使用者則會看到不同版本,協助您判斷哪個版本或功能較為成功 (這個程序通常稱為 A/B 測試)。功能旗標分配作業可協助您定義如何分配流量,以及將分配到的變體與應用程式整合。
本指南說明如何定義變數、設定流量分配,以及將變數與應用程式整合。
為簡化追蹤和分析作業,您應為變數指派說明性字串 ID (例如「實驗」和「基準」),並直接在程式碼和分析管道中使用這些名稱。
必要條件
開始前,請先確認下列事項:
- 完成部署功能旗標快速入門導覽課程,或使用功能旗標 (獨立快速入門導覽課程)。
- 已設定 Google Cloud CLI 環境,可管理 App Lifecycle Manager 資源。
設定功能旗標分配
如要設定實驗功能旗標,請按照下列步驟操作:
定義隨機屬性 (例如
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} ] }], "defaultTarget": "search-split-50-50", "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/user-id-attr"] }'在後端服務中,初始化 OpenFeature SDK,並將
userID插入評估環境。使用BooleanValueDetails(或您語言的對等項目) 方法,在應用程式中擷取variantID(字串)。這樣一來,您就能根據描述性名稱切換後端邏輯,而不只是布林值。// 1. Prepare evaluation context evalCtx := 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(), )比較「實驗」群組和「基準」群組的指標,即可手動分析新演算法是否能提升後端效率或搜尋關聯性,再決定是否全面推出。
後續步驟
- 瞭解如何排解問題。