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

Adds Date Processor Plugin #5895

Merged
merged 3 commits into from Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/processors/all/all.go
Expand Up @@ -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"
Expand Down
69 changes: 69 additions & 0 deletions 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"
Anaisdg marked this conversation as resolved.
Show resolved Hide resolved

`

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{}
})
}

/**
Anaisdg marked this conversation as resolved.
Show resolved Hide resolved
*

[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"


*/
57 changes: 57 additions & 0 deletions plugins/processors/date/date_test.go
@@ -0,0 +1,57 @@
package date

import (
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/assert"
)

func newMetric(name string, tags map[string]string, fields map[string]interface{}, metricTime time.Time) telegraf.Metric {
Anaisdg marked this conversation as resolved.
Show resolved Hide resolved
if tags == nil {
tags = map[string]string{}
}
if fields == nil {
fields = map[string]interface{}{}
}
m, _ := metric.New(name, tags, fields, metricTime)
return m
}

func TestDateTag(t *testing.T) {
Anaisdg marked this conversation as resolved.
Show resolved Hide resolved
dateFormatMonth := Date{
TagKey: "month",
DateFormat: "Jan",
}

dateFormatYear := Date{
TagKey: "year",
DateFormat: "2006",
}

currentTime := time.Now()
month := currentTime.Format("Jan")
year := currentTime.Format("2006")

m1 := newMetric("foo", nil, nil, currentTime)
m2 := newMetric("bar", nil, nil, currentTime)
m3 := newMetric("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'")

m4 := newMetric("foo", nil, nil, currentTime)
m5 := newMetric("bar", nil, nil, currentTime)
m6 := newMetric("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'")

m7 := newMetric("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'")
}