Skip to content
Snippets Groups Projects
Commit 2b0cd203 authored by Cameron Sparr's avatar Cameron Sparr
Browse files

Add Copy() function to Metric interface

parent 536dbfb7
No related branches found
No related tags found
No related merge requests found
...@@ -269,7 +269,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er ...@@ -269,7 +269,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er
var dropOriginal bool var dropOriginal bool
if !m.IsAggregate() { if !m.IsAggregate() {
for _, agg := range a.Config.Aggregators { for _, agg := range a.Config.Aggregators {
if ok := agg.Add(copyMetric(m)); ok { if ok := agg.Add(m.Copy()); ok {
dropOriginal = true dropOriginal = true
} }
} }
...@@ -279,7 +279,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er ...@@ -279,7 +279,7 @@ func (a *Agent) flusher(shutdown chan struct{}, metricC chan telegraf.Metric) er
if i == len(a.Config.Outputs)-1 { if i == len(a.Config.Outputs)-1 {
o.AddMetric(m) o.AddMetric(m)
} else { } else {
o.AddMetric(copyMetric(m)) o.AddMetric(m.Copy())
} }
} }
} }
...@@ -385,19 +385,3 @@ func (a *Agent) Run(shutdown chan struct{}) error { ...@@ -385,19 +385,3 @@ func (a *Agent) Run(shutdown chan struct{}) error {
wg.Wait() wg.Wait()
return nil 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
}
...@@ -55,6 +55,9 @@ type Metric interface { ...@@ -55,6 +55,9 @@ type Metric interface {
SetAggregate(bool) SetAggregate(bool)
// IsAggregate returns true if the metric is an aggregate // IsAggregate returns true if the metric is an aggregate
IsAggregate() bool IsAggregate() bool
// Copy copies the metric
Copy() Metric
} }
// metric is a wrapper of the influxdb client.Point struct // metric is a wrapper of the influxdb client.Point struct
...@@ -175,3 +178,19 @@ func (m *metric) IsAggregate() bool { ...@@ -175,3 +178,19 @@ func (m *metric) IsAggregate() bool {
func (m *metric) SetAggregate(b bool) { func (m *metric) SetAggregate(b bool) {
m.isaggregate = b 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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment