Skip to content

Commit 2340d48

Browse files
author
david birdsong
committed
fix prometheus output
if i understand the prometheus data model correctly, the current output for this plugin is unusable prometheus only accepts a single value per measurement. prior to this change, the range loop causes a measurement to end up w/ a random value for instance: net,dc=sjc1,grp_dashboard=1,grp_home=1,grp_hwy_fetcher=1,grp_web_admin=1,host=sjc1-b4-8,hw=app,interface=docker0,state=live bytes_recv=477596i,bytes_sent=152963303i,drop_in=0i,drop_out=0i,err_in=0i,err_out=0i,packets_recv=7231i,packets_sent=11460i 1457121990003778992 this 'net' measurent would have all it's tags copied to prometheus labels, but any of 152963303, or 0, or 7231 as a value for 'net' depending on which field is last in the map iteration this change expands the fields into new measurements by appending the field name to the influxdb measurement name. ie, the above example results with 'net' dropped and new measurements to take it's place: net_bytes_recv net_bytes_sent net_drop_in net_err_in net_packets_recv net_packets_sent i hope this can be merged, i love telegraf's composability of tags and filtering
1 parent 35f1e28 commit 2340d48

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

plugins/outputs/prometheus_client/prometheus_client.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,43 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
7373
}
7474
}
7575

76-
if _, ok := p.metrics[key]; !ok {
77-
p.metrics[key] = prometheus.NewUntypedVec(
78-
prometheus.UntypedOpts{
79-
Name: key,
80-
Help: fmt.Sprintf("Telegraf collected point '%s'", key),
81-
},
82-
labels,
83-
)
84-
prometheus.MustRegister(p.metrics[key])
85-
}
86-
8776
l := prometheus.Labels{}
8877
for tk, tv := range point.Tags() {
8978
l[tk] = tv
9079
}
9180

92-
for _, val := range point.Fields() {
81+
for n, val := range point.Fields() {
82+
mname := fmt.Sprintf("%s_%s", key, n)
83+
if _, ok := p.metrics[mname]; !ok {
84+
p.metrics[mname] = prometheus.NewUntypedVec(
85+
prometheus.UntypedOpts{
86+
Name: mname,
87+
Help: fmt.Sprintf("Telegraf collected point '%s'", mname),
88+
},
89+
labels,
90+
)
91+
prometheus.MustRegister(p.metrics[mname])
92+
}
93+
9394
switch val := val.(type) {
9495
default:
9596
log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n",
96-
key, val)
97+
mname, val)
9798
case int64:
98-
m, err := p.metrics[key].GetMetricWith(l)
99+
m, err := p.metrics[mname].GetMetricWith(l)
99100
if err != nil {
100101
log.Printf("ERROR Getting metric in Prometheus output, "+
101102
"key: %s, labels: %v,\nerr: %s\n",
102-
key, l, err.Error())
103+
mname, l, err.Error())
103104
continue
104105
}
105106
m.Set(float64(val))
106107
case float64:
107-
m, err := p.metrics[key].GetMetricWith(l)
108+
m, err := p.metrics[mname].GetMetricWith(l)
108109
if err != nil {
109110
log.Printf("ERROR Getting metric in Prometheus output, "+
110111
"key: %s, labels: %v,\nerr: %s\n",
111-
key, l, err.Error())
112+
mname, l, err.Error())
112113
continue
113114
}
114115
m.Set(val)

0 commit comments

Comments
 (0)