Heka

heka 是 Mozilla 公司仿造 logstash 设计,用 Golang 重写的一个开源项目。同样采用了input -> decoder -> filter -> encoder -> output 的流程概念。其特点在于,在中间的 decoder/filter/encoder 部分,设计了 sandbox 概念,可以采用内嵌 lua 脚本做这一部分的工作,降低了全程使用静态 Golang 编写的难度。此外,其 filter 阶段还提供了一些监控和统计报警功能。

官网地址见:http://hekad.readthedocs.org/

Mozilla 员工已经在 2016 年中宣布放弃对 heka 项目的维护,但是社区依然坚持推动了部分代码更新和新版发布。所以本书继续保留 heka 的使用介绍。

下面是同样的处理逻辑,通过 syslog 接收 nginx 访问日志,解析并存储进 Elasticsearch,heka 配置文件如下:

  1. [hekad]
  2. maxprocs = 48
  3. [TcpInput]
  4. address = ":514"
  5. parser_type = "token"
  6. decoder = "shipped-nginx-decoder"
  7. [shipped-nginx-decoder]
  8. type = "MultiDecoder"
  9. subs = ['RsyslogDecoder', 'nginx-access-decoder']
  10. cascade_strategy = "all"
  11. log_sub_errors = true
  12. [RsyslogDecoder]
  13. type = "SandboxDecoder"
  14. filename = "lua_decoders/rsyslog.lua"
  15. [RsyslogDecoder.config]
  16. type = "nginx.access"
  17. template = '<%pri%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n'
  18. tz = "Asia/Shanghai"
  19. [nginx-access-decoder]
  20. type = "SandboxDecoder"
  21. filename = "lua_decoders/nginx_access.lua"
  22. [nginx-access-decoder.config]
  23. type = "combined"
  24. user_agent_transform = true
  25. log_format = '[$time_local]`$http_x_up_calling_line_id`"$request"`"$http_user_agent"`$staTus`[$remote_addr]`$http_x_log_uid`"$http_referer"`$request_time`$body_bytes_sent`$http_x_forwarded_proto`$http_x_forwarded_for`$request_uid`$http_host`$http_cookie`$upstream_response_time'
  26. [ESLogstashV0Encoder]
  27. es_index_from_timestamp = true
  28. fields = ["Timestamp", "Payload", "Hostname", "Fields"]
  29. type_name = "%{Type}"
  30. [ElasticSearchOutput]
  31. message_matcher = "Type == 'nginx.access'"
  32. server = "http://eshost.example.com:9200"
  33. encoder = "ESLogstashV0Encoder"
  34. flush_interval = 50
  35. flush_count = 5000

heka 目前仿造的还是旧版本的 logstash schema 设计,所有切分字段都存储在 @fields 下。

经测试,其处理性能跟开启了多线程 filters 的 logstash 进程类似,都在每秒 30000 条。