使用 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. 如需为 HTTPS 请求生成 HTTP 级分布式跟踪 span 并传播 W3C 跟踪记录上下文标头,您必须在 Secure Web Proxy 网关上启用 TLS 检查

角色与权限

如需获得配置遥测政策和查看轨迹所需的权限,请让管理员为您授予以下 Identity and Access Management (IAM) 角色:

  • 如需创建、更新或删除遥测政策:Network Services Admin (roles/networkservices.admin) 或 Compute Network Admin (roles/compute.networkAdmin)
  • 如需在 Google Cloud 控制台中查看和分析跟踪记录范围:Cloud Trace User (roles/cloudtrace.user)
  • 如需将跟踪范围导出到 Trace,请确保以下服务账号拥有 Cloud Trace Agent 角色 (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 字符串 遥测政策的唯一标识符。
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 探索器页面。

    前往 Trace 探索器

  2. 选择您的 Google Cloud 项目。

  3. 过滤条件字段中,使用各种属性搜索轨迹。

  4. 点击散点图或时间轴视图中的任何轨迹,即可检查 span 详细信息,例如总时长、代理处理延迟时间和外部服务器 IP 详细信息。

将跟踪记录与日志和指标相关联

Secure Web Proxy 可关联轨迹、日志和指标:

  • 日志-轨迹相关性:写入 Cloud Logging 的每个代理事务日志条目都包含一个 trace 字段,其格式为:projects/PROJECT_ID/traces/TRACE_ID

    • Logs Explorer中,点击日志条目,然后选择查看跟踪记录详细信息,即可直接前往 Trace 中对应的 span。
    • 在 Trace 中,点击跟踪详情窗格中的显示日志,以查看与相应请求关联的所有事务日志。
  • 指标相关性分析:将 Cloud Monitoring 指标(例如 networkservices.googleapis.com/https/total_latencies)中观察到的延迟时间峰值与跟踪区间细分进行比较,以确定延迟时间峰值是出现在代理中还是外部源服务器中。

后续步骤