使用 Cloud Trace 追蹤輸出流量

本文說明如何使用 Cloud Trace,為 Secure Web Proxy 設定分散式要求追蹤。

如要瞭解相關概念、主要優點和架構詳細資料,請參閱分散式追蹤總覽

事前準備

  1. 完成初始設定步驟
  2. 在專案中啟用 Compute Engine API、Network Services API 和 Cloud Trace API。

    gcloud services enable \
        compute.googleapis.com \
        networkservices.googleapis.com \
        cloudtrace.googleapis.com
    
  3. 請確認您已部署有效的 Secure Web Proxy 閘道執行個體。詳情請參閱「部署 Secure Web Proxy 執行個體」。

  4. 如要產生 HTTP 層級的分散式追蹤範圍,並為 HTTPS 要求傳播 W3C 追蹤內容標頭,您必須在 Secure Web Proxy 閘道上啟用 TLS 檢查

角色和權限

如要取得設定遙測政策及查看追蹤記錄所需的權限,請要求管理員授予您下列 Identity and Access Management (IAM) 角色:

  • 如要建立、更新或刪除遙測政策:網路服務管理員 (roles/networkservices.admin) 或 Compute 網路管理員 (roles/compute.networkAdmin)
  • 如要在 Google Cloud 控制台中查看及分析追蹤記錄範圍,請執行下列操作: Cloud Trace 使用者 (roles/cloudtrace.user)
  • 如要將追蹤範圍匯出至 Cloud Trace,請確認下列服務帳戶具有 Cloud Trace 代理者角色 (roles/cloudtrace.agent):

    • Compute Engine 服務代理人: service-PROJECT_NUMBER@compute-system.
    • 網路安全服務代理人: service-PROJECT_NUMBER@gcp-sa-networksecurity.
    • 用戶端虛擬機器 (VM) 服務帳戶 (如果用戶端應用程式產生或傳播 span)

遙測政策設定結構定義

如要在 YAML 檔案中定義遙測政策,請使用下列結構:

    name: projects/PROJECT_ID/locations/REGION/telemetryPolicies/POLICY_NAME
    telemetryTarget:
      resources:
      - "//networkservices.googleapis.com/projects/PROJECT_ID/locations/REGION/gateways/GATEWAY_NAME"
    description: "OPTIONAL_DESCRIPTION"
    tracingConfiguration:
      samplingRate: SAMPLING_RATE
      parentBasedSampling:
        enabled: true
  

下表說明遙測政策中的欄位:

欄位 類型 說明
name 字串 遙測政策的專屬 ID。
telemetryTarget.resources 字串清單 要套用這項政策的 Secure Web Proxy 閘道資源網址。閘道必須與政策位於相同區域。

格式: //networkservices.googleapis.com/projects/PROJECT_ID/locations/REGION/gateways/GATEWAY_NAME

或者,也可以使用簡稱格式: projects/PROJECT_ID/locations/REGION/gateways/GATEWAY_NAME

description 字串 選用。這個欄位提供遙測政策的說明,方便使用者閱讀。
tracingConfiguration.samplingRate 浮點值 要追蹤的要求比例,指定為介於 0.0 (0%) 和 1.0 (100%) 之間的小數。舉例來說, 0.01 會對 1% 的要求取樣。
tracingConfiguration.parentBasedSampling.enabled 布林值 指出是否要啟用上游追蹤取樣決策 (truefalse)。

設定分散式追蹤

如要使用 Google Cloud CLI 將追蹤政策套用至 Secure Web Proxy 閘道,請執行下列操作:

  1. 建立名為 swp-tracing-policy.yaml 的 YAML 政策檔案。

    cat <<EOF > swp-tracing-policy.yaml
    name: projects/PROJECT_ID/locations/REGION/telemetryPolicies/my-swp-tracing-policy
    telemetryTarget:
      resources:
        - "//networkservices.googleapis.com/projects/PROJECT_ID/locations/REGION/gateways/GATEWAY_NAME"
    description: "Distributed tracing policy for the Secure Web Proxy gateway"
    tracingConfiguration:
      samplingRate: 0.01
      parentBasedSampling:
        enabled: true
    EOF
    

    更改下列內容:

    • PROJECT_ID: Google Cloud 專案的 ID
    • REGION:部署 Secure Web Proxy 閘道的區域,例如 us-central1
    • GATEWAY_NAME:Secure Web Proxy 閘道執行個體的名稱
  2. 使用 gcloud beta network-services telemetry-policies import 指令匯入遙測政策:

    gcloud beta network-services telemetry-policies import my-swp-tracing-policy \
        --source=swp-tracing-policy.yaml \
        --location=REGION
    
  3. 使用 gcloud beta network-services telemetry-policies describe 指令確認政策已套用:

    gcloud beta network-services telemetry-policies describe my-swp-tracing-policy \
        --location=REGION
    

附加或卸離目標閘道

如要更新政策定義或附加多個區域閘道,請按照下列步驟操作:

  1. 更新 swp-tracing-policy.yaml 檔案中的 telemetryTarget.resources 清單,納入所有目標閘道資源 URI。

    telemetryTarget:
      resources:
        - "//networkservices.googleapis.com/projects/PROJECT_ID/locations/us-central1/gateways/prod-swp-uc1"
        - "//networkservices.googleapis.com/projects/PROJECT_ID/locations/us-central1/gateways/staging-swp-uc1"
    
  2. 使用 gcloud beta network-services telemetry-policies import 指令匯入更新後的遙測政策:

    gcloud beta network-services telemetry-policies import my-swp-tracing-policy \
        --source=swp-tracing-policy.yaml \
        --location=REGION
    

    更改下列內容:

    • PROJECT_ID: Google Cloud 專案的 ID
    • REGION:部署 Secure Web Proxy 閘道的區域,例如 us-central1

在 Trace 中查看及分析追蹤記錄

  1. 前往 Google Cloud 控制台的「Trace Explorer」頁面。

    前往「Trace Explorer」

  2. 選取 Google Cloud 專案。

  3. 在「Filter」(篩選器) 欄位中,使用各種屬性搜尋追蹤記錄。

  4. 按一下散布圖或時間軸檢視畫面中的任何追蹤記錄,即可檢查時距詳細資料,例如總時間長度、Proxy 處理延遲時間和外部伺服器 IP 詳細資料。

將追蹤記錄與記錄和指標相關聯

Secure Web Proxy 會建立追蹤記錄、記錄和指標之間的關聯:

  • 記錄檔與追蹤記錄的關聯:寫入 Cloud Logging 的每個 Proxy 交易記錄項目都包含 trace 欄位,格式為:projects/PROJECT_ID/traces/TRACE_ID

    • Logs Explorer 中,按一下記錄項目,然後選取 View trace details,直接前往 Trace 中對應的時距。
    • 在 Trace 的「Trace Details」(追蹤記錄詳細資料) 窗格中,按一下「Show logs」(顯示記錄),即可查看與該要求相關聯的所有交易記錄。
  • 指標關聯:比較 Cloud Monitoring 指標 (例如 networkservices.googleapis.com/https/total_latencies) 中觀察到的延遲尖峰,以及追蹤範圍的細目,判斷延遲尖峰發生在 Proxy 內,還是外部來源伺服器。

後續步驟