2019-06-14 19:08:10 +00:00
|
|
|
package date
|
|
|
|
|
|
|
|
import (
|
2020-05-08 00:19:03 +00:00
|
|
|
"errors"
|
2020-01-14 23:16:27 +00:00
|
|
|
"time"
|
|
|
|
|
2019-06-14 19:08:10 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2020-01-14 23:16:27 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2019-06-14 19:08:10 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sampleConfig = `
|
2020-05-08 00:19:03 +00:00
|
|
|
## New tag to create
|
|
|
|
tag_key = "month"
|
2019-06-14 19:08:10 +00:00
|
|
|
|
2020-05-08 00:19:03 +00:00
|
|
|
## New field to create (cannot set both field_key and tag_key)
|
|
|
|
# field_key = "month"
|
2020-01-14 23:16:27 +00:00
|
|
|
|
2020-05-08 00:19:03 +00:00
|
|
|
## Date format string, must be a representation of the Go "reference time"
|
|
|
|
## which is "Mon Jan 2 15:04:05 -0700 MST 2006".
|
|
|
|
date_format = "Jan"
|
2020-01-14 23:16:27 +00:00
|
|
|
|
2020-05-08 00:19:03 +00:00
|
|
|
## If destination is a field, date format can also be one of
|
|
|
|
## "unix", "unix_ms", "unix_us", or "unix_ns", which will insert an integer field.
|
|
|
|
# date_format = "unix"
|
|
|
|
|
|
|
|
## Offset duration added to the date string when writing the new tag.
|
|
|
|
# date_offset = "0s"
|
|
|
|
|
|
|
|
## Timezone to use when creating the tag or field using a reference time
|
|
|
|
## string. This can be set to one of "UTC", "Local", or to a location name
|
|
|
|
## in the IANA Time Zone database.
|
|
|
|
## example: timezone = "America/Los_Angeles"
|
|
|
|
# timezone = "UTC"
|
2019-06-14 19:08:10 +00:00
|
|
|
`
|
|
|
|
|
2020-01-14 23:16:27 +00:00
|
|
|
const defaultTimezone = "UTC"
|
|
|
|
|
2019-06-14 19:08:10 +00:00
|
|
|
type Date struct {
|
2020-01-14 23:16:27 +00:00
|
|
|
TagKey string `toml:"tag_key"`
|
2020-05-08 00:19:03 +00:00
|
|
|
FieldKey string `toml:"field_key"`
|
2020-01-14 23:16:27 +00:00
|
|
|
DateFormat string `toml:"date_format"`
|
|
|
|
DateOffset internal.Duration `toml:"date_offset"`
|
|
|
|
Timezone string `toml:"timezone"`
|
|
|
|
|
|
|
|
location *time.Location
|
2019-06-14 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Date) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Date) Description() string {
|
|
|
|
return "Dates measurements, tags, and fields that pass through this filter."
|
|
|
|
}
|
|
|
|
|
2020-01-14 23:16:27 +00:00
|
|
|
func (d *Date) Init() error {
|
2020-05-08 00:19:03 +00:00
|
|
|
// Check either TagKey or FieldKey specified
|
|
|
|
if len(d.FieldKey) > 0 && len(d.TagKey) > 0 {
|
|
|
|
return errors.New("Only one of field_key or tag_key can be specified")
|
|
|
|
} else if len(d.FieldKey) == 0 && len(d.TagKey) == 0 {
|
|
|
|
return errors.New("One of field_key or tag_key must be specified")
|
|
|
|
}
|
|
|
|
|
2020-01-14 23:16:27 +00:00
|
|
|
var err error
|
|
|
|
// LoadLocation returns UTC if timezone is the empty string.
|
|
|
|
d.location, err = time.LoadLocation(d.Timezone)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:08:10 +00:00
|
|
|
func (d *Date) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
for _, point := range in {
|
2020-01-14 23:16:27 +00:00
|
|
|
tm := point.Time().In(d.location).Add(d.DateOffset.Duration)
|
2020-05-08 00:19:03 +00:00
|
|
|
if len(d.TagKey) > 0 {
|
|
|
|
point.AddTag(d.TagKey, tm.Format(d.DateFormat))
|
|
|
|
} else if len(d.FieldKey) > 0 {
|
|
|
|
switch d.DateFormat {
|
|
|
|
case "unix":
|
|
|
|
point.AddField(d.FieldKey, tm.Unix())
|
|
|
|
case "unix_ms":
|
|
|
|
point.AddField(d.FieldKey, tm.UnixNano()/1000000)
|
|
|
|
case "unix_us":
|
|
|
|
point.AddField(d.FieldKey, tm.UnixNano()/1000)
|
|
|
|
case "unix_ns":
|
|
|
|
point.AddField(d.FieldKey, tm.UnixNano())
|
|
|
|
default:
|
|
|
|
point.AddField(d.FieldKey, tm.Format(d.DateFormat))
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
processors.Add("date", func() telegraf.Processor {
|
2020-01-14 23:16:27 +00:00
|
|
|
return &Date{
|
|
|
|
Timezone: defaultTimezone,
|
|
|
|
}
|
2019-06-14 19:08:10 +00:00
|
|
|
})
|
|
|
|
}
|