Use labels in prometheus output for string fields (#3350)

This commit is contained in:
Daniel Nelson 2017-10-18 17:42:30 -07:00 committed by GitHub
parent a6de4577b0
commit 7ef88c4bf9
2 changed files with 37 additions and 0 deletions

View File

@ -242,6 +242,15 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
labels[sanitize(k)] = v
}
// Prometheus doesn't have a string value type, so convert string
// fields to labels.
for fn, fv := range point.Fields() {
switch fv := fv.(type) {
case string:
labels[sanitize(fn)] = fv
}
}
for fn, fv := range point.Fields() {
// Ignore string and bool fields.
var value float64

View File

@ -324,6 +324,34 @@ func TestWrite_Tags(t *testing.T) {
require.True(t, now.Before(sample2.Expiration))
}
func TestWrite_StringFields(t *testing.T) {
now := time.Now()
p1, err := metric.New(
"foo",
make(map[string]string),
map[string]interface{}{"value": 1.0, "status": "good"},
now,
telegraf.Counter)
p2, err := metric.New(
"bar",
make(map[string]string),
map[string]interface{}{"status": "needs numeric field"},
now,
telegraf.Gauge)
var metrics = []telegraf.Metric{p1, p2}
client := NewClient()
err = client.Write(metrics)
require.NoError(t, err)
fam, ok := client.fam["foo"]
require.True(t, ok)
require.Equal(t, 1, fam.LabelSet["status"])
fam, ok = client.fam["bar"]
require.False(t, ok)
}
func TestExpire(t *testing.T) {
client := NewClient()