news 2026/8/12 18:41:07

Prometheus 监控 Google Cloud Monitoring Exporter 全栈实战:从 GCE 到 Cloud Run 的 GCP 资源可观测性

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Prometheus 监控 Google Cloud Monitoring Exporter 全栈实战:从 GCE 到 Cloud Run 的 GCP 资源可观测性

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 MonitoringStackdriver 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.yml

Exporter 默认监听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.yml

4. 编写采集配置文件 (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_MEANREDUCE_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_typeproject_idinstance_idcluster_namedatabase_id等)。

GCP 服务指标示例 (Prometheus 名称)含义
Compute Enginestackdriver_compute_googleapis_com_instance_cpu_utilization_mean{instance_id="123"}CPU 使用率 (0-1)
Compute Enginestackdriver_compute_googleapis_com_instance_memory_usage_mean内存使用率 (0-1)
GKEstackdriver_kubernetes_io_container_cpu_core_usage_time_sum{cluster_name="my-cluster"}容器 CPU 使用核时
Cloud SQLstackdriver_cloudsql_googleapis_com_database_cpu_utilization_mean数据库 CPU 使用率
Cloud Storagestackdriver_storage_googleapis_com_storage_total_bytes_mean存储桶总字节数
Cloud Runstackdriver_run_googleapis_com_request_count_sum{service_name="my-service"}请求计数速率
Cloud Functionsstackdriver_cloudfunctions_googleapis_com_function_execution_count_sum函数执行次数
Pub/Substackdriver_pubsub_googleapis_com_subscription_num_undelivered_messages_mean订阅未传递消息数
Load Balancerstackdriver_loadbalancing_googleapis_com_https_request_count_sumHTTPS 请求速率

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 全览:使用变量projectregion切换,集成多个服务卡片和趋势图。

导入后选择数据源,确保 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 按读取的时间序列数请求次数计费。优化建议:

  • 合理设置periodscrape_interval(300s 或更长),减少 API 调用频率和数据量。
  • 使用dimensions精确过滤资源,避免通配符拉取整个项目的所有时间序列。
  • 关闭不需要的指标,或按环境(生产/测试)分组配置不同的 Exporter。
9.3 标签注入与资源发现

Exporter 支持动态发现项目中的资源,并注入资源标签(如instance_namezonecluster_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 资源的全方位健康,真正实现混合云、多云架构的全栈透明化监控。部署它,为每一份云上资源点亮可观测之灯,让云原生运维走向精准与高效。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/12 18:40:20

阿里云免费SSL证书自动化续签实战:基于CLI与脚本的运维方案

1. 问题缘起&#xff1a;免费午餐的代价 如果你用过阿里云的免费SSL证书&#xff0c;那你一定对那个“三个月有效期”的设定又爱又恨。爱的是&#xff0c;它确实免费&#xff0c;给个人站长、测试环境、小型项目省下了真金白银&#xff1b;恨的是&#xff0c;每三个月就要手动操…

作者头像 李华
网站建设 2026/8/12 18:39:28

Windows 10 下 AirSim + Unreal Engine 4.27.2 环境搭建全攻略与避坑指南

1. 项目概述与核心价值 最近在折腾无人机和自动驾驶的仿真项目&#xff0c;发现AirSim这个微软开源的仿真平台是真香&#xff0c;它基于Unreal Engine&#xff0c;能提供极其逼真的物理环境和传感器模拟。但说实话&#xff0c;第一次在Windows 10上搭建AirSim Unreal Engine 4…

作者头像 李华
网站建设 2026/8/12 18:36:39

Java 大模型服务怎么验:Schema、依赖隔离与 Eval 回归

Java 大模型服务怎么验&#xff1a;Schema、依赖隔离与 Eval 回归 模型回答“看起来通顺”并不能说明它能进入业务链路。结构化输出可能不符合契约&#xff0c;检索结果也可能和回答脱节。评估应把这两类问题拆开&#xff1a;先校验硬约束&#xff0c;再用固定样本集观察回答质…

作者头像 李华
网站建设 2026/8/12 18:35:55

Windows Defender深度管控指南:从原理到脚本实现彻底静默

1. 项目概述与核心诉求 最近在几个技术群里&#xff0c;经常看到有朋友在讨论一个老生常谈但又总有人踩坑的问题&#xff1a;怎么在Windows 10和11上彻底关掉或者移除Windows Defender。有人是为了跑一些自己写的脚本或者小众开发工具&#xff0c;被误报拦截搞得心烦意乱&#…

作者头像 李华
网站建设 2026/8/12 18:35:35

Linux 终端命令速查表 -- 07 文件与文件夹速查表

目录导航 pwd 打印当前所在目录。 pwdcd 切换到另一个目录:传入路径,或使用快捷方式。 cd [path]向上返回一级,到父目录。 cd ..跳转到主目录(直接输入 cd 命令后直接按【 enter 】键效果相同)。 cd ~返回之前所在的目录。 cd -列出内容 ls 列出目录内容。-l 显示详…

作者头像 李华
网站建设 2026/8/12 18:34:36

终极解决方案:3分钟让Windows资源管理器完美显示HEIC缩略图

终极解决方案&#xff1a;3分钟让Windows资源管理器完美显示HEIC缩略图 【免费下载链接】windows-heic-thumbnails Enable Windows Explorer to display thumbnails for HEIC/HEIF files 项目地址: https://gitcode.com/gh_mirrors/wi/windows-heic-thumbnails 如果你正…

作者头像 李华