Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use labels in prometheus output for string fields #3350

Merged
merged 1 commit into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,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
Expand Down
28 changes: 28 additions & 0 deletions plugins/outputs/prometheus_client/prometheus_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,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()

Expand Down