Skip to content

Commit

Permalink
Add Copy() function to Metric interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Nov 23, 2016
1 parent 536dbfb commit 2b0cd20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
20 changes: 2 additions & 18 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er
var dropOriginal bool
if !m.IsAggregate() {
for _, agg := range a.Config.Aggregators {
if ok := agg.Add(copyMetric(m)); ok {
if ok := agg.Add(m.Copy()); ok {
dropOriginal = true
}
}
Expand All @@ -279,7 +279,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er
if i == len(a.Config.Outputs)-1 {
o.AddMetric(m)
} else {
o.AddMetric(copyMetric(m))
o.AddMetric(m.Copy())
}
}
}
Expand Down Expand Up @@ -385,19 +385,3 @@ func (a *Agent) Run(shutdown chan struct{}) error {
wg.Wait()
return nil
}

func copyMetric(m telegraf.Metric) telegraf.Metric {
t := time.Time(m.Time())

tags := make(map[string]string)
fields := make(map[string]interface{})
for k, v := range m.Tags() {
tags[k] = v
}
for k, v := range m.Fields() {
fields[k] = v
}

out, _ := telegraf.NewMetric(m.Name(), tags, fields, t)
return out
}
19 changes: 19 additions & 0 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type Metric interface {
SetAggregate(bool)
// IsAggregate returns true if the metric is an aggregate
IsAggregate() bool

// Copy copies the metric
Copy() Metric
}

// metric is a wrapper of the influxdb client.Point struct
Expand Down Expand Up @@ -175,3 +178,19 @@ func (m *metric) IsAggregate() bool {
func (m *metric) SetAggregate(b bool) {
m.isaggregate = b
}

func (m *metric) Copy() Metric {
t := time.Time(m.Time())

tags := make(map[string]string)
fields := make(map[string]interface{})
for k, v := range m.Tags() {
tags[k] = v
}
for k, v := range m.Fields() {
fields[k] = v
}

out, _ := NewMetric(m.Name(), tags, fields, t)
return out
}

0 comments on commit 2b0cd20

Please sign in to comment.