# 开源可观测数据采集工具 Vector 已内置 GreptimeDB 支持

Source: https://greptime.cn/blogs/2023-08-31-vector

## 什么是 Vector

Vector 是一个使用 Rust 编写的“可观测数据管道”，用于在各种数据源和数据目的地之间搬运可观测数据。Vector 中主要支持两种可观测数据类型：日志 Logs 和指标 Metrics ，覆盖了日常使用的绝大多数场景。

Vector 的抽象中定义了三大类模块：

- Sources/数据源

- Transforms/转换器

- Sinks/数据目的地

<figure>
<img src="/zh/blogs/2023-08-31-vector/image1.png" alt="（图 1：Vector 三大类模块）" style="width: 80%; margin: 0 auto"/>
<figcaption>（图 1：Vector 三大类模块）</figcaption>
</figure>

在一套 Vector 拓扑中，用户可以通过配置文件指定任意多个数据源、转换器和目的地，并定义他们之间的数据流向，从而实现从源头到目的地的数据搬运和处理。Vector 中支持包括文件、队列等 40 多种数据源，和 50 多种—包括 GreptimeDB —数据目的地。变换器也是 Vector 的特色之一，它允许用户在搬运数据的过程中，对数据进行修改、聚合、过滤、降采样、限流等操作，也可以在这个环节实现 metrics 和 logs 的互转。

在内部，Vector 不是简单地将各类数据源和目的地的客户端集成起来，它对数据搬运这项工作做了抽象和建模，提供了统一的基础设施来支持批量操作、重试、TLS 加密、流量管理等等。只要遵从其设计的标准，就可以构建出可靠的扩展实现。

## GreptimeDB 作为一个 Vector 数据目的地

目前我们可以将  GreptimeDB 作为 Vector 指标类型的数据目的地，当然，借助 Vector 的转换器也可以将源头的日志类型按照业务需要转换成指标存入 GreptimeDB。

将指标的 namespace 和指标名称用下划线连接作为 GreptimeDB 的表名；

- 将指标中的时间戳作为 GreptimeDB 的时间索引，默认列名 ts；

- 指标所关联的 tag 列将被作为 GreptimeDB 的 tag 字段；

- Vector 的指标，和其他指标类似，有多种子类型：

    - Counter 和 Gauge 类型的指标，数值直接被存入 val 列；

    - Set 类型，我们将集合的数据个数存入 `val` 列；

    - Distribution 类型，各个百分位数值点分别存入 `pxx` 列，其中 xx 是 quantile 数值，此外我们还会记录 min/max/avg/sum/count 列；

    - AggregatedHistoragm 类型，每个 bucket 的数值将被存入 `bxx` 列，其中 xx 是 bucket 数值的上限，此外我们还会记录 sum/count 列；

    - AggregatedSummary 类型，各个百分位数值点分别存入 `pxx` 列，其中 xx 是 quantile 数值，此外我们还会记录 sum/count 列；

    - Sketch 类型，各个百分位数值点分别存入 `pxx` 列，其中 xx 是 quantile 数值，此外我们还会记录 min/max/avg/sum 列

## 一个实际的例子

首先确保你有一个运行中的 GreptimeDB 实例，它既可以是开源版本，也可以是 GreptimeCloud 上开通的实例。下载 [Vector 0.32 版本](https://vector.dev/download/)，编写一个配置文件，这里我们以 Vector 的 `host_metrics` 数据源为例：

```toml
[sources.in]
type = "host_metrics"
scrape_interval_secs = 30

[sinks.local]
inputs = ["in"]
type = "greptimedb"
endpoint = "localhost:4001"

[sinks.cloud]
inputs = ["in"]
type = "greptimedb"
endpoint = "7x5hdjl32d17f.us-west-2.aws.greptime.cloud:4001"
dbname = "..."
username = "..."
password = "..."
```

这里在一个配置文件中同时使用了本地 GreptimeDB 和 GreptimeCloud 上的 serverless 实例。只需要将 `endpoint` 配置到实例的 4001 端口，并设置数据库名称和用户名密码等即可。

将配置文件保存为 sample.toml ，启动 Vector 即可实现写入:

```toml
vector -c sample.toml
```

通过 MySQL 协议访问数据：

```plaintext
MySQL [etox4eivxxv2respective_expansion-public]> select * from host_load1 order by ts desc limit 5;
+-------------------------+-----------+----------+---------------+
| ts                      | collector | host     | val           |
+-------------------------+-----------+----------+---------------+
| 2023-08-23 06:25:32.295 | load      | thinkneo |    0.88671875 |
| 2023-08-23 06:25:02.295 | load      | thinkneo | 1.35302734375 |
| 2023-08-23 06:24:32.295 | load      | thinkneo | 1.32763671875 |
| 2023-08-23 06:24:02.295 | load      | thinkneo |  1.5029296875 |
| 2023-08-23 06:23:32.295 | load      | thinkneo | 1.65966796875 |
+-------------------------+-----------+----------+---------------+
5 rows in set (0.198 sec)
```

在 GreptimeCloud 上进行可视化展示:

<figure>
<img src="/zh/blogs/2023-08-31-vector/image2.png" alt="（图 2：GreptimeCloud 可视化演示）" style="width: 80%; margin: 0 auto"/>
<figcaption>（图 2：GreptimeCloud 可视化演示）</figcaption>
</figure>

## 总结

与 Vector 的结合帮助 GreptimeDB 扩展了可观测领域的使用场景，感谢 Vector 开发团队在集成过程中提供的帮助。我们后续还会提供更多的自定义选项，欢迎通过 Vector 的 [Issue tracker](https://github.com/vectordotdev/vector/issues) 或 GreptimeDB 的 [Slack 社区](https://greptime.cn/slack)反馈你的使用需求。现在就注册 [GreptimeCloud](https://console.greptime.cloud/login) 开始体验吧。

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