集成 TiDB Cloud 与 Prometheus 和 Grafana
TiDB Cloud 提供了一个 Prometheus API 端点。如果你拥有 Prometheus 服务,可以轻松地通过该端点监控 TiDB Cloud 的关键指标。
本文档介绍了如何配置 Prometheus 服务以从 TiDB Cloud 端点读取关键指标,以及如何使用 Grafana 查看这些指标。
Prometheus 集成版本
自 2022 年 3 月 15 日起,TiDB Cloud 支持项目级 Prometheus 集成(Beta)。自 2025 年 10 月 21 日起,TiDB Cloud 推出了集群级 Prometheus 集成(预览版)。
集群级 Prometheus 集成(预览版):如果在 2025 年 10 月 21 日前,你的组织内没有未删除的遗留项目级 Prometheus 集成,TiDB Cloud 将为你的组织提供集群级 Prometheus 集成(预览版),以体验最新的增强功能。
遗留项目级 Prometheus 集成(Beta):如果在 2025 年 10 月 21 日前,你的组织内至少有一个未删除的遗留项目级 Prometheus 集成,TiDB Cloud 会保留现有和新建的项目级集成,以避免影响当前的仪表盘。
前提条件
要将 TiDB Cloud 与 Prometheus 集成,你必须拥有自托管或托管的 Prometheus 服务。
要为 TiDB Cloud 设置第三方指标集成,你必须在 TiDB Cloud 中拥有
Organization Owner或Project Owner权限。要查看集成页面,至少需要Project Viewer角色以访问 TiDB Cloud 项目下的目标集群。
限制
- Prometheus 和 Grafana 集成目前仅适用于 TiDB Cloud Dedicated 集群。
- 当集群状态为 CREATING、RESTORING、PAUSED 或 RESUMING 时,不支持 Prometheus 和 Grafana 集成。
步骤
步骤 1. 获取 Prometheus 的 scrape_config 文件
在配置 Prometheus 服务以读取 TiDB Cloud 指标之前,你需要先在 TiDB Cloud 中生成一个 scrape_config YAML 文件。该 scrape_config 文件包含一个唯一的 bearer token,允许 Prometheus 服务监控你的目标集群。
根据你的 Prometheus 集成版本,获取 Prometheus 的 scrape_config 文件和访问集成页面的步骤有所不同。
- 在 TiDB Cloud 控制台中,进入你项目的 Clusters 页面,然后点击目标集群名称进入其概览页面。
- 在左侧导航栏,点击 Settings > Integrations。
- 在 Integrations 页面,点击 Integration to Prometheus (Preview)。
- 点击 Add File,为当前集群生成并显示
scrape_config文件。 - 复制
scrape_config文件内容,供后续使用。
- 在 TiDB Cloud 控制台中,使用左上角的下拉框切换到目标项目。
- 在左侧导航栏,点击 Project Settings > Integrations。
- 在 Integrations 页面,点击 Integration to Prometheus (BETA)。
- 点击 Add File,为当前项目生成并显示 scrape_config 文件。
- 复制 scrape_config 文件内容,供后续使用。
步骤 2. 集成 Prometheus
在你的 Prometheus 服务指定的监控目录中,找到 Prometheus 配置文件。
例如,
/etc/prometheus/prometheus.yml。在 Prometheus 配置文件中,找到
scrape_configs部分,然后将从 TiDB Cloud 获取的scrape_config文件内容复制到该部分。在 Prometheus 服务中,检查 Status > Targets,确认新的
scrape_config文件已被读取。如果没有生效,可能需要重启 Prometheus 服务。
步骤 3. 使用 Grafana GUI 仪表盘可视化指标
当你的 Prometheus 服务已从 TiDB Cloud 读取指标后,可以按如下方式使用 Grafana GUI 仪表盘进行可视化:
根据你的 Prometheus 集成版本,下载 TiDB Cloud for Prometheus 的 Grafana 仪表盘 JSON 的链接不同。
将该 JSON 导入到你自己的 Grafana GUI 以可视化指标。
(可选)根据需要自定义仪表盘,例如添加或移除面板、修改数据源和调整显示选项。
关于如何使用 Grafana 的更多信息,请参见 Grafana 官方文档。
scrape_config 轮换的最佳实践
为提升数据安全性,建议定期轮换 scrape_config 文件中的 bearer token。
- 按照 步骤 1 为 Prometheus 创建新的
scrape_config文件。 - 将新文件的内容添加到 Prometheus 配置文件中。
- 确认 Prometheus 服务仍能从 TiDB Cloud 读取数据后,从 Prometheus 配置文件中移除旧的
scrape_config文件内容。 - 在项目或集群的 Integrations 页面,删除对应的旧
scrape_config文件,以阻止他人使用其访问 TiDB Cloud Prometheus 端点。
Prometheus 可用指标
Prometheus 会跟踪你的 TiDB 集群的以下指标数据。
| 指标名称 | 指标类型 | 标签 | 描述 |
|---|---|---|---|
| tidbcloud_db_queries_total | count | sql_type: Select\|Insert\|...cluster_name: <cluster name>instance: tidb-0\|tidb-1…component: tidb | 执行的语句总数 |
| tidbcloud_db_failed_queries_total | count | type: planner:xxx\|executor:2345\|...cluster_name: <cluster name>instance: tidb-0\|tidb-1…component: tidb | 执行错误的总数 |
| tidbcloud_db_connections | gauge | cluster_name: <cluster name>instance: tidb-0\|tidb-1…component: tidb | 当前 TiDB 服务器的连接数 |
| tidbcloud_db_query_duration_seconds | histogram | sql_type: Select\|Insert\|...cluster_name: <cluster name>instance: tidb-0\|tidb-1…component: tidb | 语句的耗时直方图 |
| tidbcloud_changefeed_latency | gauge | changefeed_id | changefeed 上游与下游的数据同步延迟 |
| tidbcloud_changefeed_checkpoint_ts | gauge | changefeed_id | changefeed 的检查点时间戳,表示已成功写入下游的最大 TSO(Timestamp Oracle) |
| tidbcloud_changefeed_replica_rows | gauge | changefeed_id | changefeed 每秒写入下游的同步行数 |
| tidbcloud_node_storage_used_bytes | gauge | cluster_name: <cluster name>instance: tikv-0\|tikv-1…\|tiflash-0\|tiflash-1…component: tikv\|tiflash | TiKV/TiFlash 节点的磁盘已用字节数 |
| tidbcloud_node_storage_capacity_bytes | gauge | cluster_name: <cluster name>instance: tikv-0\|tikv-1…\|tiflash-0\|tiflash-1…component: tikv\|tiflash | TiKV/TiFlash 节点的磁盘容量字节数 |
| tidbcloud_node_cpu_seconds_total | count | cluster_name: <cluster name>instance: tidb-0\|tidb-1…\|tikv-0…\|tiflash-0…component: tidb\|tikv\|tiflash | TiDB/TiKV/TiFlash 节点的 CPU 使用量 |
| tidbcloud_node_cpu_capacity_cores | gauge | cluster_name: <cluster name>instance: tidb-0\|tidb-1…\|tikv-0…\|tiflash-0…component: tidb\|tikv\|tiflash | TiDB/TiKV/TiFlash 节点的 CPU 限制核数 |
| tidbcloud_node_memory_used_bytes | gauge | cluster_name: <cluster name>instance: tidb-0\|tidb-1…\|tikv-0…\|tiflash-0…component: tidb\|tikv\|tiflash | TiDB/TiKV/TiFlash 节点的已用内存字节数 |
| tidbcloud_node_memory_capacity_bytes | gauge | cluster_name: <cluster name>instance: tidb-0\|tidb-1…\|tikv-0…\|tiflash-0…component: tidb\|tikv\|tiflash | TiDB/TiKV/TiFlash 节点的内存容量字节数 |
| tidbcloud_node_storage_available_bytes | gauge | instance: tidb-0\|tidb-1\|...component: tikv\|tiflashcluster_name: <cluster name> | TiKV/TiFlash 节点可用磁盘空间(字节) |
| tidbcloud_disk_read_latency | histogram | instance: tidb-0\|tidb-1\|...component: tikv\|tiflashcluster_name: <cluster name>device: nvme.*\|dm.* | 每个存储设备的读延迟(秒) |
| tidbcloud_disk_write_latency | histogram | instance: tidb-0\|tidb-1\|...component: tikv\|tiflashcluster_name: <cluster name>device: nvme.*\|dm.* | 每个存储设备的写延迟(秒) |
| tidbcloud_kv_request_duration | histogram | instance: tidb-0\|tidb-1\|...component: tikvcluster_name: <cluster name>type: BatchGet\|Commit\|Prewrite\|... | TiKV 按类型请求的耗时(秒) |
| tidbcloud_component_uptime | histogram | instance: tidb-0\|tidb-1\|...component: tidb\|tikv\|tiflashcluster_name: <cluster name> | TiDB 组件的运行时长(秒) |
| tidbcloud_ticdc_owner_resolved_ts_lag | gauge | changefeed_id: <changefeed-id>cluster_name: <cluster name> | changefeed owner 的 resolved timestamp 延迟(秒) |
| tidbcloud_changefeed_status | gauge | changefeed_id: <changefeed-id>cluster_name: <cluster name> | changefeed 状态:-1: 未知0: 正常1: 警告2: 失败3: 已停止4: 已完成6: 警告7: 其他 |
| tidbcloud_resource_manager_resource_unit_read_request_unit | gauge | cluster_name: <cluster name>resource_group: <group-name> | Resource Manager 消耗的读请求单元 |
| tidbcloud_resource_manager_resource_unit_write_request_unit | gauge | cluster_name: <cluster name>resource_group: <group-name> | Resource Manager 消耗的写请求单元 |
对于集群级 Prometheus 集成,还可获取以下额外指标:
| 指标名称 | 指标类型 | 标签 | 描述 |
|---|---|---|---|
| tidbcloud_dm_task_status | gauge | instance: instancetask: taskcluster_name: <cluster name> | 数据迁移任务状态: 0: 无效 1: 新建 2: 运行中 3: 暂停 4: 停止 5: 完成 15: 错误 |
| tidbcloud_dm_syncer_replication_lag_bucket | gauge | instance: instancecluster_name: <cluster name> | 数据迁移的同步延迟(bucket) |
| tidbcloud_dm_syncer_replication_lag_gauge | gauge | instance: instancetask: taskcluster_name: <cluster name> | 数据迁移的同步延迟(gauge) |
| tidbcloud_dm_relay_read_error_count | count | instance: instancecluster_name: <cluster name> | 从主库读取 binlog 失败的次数 |
常见问题
为什么同一指标在 Grafana 和 TiDB Cloud 控制台上同时显示的数值不同?
Grafana 和 TiDB Cloud 的聚合计算逻辑不同,因此显示的聚合值可能存在差异。你可以在 Grafana 中调整
mini step配置,以获得更细粒度的指标值。