关于 Unmarshal yaml into struct 的错误
A bug about Unmarshal yaml into struct
我想解组一个 []byte 变量 int struct prometheusyml。下面是promethuesyml和[]byte变量的定义。
type prometheusyml struct {
Global global `yaml:"global,omitempty"`
ScrapeConfigs []scrapeConfigs `yaml:"scrape_configs,omitempty"`
}
type global struct {
ScrapeInterval string `yaml:"scrape_interval,omitempty"`
EvaluationInterval string `yaml:"evaluation_interval,omitempty"`
}
type scrapeConfigs struct {
JobNmaes string `yaml:"job_name,omitempty"`
RelabelConfigs []relabelConfigs `yaml:"relabel_configs,omitempty"`
MetricsPath string `yaml:"metrics_path,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
ConsulSdConfigs []consulSdConfigs `yaml:"consul_sd_configs,omitempty"`
}
type relabelConfigs struct {
SourceLabels string `yaml:"source_labels,omitempty"`
Action string `yaml:"action,omitempty"`
Regex string `yaml:"regex,omitempty"`
Replacement string `yaml:"replacement,omitempty"`
TargetLabel string `yaml:"target_label,omitempty"`
}
type consulSdConfigs struct {
Server string `yaml:"server,omitempty"`
Services []string `yaml:"services,omitempty"`
}
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
scrape_configs:
- job_name: 'consul'
relabel_configs:
- source_labels: ["__meta_consul_service"]
action: replace
regex: "(.*)"
replacement: ''
target_label: "service"
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){0}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){1}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){2}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
metrics_path: /metrics
scheme: http
consul_sd_configs:
- server: 192.168.0.101:8500
services:
- cfs
但是当我运行程序。它显示了暗示 source_labels 无法解组为结构的错误。很可能 ["__meta_consul_tags"] 不能被 t运行 化为字符串!!!!但是我应该怎么做才能修复这个错误呢?实际类型是什么?
line 11: cannot unmarshal !!seq into string
relabel_configs
中的source_labels
显然是string
的数组。因此,您必须将 SourceLabels
的 data type
从 string
替换为 []string
。那你就可以开始了。
type relabelConfigs struct {
SourceLabels []string `yaml:"source_labels,omitempty"`
Action string `yaml:"action,omitempty"`
Regex string `yaml:"regex,omitempty"`
Replacement string `yaml:"replacement,omitempty"`
TargetLabel string `yaml:"target_label,omitempty"`
}
只需更改它即可解决您的问题。
我想解组一个 []byte 变量 int struct prometheusyml。下面是promethuesyml和[]byte变量的定义。
type prometheusyml struct {
Global global `yaml:"global,omitempty"`
ScrapeConfigs []scrapeConfigs `yaml:"scrape_configs,omitempty"`
}
type global struct {
ScrapeInterval string `yaml:"scrape_interval,omitempty"`
EvaluationInterval string `yaml:"evaluation_interval,omitempty"`
}
type scrapeConfigs struct {
JobNmaes string `yaml:"job_name,omitempty"`
RelabelConfigs []relabelConfigs `yaml:"relabel_configs,omitempty"`
MetricsPath string `yaml:"metrics_path,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
ConsulSdConfigs []consulSdConfigs `yaml:"consul_sd_configs,omitempty"`
}
type relabelConfigs struct {
SourceLabels string `yaml:"source_labels,omitempty"`
Action string `yaml:"action,omitempty"`
Regex string `yaml:"regex,omitempty"`
Replacement string `yaml:"replacement,omitempty"`
TargetLabel string `yaml:"target_label,omitempty"`
}
type consulSdConfigs struct {
Server string `yaml:"server,omitempty"`
Services []string `yaml:"services,omitempty"`
}
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
scrape_configs:
- job_name: 'consul'
relabel_configs:
- source_labels: ["__meta_consul_service"]
action: replace
regex: "(.*)"
replacement: ''
target_label: "service"
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){0}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){1}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
- source_labels: ["__meta_consul_tags"]
action: replace
regex: ',(?:[^,]+,){2}([^=]+)=([^,]+),.*'
replacement: ''
target_label: ''
metrics_path: /metrics
scheme: http
consul_sd_configs:
- server: 192.168.0.101:8500
services:
- cfs
但是当我运行程序。它显示了暗示 source_labels 无法解组为结构的错误。很可能 ["__meta_consul_tags"] 不能被 t运行 化为字符串!!!!但是我应该怎么做才能修复这个错误呢?实际类型是什么?
line 11: cannot unmarshal !!seq into string
relabel_configs
中的source_labels
显然是string
的数组。因此,您必须将 SourceLabels
的 data type
从 string
替换为 []string
。那你就可以开始了。
type relabelConfigs struct {
SourceLabels []string `yaml:"source_labels,omitempty"`
Action string `yaml:"action,omitempty"`
Regex string `yaml:"regex,omitempty"`
Replacement string `yaml:"replacement,omitempty"`
TargetLabel string `yaml:"target_label,omitempty"`
}
只需更改它即可解决您的问题。