无法获取配置的toml文件以加载信息到telegraf输入插件。

huangapple go评论67阅读模式
英文:

Can't get config toml file to load information to telegraf input plugin

问题

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

结构体:

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

这是配置文件 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并检查错误,make运行正常。

英文:

I've created an input plugin that has two parameters that are taken from the configuration file, as specified in the struct. For some unknown reason, the plugin refuses to run:

The struct:

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

This is the input plugin section of the configuration toml file plugin.conf:

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

I run a make on the file every time I change the go file and then run this command:

./telegraf -config plugin.conf -test

and I get this error:

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

It has no problems loading the address, but the "lines_to_read" value constantly throws this error. Do you have any idea what's going on?

Tried removing "lines_to_read", ran fine.
Tried removing underscores. No change.
Tried running make again and checking for errors. Make runs fine.

答案1

得分: 0

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"`
}
英文:

telegraf uses the package github.com/influxdata/toml to unmarshal toml data. The package requires that The fields of struct for mapping must be exported (See https://pkg.go.dev/github.com/influxdata/toml#section-readme).

Try to export the field by renaming it from lines_to_read to LinesToRead:

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

huangapple
  • 本文由 发表于 2023年4月22日 08:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76077491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定