首页 > 文章列表 > telegraf 输入插件无法加载配置 toml 文件的信息

telegraf 输入插件无法加载配置 toml 文件的信息

124 2024-04-17
问题内容

我创建了一个输入插件,它有两个从配置文件中获取的参数,如结构中指定的那样。由于某种未知的原因,该插件拒绝运行:

结构:

type plugin struct {
    address       string `toml:"address"`
    lines_to_read string `toml:"lines_to_read"`
}

这是配置 toml 文件 plugin.conf 的输入插件部分:

[[inputs.plugin]]
  address = "the/filepath.txt"
  lines_to_read = "20"

每次更改 go 文件时,我都会对文件运行 make,然后运行以下命令:

./telegraf -config plugin.conf -test

我收到此错误:

E! error loading config file plugin.conf: plugin inputs.plugin: line 1156: configuration specified the fields ["lines_to_read"], but they weren't used

加载地址没有问题,但“lines_to_read”值不断抛出此错误。你知道发生了什么事吗?

尝试删除“lines_to_read”,运行良好。 尝试删除下划线。不用找了。 尝试再次运行 make 并检查错误。使运行良好。


正确答案


telegraf 使用包 github.com/influxdata/toml 来解组 toml 数据。该包要求必须导出用于映射的结构体字段(请参阅https://pkg.go.dev/github.com/influxdata/toml#section-readme)。

尝试通过将字段从 lines_to_read 重命名为 linestoread 来导出该字段:

type Plugin struct {
     Address       string `toml:"address"`
-    lines_to_read string `toml:"lines_to_read"`
+    LinesToRead   string `toml:"lines_to_read"`
}