Skip to content
Snippets Groups Projects
Commit ec4efe5b authored by Daniel Nelson's avatar Daniel Nelson Committed by GitHub
Browse files

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

parent adb1f558
No related branches found
No related tags found
No related merge requests found
...@@ -242,6 +242,15 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { ...@@ -242,6 +242,15 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
labels[sanitize(k)] = v 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() { for fn, fv := range point.Fields() {
// Ignore string and bool fields. // Ignore string and bool fields.
var value float64 var value float64
......
...@@ -324,6 +324,34 @@ func TestWrite_Tags(t *testing.T) { ...@@ -324,6 +324,34 @@ func TestWrite_Tags(t *testing.T) {
require.True(t, now.Before(sample2.Expiration)) 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) { func TestExpire(t *testing.T) {
client := NewClient() client := NewClient()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment