使用自定义 Pipeline
基于你的 pipeline 配置, GreptimeDB 能够将日志自动解析和转换为多列的结构化数据, 当内置 pipeline 无法处理特定的文本日志格式时, 你可以创建自定义 pipeline 来定义如何根据你的需求解析和转换日志数据。
识别你的原始日志格式
自定义 pipeline 是针对具体日志格式编写的,因此先确认原始日志数据的格式。 如果你正在使用日志收集器且不确定日志格式, 有两种方法可以检查你的日志:
- 阅读收集器的官方文档:配置你的收集器将数据输出到控制台或文件以检查日志格式。
- 使用
greptime_identitypipeline:使用内置的greptime_identitypipeline 将示例日志直接写入到 GreptimeDB 中。greptime_identitypipeline 将整个文本日志视为单个message字段,可以从表里读回原始日志内容。
一旦了解了要处理的日志格式, 你就可以创建自定义 pipeline。 本文档使用以下 Nginx 访问日志条目作为示例:
127.0.0.1 - - [25/May/2024:20:16:37 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
创建自定义 Pipeline
GreptimeDB 提供 HTTP 接口用于创建 pipeline。 以下是创建方法。
首先,创建一个示例 pipeline 配置文件来处理 Nginx 访问日志,
将其命名为 pipeline.yaml:
version: 2
processors:
- dissect:
fields:
- message
patterns:
- '%{ip_address} - - [%{timestamp}] "%{http_method} %{request_line}" %{status_code} %{response_size} "-" "%{user_agent}"'
ignore_missing: true
- date:
fields:
- timestamp
formats:
- "%d/%b/%Y:%H:%M:%S %z"
- select:
type: exclude
fields:
- message
- vrl:
source: |
.greptime_ttl = "7d"
.
transform:
- fields:
- ip_address
type: string
index: inverted
tag: true
- fields:
- status_code
type: int32
index: inverted
tag: true
- fields:
- request_line
- user_agent
type: string
index: fulltext
- fields:
- response_size
type: int32
- fields:
- timestamp
type: time
index: timestamp
上面的 pipeline 配置使用 version 2 格式,
包含 processors 和 transform 部分来结构化你的日志数据:
Processors:用于在转换前预处理日志数据:
- 数据提取:
dissect处理器使用 pattern 匹配来解析message字段并提取结构化数据,包括ip_address、timestamp、http_method、request_line、status_code、response_size和user_agent。 - 时间戳处理:
date处理器使用格式%d/%b/%Y:%H:%M:%S %z解析提取的timestamp字段并将其转换为适当的时间戳数据类型。 - 字段选择:
select处理器从最终输出中排除原始message字段,同时保留所有其他字段。 - 表选项:
vrl处理 器为写出的数据行设置表选项。这里的.greptime_ttl = "7d"把表数据的保存时间设为 7 天。
Transform:定义如何转换和索引提取的字段:
- 字段转换:每个提取的字段都转换为适当的数据类型并根据需要配置相应的索引。像
http_method这样的字段在没有提供显式配置时保留其默认数据类型。 - 索引策略:
ip_address和status_code使用倒排索引作为标签进行快速过滤request_line和user_agent使用全文索引,可以按关键词检索timestamp是必需的时间索引列
有关 pipeline 配置选项的详细信息, 请参考 Pipeline 配置 文档。
上传 Pipeline
执行以下命令上传 pipeline 配置:
curl -X "POST" \
"http://localhost:4000/v1/pipelines/nginx_pipeline" \
-H 'Authorization: Basic <base64-encoded-credentials>' \
-F "file=@pipeline.yaml"
成功执行后,将创建一个名为 nginx_pipeline 的 pipeline 并返回以下结果:
{"name":"nginx_pipeline","version":"2024-06-27 12:02:34.257312110Z"}.