2018-03-28 00:30:51 +00:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TimeFunc func() time.Time
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
TimeFunc
|
2018-04-02 21:32:33 +00:00
|
|
|
TimePrecision time.Duration
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
*metric
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuilder() *Builder {
|
|
|
|
b := &Builder{
|
2018-04-02 21:32:33 +00:00
|
|
|
TimeFunc: time.Now,
|
|
|
|
TimePrecision: 1 * time.Nanosecond,
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
b.Reset()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) SetName(name string) {
|
|
|
|
b.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) AddTag(key string, value string) {
|
|
|
|
b.metric.AddTag(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) AddField(key string, value interface{}) {
|
|
|
|
b.metric.AddField(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) SetTime(tm time.Time) {
|
|
|
|
b.tm = tm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Reset() {
|
2018-08-22 23:28:50 +00:00
|
|
|
b.metric = &metric{
|
|
|
|
tp: telegraf.Untyped,
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Metric() (telegraf.Metric, error) {
|
|
|
|
if b.tm.IsZero() {
|
2018-04-02 21:32:33 +00:00
|
|
|
b.tm = b.TimeFunc().Truncate(b.TimePrecision)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.metric, nil
|
|
|
|
}
|