
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.viewerMonitoring Viewer角色即可读取所有监控指标。如果只需监控特定项目可将角色范围限定到该项目。也可使用更精细的自定义角色仅允许monitoring.timeSeries.list权限。2.3 设置环境变量将下载的 JSON 密钥文件挂载到容器中并通过环境变量GOOGLE_APPLICATION_CREDENTIALS指向密钥路径。3. 部署 Stackdriver Exporter3.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-filegoogleServiceAccountKeyservice-account-key.json\--setstackdriverExporter.configFilestackdriver_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:stackdriverscrape_interval:300s# 与配置中 period 匹配避免频繁拉取static_configs:-targets:[stackdriver-exporter:9255]labels:cloud:gcpproject:my-gcp-project若监控多个 GCP 项目可部署多个 Exporter 实例每个负责一个项目并在 Prometheus 中用不同 job 或project标签区分。6. 核心监控指标与 PromQLExporter 生成的指标名格式为stackdriver_metric_name_statistic并携带丰富的资源标签如resource_type、project_id、instance_id、cluster_name、database_id等。GCP 服务指标示例 (Prometheus 名称)含义Compute Enginestackdriver_compute_googleapis_com_instance_cpu_utilization_mean{instance_id123}CPU 使用率 (0-1)Compute Enginestackdriver_compute_googleapis_com_instance_memory_usage_mean内存使用率 (0-1)GKEstackdriver_kubernetes_io_container_cpu_core_usage_time_sum{cluster_namemy-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_namemy-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.8GKE 集群内存使用率stackdriver_kubernetes_io_container_memory_used_bytes_mean / on(cluster_name) ...需结合 limitCloud SQL 磁盘使用率stackdriver_cloudsql_googleapis_com_database_disk_usage_mean 0.85Cloud 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 DashboardDashboard ID15127社区打造涵盖 GCE、GKE、Cloud SQL、Cloud Run 等核心服务GCE Instance MetricsID13222专注于虚拟机 CPU、内存、磁盘、网络。GKE Cluster MonitoringID13824结合 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_mean0.85for:10mlabels:severity:warningannotations:summary:GCE 实例 {{ $labels.instance_id }} CPU 使用率超过 85%-alert:GCEInstanceHighMemoryexpr:stackdriver_compute_googleapis_com_instance_memory_usage_mean0.9for:10mlabels:severity:warningannotations:summary:GCE 实例 {{ $labels.instance_id }} 内存使用率超过 90%-alert:CloudSQLDiskUsageHighexpr:stackdriver_cloudsql_googleapis_com_database_disk_usage_mean0.85for:10mlabels:severity:criticalannotations:summary:Cloud SQL 实例 {{ $labels.database_id }} 磁盘使用率超过 85%-alert:CloudSQLHighConnectionsexpr:stackdriver_cloudsql_googleapis_com_database_connections_mean0.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_mean1000for:10mlabels:severity:warningannotations:summary:Pub/Sub 订阅 {{ $labels.subscription_id }} 消息积压超过 1000 条-alert:GKENodeCPUHighexpr:stackdriver_kubernetes_io_node_cpu_utilization_mean0.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_interval300s 或更长减少 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 ExporterGoogle Cloud Platform 的监控数据不再是孤岛。从 GCE 的 CPU/内存、GKE 的容器资源、Cloud SQL 的存储与连接到 Cloud Run 的请求错误、Pub/Sub 的积压量所有关键信号都接入 Prometheus 统一平台。配合 Grafana 仪表盘和 Alertmanager 的及时告警你可以在同一套可观测体系中掌握自建服务和 GCP 资源的全方位健康真正实现混合云、多云架构的全栈透明化监控。部署它为每一份云上资源点亮可观测之灯让云原生运维走向精准与高效。