diff --git a/plugins/parsers/graphite/parser.go b/plugins/parsers/graphite/parser.go index d7505133b73e4..468c859f3a0ef 100644 --- a/plugins/parsers/graphite/parser.go +++ b/plugins/parsers/graphite/parser.go @@ -27,6 +27,7 @@ type Parser struct { DefaultTags map[string]string ` toml:"-"` templateEngine *templating.Engine + timeFunc func() time.Time } func (p *Parser) Init() error { @@ -45,9 +46,17 @@ func (p *Parser) Init() error { return fmt.Errorf("creating template engine failed: %w ", err) } + if p.timeFunc == nil { + p.timeFunc = time.Now + } + return nil } +func (p *Parser) SetTimeFunc(fn func() time.Time) { + p.timeFunc = fn +} + func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { // parse even if the buffer begins with a newline if len(buf) != 0 && buf[0] == '\n' { @@ -119,7 +128,7 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) { } // If no 3rd field, use now as timestamp - timestamp := time.Now().UTC() + timestamp := p.timeFunc() if len(fields) == 3 { // Parse timestamp.