Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion plugins/parsers/graphite/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Parser struct {
DefaultTags map[string]string ` toml:"-"`

templateEngine *templating.Engine
timeFunc func() time.Time
}

func (p *Parser) Init() error {
Expand All @@ -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' {
Expand Down Expand Up @@ -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.
Expand Down
Loading