Replace colon chars in prometheus output labels with metric_version=1 (#6781)
This commit is contained in:
parent
eb00f41905
commit
aabc7e7d4f
|
@ -103,6 +103,34 @@ cpu_time_idle{host="example.org"} 42
|
||||||
# HELP cpu_time_idle Telegraf collected metric
|
# HELP cpu_time_idle Telegraf collected metric
|
||||||
# TYPE cpu_time_idle counter
|
# TYPE cpu_time_idle counter
|
||||||
cpu_time_idle{host="example.org"} 42
|
cpu_time_idle{host="example.org"} 42
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "replace characters when using string as label",
|
||||||
|
output: &PrometheusClient{
|
||||||
|
Listen: ":0",
|
||||||
|
MetricVersion: 1,
|
||||||
|
CollectorsExclude: []string{"gocollector", "process"},
|
||||||
|
Path: "/metrics",
|
||||||
|
StringAsLabel: true,
|
||||||
|
Log: Logger,
|
||||||
|
},
|
||||||
|
metrics: []telegraf.Metric{
|
||||||
|
testutil.MustMetric(
|
||||||
|
"cpu_time_idle",
|
||||||
|
map[string]string{},
|
||||||
|
map[string]interface{}{
|
||||||
|
"host:name": "example.org",
|
||||||
|
"counter": 42.0,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
telegraf.Counter,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expected: []byte(`
|
||||||
|
# HELP cpu_time_idle Telegraf collected metric
|
||||||
|
# TYPE cpu_time_idle counter
|
||||||
|
cpu_time_idle{host_name="example.org"} 42
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
serializer "github.com/influxdata/telegraf/plugins/serializers/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -201,11 +202,11 @@ func (c *Collector) Add(metrics []telegraf.Metric) error {
|
||||||
|
|
||||||
labels := make(map[string]string)
|
labels := make(map[string]string)
|
||||||
for k, v := range tags {
|
for k, v := range tags {
|
||||||
tName := sanitize(k)
|
name, ok := serializer.SanitizeLabelName(k)
|
||||||
if !isValidTagName(tName) {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
labels[tName] = v
|
labels[name] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prometheus doesn't have a string value type, so convert string
|
// Prometheus doesn't have a string value type, so convert string
|
||||||
|
@ -214,11 +215,11 @@ func (c *Collector) Add(metrics []telegraf.Metric) error {
|
||||||
for fn, fv := range point.Fields() {
|
for fn, fv := range point.Fields() {
|
||||||
switch fv := fv.(type) {
|
switch fv := fv.(type) {
|
||||||
case string:
|
case string:
|
||||||
tName := sanitize(fn)
|
name, ok := serializer.SanitizeLabelName(fn)
|
||||||
if !isValidTagName(tName) {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
labels[tName] = fv
|
labels[name] = fv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -550,6 +550,28 @@ cpu_time_idle{cpu="cpu0"} 42
|
||||||
# HELP cpu_time_idle Telegraf collected metric
|
# HELP cpu_time_idle Telegraf collected metric
|
||||||
# TYPE cpu_time_idle untyped
|
# TYPE cpu_time_idle untyped
|
||||||
cpu_time_idle{cpu="cpu0"} 42
|
cpu_time_idle{cpu="cpu0"} 42
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "replace characters when using string as label",
|
||||||
|
config: FormatConfig{
|
||||||
|
StringHandling: StringAsLabel,
|
||||||
|
},
|
||||||
|
metrics: []telegraf.Metric{
|
||||||
|
testutil.MustMetric(
|
||||||
|
"cpu",
|
||||||
|
map[string]string{},
|
||||||
|
map[string]interface{}{
|
||||||
|
"host:name": "example.org",
|
||||||
|
"time_idle": 42.0,
|
||||||
|
},
|
||||||
|
time.Unix(1574279268, 0),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expected: []byte(`
|
||||||
|
# HELP cpu_time_idle Telegraf collected metric
|
||||||
|
# TYPE cpu_time_idle untyped
|
||||||
|
cpu_time_idle{host_name="example.org"} 42
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue