diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index df546c192..50a9224cd 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -73,42 +73,43 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { } } - if _, ok := p.metrics[key]; !ok { - p.metrics[key] = prometheus.NewUntypedVec( - prometheus.UntypedOpts{ - Name: key, - Help: fmt.Sprintf("Telegraf collected point '%s'", key), - }, - labels, - ) - prometheus.MustRegister(p.metrics[key]) - } - l := prometheus.Labels{} for tk, tv := range point.Tags() { l[tk] = tv } - for _, val := range point.Fields() { + for n, val := range point.Fields() { + mname := fmt.Sprintf("%s_%s", key, n) + if _, ok := p.metrics[mname]; !ok { + p.metrics[mname] = prometheus.NewUntypedVec( + prometheus.UntypedOpts{ + Name: mname, + Help: fmt.Sprintf("Telegraf collected point '%s'", mname), + }, + labels, + ) + prometheus.MustRegister(p.metrics[mname]) + } + switch val := val.(type) { default: log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n", - key, val) + mname, val) case int64: - m, err := p.metrics[key].GetMetricWith(l) + m, err := p.metrics[mname].GetMetricWith(l) if err != nil { log.Printf("ERROR Getting metric in Prometheus output, "+ "key: %s, labels: %v,\nerr: %s\n", - key, l, err.Error()) + mname, l, err.Error()) continue } m.Set(float64(val)) case float64: - m, err := p.metrics[key].GetMetricWith(l) + m, err := p.metrics[mname].GetMetricWith(l) if err != nil { log.Printf("ERROR Getting metric in Prometheus output, "+ "key: %s, labels: %v,\nerr: %s\n", - key, l, err.Error()) + mname, l, err.Error()) continue } m.Set(val) diff --git a/plugins/outputs/prometheus_client/prometheus_client_test.go b/plugins/outputs/prometheus_client/prometheus_client_test.go index 16414a8e4..5793e1569 100644 --- a/plugins/outputs/prometheus_client/prometheus_client_test.go +++ b/plugins/outputs/prometheus_client/prometheus_client_test.go @@ -46,8 +46,8 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) { value float64 tags map[string]string }{ - {"test_point_1", 0.0, tags}, - {"test_point_2", 1.0, tags}, + {"test_point_1_value", 0.0, tags}, + {"test_point_2_value", 1.0, tags}, } var acc testutil.Accumulator @@ -78,8 +78,8 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) { name string value float64 }{ - {"test_point_3", 0.0}, - {"test_point_4", 1.0}, + {"test_point_3_value", 0.0}, + {"test_point_4_value", 1.0}, } require.NoError(t, p.Gather(&acc))