import ( "testing" "github.com/influxdb/telegraf/plugins/parsers/influx" "github.com/influxdb/telegraf" "github.com/influxdb/telegraf/testutil" ) func TestAccumulator_AddFields(t *testing.T) { s := &influx.InfluxParser{} acc := testutil.Accumulator{} m1, err := s.ParseLine("cpu,host=server1 usage_user=80") require.NoError(t, err) m2, err := s.ParseLine("cpu,host=server1 usage_system=20") require.NoError(t, err) acc.AddFields("cpu", m1.Fields, m1.Tags) acc.AddFields("cpu", m2.Fields, m2.Tags) metrics := acc.GetTelegrafMetrics() require.Equal(t, len(metrics), 1) require.Equal(t, metrics[0].Measurement, "cpu") require.Equal(t, metrics[0].Tags["host"], "server1") require.Equal(t, metrics[0].Fields["usage_user"], 80.0) require.Equal(t, metrics[0].Fields["usage_system"], 20.0) }
import ( "testing" "github.com/influxdb/telegraf/testutil" ) func TestAccumulator_Reset(t *testing.T) { acc := testutil.Accumulator{} acc.AddFields("cpu", map[string]interface{}{"usage_user": 80}, map[string]string{"host": "server1"}) acc.AddFields("cpu", map[string]interface{}{"usage_idle": 20}, map[string]string{"host": "server2"}) metrics := acc.GetTelegrafMetrics() require.Equal(t, len(metrics), 2) acc.Reset() metrics = acc.GetTelegrafMetrics() require.Equal(t, len(metrics), 0) }This example demonstrates how to reset the accumulator. In this example, we add two metrics (cpu usage for server 1 and 2) and then reset the accumulator. We then use assertions to validate that the accumulator is empty after the reset.