日志、指标和跟踪记录

本文档介绍了安全 Web 代理如何记录其运营数据,如何使用 Cloud Monitoring API 指标分析代理性能,以及如何使用分布式跟踪来观察请求生命周期。

日志

Secure Web Proxy 使用 Cloud Logging 让您捕获信息并在以下关键领域执行各种任务:

  • 监控和合规性

    • 强制遵守法规,提高网络安全性,并提供对发送到允许的外部目的地的出站 Web 流量的可见性。
    • 获取对工作负载流量的宝贵控制层和可见性。
    • 获取关键洞见,以便有效监控安全运维中心 (SOC)。
    • 使用日志检测和跟踪安全性事件,以便主动应对威胁。
  • 部署和配置

    • 跟踪初始基础架构设置、用户账号创建和配置更改。
    • 监控潜在错误,以帮助确保部署顺利且安全。
    • 了解政策调整的影响并优化保护。

日志类型

Secure Web Proxy 使用日志记录捕获和存储以下类型的日志:

  • Cloud Audit Logs:捕获对 Secure Web Proxy 资源的管理更改和数据访问。这些日志包括:

  • 代理事务日志: 捕获有关 您的 Secure Web Proxy 处理的特定 Web 请求的详细信息。这些日志可提供有关安全 Web 代理实例的流量流、政策执行情况和性能的洞见。

配置日志提醒

您可以为 Secure Web Proxy 写入日志记录的事件(例如流量拒绝或特定规则匹配)配置提醒。如需有关如何设置这些提醒的一般说明,请参阅 配置基于日志的提醒。

以下是一些可用于为 Secure Web Proxy 配置基于日志的提醒的查询示例:

针对 Secure Web Proxy 政策拒绝的流量的提醒

当 Secure Web Proxy 政策拒绝请求时,使用以下查询触发提醒:

logName="projects/PROJECT_ID/logs/networkservices.googleapis.com%2Fgateway_requests"
resource.type="networkservices.googleapis.com/Gateway"
jsonPayload.enforcedGatewaySecurityPolicy.matchedRules.action="DENIED"

将 PROJECT_ID 替换为目标 Google Cloud 项目的 ID。

针对 Secure Web Proxy 规则拒绝的流量的提醒

当特定 Secure Web Proxy 规则(例如 my-specific-deny-rule)拒绝请求时,使用以下查询触发提醒:

logName="projects/PROJECT_ID/logs/networkservices.googleapis.com%2Fgateway_requests"
resource.type="networkservices.googleapis.com/Gateway"
jsonPayload.enforcedGatewaySecurityPolicy.matchedRules.action="DENIED"
AND jsonPayload.enforcedGatewaySecurityPolicy.matchedRules.name=~"projects/PROJECT_ID/locations/REGION/gatewaySecurityPolicies/POLICY_NAME/rules/my-specific-deny-rule$"

替换以下内容:

  • PROJECT_ID:目标 Google Cloud 项目的 ID
  • REGION:Secure Web Proxy 实例的区域
  • POLICY_NAME:Secure Web Proxy 政策的名称

针对服务速率限制政策限制的流量的提醒

当服务速率限制政策限制请求时,使用以下查询触发提醒:

logName="projects/PROJECT_ID/logs/networkservices.googleapis.com%2Fgateway_requests"
resource.type="networkservices.googleapis.com/Gateway"
jsonPayload.rateLimitPolicyInfo.overallResult="THROTTLED"

将 PROJECT_ID 替换为目标 Google Cloud 项目的 ID。

针对高延迟请求的提醒

当请求超过特定延迟时间(例如 5 秒)时,使用以下查询接收通知:

logName="projects/'PROJECT_ID'/logs/networkservices.googleapis.com%2Fgateway_requests"
resource.type="networkservices.googleapis.com/Gateway"
latency >= "5s"

将 PROJECT_ID 替换为目标 Google Cloud 项目的 ID。

在 Cloud Logging 中查看日志

  1. 在 Google Cloud 控制台中,前往 Logs Explorer 页面。

    转到 Logs Explorer

  2. 选择您的 Google Cloud 项目。

  3. 使用下拉菜单选择 networkservices.googleapis.com/Gateway 资源或安全 Web 代理实例。

如需了解详情,请参阅 使用日志浏览器。

日志条目示例

安全 Web 代理每次处理请求时都会生成详细的日志条目,跟踪其操作和应用的政策。以下示例展示了 Secure Web Proxy 日志的工作方式。

允许日志条目示例

以下日志条目显示,Secure Web Proxy 实例拦截并检查了 www.example.com的 HTTPS 流量,然后允许其继续访问目标网站。政策和规则名称分别为 swp-policy 和 allow-port-443。

字段 值
enforcedGatewaySecurityPolicy
{
        "requestWasTlsIntercepted": true,
        "hostname": "www.example.com",
        "matchedRules": [
          {
            "action": "ALLOWED",
            "name": "projects/76537/locations/us-central1/gatewaySecurityPolicies/swp-policy/rules/allow-port-443"
          }
        ]
      }
httpRequest
{
        "requestMethod": "GET",
        "requestUrl": "https://www.example.com/",
        "requestSize": "41",
        "status": 200,
        "responseSize": "1446",
        "userAgent": "curl/7.74.0",
        "remoteIp": "10.128.0.12:35418",
        "serverIp": "93.184.216.34:443",
        "latency": "0.051800s",
        "protocol": "HTTP/2"
      }
resource
{
        "type": "networkservices.googleapis.com/Gateway",
        "labels": {
          "gateway_name": "multi-level-tlds",
          "network_name": "projects/76537/global/networks/default",
          "location": "us-central1",
          "resource_container": "",
          "gateway_type": "SECURE_WEB_GATEWAY"
        }
      }
timestamp "2024-02-15T16:56:19.570534Z"
severity "INFO"
logName "projects/76537/logs/networkservices.googleapis.com%2Fgateway_requests"
receiveTimestamp "2024-02-15T16:56:20.714988329Z"

拒绝日志条目示例

此日志条目显示,Secure Web Proxy 实例检查了流量,检查对象为 www.example.com:443,并由于 Secure Web Proxy 政策中的 default_denied 规则而拒绝了 HTTPS 请求。

字段 值
enforcedGatewaySecurityPolicy
{
        "hostname": "www.example.com:443",
        "matchedRules": [
          {
            "name": "default_denied",
            "action": "DENIED"
          }
        ]
      }
httpRequest
{
        "requestMethod": "CONNECT",
        "requestSize": "122",
        "status": 403,
        "responseSize": "141",
        "userAgent": "curl/7.74.0",
        "remoteIp": "10.128.0.12:36338",
        "latency": "0.000133s",
        "protocol": "HTTP/1.1"
      }
resource
{
        "type": "networkservices.googleapis.com/Gateway",
        "labels": {
          "gateway_type": "SECURE_WEB_GATEWAY",
          "resource_container": "",
          "location": "us-central1",
          "network_name": "projects/gcp-1768/global/networks/default",
          "gateway_name": "high-latency-repro"
        }
      }
timestamp "2024-02-15T16:55:00.089727Z"
severity "WARNING"
logName "projects/gcp-1768/logs/networkservices.googleapis.com%2Fgateway_requests"
receiveTimestamp "2024-02-15T16:55:04.456901833Z"

服务速率限制政策日志条目示例

以下日志条目显示,Secure Web Proxy 实例限制了请求,因为客户端超出了 rl_default_rpm_limit RateLimitBucket 下的 RateLimitPolicy(名为 rl-egress-policy)。

字段 值
rateLimitPolicyInfo
{
          "policies": [
            {
              "name": "projects/76537/locations/us-central1/rateLimitPolicies/rl-egress-policy",
              "buckets": [
                {
                  "bucket": "rl_default_rpm_limit",
                  "key": "spiffe://projects-123.global.workload.id.goog/ns/ns1/sa/hellomcp",
                  "result": "THROTTLED"
                }
              ],
              "result": "THROTTLED",
              "details": "Default user rate limit rate exceeded client-side."
            }
          ],
          "overallResult": "THROTTLED"
        }
httpRequest
{
          "requestMethod": "POST",
          "requestUrl": "https://example.com/v1/chat/completions",
          "requestSize": "144",
          "status": 429,
          "responseSize": "85",
          "userAgent": "curl/7.74.0",
          "remoteIp": "10.128.0.12:35418",
          "serverIp": "93.184.216.34:443",
          "latency": "0.001200s",
          "protocol": "HTTP/2"
        }
resource
{
          "type": "networkservices.googleapis.com/Gateway",
          "labels": {
            "gateway_type": "SECURE_WEB_GATEWAY",
            "resource_container": "",
            "location": "us-central1",
            "network_name": "projects/76537/global/networks/default",
            "gateway_name": "egress-swp-gateway"
          }
        }
timestamp "2026-02-15T16:56:19.570534Z"
severity "WARNING"
logName "projects/76537/logs/networkservices.googleapis.com%2Fgateway_requests"
receiveTimestamp "2026-02-15T16:56:20.714988329Z"

使用 Cloud Trace 进行分布式跟踪

Secure Web Proxy 支持与 Trace集成的内置分布式请求跟踪。

日志跟踪记录关联

写入 Logging 的每个代理事务日志条目 都包含一个 trace 字段,其中包含 Google Cloud 跟踪记录资源 字符串:

<pre class="devsite-click-to-copy">
"trace": "projects/PROJECT_ID/traces/TRACE_ID"
</pre>

替换以下内容:

  • PROJECT_ID:您的 Google Cloud 项目的 ID
  • TRACE_ID:来自客户端或跟踪记录 span 的 128 位(32 个十六进制字符)跟踪记录 ID。例如,4bf92f3577b34da6a3ce929d0e0e4736。

您可以使用此跟踪记录 ID 将日志条目与其对应的跟踪记录 span 相关联:

  • 从日志到跟踪记录:在 Logs Explorer 中,点击任何日志 条目的 trace 字段,然后选择查看跟踪记录详细信息 以在 Trace 中打开 完整请求瀑布图。
  • 从跟踪记录到日志:在 Trace 中,点击显示日志在 跟踪记录详细信息面板上,以查看与跟踪记录 关联的所有代理事务日志。

如需了解详情,请参阅使用 Trace 进行分布式跟踪。

指标

Cloud Monitoring API 指标可帮助您观察 Secure Web Proxy 实例的性能、健康状况和使用情况。

受监控的资源

Secure Web Proxy 的指标在以下受监控的资源下导出:

  • networkservices.googleapis.com/Gateway:表示 Secure Web Proxy 网关实例。

下表介绍了 networkservices.googleapis.com/Gateway 的可用资源。您可以使用这些资源来过滤和汇总监控数据。

标签 说明
resource_container 与 Secure Web Proxy 网关实例关联的项目的 ID。
location Google Cloud 部署网关的区域。
gateway_id Secure Web Proxy 网关实例的唯一名称。
gateway_type 网关类型,设置为 SECURE_WEB_GATEWAY。
network_name 与网关关联的 Virtual Private Cloud (VPC) 网络的名称。

可用指标

Gateway 资源提供以下指标。所有指标都以 networkservices.googleapis.com/https/ 为前缀。

指标类型 显示名称 种类、类型、单元 说明
networkservices.googleapis.com/https/request_count 请求数 DELTA、INT64、1 代理处理的请求总数。
networkservices.googleapis.com/https/request_bytes_count 请求字节数 DELTA、INT64、By 请求中收到的字节总数。
networkservices.googleapis.com/https/response_bytes_count 响应字节数 DELTA、INT64、By 响应中发送的字节总数。
networkservices.googleapis.com/https/total_latencies 总延迟时间 DELTA、DIST、ms 从收到请求的第一个字节到发送响应的最后一个字节的时间。
networkservices.googleapis.com/https/backend_latencies 后端延迟时间 DELTA、DIST、ms 从请求发送到后端到收到响应的第一个字节的时间。

创建自定义信息中心

如需构建自定义信息中心以直观呈现 Secure Web Proxy 实例的性能和流量趋势,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往信息中心 页面。

    转到“信息中心”

  2. 点击 创建自定义信息中心 。

  3. 点击 添加 Widget ,然后 选择 折线图 选项。

  4. 在指标 字段中,搜索 networkservices.googleapis.com/https/request_count。

  5. 在过滤条件 字段中,如果您配置了多个 Secure Web Proxy 实例,则可以按 gateway_id 进行过滤。

  6. 在聚合 部分中,将对齐周期 设置为 1 minute,然后为校准器 选择 rate。

    这会将原始请求数据转换为每秒请求数,从而更轻松地查看和分析随时间变化的一致流量趋势。

  7. 如需保存 Widget,请点击应用 。

后续步骤