# Grafana GreptimeDB 数据源插件上线啦！全面替代 Prometheus 插件

Source: https://greptime.cn/blogs/2024-06-27-grafana

## 为什么创建 GreptimeDB 数据源插件

此前，用户可以通过 Prometheus 数据源插件，设置连接到 GreptimeDB 来进行 PromQL 查询。

GrpetimeDB 支持了 80% 以上的 PromQL 语法。但是，由于 GreptimeDB 底层使用多值模型，而非 Prometheus 的单值模型，同时 GreptimeDB 支持了许多额外高级功能，使用 Grafana Prometheus 数据源插件连接 GreptimeDB 并不能释放 GreptimeDB 的所有潜能。

因此，**我们基于 Prometheus 数据源插件，开发了 Grafana GreptimeDB 数据源插件。该插件原生支持 GreptimeDB 特有的功能，比如支持多值模型，以期更友好、全面地支持 GreptimeDB 相关功能**。另外，和直接使用 Prometheus 数据源相比，它还解决了一些兼容性问题。

## 以 Prometheus 数据源插件作为基石

我们是基于 [Grafana Prometheus 内置数据源插件](https://github.com/grafana/grafana/tree/v10.3.3/public/app/plugins/datasource/prometheus) 修改的。Prometheus 数据源插件是集成在 Grafana 里的内置插件，并不是一个标准插件，不能独立运行。

为使 GreptimeDB 插件能作为标准插件运行，主要做了以下改动：

- - 解决前端依赖缺失问题：通过 Webpack alias 解决依赖地址变动问题，并提炼部分代码，减少不必要的依赖;

实现 Go 相关的后台功能（插件内部并没有包含相关 Go 代码）。

## 安装插件

可以选择以下两种方式任意一种安装 GreptimeDB 数据源插件：

1. 从 发布页面[2] 获取最新版本，解压文件到你的 Grafana 插件目录[3]：

下载地址形如下：

https://github.com/GreptimeTeam/greptimedb-grafana-datasource/releases/tag/v1.0.37

2. 使用 Grafana 命令行工具下载并安装：

```shell
grafana cli --pluginUrl https://github.com/GreptimeTeam/greptimedb-grafana-datasource/releases/latest/download/info8fcc-greptimedb-datasource.zip plugins install info8fcc
```

## 配置连接

1. 填写 GreptimeDB 服务器 URL，例如 `http://<host>:4000`。

2. 填写数据库名称，默认为 `public`。

3. 如果需要身份验证，选择 basic auth，并填写 GreptimeDB 的用户名和密码。

4. 点击 `Save & Test` 按钮测试连接。

## 基于本地 GreptimeDB 搭建 Node Exporter Dashboard

在 GreptimeDB 数据源插件开源仓库中，我们提供了一个开箱即用的 Docker Compose 配置文件，可以一键拉起 GreptimeDB、Prometheus、Prometheus Node Exporter、Grafana 和本插件。

克隆仓库并运行 Docker Compose 启动服务：

```bash
git clone https://github.com/GreptimeTeam/greptimedb-grafana-datasource.git
cd greptimedb-grafana-datasource/docker
docker compose up
```

基于 GreptimeDB 数据源插件，上述配置文件的方式可以使用内置的 GreptimeDB Data Source Connection 和 Node Exporter Dashboard，可直接运行查看效果：

1. 输入 http://localhost:3000 打开 Grafana 界面

2. 默认用户名和密码均为 `admin`

3. 在 Dashboards 页面可以看到 Node Exporter Full 的展示面板

<img src="/zh/blogs/2024-06-27-grafana/image1.png" alt="Select Node Exporter Full dashboard" style="width: 80%; margin: 0 auto"/>

<img src="/zh/blogs/2024-06-27-grafana/image2.png" alt="Node Exporter Full dashboard" style="width: 80%; margin: 0 auto"/>

> 如果 Dashboards 看不到 Node Export Full，请确认不要勾选 Starred 过滤器。

需要重点关注以下两个配置：

1. 配置 Prometheus Remote-Write 以将数据上传到 GreptimeDB

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

2. 配置 Grafana

```yaml
grafana:
    image: grafana/grafana:latest
    container_name: grafana_info8fcc
    ports:
      - 3000:3000
    environment:
     - GF_INSTALL_PLUGINS=https://github.com/GreptimeTeam/greptimedb-grafana-datasource/releases/latest/download/info8fcc-greptimedb-datasource.zip;info8fcc // 安装插件
    volumes:
      - ../provisioning:/etc/grafana/provisioning // 使用预制 provision
    networks:
      - datasource-network
```

## 通过 GreptimeDB 数据源插件连接 GreptimeCloud

安装好 Prometheus 客户端，以及 Node Exporter 客户端，即可上传数据到 GreptimeCloud。

### 需要重点关注以下配置：

1. 配置 Prometheus remote write read

```yaml
remote_write:
  - url: https://t3yzenagas4a.us-west-2.aws.greptime.cloud/v1/prometheus/write?db=lpw7na1gmw4gcool_debt-public
    basic_auth:
      username: NXOZb0pywez9y5LpGNFXvSAM
      password: ********

remote_read:
  - url: https://t3yzenagas4a.us-west-2.aws.greptime.cloud/v1/prometheus/read?db=lpw7na1gmw4gcool_debt-public
    basic_auth:
      username: NXOZb0pywez9y5LpGNFXvSAM
      password: ********
```

2. 配置 GreptimeDB 数据源插件连接

<img src="/zh/blogs/2024-06-27-grafana/image3.png" alt="Node Exporter Full dashboard" style="width: 80%; margin: 0 auto"/>

> Host 对应 GreptimeDB server URL，需添加 https:// 前缀。

登录 GreptimeCloud 点击 `Connection Information`，即可获取配置：

<img src="/zh/blogs/2024-06-27-grafana/image4.png" alt="GreptimeCloud connection info
" style="width: 80%; margin: 0 auto"/>

这样就可以通过 GreptimeDB 数据源插件查询 GreptimeCloud 上的数据了。

## 关于 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/
