From 3e5cfad2b051b454c50e248e544791d1aecd6586 Mon Sep 17 00:00:00 2001 From: Anaisdg <30506042+Anaisdg@users.noreply.github.com> Date: Fri, 14 Jun 2019 14:08:10 -0500 Subject: [PATCH] Add Date Processor Plugin (#5895) --- plugins/processors/all/all.go | 1 + plugins/processors/date/README.md | 31 +++++++++++++ plugins/processors/date/date.go | 69 ++++++++++++++++++++++++++++ plugins/processors/date/date_test.go | 67 +++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 plugins/processors/date/README.md create mode 100644 plugins/processors/date/date.go create mode 100644 plugins/processors/date/date_test.go diff --git a/plugins/processors/all/all.go b/plugins/processors/all/all.go index 41e2707d3..65580a46f 100644 --- a/plugins/processors/all/all.go +++ b/plugins/processors/all/all.go @@ -2,6 +2,7 @@ package all import ( _ "github.com/influxdata/telegraf/plugins/processors/converter" + _ "github.com/influxdata/telegraf/plugins/processors/date" _ "github.com/influxdata/telegraf/plugins/processors/enum" _ "github.com/influxdata/telegraf/plugins/processors/override" _ "github.com/influxdata/telegraf/plugins/processors/parser" diff --git a/plugins/processors/date/README.md b/plugins/processors/date/README.md new file mode 100644 index 000000000..e2bd245e5 --- /dev/null +++ b/plugins/processors/date/README.md @@ -0,0 +1,31 @@ +# Date Processor Plugin + +The `date` processor adds the months and years as tags to your data. + +Provides the ability to group by months or years. + +A few example usecases include: +1) consumption data for utilities on per month basis +2) bandwith capacity per month +3) compare energy production or sales on a yearly or monthly basis + + +### Configuration: + +```toml +[[processors.date]] + ##Specify the date tags to add rename operation. + tagKey = "month" + dateFormat = "Jan" +``` + +### Tags: + +Tags are applied by this processor. + +### Example processing: + +``` +- throughput, hostname=example.com lower=10i,upper=1000i,mean=500i 1502489900000000000 ++ throughput,host=backend.example.com,month=Mar min=10i,max=1000i,mean=500i 1502489900000000000 +``` diff --git a/plugins/processors/date/date.go b/plugins/processors/date/date.go new file mode 100644 index 000000000..844f99cc7 --- /dev/null +++ b/plugins/processors/date/date.go @@ -0,0 +1,69 @@ +package date + +import ( + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/processors" +) + +const sampleConfig = ` +##Specify the date tags to add +tagKey = "month" +dateFormat = "%m" + +` + +type Date struct { + TagKey string `toml:"tagKey"` + DateFormat string `toml:"dateFormat"` +} + +func (d *Date) SampleConfig() string { + return sampleConfig +} + +func (d *Date) Description() string { + return "Dates measurements, tags, and fields that pass through this filter." +} + +func (d *Date) Apply(in ...telegraf.Metric) []telegraf.Metric { + for _, point := range in { + point.AddTag(d.TagKey, point.Time().Format(d.DateFormat)) + + } + + return in + +} + +func init() { + processors.Add("date", func() telegraf.Processor { + return &Date{} + }) +} + +/** + * + +[processors.date] + jdfj + + ##Set Months to True or False + tagKey = "month" + dateFormat = "%m" // January + +[processors.date] + jdfj + + ##Set Months to True or False + tagKey = "day_of_week" + dateFormat = "%d" // Wednesday + + + # [[processors.regex.fields]] + # key = "request" + # pattern = ".*category=(\\w+).*" + # replacement = "${1}" + # result_key = "search_category" + + +*/ diff --git a/plugins/processors/date/date_test.go b/plugins/processors/date/date_test.go new file mode 100644 index 000000000..98d88b351 --- /dev/null +++ b/plugins/processors/date/date_test.go @@ -0,0 +1,67 @@ +package date + +import ( + "testing" + "time" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/metric" + "github.com/stretchr/testify/assert" +) + +func MustMetric(name string, tags map[string]string, fields map[string]interface{}, metricTime time.Time) telegraf.Metric { + if tags == nil { + tags = map[string]string{} + } + if fields == nil { + fields = map[string]interface{}{} + } + m, _ := metric.New(name, tags, fields, metricTime) + return m +} + +func TestMonthTag(t *testing.T) { + dateFormatMonth := Date{ + TagKey: "month", + DateFormat: "Jan", + } + + currentTime := time.Now() + month := currentTime.Format("Jan") + + m1 := MustMetric("foo", nil, nil, currentTime) + m2 := MustMetric("bar", nil, nil, currentTime) + m3 := MustMetric("baz", nil, nil, currentTime) + monthApply := dateFormatMonth.Apply(m1, m2, m3) + assert.Equal(t, map[string]string{"month": month}, monthApply[0].Tags(), "should add tag 'month'") + assert.Equal(t, map[string]string{"month": month}, monthApply[1].Tags(), "should add tag 'month'") + assert.Equal(t, map[string]string{"month": month}, monthApply[2].Tags(), "should add tag 'month'") +} + +func TestYearTag(t *testing.T) { + dateFormatYear := Date{ + TagKey: "year", + DateFormat: "2006", + } + currentTime := time.Now() + year := currentTime.Format("2006") + + m4 := MustMetric("foo", nil, nil, currentTime) + m5 := MustMetric("bar", nil, nil, currentTime) + m6 := MustMetric("baz", nil, nil, currentTime) + yearApply := dateFormatYear.Apply(m4, m5, m6) + assert.Equal(t, map[string]string{"year": year}, yearApply[0].Tags(), "should add tag 'year'") + assert.Equal(t, map[string]string{"year": year}, yearApply[1].Tags(), "should add tag 'year'") + assert.Equal(t, map[string]string{"year": year}, yearApply[2].Tags(), "should add tag 'year'") +} + +func TestOldDateTag(t *testing.T) { + dateFormatYear := Date{ + TagKey: "year", + DateFormat: "2006", + } + + m7 := MustMetric("foo", nil, nil, time.Date(1993, 05, 27, 0, 0, 0, 0, time.UTC)) + customDateApply := dateFormatYear.Apply(m7) + assert.Equal(t, map[string]string{"year": "1993"}, customDateApply[0].Tags(), "should add tag 'year'") +}