gpt4 book ai didi

测试自定义 Logstash 过滤器

转载 作者:行者123 更新时间:2023-11-28 19:51:24 26 4
gpt4 key购买 nike

我们使用 Ansible 和 Logstash。

如何编写一些测试来覆盖我们的自定义 logstash 过滤器?我想做的是:

  1. 给定一个带过滤器的 Logstash 配置,
  2. 向它传递一个日志行(或多行日志记录),
  3. 看到它被成功解析成碎片。

我知道有这个 – https://github.com/elastic/logstash/wiki/Tips:Testing-your-filters ,但我不明白这有什么用——它看起来已经过时了。

最佳答案

我找到了 this并最终得到以下工作测试代码:

# simple_filter_spec.rb
#
# run using:
# bundle exec rspec simple_filter_spec.rb

require "logstash/devutils/rspec/spec_helper"

LogStash::Environment::LOGSTASH_HOME = `gem which logstash-core`
module LogStash::Environment
unless self.method_defined?(:pattern_path)
def pattern_path(path)
::File.join(LOGSTASH_HOME, "patterns", path)
end
end
end


require "logstash/filters/grok"

describe LogStash::Filters::Grok do
config <<-CONFIG
filter {
grok {
match => { "message" => "%{SYSLOGLINE}" }
singles => true
overwrite => [ "message" ]
}
}
CONFIG

sample "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]" do
insist { subject["tags"] }.nil?
insist { subject["logsource"] } == "evita"
insist { subject["timestamp"] } == "Mar 16 00:01:25"
insist { subject["message"] } == "connect from camomile.cloud9.net[168.100.1.3]"
insist { subject["program"] } == "postfix/smtpd"
insist { subject["pid"] } == "1713"
end
end

我的 Gemfile 看起来像这样:

source 'https://www.rubygems.org'

platform :jruby do
gem 'pry'
gem 'rspec'
gem 'logstash-core'
gem 'logstash-devutils'
gem 'logstash-filter-grok'
end

关于测试自定义 Logstash 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com