# 如何配置 GreptimeDB 作为 Prometheus 的长期存储

Source: https://greptime.cn/blogs/2024-08-09-prometheus

[Prometheus](https://prometheus.io/) 是一个开源的监控和报警系统，用于收集和处理时间序列数据。它通常与其他工具（如 [Grafana](https://grafana.com/)）结合使用，以提供可视化和分析功能。然而，Prometheus 本身的存储能力有限，因此在处理大量时间序列数据时，需要使用外部存储来扩展其功能。[GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 是一个专门为时间序列数据设计的分布式数据库，具有高性能、高可用性和可扩展性等特点。在本教程中，我们将学习如何设置 GreptimeDB 作为 Prometheus 的长期存储。

## 1. 安装 GreptimeDB
首先需要安装 GreptimeDB，用户可以从官方网站下载适合你操作系统的二进制文件，并按照安装说明进行安装。在本教程中，我们将使用 Docker 来安装 GreptimeDB 单机版：

```bash
docker run -p 127.0.0.1:4000-4003:4000-4003 \
-v "$(pwd)/greptimedb:/tmp/greptimedb" \
--name greptime --rm \
greptime/greptimedb:v0.9.1 standalone start \
--http-addr 0.0.0.0:4000 \
--rpc-addr 0.0.0.0:4001 \
--mysql-addr 0.0.0.0:4002 \
--postgres-addr 0.0.0.0:4003
```

如果一切正常，可以打开浏览器访问 http://localhost:4000/dashboard/ ，将会看到 GreptimeDB 内置的 Dashboard，可以查看表信息、查询等。

如果你的数据规模较大，我们推荐你使用 GreptimeDB 集群版本，它具备无限水平扩展能力，请阅读这篇[性能报告](https://greptime.cn/blogs/2024-08-01-grafana)。

更多 GreptimeDB 安装说明请阅读[文档](https://docs.greptime.com/getting-started/installation/overview)。

## 2. 配置 Prometheus
接下来，需要配置 Prometheus 以使用 GreptimeDB 作为长期存储。

首先从 https://github.com/prometheus/prometheus/releases/tag/v2.53.1 选择对应的操作系统和处理器架构版本下载 Prometheus。

比如我使用的是 Mac Arm64 系统：

```bash
curl -fsSL -O \
  https://github.com/prometheus/prometheus/releases/download/v2.53.1/prometheus-2.53.1.darwin-arm64.tar.gz
```

解压后，进入 Prometheus 安装目录：

```bash
tar zxvf prometheus-2.53.1.darwin-arm64.tar.gz
cd prometheus-2.53.1.darwin-arm64
```

打开 Prometheus 的配置文件（`prometheus.yml`），在末尾添加以下内容：

```yaml
remote_write:
  - url: http://localhost:4000/v1/prometheus/write?db=public
remote_read:
  - url: http://localhost:4000/v1/prometheus/read?db=public
```

我们默认写入的数据库的是 `public`。

## 3. 启动 Prometheus

完成配置后启动 Prometheus，在命令行中，执行以下命令：

```plain text
./prometheus --config.file=./prometheus.yml
```

如果一切正常，刷新 http://localhost:4000/dashboard/ 页面，将会看到 Prometheus 的数据已经写入了：

<img src="/zh/blogs/2024-08-09-prometheus/p1.png" alt="Data Ingestion" style="width: 75%; margin: 0 auto"/>

用户也可以使用 SQL 来查询这些数据，点击左侧菜单 + 号打开一个查询界面，输入 SQL 语句：

```sql
select * from go_info order by greptime_timestamp desc limit 10;
```

<img src="/zh/blogs/2024-08-09-prometheus/p2.png" alt="Search Page" style="width: 75%; margin: 0 auto"/>

更多关于 Prometheus 到 GreptimeDB 的数据模型映射请阅读[文档](https://docs.greptime.com/user-guide/ingest-data/for-observability/prometheus#data-model)。

## 4. 配置 Grafana 直接访问 GreptimeDB

在前三步完成后，GreptimeDB 已经配置为 Prometheus 的长期存储后端。

第四步是额外彩蛋，因为 GreptimeDB 原生支持 PromQL，**事实上用户可以让 Grafana 直接连接 GreptimeDB，而不需要通过 Prometheus Remote Read 协议中转一道，这样带来的性能更好、延迟更低**。

首先通过 Docker 启动一个 Grafana：

```bash
docker run -d -p 3000:3000 --name=grafana --rm grafana/grafana-oss
```

打开浏览器 http://localhost:3000/login 访问并登录 Grafana，默认账户名和密码都是 admin。

创建一个[数据源](http://localhost:3000/connections/datasources/new)，选择 Prometheus 数据源：

<img src="/zh/blogs/2024-08-09-prometheus/p3.png" alt="Prometheus Datasource" style="width: 75%; margin: 0 auto"/>

<img src="/zh/blogs/2024-08-09-prometheus/p4.png" alt="Prometheus Datasource-2" style="width: 75%; margin: 0 auto"/>

通过下列命令查看 GreptimeDB 容器的 IP:

```bash
docker inspect greptime |grep IPAddress
```

假设输出是：

```yaml
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
```

那么可以将 GreptimeDB server URL 设置为：http://172.17.0.3:4000/v1/prometheus，请修改成你那边输出的正确 IP。

save & test 应该一切正常。

接下来我们去创建一个 Dashboard，我们选择导入一个，进入[菜单](http://localhost:3000/dashboard/import)。

因为本文只采集了 Prometheus 的指标，所以我们选择导入 [Prometheus 2.0 Overview](https://grafana.com/grafana/dashboards/3662-prometheus-2-0-overview/) 作为 Prometheus 自身的监控大盘：

<img src="/zh/blogs/2024-08-09-prometheus/p5.png" alt="Prometheus Metrics" style="width: 75%; margin: 0 auto"/>

导入 URL 填写：https://grafana.com/grafana/dashboards/3662-prometheus-2-0-overview/ 

<img src="/zh/blogs/2024-08-09-prometheus/p6.png" alt="URL Form" style="width: 75%; margin: 0 auto"/>

选择我们刚刚创建的 Prometheus 数据源：

<img src="/zh/blogs/2024-08-09-prometheus/p7.png" alt="Select the Prometheus Datasource" style="width: 75%; margin: 0 auto"/>

如果一切顺利，你应该可以看到 Prometheus 的监控大盘：

<img src="/zh/blogs/2024-08-09-prometheus/p8.png" alt="Monitoring Dashboard" style="width: 75%; margin: 0 auto"/>

<img src="/zh/blogs/2024-08-09-prometheus/p9.png" alt="Monitoring Dashboard-2" style="width: 75%; margin: 0 auto"/>

感谢阅读，更多关于 GreptimeDB 的信息，请阅读[官方文档](https://docs.greptime.com/)。

如果你不想自己托管一个 GreptimeDB，欢迎尝试全托管的数据库服务 [GreptimeCloud](https://greptime.com/product/cloud)。

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
