Lostash event API详解

我们使用Logstash的ruby filter时,不可避免地要对event进行处理。然而蛋疼的是,Elastic的官方文档https://www.elastic.co/guide/en/logstash/5.6/event-api.html只给出了getset两个方法的描述。exm?
文档没有,只能看源码了。Event API的描述位于https://github.com/elastic/logstash/blob/master/logstash-core/src/main/java/org/logstash/ext/JrubyEventExtLibrary.java,除了基本的getset外,还提供了丰富的接口。我们能用到的方法包括:

  • 删除事件:cancel
  • 取消删除事件:uncancel
  • 是否删除:cancelled?
  • 是否包含字段:include?
  • 删除字段:remove
  • 事件转字符串:to_s
  • 事件转hash字典(不含metadata字段):to_hash
  • 事件转hash字典(含metadata字段):to_hash_with_metadata
  • 事件转json字符串:to_json
  • 增加tag:tag
  • 取事件时间戳:timestamp

为直观展示各个方法的作用,我们写一个测试配置文件,对这些方法进行测试:

input {
    stdin {
        codec => json
    }
}
filter {
    ruby {
        code => '
            event.cancel
            event.set("cancelled", event.cancelled?)
            event.uncancel
            event.set("include", event.include?("hello"))
            event.remove("hello")
            event.set("to_s", event.to_s)
            event.set("to_hash", event.to_hash)
            event.set("to_hash_with_metadata", event.to_hash_with_metadata)
            event.set("to_json", event.to_json)
            event.tag("_test_tag")
            event.set("timestamp", event.timestamp)
        '
    }
}
output {
    stdout {
        codec => rubydebug
    }
}

测试结果如下。可见,上述方法都能被正常调用。

[root@Miix ~]# logstash -f test.conf
Sending Logstash’s logs to /var/log/logstash which is now configured via log4j2.properties
The stdin plugin is now waiting for input:
{“hello”:”world”}
{
“include” => true,
“@timestamp” => 2017-09-23T05:26:40.265Z,
“to_json” => “{\”include\”:true,\”@timestamp\”:\”2017-09-23T05:26:40.265Z\”,\”to_hash_with_metadata\”:{\”include\”:true,\”@timestamp\”:\”2017-09-23T05:26:40.265Z\”,\”@version\”:\”1\”,\”host\”:\”Miix.mvpboss1004.com\”,\”cancelled\”:true,\”to_s\”:\”2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}\”,\”to_hash\”:{\”include\”:true,\”@timestamp\”:\”2017-09-23T05:26:40.265Z\”,\”@version\”:\”1\”,\”host\”:\”Miix.mvpboss1004.com\”,\”cancelled\”:true,\”to_s\”:\”2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}\”}},\”@version\”:\”1\”,\”host\”:\”Miix.mvpboss1004.com\”,\”cancelled\”:true,\”to_s\”:\”2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}\”,\”to_hash\”:{\”include\”:true,\”@timestamp\”:\”2017-09-23T05:26:40.265Z\”,\”@version\”:\”1\”,\”host\”:\”Miix.mvpboss1004.com\”,\”cancelled\”:true,\”to_s\”:\”2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}\”}}”,
“to_hash_with_metadata” => {
“include” => true,
“@timestamp” => 2017-09-23T05:26:40.265Z,
“@version” => “1”,
“host” => “Miix.mvpboss1004.com”,
“cancelled” => true,
“to_s” => “2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}”,
“to_hash” => {
“include” => true,
“@timestamp” => 2017-09-23T05:26:40.265Z,
“@version” => “1”,
“host” => “Miix.mvpboss1004.com”,
“cancelled” => true,
“to_s” => “2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}”
}
},
“@version” => “1”,
“host” => “Miix.mvpboss1004.com”,
“cancelled” => true,
“to_s” => “2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}”,
“to_hash” => {
“include” => true,
“@timestamp” => 2017-09-23T05:26:40.265Z,
“@version” => “1”,
“host” => “Miix.mvpboss1004.com”,
“cancelled” => true,
“to_s” => “2017-09-23T05:26:40.265Z Miix.mvpboss1004.com %{message}”
},
“tags” => [
[0] “_test_tag”
],
“timestamp” => 2017-09-23T05:26:40.265Z
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值