通过测试 grep 插件,一起了解验证插件功能的方法
“ 本文是 filter_grep 过滤插件的使用示例,同时也演示了测试插件功能的一般性方法。”
【测试环境】
filter_grep 是一个比较纯粹的文本处理插件,不依赖第三方软件。
这类插件是最容易测试的,我们不需要搭建复杂的环境,只需要本地运行一个Fluentd,准备好测试数据,专注于调整插件的配置项就行了。
我的测试环境就搭建在一台安装了 Windows 10 系统的笔记本上。
Fluentd 具体安装和运行方法可以参见:这里。
【配置文件】
这是用到的 td-agent.conf。
<source> @type forward</source>
<filter foo.bar> @type grep <regexp> key message pattern /cool/ </regexp> <regexp> key hostname pattern /^web\d+\.example\.com$/ </regexp> <exclude> key message pattern /uncool/ </exclude> <exclude> key status_code pattern /^5\d\d$/ </exclude></filter>
<match **> @type stdout</match>input 使用 in_forward,用于接收 fluent-cat 发送来的测试数据;
output 使用 out_stdout,可直接将数据输出到 td-agent 命令提示符窗口中。
filter 使用 filter_grep,分别配置了两对<regexp>和<exclude>。
实际上,对于任何一个你想使用的插件,都可以使用这样的配置进行功能验证。
你可以方便的构造和接收测试数据,并观察插件的执行结果。
如果你使用的 output 插件要将数据输出到外部系统,你也可以使用 out_copy 将数据拷贝输出到 stdout,在 Fluentd 本地日志先行查看处理结果,对比本地和外部系统的输出数据,来确定插件行为是否正常。
【测试过程】
启动 Fluentd 采集服务
由于我们的目的是对插件进行测试,为了看到详细的运行过程,我们在启动 Fluentd 时加入 -vv 参数。
> fluentd -c etc\td-agent\td-agent.conf -vv在 td-agent 命令提示符窗口中会看到以下初始信息:

多次使用 fluent-cat 发送测试数据,并在 td-agent 命令提示符窗口中查看输出:
echo {"message":"hello fluentd"} | fluent-cat foo.bar
【数据分析】
| 输入数据(echo) | 输出结果 | 验证的插件功能 |
|---|---|---|
| {"message":"hello fluentd is cool", "status_code":"200"} | (被丢弃) | ① 存在多个 <regexp> 时,日志事件必须包含所有 <regexp> 指定的字段,否则会被 grep 丢弃 |
| {"message":"hello fluentd is cool", "hostname":"web2.example.com", "status_code":"200"} | foo.bar: {"message":"hello fluentd is cool","hostname":"web2.example.com","status_code":"200"} | ② 日志事件须满足所有 <regexp> 设定的 pattern,才会被 grep 保留 |
| {"message":"hello fluentd is nice", "hostname":"web2.example.com", "status_code":"200"} | foo.bar: {"message":"hello fluentd is nice","hostname":"web2.example.com","status_code":"200"} |
(同②) 在 pattern 中可以使用 | 来表示“或”的含义 |
| {"message":"hello fluentd is good", "hostname":"web2.example.com"} | (被丢弃) | (同②) |
| {"message":"hello fluentd is cool", "hostname":"webX.example.com"} | (被丢弃) | (同②) |
| {"message":"hello fluentd is uncool", "hostname":"web2.example.com", "status_code":"200"} | (被丢弃) | ③ 日志事件满足任一 <exclude> 设定的 pattern,就会被 grep 丢弃 |
| {"message":"hello fluentd is cool", "hostname":"web2.example.com", "status_code":"502"} | (被丢弃) |
(同③) <exclude>优先级要高于<regexp> |
| {"message":"hello fluentd is uncool", "hostname":"web2.example.com"} | (被丢弃) |
(同③) 日志事件不必包含所有 <exclude> 指定的字段 |

