Prometheus 监控 Google Cloud Monitoring Exporter 全栈实战:从 GCE 到 Cloud Run 的 GCP 资源可观测性
在 Google Cloud Platform (GCP) 上,无数计算、存储、数据库和无服务器资源支撑着现代应用。GCP 的 Cloud Monitoring(原 Stackdriver)汇聚了这些资源的丰富指标,但它独立于自建 Prometheus,导致监控数据割裂。Stackdriver Exporter(由 Prometheus 社区官方维护)正是弥合这一鸿沟的桥梁——它通过 Cloud Monitoring API v3 将 GCE 虚拟机、GKE 集群、Cloud SQL、Cloud Storage、Cloud Run、Cloud Functions、Pub/Sub 等服务的核心指标拉取并转化为 Prometheus 标准格式,让你在统一的可观测平台上洞察所有 GCP 资源的脉搏。本文将带你从零配置 GCP 服务账号、部署 Exporter、编写采集规则,到构建 Grafana 大屏与告警落地,实现多云统一可观测性。
1. 为什么需要 Stackdriver Exporter?
| 原生 Cloud Monitoring | Stackdriver Exporter 带来的增强 |
|---|---|
| 监控数据仅在 GCP 控制台或 API 中 | 所有指标流入 Prometheus,可长期存储、聚合、关联 |
| 告警渠道独立(Cloud Alerting) | 使用 Alertmanager 统一告警路由,避免多套通知 |
| 仪表盘局限于 Cloud Monitoring 内置 | 结合 Grafana 强大定制能力,创建跨云、跨区域动态看板 |
| 无法与自建服务指标关联 | 通过 PromQL 将 GCP 资源指标与应用指标关联,快速定位瓶颈 |
官方prometheus/stackdriver_exporter支持所有 Cloud Monitoring 指标类型、聚合、对齐,并通过 YAML 配置实现灵活的指标筛选与聚合。
2. 配置 GCP 服务账号与权限
Stackdriver Exporter 需要读取 Cloud Monitoring 指标。为此,需创建一个 GCP 服务账号并赋予最小权限。
2.1 创建服务账号
在 GCP 控制台「IAM 与管理」->「服务账号」中创建新账号,并为其生成 JSON 密钥文件(下载后妥善保管)。
2.2 授予角色
为服务账号授予roles/monitoring.viewer(Monitoring Viewer)角色,即可读取所有监控指标。如果只需监控特定项目,可将角色范围限定到该项目。也可使用更精细的自定义角色,仅允许monitoring.timeSeries.list权限。
2.3 设置环境变量
将下载的 JSON 密钥文件挂载到容器中,并通过环境变量GOOGLE_APPLICATION_CREDENTIALS指向密钥路径。
3. 部署 Stackdriver Exporter
3.1 Docker 部署
dockerrun-d\--namestackdriver_exporter\-p9255:9255\-v/path/to/stackdriver_exporter.yml:/etc/stackdriver_exporter/stackdriver_exporter.yml\-v/path/to/service-account-key.json:/etc/gcp/sa-key.json\-eGOOGLE_APPLICATION_CREDENTIALS=/etc/gcp/sa-key.json\prometheuscommunity/stackdriver-exporter:v0.14.0\--config.file=/etc/stackdriver_exporter/stackdriver_exporter.ymlExporter 默认监听9255端口,/metrics端点提供 Prometheus 指标。
3.2 Helm 部署(在 GKE 中)
helm repoaddprometheus-community https://prometheus-community.github.io/helm-charts helminstallstackdriver-exporter prometheus-community/stackdriver-exporter\--set-filegoogleServiceAccountKey=service-account-key.json\--setstackdriverExporter.configFile=stackdriver_exporter.yml4. 编写采集配置文件 (stackdriver_exporter.yml)
该配置文件定义了要抓取哪些 GCP 资源的哪些指标,支持过滤、聚合、对齐周期等。
# stackdriver_exporter.ymlstackdriver:projectId:my-gcp-project# GCP 项目 IDmonitoring:typePrefix:'stackdriver_'# Prometheus 指标名前缀metrics:# Google Compute Engine (GCE) 实例-name:compute.googleapis.com/instance/cpu/utilizationperiod:300s# 时间窗口 5 分钟aligner:ALIGN_MEANreducer:REDUCE_MEANdimensions:resource.labels.instance_id:.*-name:compute.googleapis.com/instance/memory/usageperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:compute.googleapis.com/instance/network/received_bytes_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM-name:compute.googleapis.com/instance/network/sent_bytes_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM# Google Kubernetes Engine (GKE) 容器指标-name:kubernetes.io/container/cpu/core_usage_timeperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUMdimensions:resource.labels.cluster_name:.*-name:kubernetes.io/container/memory/used_bytesperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN# Cloud SQL 数据库-name:cloudsql.googleapis.com/database/cpu/utilizationperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:cloudsql.googleapis.com/database/memory/usageperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:cloudsql.googleapis.com/database/connectionsperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:cloudsql.googleapis.com/database/disk/usageperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN# Cloud Storage-name:storage.googleapis.com/storage/total_bytesperiod:3600saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:storage.googleapis.com/storage/object_countperiod:3600saligner:ALIGN_MEANreducer:REDUCE_MEAN# Cloud Run 服务-name:run.googleapis.com/container/cpu/utilizationsperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:run.googleapis.com/container/memory/utilizationsperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:run.googleapis.com/request_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM-name:run.googleapis.com/error_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM# Cloud Functions-name:cloudfunctions.googleapis.com/function/execution_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM-name:cloudfunctions.googleapis.com/function/error_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM# Pub/Sub-name:pubsub.googleapis.com/subscription/num_undelivered_messagesperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN-name:pubsub.googleapis.com/topic/send_request_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM# Cloud Load Balancing-name:loadbalancing.googleapis.com/https/request_countperiod:300saligner:ALIGN_RATEreducer:REDUCE_SUM-name:loadbalancing.googleapis.com/https/total_latenciesperiod:300saligner:ALIGN_MEANreducer:REDUCE_MEAN参数说明:
period:指标采样的时间窗口,建议与 Cloud Monitoring 的数据粒度匹配(一般为 60s 或 300s)。aligner:时间对齐方式,如ALIGN_MEAN(均值)、ALIGN_RATE(速率)、ALIGN_SUM等。reducer:跨时间序列的归约方式,如REDUCE_MEAN、REDUCE_SUM。dimensions:可选,用于过滤特定资源标签(如实例 ID、集群名称)。
可以在 Cloud Monitoring 的 Metrics Explorer 中预先验证指标名称和维度,确保配置正确。
5. 配置 Prometheus 抓取
scrape_configs:-job_name:'stackdriver'scrape_interval:300s# 与配置中 period 匹配,避免频繁拉取static_configs:-targets:['stackdriver-exporter:9255']labels:cloud:'gcp'project:'my-gcp-project'若监控多个 GCP 项目,可部署多个 Exporter 实例,每个负责一个项目,并在 Prometheus 中用不同 job 或project标签区分。
6. 核心监控指标与 PromQL
Exporter 生成的指标名格式为stackdriver_<metric_name>_<statistic>,并携带丰富的资源标签(如resource_type、project_id、instance_id、cluster_name、database_id等)。
| GCP 服务 | 指标示例 (Prometheus 名称) | 含义 |
|---|---|---|
| Compute Engine | stackdriver_compute_googleapis_com_instance_cpu_utilization_mean{instance_id="123"} | CPU 使用率 (0-1) |
| Compute Engine | stackdriver_compute_googleapis_com_instance_memory_usage_mean | 内存使用率 (0-1) |
| GKE | stackdriver_kubernetes_io_container_cpu_core_usage_time_sum{cluster_name="my-cluster"} | 容器 CPU 使用核时 |
| Cloud SQL | stackdriver_cloudsql_googleapis_com_database_cpu_utilization_mean | 数据库 CPU 使用率 |
| Cloud Storage | stackdriver_storage_googleapis_com_storage_total_bytes_mean | 存储桶总字节数 |
| Cloud Run | stackdriver_run_googleapis_com_request_count_sum{service_name="my-service"} | 请求计数速率 |
| Cloud Functions | stackdriver_cloudfunctions_googleapis_com_function_execution_count_sum | 函数执行次数 |
| Pub/Sub | stackdriver_pubsub_googleapis_com_subscription_num_undelivered_messages_mean | 订阅未传递消息数 |
| Load Balancer | stackdriver_loadbalancing_googleapis_com_https_request_count_sum | HTTPS 请求速率 |
PromQL 示例:
- GCE 实例 CPU 使用率超过 80%:
stackdriver_compute_googleapis_com_instance_cpu_utilization_mean > 0.8 - GKE 集群内存使用率:
stackdriver_kubernetes_io_container_memory_used_bytes_mean / on(cluster_name) ...(需结合 limit) - Cloud SQL 磁盘使用率:
stackdriver_cloudsql_googleapis_com_database_disk_usage_mean > 0.85 - Cloud Run 错误率:
rate(stackdriver_run_googleapis_com_error_count_sum[5m]) / rate(stackdriver_run_googleapis_com_request_count_sum[5m]) - Cloud Functions 错误率:
rate(stackdriver_cloudfunctions_googleapis_com_function_error_count_sum[5m]) / rate(stackdriver_cloudfunctions_googleapis_com_function_execution_count_sum[5m]) - Pub/Sub 消息积压:
stackdriver_pubsub_googleapis_com_subscription_num_undelivered_messages_mean > 1000
注意:指标名中的
/会被转换为_,但可能保留googleapis字样。实际名称可通过 Exporter 的/metrics端点查看。
7. Grafana 仪表盘推荐
- GCP Cloud Monitoring Exporter Dashboard:Dashboard ID15127(社区打造,涵盖 GCE、GKE、Cloud SQL、Cloud Run 等核心服务)
- GCE Instance Metrics:ID13222,专注于虚拟机 CPU、内存、磁盘、网络。
- GKE Cluster Monitoring:ID13824,结合 Prometheus 原生 K8s 指标,展示集群资源。
- Cloud Run / Cloud Functions:可使用 ID15330并结合自定义面板。
- 综合 GCP 全览:使用变量
project、region切换,集成多个服务卡片和趋势图。
导入后选择数据源,确保 Prometheus 实例包含 stackdriver 指标。
8. 告警规则实战
groups:-name:gcp_stackdriver_alertsrules:-alert:GCEInstanceHighCPUexpr:stackdriver_compute_googleapis_com_instance_cpu_utilization_mean>0.85for:10mlabels:severity:warningannotations:summary:"GCE 实例 {{ $labels.instance_id }} CPU 使用率超过 85%"-alert:GCEInstanceHighMemoryexpr:stackdriver_compute_googleapis_com_instance_memory_usage_mean>0.9for:10mlabels:severity:warningannotations:summary:"GCE 实例 {{ $labels.instance_id }} 内存使用率超过 90%"-alert:CloudSQLDiskUsageHighexpr:stackdriver_cloudsql_googleapis_com_database_disk_usage_mean>0.85for:10mlabels:severity:criticalannotations:summary:"Cloud SQL 实例 {{ $labels.database_id }} 磁盘使用率超过 85%"-alert:CloudSQLHighConnectionsexpr:stackdriver_cloudsql_googleapis_com_database_connections_mean>0.8 * on(database_id) stackdriver_cloudsql_googleapis_com_database_connections_maxfor:5mlabels:severity:warningannotations:summary:"Cloud SQL 连接数接近上限"-alert:CloudRunHighErrorRateexpr:rate(stackdriver_run_googleapis_com_error_count_sum[5m]) / rate(stackdriver_run_googleapis_com_request_count_sum[5m])>0.05for:5mlabels:severity:criticalannotations:summary:"Cloud Run 服务 {{ $labels.service_name }} 错误率超过 5%"-alert:PubSubMessageBacklogexpr:stackdriver_pubsub_googleapis_com_subscription_num_undelivered_messages_mean>1000for:10mlabels:severity:warningannotations:summary:"Pub/Sub 订阅 {{ $labels.subscription_id }} 消息积压超过 1000 条"-alert:GKENodeCPUHighexpr:stackdriver_kubernetes_io_node_cpu_utilization_mean>0.9for:10mlabels:severity:warningannotations:summary:"GKE 节点 {{ $labels.node_name }} CPU 使用率超过 90%"可根据实际需要扩展 Cloud Storage 容量告警、Cloud Functions 执行超时等。
9. 进阶:多项目、成本优化与标签注入
9.1 监控多个 GCP 项目
为每个 GCP 项目部署独立的 Exporter,通过不同的project标签区分。可以创建一个集中式的 Prometheus,使用文件服务发现管理多个 target。或者在同一 Exporter 中通过多个projectId配置实现(若 Exporter 支持),但通常多实例更清晰。
9.2 降低 API 成本
Cloud Monitoring API 按读取的时间序列数和请求次数计费。优化建议:
- 合理设置
period和scrape_interval(300s 或更长),减少 API 调用频率和数据量。 - 使用
dimensions精确过滤资源,避免通配符拉取整个项目的所有时间序列。 - 关闭不需要的指标,或按环境(生产/测试)分组配置不同的 Exporter。
9.3 标签注入与资源发现
Exporter 支持动态发现项目中的资源,并注入资源标签(如instance_name、zone、cluster_name)到 Prometheus 标签。通过设置dimensions中的resource.labels.*并赋予具体值或正则,即可在最终指标中获得易读的标签,方便告警分组和 Grafana 变量。
9.4 与 Node Exporter / cAdvisor 互补
GCE 实例内部还可安装 Node Exporter 获取操作系统级指标(如node_cpu_seconds_total),而 Stackdriver Exporter 提供的是虚拟化层面的外部视图。两者结合可形成“内部+外部”双重监控,快速识别是实例本身问题还是 GCP 底层问题。GKE 中,cAdvisor 的指标与 Stackdriver 的容器指标可互相校验。
10. 总结
通过 Stackdriver Exporter,Google Cloud Platform 的监控数据不再是孤岛。从 GCE 的 CPU/内存、GKE 的容器资源、Cloud SQL 的存储与连接,到 Cloud Run 的请求错误、Pub/Sub 的积压量,所有关键信号都接入 Prometheus 统一平台。配合 Grafana 仪表盘和 Alertmanager 的及时告警,你可以在同一套可观测体系中掌握自建服务和 GCP 资源的全方位健康,真正实现混合云、多云架构的全栈透明化监控。部署它,为每一份云上资源点亮可观测之灯,让云原生运维走向精准与高效。