Google Cloud Monitoring 数据源
Grafana 内置支持 Google Cloud Monitoring。本主题介绍特定于 Google Cloud Monitoring 数据源的查询、模板、变量及其他配置。
有关如何向 Grafana 添加数据源的说明,请参阅管理文档。只有具有组织管理员角色的用户才能添加数据源。
添加 Google Cloud Monitoring 数据源后,您可以配置它,以便您的 Grafana 实例用户可以在其查询编辑器中创建查询,并在构建仪表盘和使用探索时应用标注。
开始之前
在配置数据源之前,请完成以下步骤。
配置 Google 身份验证
在请求 Google Cloud Monitoring 的数据之前,必须配置身份验证。所有对 Google API 的请求均由 Grafana 后端在服务器端执行。
有关身份验证选项和配置详情,请参阅Google 身份验证。
配置 Google 身份验证时,请注意以下与 Google Cloud Monitoring 相关的额外步骤。
配置 GCP 服务帐户
当您创建 Google Cloud Platform (GCP) 服务帐户和密钥文件时,该服务帐户必须具有 Monitoring Viewer 角色(角色 > 选择角色 > Monitoring > Monitoring Viewer)

授予 GCE 默认服务帐户范围
如果 Grafana 运行在 Google Compute Engine (GCE) 虚拟机上,当您配置 GCE 默认服务帐户时,还必须授予该服务帐户访问“Cloud Monitoring API”范围的权限。
启用必要的 Google Cloud Platform API
在请求 Google Cloud Monitoring 的数据之前,必须先在 Google 端启用必要的 API。
打开 Monitoring 和 Cloud Resource Manager API 页面
在每个页面上,点击
Enable
按钮。启用 GCP API
配置数据源
要配置数据源的基本设置,请完成以下步骤
在左侧菜单中点击Connections。
在 Your connections(您的连接)下,点击Data sources(数据源)。
在搜索栏中输入
Google Cloud Monitoring
。点击Google Cloud Monitoring。
显示数据源的Settings(设置)选项卡。
设置数据源的基本配置选项
名称 描述 名称 设置您在面板和查询中引用数据源时使用的名称。 默认 设置是否为新面板预先选择此数据源。
预配数据源
您可以将数据源在 YAML 文件中定义和配置,作为 Grafana 预配系统的一部分。有关预配的更多信息以及可用配置选项,请参阅预配 Grafana。
预配示例
使用 JWT (服务帐户密钥文件) 身份验证类型
apiVersion: 1
datasources:
- name: Google Cloud Monitoring
type: stackdriver
access: proxy
jsonData:
tokenUri: https://oauth2.googleapis.com/token
clientEmail: stackdriver@myproject.iam.gserviceaccount.com
authenticationType: jwt
defaultProject: my-project-name
secureJsonData:
privateKey: |
-----BEGIN PRIVATE KEY-----
POSEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb1u1Srw8ICYHS
...
yA+23427282348234=
-----END PRIVATE KEY-----
使用 JWT (服务帐户私钥路径) 身份验证类型
apiVersion: 1
datasources:
- name: Google Cloud Monitoring
type: stackdriver
access: proxy
jsonData:
tokenUri: https://oauth2.googleapis.com/token
clientEmail: stackdriver@myproject.iam.gserviceaccount.com
authenticationType: jwt
defaultProject: my-project-name
privateKeyPath: /etc/secrets/gce.pem
使用 GCE 默认服务帐户身份验证
apiVersion: 1
datasources:
- name: Google Cloud Monitoring
type: stackdriver
access: proxy
jsonData:
authenticationType: gce
导入预配置的仪表盘
Google Cloud Monitoring 数据源附带为一些最受欢迎的 GCP 服务预配置的仪表盘。这些精心设计的仪表盘基于 GCP 仪表盘示例仓库中的类似仪表盘。

导入精心设计仪表盘的步骤
导航至数据源的配置页面。
选择Dashboards(仪表盘)选项卡。
这将显示精选的可导入仪表盘列表。
选择要导入的仪表盘的Import(导入)按钮。
每次加载仪表盘时,仪表盘都包含一个模板变量,该变量填充了配置的服务帐户可访问的项目。Grafana 加载仪表盘后,您可以从下拉列表中选择一个项目。
自定义导入的仪表盘
要自定义其中一个仪表盘,我们建议您将其另存为其他名称。否则,升级 Grafana 可能会用新版本覆盖自定义的仪表盘。
查询数据源
Google Cloud Monitoring 查询编辑器帮助您构建两种类型的查询:指标和服务级别目标 (SLO)。
详情请参阅查询编辑器文档。
使用模板变量
您可以使用变量代替在指标查询中硬编码服务器、应用程序和传感器名称等详细信息。Grafana 在仪表盘顶部的下拉选择框中列出这些变量,以帮助您更改仪表盘中显示的数据。Grafana 将此类变量称为模板变量。
详情请参阅模板变量文档。