Remove Add() function from accumulator

This commit is contained in:
Cameron Sparr 2016-08-31 12:15:14 +01:00
parent 03d8abccdd
commit 6dbbe65897
8 changed files with 110 additions and 73 deletions

View File

@ -6,11 +6,6 @@ type Accumulator interface {
// Create a point with a value, decorating it with tags // Create a point with a value, decorating it with tags
// NOTE: tags is expected to be owned by the caller, don't mutate // NOTE: tags is expected to be owned by the caller, don't mutate
// it after passing to Add. // it after passing to Add.
Add(measurement string,
value interface{},
tags map[string]string,
t ...time.Time)
AddFields(measurement string, AddFields(measurement string,
fields map[string]interface{}, fields map[string]interface{},
tags map[string]string, tags map[string]string,

View File

@ -38,22 +38,6 @@ type accumulator struct {
errCount uint64 errCount uint64
} }
func (ac *accumulator) Add(
measurement string,
value interface{},
tags map[string]string,
t ...time.Time,
) {
fields := make(map[string]interface{})
fields["value"] = value
if !ac.inputConfig.Filter.ShouldNamePass(measurement) {
return
}
ac.AddFields(measurement, fields, tags, t...)
}
func (ac *accumulator) AddFields( func (ac *accumulator) AddFields(
measurement string, measurement string,
fields map[string]interface{}, fields map[string]interface{},

View File

@ -23,9 +23,15 @@ func TestAdd(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -50,9 +56,15 @@ func TestAddNoPrecisionWithInterval(t *testing.T) {
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.SetPrecision(0, time.Second) a.SetPrecision(0, time.Second)
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -77,9 +89,15 @@ func TestAddNoIntervalWithPrecision(t *testing.T) {
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.SetPrecision(time.Second, time.Millisecond) a.SetPrecision(time.Second, time.Millisecond)
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -105,9 +123,15 @@ func TestAddDisablePrecision(t *testing.T) {
a.SetPrecision(time.Second, time.Millisecond) a.SetPrecision(time.Second, time.Millisecond)
a.DisablePrecision() a.DisablePrecision()
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -132,7 +156,9 @@ func TestDifferentPrecisions(t *testing.T) {
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.SetPrecision(0, time.Second) a.SetPrecision(0, time.Second)
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
assert.Equal(t, assert.Equal(t,
@ -140,7 +166,9 @@ func TestDifferentPrecisions(t *testing.T) {
actual) actual)
a.SetPrecision(0, time.Millisecond) a.SetPrecision(0, time.Millisecond)
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm = <-a.metrics testm = <-a.metrics
actual = testm.String() actual = testm.String()
assert.Equal(t, assert.Equal(t,
@ -148,7 +176,9 @@ func TestDifferentPrecisions(t *testing.T) {
actual) actual)
a.SetPrecision(0, time.Microsecond) a.SetPrecision(0, time.Microsecond)
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm = <-a.metrics testm = <-a.metrics
actual = testm.String() actual = testm.String()
assert.Equal(t, assert.Equal(t,
@ -156,7 +186,9 @@ func TestDifferentPrecisions(t *testing.T) {
actual) actual)
a.SetPrecision(0, time.Nanosecond) a.SetPrecision(0, time.Nanosecond)
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm = <-a.metrics testm = <-a.metrics
actual = testm.String() actual = testm.String()
assert.Equal(t, assert.Equal(t,
@ -172,9 +204,15 @@ func TestAddDefaultTags(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -342,9 +380,15 @@ func TestAddInts(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", int(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", int32(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": int(101)},
a.Add("acctest", int64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": int32(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": int64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -369,8 +413,12 @@ func TestAddFloats(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", float32(101), map[string]string{"acc": "test"}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]interface{}{"value": float32(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -391,8 +439,12 @@ func TestAddStrings(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", "test", map[string]string{"acc": "test"}) a.AddFields("acctest",
a.Add("acctest", "foo", map[string]string{"acc": "test"}, now) map[string]interface{}{"value": "test"},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": "foo"},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -413,8 +465,10 @@ func TestAddBools(t *testing.T) {
defer close(a.metrics) defer close(a.metrics)
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.Add("acctest", true, map[string]string{"acc": "test"}) a.AddFields("acctest",
a.Add("acctest", false, map[string]string{"acc": "test"}, now) map[string]interface{}{"value": true}, map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": false}, map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()
@ -440,9 +494,15 @@ func TestAccFilterTags(t *testing.T) {
a.inputConfig = &models.InputConfig{} a.inputConfig = &models.InputConfig{}
a.inputConfig.Filter = filter a.inputConfig.Filter = filter
a.Add("acctest", float64(101), map[string]string{}) a.AddFields("acctest",
a.Add("acctest", float64(101), map[string]string{"acc": "test"}) map[string]interface{}{"value": float64(101)},
a.Add("acctest", float64(101), map[string]string{"acc": "test"}, now) map[string]string{})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"})
a.AddFields("acctest",
map[string]interface{}{"value": float64(101)},
map[string]string{"acc": "test"}, now)
testm := <-a.metrics testm := <-a.metrics
actual := testm.String() actual := testm.String()

View File

@ -1478,19 +1478,23 @@ func (m *Mysql) gatherTableSchema(db *sql.DB, serv string, acc telegraf.Accumula
tags["schema"] = tableSchema tags["schema"] = tableSchema
tags["table"] = tableName tags["table"] = tableName
acc.Add(newNamespace("info_schema", "table_rows"), tableRows, tags) acc.AddFields(newNamespace("info_schema", "table_rows"),
map[string]interface{}{"value": tableRows}, tags)
dlTags := copyTags(tags) dlTags := copyTags(tags)
dlTags["component"] = "data_length" dlTags["component"] = "data_length"
acc.Add(newNamespace("info_schema", "table_size", "data_length"), dataLength, dlTags) acc.AddFields(newNamespace("info_schema", "table_size", "data_length"),
map[string]interface{}{"value": dataLength}, dlTags)
ilTags := copyTags(tags) ilTags := copyTags(tags)
ilTags["component"] = "index_length" ilTags["component"] = "index_length"
acc.Add(newNamespace("info_schema", "table_size", "index_length"), indexLength, ilTags) acc.AddFields(newNamespace("info_schema", "table_size", "index_length"),
map[string]interface{}{"value": indexLength}, ilTags)
dfTags := copyTags(tags) dfTags := copyTags(tags)
dfTags["component"] = "data_free" dfTags["component"] = "data_free"
acc.Add(newNamespace("info_schema", "table_size", "data_free"), dataFree, dfTags) acc.AddFields(newNamespace("info_schema", "table_size", "data_free"),
map[string]interface{}{"value": dataFree}, dfTags)
versionTags := copyTags(tags) versionTags := copyTags(tags)
versionTags["type"] = tableType versionTags["type"] = tableType
@ -1498,7 +1502,8 @@ func (m *Mysql) gatherTableSchema(db *sql.DB, serv string, acc telegraf.Accumula
versionTags["row_format"] = rowFormat versionTags["row_format"] = rowFormat
versionTags["create_options"] = createOptions versionTags["create_options"] = createOptions
acc.Add(newNamespace("info_schema", "table_version"), version, versionTags) acc.AddFields(newNamespace("info_schema", "table_version"),
map[string]interface{}{"value": version}, versionTags)
} }
} }
return nil return nil

View File

@ -166,7 +166,9 @@ func (s *SQLServer) accRow(query Query, acc telegraf.Accumulator, row scanner) e
if query.ResultByRow { if query.ResultByRow {
// add measurement to Accumulator // add measurement to Accumulator
acc.Add(measurement, *columnMap["value"], tags, time.Now()) acc.AddFields(measurement,
map[string]interface{}{"value": *columnMap["value"]},
tags, time.Now())
} else { } else {
// values // values
for header, val := range columnMap { for header, val := range columnMap {

View File

@ -53,7 +53,9 @@ func TestSqlServer_ParseMetrics(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// add value to Accumulator // add value to Accumulator
acc.Add(measurement, value, tags, time.Now()) acc.AddFields(measurement,
map[string]interface{}{"value": value},
tags, time.Now())
// assert // assert
acc.AssertContainsTaggedFields(t, measurement, map[string]interface{}{"value": value}, tags) acc.AssertContainsTaggedFields(t, measurement, map[string]interface{}{"value": value}, tags)

View File

@ -26,7 +26,7 @@ type PS interface {
func add(acc telegraf.Accumulator, func add(acc telegraf.Accumulator,
name string, val float64, tags map[string]string) { name string, val float64, tags map[string]string) {
if val >= 0 { if val >= 0 {
acc.Add(name, val, tags) acc.AddFields(name, map[string]interface{}{"value": val}, tags)
} }
} }

View File

@ -35,17 +35,6 @@ type Accumulator struct {
debug bool debug bool
} }
// Add adds a measurement point to the accumulator
func (a *Accumulator) Add(
measurement string,
value interface{},
tags map[string]string,
t ...time.Time,
) {
fields := map[string]interface{}{"value": value}
a.AddFields(measurement, fields, tags, t...)
}
func (a *Accumulator) NMetrics() uint64 { func (a *Accumulator) NMetrics() uint64 {
return atomic.LoadUint64(&a.nMetrics) return atomic.LoadUint64(&a.nMetrics)
} }