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

agent/accumulator: expose GetTime to give input plugins a chance to r… #4078

Merged
merged 5 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
agent/accumulator: expose GetTime to give input plugins a chance to r…
…euse a timestamp for several Add* calls + use this on plugins/system to fix telegraf-sample/influxdb-point mismatch pb
  • Loading branch information
zerodeux committed Apr 26, 2018
commit c33a414ad99192db8f0afb2b12b0dd1f4ece51a3
1 change: 1 addition & 0 deletions accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Accumulator interface {
t ...time.Time)

SetPrecision(precision, interval time.Duration)
GetTime(t []time.Time) time.Time

AddError(err error)
}
12 changes: 6 additions & 6 deletions agent/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ac *accumulator) AddFields(
tags map[string]string,
t ...time.Time,
) {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Untyped, ac.getTime(t)); m != nil {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Untyped, ac.GetTime(t)); m != nil {
ac.metrics <- m
}
}
Expand All @@ -60,7 +60,7 @@ func (ac *accumulator) AddGauge(
tags map[string]string,
t ...time.Time,
) {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Gauge, ac.getTime(t)); m != nil {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Gauge, ac.GetTime(t)); m != nil {
ac.metrics <- m
}
}
Expand All @@ -71,7 +71,7 @@ func (ac *accumulator) AddCounter(
tags map[string]string,
t ...time.Time,
) {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Counter, ac.getTime(t)); m != nil {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Counter, ac.GetTime(t)); m != nil {
ac.metrics <- m
}
}
Expand All @@ -82,7 +82,7 @@ func (ac *accumulator) AddSummary(
tags map[string]string,
t ...time.Time,
) {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Summary, ac.getTime(t)); m != nil {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Summary, ac.GetTime(t)); m != nil {
ac.metrics <- m
}
}
Expand All @@ -93,7 +93,7 @@ func (ac *accumulator) AddHistogram(
tags map[string]string,
t ...time.Time,
) {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Histogram, ac.getTime(t)); m != nil {
if m := ac.maker.MakeMetric(measurement, fields, tags, telegraf.Histogram, ac.GetTime(t)); m != nil {
ac.metrics <- m
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (ac *accumulator) SetPrecision(precision, interval time.Duration) {
}
}

func (ac accumulator) getTime(t []time.Time) time.Time {
func (ac *accumulator) GetTime(t []time.Time) time.Time {
var timestamp time.Time
if len(t) > 0 {
timestamp = t[0]
Expand Down
14 changes: 14 additions & 0 deletions agent/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ func TestSetPrecision(t *testing.T) {
}
}

func TestGetTime(t *testing.T) {
metrics := make(chan telegraf.Metric, 10)
defer close(metrics)
a := NewAccumulator(&TestMetricMaker{}, metrics)

t0 := a.GetTime(nil)
assert.InDelta(t, 0, time.Now().Sub(t0), 1e9, "returns current timestamp")

a.SetPrecision(0, time.Nanosecond)
t1 := time.Unix(42,99)
t2 := a.GetTime([]time.Time{t1})
assert.Equal(t, t1, t2, "passthru given timestamp")
}

type TestMetricMaker struct {
}

Expand Down
8 changes: 5 additions & 3 deletions plugins/inputs/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
return err
}

time := acc.GetTime(nil)

fields := map[string]interface{}{
"load1": loadavg.Load1,
"load5": loadavg.Load5,
Expand All @@ -43,7 +45,7 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
return err
}

acc.AddGauge("system", fields, nil)
acc.AddGauge("system", fields, nil, time)

hostinfo, err := host.Info()
if err != nil {
Expand All @@ -52,10 +54,10 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {

acc.AddCounter("system", map[string]interface{}{
"uptime": hostinfo.Uptime,
}, nil)
}, nil, time)
acc.AddFields("system", map[string]interface{}{
"uptime_format": format_uptime(hostinfo.Uptime),
}, nil)
}, nil, time)

return nil
}
Expand Down
4 changes: 4 additions & 0 deletions testutil/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func (a *Accumulator) SetPrecision(precision, interval time.Duration) {
return
}

func (a *Accumulator) GetTime(t []time.Time) time.Time {
return time.Now()
}

func (a *Accumulator) DisablePrecision() {
return
}
Expand Down