From b355536b207b7675fbf0a9cf8d7aff654178f0e8 Mon Sep 17 00:00:00 2001 From: Pranay Kanwar Date: Wed, 21 Feb 2018 07:02:18 +0530 Subject: [PATCH] Convert boolean metric values to float in datadog output (#3804) --- plugins/outputs/datadog/datadog.go | 9 +++++++-- plugins/outputs/datadog/datadog_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/plugins/outputs/datadog/datadog.go b/plugins/outputs/datadog/datadog.go index 65b7ab751..92d31e128 100644 --- a/plugins/outputs/datadog/datadog.go +++ b/plugins/outputs/datadog/datadog.go @@ -97,7 +97,7 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error { metricCounter++ } } else { - log.Printf("I! unable to build Metric for %s, skipping\n", m.Name()) + log.Printf("I! unable to build Metric for %s due to error '%v', skipping\n", m.Name(), err) } } @@ -150,7 +150,7 @@ func buildMetrics(m telegraf.Metric) (map[string]Point, error) { } var p Point if err := p.setValue(v); err != nil { - return ms, fmt.Errorf("unable to extract value from Fields, %s", err.Error()) + return ms, fmt.Errorf("unable to extract value from Fields %v error %v", k, err.Error()) } p[0] = float64(m.Time().Unix()) ms[k] = p @@ -189,6 +189,11 @@ func (p *Point) setValue(v interface{}) error { p[1] = float64(d) case float64: p[1] = float64(d) + case bool: + p[1] = float64(0) + if d { + p[1] = float64(1) + } default: return fmt.Errorf("undeterminable type") } diff --git a/plugins/outputs/datadog/datadog_test.go b/plugins/outputs/datadog/datadog_test.go index 2d3095be1..6a97bad5f 100644 --- a/plugins/outputs/datadog/datadog_test.go +++ b/plugins/outputs/datadog/datadog_test.go @@ -152,6 +152,22 @@ func TestBuildPoint(t *testing.T) { }, nil, }, + { + testutil.TestMetric(bool(true), "test7"), + Point{ + float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), + 1.0, + }, + nil, + }, + { + testutil.TestMetric(bool(false), "test8"), + Point{ + float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), + 0.0, + }, + nil, + }, } for _, tt := range tagtests { pt, err := buildMetrics(tt.ptIn)