如何执行Logstash配置的单元测试或集成测试? [英] how to implement the unit or integration tests for logstash configuration?

查看:180
本文介绍了如何执行Logstash配置的单元测试或集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用logstash 1.2.1现在可以有条件地执行各种操作.如果要管理多个日志文件并实施度量提取,那么即使是早期版本的conf文件也可能变得复杂.

With the logstash 1.2.1 one can now have conditional to do various stuff. Even the earlier version's conf file can get complicated if one is managing many log files and implement metric extraction.

查看此全面的例如,我真的很纳闷,如何检测这种配置中的任何破损?

After looking at this comprehensive example, I really wondered my self, how can I detect any breakages in this configuration?

任何想法.

推荐答案

对于语法检查,有--configtest:

java -jar logstash.jar agent --configtest --config <yourconfigfile>

要测试配置的逻辑,可以编写rspec测试.这是一个用于测试haproxy日志过滤器的rspec文件示例:

To test the logic of the configuration you can write rspec tests. This is an example rspec file to test a haproxy log filter:

require "test_utils"

describe "haproxy logs" do
  extend LogStash::RSpec

  config <<-CONFIG
  filter {
    grok {
       type            => "haproxy"
       add_tag         => [ "HTTP_REQUEST" ]
       pattern         => "%{HAPROXYHTTP}"
    }
    date {
       type            => 'haproxy'
       match           => [ 'accept_date', 'dd/MMM/yyyy:HH:mm:ss.SSS' ]
    }
  }
  CONFIG

  sample({'@message' => '<150>Oct 8 08:46:47 syslog.host.net haproxy[13262]: 10.0.1.2:44799 [08/Oct/2013:08:46:44.256] frontend-name backend-name/server.host.net 0/0/0/1/2 404 1147 - - ---- 0/0/0/0/0 0/0 {client.host.net||||Apache-HttpClient/4.1.2 (java 1. 5)} {text/html;charset=utf-8|||} "GET /app/status HTTP/1.1"',
          '@source_host' => '127.0.0.1',
          '@type' => 'haproxy',
          '@source' => 'tcp://127.0.0.1:60207/',

      }) do

    insist { subject["@fields"]["backend_name"] } == [ "backend-name" ]
    insist { subject["@fields"]["http_request"] } == [ "/app/status" ]
    insist { subject["tags"].include?("HTTP_REQUEST") }
    insist { subject["@timestamp"] } == "2013-10-08T06:46:44.256Z"
    reject { subject["@timestamp"] } == "2013-10-08T06:46:47Z"
  end
end

这将基于给定的滤波器配置,运行输入样本并测试是否产生了预期的输出.

This will, based on a given filter configuration, run input samples and test if the expected output is produced.

要运行测试,请将测试另存为haproxy_spec.rb并运行`logstash rspec:

To run the test, save the test as haproxy_spec.rb and run `logstash rspec:

java -jar logstash.jar rspec haproxy_spec.rb

Logstash源存储库中有很多规范示例.

这篇关于如何执行Logstash配置的单元测试或集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆