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

Updated code for processors.date with offsets #6877

Closed
Crashtein opened this issue Jan 8, 2020 · 1 comment · Fixed by #6886
Closed

Updated code for processors.date with offsets #6877

Crashtein opened this issue Jan 8, 2020 · 1 comment · Fixed by #6886
Labels
feature request Requests for new plugin and for new features to existing plugins
Milestone

Comments

@Crashtein
Copy link

Feature Request

I'm proposing a feature for processors.date. I needed grouping by time(1d,6h) (6 hours offset because of fact that shifts in factory starts at 6:00AM) and by new tag "month" - created by this plugin. All in factory counts between 6:00AM to 6:00AM.

Proposal:

I've made some changes in /plugins/processors/date/date.go. Now there is a new parameter in .conf file called time_shift with working as described in code section toml. Here is complete new code of date plugin:

package date

import (
	"time"
	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/plugins/processors"
)

const sampleConfig = `
  ## New tag to create
  tag_key = "month"

  ## 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"
  ## Time shift in hours. allows to configure tag value changing at diffierent time of day. 
  ## Input as string. Working example: if time_shift = "-6.0h", month will change 6 hours later.
  time_shift = "0.0h"
`

type Date struct {
	TagKey     string `toml:"tag_key"`
	DateFormat string `toml:"date_format"`
	TimeShift  string `toml:"time_shift"`
}

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 {
		timeshiftParsed, _ := time.ParseDuration(d.TimeShift)
		timeref := point.Time().Add(timeshiftParsed)
		point.AddTag(d.TagKey, timeref.Format(d.DateFormat))
	}

	return in
}

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

Current behavior:

In described case, when i group by tag month and time(1d,6h) at the last day of the month I get 2 values: for last month and new one which is a partial values of 6:00-6:00 work-day.

Desired behavior:

With new code adding a tag month is based on time+/-time_shift so I can group by month without having two values at the end of the month, everything works as I wanted.

Use case:

Maybe in other factories it will be useful too. Of course code implements various tag offsets in standard time format duration. In this case you would be able to make new fields with values grouping by work-days/months/days easily grouping by tag added with new offset parameter.

In attachment the new code:

date.go.txt

@danielnelson danielnelson added this to the 1.14.0 milestone Jan 9, 2020
@danielnelson danielnelson added the feature request Requests for new plugin and for new features to existing plugins label Jan 9, 2020
@danielnelson
Copy link
Contributor

Seems reasonable to me, can you review #6886?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Requests for new plugin and for new features to existing plugins
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants