Skip to content

Commit

Permalink
Add timezone support to date processor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Jan 14, 2020
1 parent dc5ff68 commit 70be89e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
renamed to `sqlserver_azure_db_resource_stats` due to an issue where numeric
metrics were previously being reported incorrectly as strings.

- The `date` processor now uses the UTC timezone when creating its tag. In
previous versions the local time was used.

#### Features

- [#6730](https://github.com/influxdata/telegraf/pull/6730): Add page_faults for mongodb wired tiger.
Expand Down
16 changes: 16 additions & 0 deletions plugins/processors/date/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ A few example usecases include:

## Offset duration added to the date string when writing the new tag.
# date_offset = "0s"

## Timezone to use when generating the date. This can be set to one of
## "Local", "UTC", or to a location name in the IANA Time Zone database.
## example: timezone = "America/Los_Angeles"
# timezone = "UTC"
```

#### timezone

On Windows, only the `Local` and `UTC` zones are available by default. To use
other timezones, set the `ZONEINFO` environment variable to the location of
[`zoneinfo.zip`][zoneinfo]:
```
set ZONEINFO=C:\zoneinfo.zip
```

### Example
Expand All @@ -30,3 +44,5 @@ A few example usecases include:
- throughput lower=10i,upper=1000i,mean=500i 1560540094000000000
+ throughput,month=Jun lower=10i,upper=1000i,mean=500i 1560540094000000000
```

[zoneinfo]: https://github.com/golang/go/raw/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/lib/time/zoneinfo.zip
25 changes: 23 additions & 2 deletions plugins/processors/date/date.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package date

import (
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/processors"
Expand All @@ -16,12 +18,22 @@ const sampleConfig = `
## 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"
`

const defaultTimezone = "UTC"

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
}

func (d *Date) SampleConfig() string {
Expand All @@ -32,9 +44,16 @@ 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
}

func (d *Date) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, point := range in {
tm := point.Time().UTC().Add(d.DateOffset.Duration)
tm := point.Time().In(d.location).Add(d.DateOffset.Duration)
point.AddTag(d.TagKey, tm.Format(d.DateFormat))
}

Expand All @@ -43,6 +62,8 @@ func (d *Date) Apply(in ...telegraf.Metric) []telegraf.Metric {

func init() {
processors.Add("date", func() telegraf.Processor {
return &Date{}
return &Date{
Timezone: defaultTimezone,
}
})
}

0 comments on commit 70be89e

Please sign in to comment.