Skip to content

Commit

Permalink
perf(agent): split AddMetric
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsStegman committed Sep 19, 2024
1 parent ffee74c commit b080e56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,10 @@ func (a *Agent) runOutputs(

for metric := range unit.src {
for i, output := range unit.outputs {
if i == len(a.Config.Outputs)-1 {
output.AddMetric(metric)
if i == len(unit.outputs)-1 {
output.AddMetricNoCopy(metric)
} else {
output.AddMetric(metric.Copy())
output.AddMetric(metric)
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion models/running_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,22 @@ func (r *RunningOutput) Close() {
}

// AddMetric adds a metric to the output.
// Takes ownership of metric
// The given metric will be copied if the output selects the metric.
func (r *RunningOutput) AddMetric(metric telegraf.Metric) {
ok, err := r.Config.Filter.Select(metric)
if err != nil {
r.log.Errorf("filtering failed: %v", err)
} else if !ok {
r.MetricsFiltered.Incr(1)
return
}

r.addMetric(metric.Copy())
}

// AddMetricNoCopy adds a metric to the output.
// Takes ownership of metric regardless of whether the output selects it for outputting.
func (r *RunningOutput) AddMetricNoCopy(metric telegraf.Metric) {
ok, err := r.Config.Filter.Select(metric)
if err != nil {
r.log.Errorf("filtering failed: %v", err)
Expand All @@ -217,6 +231,10 @@ func (r *RunningOutput) AddMetric(metric telegraf.Metric) {
return
}

r.addMetric(metric)
}

func (r *RunningOutput) addMetric(metric telegraf.Metric) {
r.Config.Filter.Modify(metric)
if len(metric.FieldList()) == 0 {
r.metricFiltered(metric)
Expand Down

0 comments on commit b080e56

Please sign in to comment.