telegraf/plugins/processors/date/date.go

70 lines
1.6 KiB
Go
Raw Normal View History

2019-06-14 19:08:10 +00:00
package date
import (
"time"
2019-06-14 19:08:10 +00:00
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
2019-06-14 19:08:10 +00:00
"github.com/influxdata/telegraf/plugins/processors"
)
const sampleConfig = `
2019-06-14 19:26:47 +00:00
## New tag to create
tag_key = "month"
2019-06-14 19:08:10 +00:00
2019-06-14 19:26:47 +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"
## Offset duration added to the date string when writing the new tag.
# date_offset = "0s"
## Timezone to use when creating the tag. 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
`
const defaultTimezone = "UTC"
2019-06-14 19:08:10 +00:00
type Date struct {
TagKey string `toml:"tag_key"`
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."
}
func (d *Date) Init() error {
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 {
tm := point.Time().In(d.location).Add(d.DateOffset.Duration)
point.AddTag(d.TagKey, tm.Format(d.DateFormat))
2019-06-14 19:08:10 +00:00
}
return in
}
func init() {
processors.Add("date", func() telegraf.Processor {
return &Date{
Timezone: defaultTimezone,
}
2019-06-14 19:08:10 +00:00
})
}