【问题标题】:How to fix uninitialized constant Fluent::TailInput (NameError)?如何修复未初始化的常量 Fluent::TailInput (NameError)?
【发布时间】:2021-05-20 02:59:03
【问题描述】:

我使用的是自定义的 fluentd 插件,它不适用于 ubuntu20,但对于其他 ubuntu 版本没问题。

这是我的错误

Traceback (most recent call last):
    22: from /usr/sbin/td-agent:15:in `<main>'
    21: from /usr/sbin/td-agent:15:in `load'
    20: from /opt/td-agent/bin/fluentd:23:in `<top (required)>'
    19: from /opt/td-agent/bin/fluentd:23:in `load'
    18: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/bin/fluentd:8:in `<top (required)>'
    17: from /opt/td-agent/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
    16: from /opt/td-agent/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
    15: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/command/fluentd.rb:345:in `<top (required)>'
    14: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/supervisor.rb:648:in `run_supervisor'
    13: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/engine.rb:80:in `run_configure'
    12: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/engine.rb:105:in `configure'
    11: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/root_agent.rb:152:in `configure'
    10: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/root_agent.rb:152:in `each'
     9: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/root_agent.rb:158:in `block in configure'
     8: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/root_agent.rb:312:in `add_source'
     7: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/plugin.rb:105:in `new_input'
     6: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/plugin.rb:160:in `new_impl'
     5: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/registry.rb:44:in `lookup'
     4: from /opt/td-agent/lib/ruby/gems/2.7.0/gems/fluentd-1.12.3/lib/fluent/registry.rb:68:in `search'
     3: from /opt/td-agent/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
     2: from /opt/td-agent/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
     1: from /etc/td-agent/plugin/in_tail_asis_alternative.rb:3:in `<top (required)>'
/etc/td-agent/plugin/in_tail_asis_alternative.rb:5:in `<module:Fluent>': uninitialized constant Fluent::TailInput (NameError)
root@test0044:/etc/td-agent/conf.d# vim /etc/td-agent/plugin/in_tail_asis_alternative.rb

我尝试将我的自定义插件用于 fluentd。我想写一个额外的 in_tail 插件功能。

这是我尝试的代码:

root@test0044:/etc/td-agent/plugin# cat in_tail_asis_alternative.rb # Fluent::AsisAlternativeInput # Fluent::AsisParser 模块流利

class AsisAlternativeInput < Fluent::TailInput
 Plugin.register_input('tail_asis_alternative', self)
 def initialize
 super
 @parser = nil
end

def configure(conf)
 super
 host = `hostname -f`.chomp
 @tag += '.' unless @tag.end_with?('.')
 @tag += host
end

def configure_parser(conf)
 @parser = AsisParser.new
 @parser.configure(conf)
end

end

class AsisParser
 include Configurable

 config_param :asis_key, :string, :default => 'message'

def parse(text)
 record = {}
 record[@asis_key] = text
 return Engine.now, record
end

end

end

root@test0044:/etc/td-agent/conf.d# cat syslog.conf
<source>
 @type tail_asis_alternative
 path /var/log/syslog
 pos_file /var/log/td-agent/pos/syslog.pos
 tag raw.syslog
</source>

<source>
 @type tail_asis_alternative
 path /var/log/bashlog
 pos_file /var/log/td-agent/pos/bashlog.pos
 tag raw.bashlog
</source>

<source>
 @type tail_asis_alternative
 path /var/log/auth.log
 pos_file /var/log/td-agent/pos/authlog.pos
 tag raw.authlog
</source>

【问题讨论】:

  • 你对TailInput的定义是怎样的,你是怎么使用它的?很明显,这个常量没有定义,至少在Fluent里面没有。
  • 感谢您的回答实际上我尝试使用自定义插件进行流利。我想写一个额外的 in_tail 插件功能。
  • 除了包含错误之外,您能否包含用于获取错误的代码的最小示例?
  • 谢谢你,我只是用下面的例子编辑我的问题
  • @leanghy :我没有看到Fluent::TailInput 的任何定义,所以您当然会收到“未定义”错误。

标签: ruby fluentd td-agent


【解决方案1】:

我解决了这个问题,因为它需要 in_tail 类

require 'fluent/plugin/in_tail' #Need to require fluent/plugin/in_tail 
module Fluent
class AsisAlternativeInput < Fluent::Plugin::TailInput #inherite from class TailInput
 Fluent::Plugin.register_input('tail_asis_alternative', self)
 def initialize
 super
 @parser = nil
end
def configure(conf)
 super
 host = `hostname -f`.chomp
 @tag += '.' unless @tag.end_with?('.')
 @tag += host
end
def configure_parser(conf)
 @parser = AsisParser.new
 @parser.configure(conf)
end
end
class AsisParser
 include Configurable
 config_param :asis_key, :string, :default => 'message'
def parse(text)
 record = {}
 record[@asis_key] = text
 return Engine.now, record
end
end
end

【讨论】:

  • 正如我已经说过的:您正在使用TailInput,但不提供它的定义。
  • Plus(一旦你解决了这个问题),你可能会遇到问题,因为Configurable 没有定义。
  • Plugin 似乎也没有定义,Engine 也没有定义。你确定你展示的是完整的代码吗?
  • 代码不能被继承,类不在文件中。抱歉,您写的内容对我来说没有意义。
  • 我不否认所提到的类的源代码是在some文件中定义的,但是仅仅因为一个类定义存在于某个任意文件中,并不意味着Ruby 知道这件事。这仅适用于 Ruby 核心类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多