Skip to content

Commit

Permalink
Remove error return type from metric.New method (influxdata#9116)
Browse files Browse the repository at this point in the history
* Remove error return type from metric.New method.

* Formatting changes for linter + gofmt

* Additional linter fixes.

* More linter fixes.

* Linter fix.

* address comments
  • Loading branch information
ivorybilled authored Apr 13, 2021
1 parent 411df7d commit 842a788
Show file tree
Hide file tree
Showing 94 changed files with 1,433 additions and 1,937 deletions.
5 changes: 1 addition & 4 deletions agent/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ func (ac *accumulator) addFields(
tp telegraf.ValueType,
t ...time.Time,
) {
m, err := metric.New(measurement, tags, fields, ac.getTime(t), tp)
if err != nil {
return
}
m := metric.New(measurement, tags, fields, ac.getTime(t), tp)
if m := ac.maker.MakeMetric(m); m != nil {
ac.metrics <- m
}
Expand Down
4 changes: 2 additions & 2 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(
fields map[string]interface{},
tm time.Time,
tp ...telegraf.ValueType,
) (telegraf.Metric, error) {
) telegraf.Metric {
var vtype telegraf.ValueType
if len(tp) > 0 {
vtype = tp[0]
Expand Down Expand Up @@ -60,7 +60,7 @@ func New(
}
}

return m, nil
return m
}

// FromMetric returns a deep copy of the metric with any tracking information
Expand Down
27 changes: 10 additions & 17 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestNewMetric(t *testing.T) {
"usage_idle": float64(99),
"usage_busy": float64(1),
}
m, err := New("cpu", tags, fields, now)
require.NoError(t, err)
m := New("cpu", tags, fields, now)

require.Equal(t, "cpu", m.Name())
require.Equal(t, tags, m.Tags())
Expand All @@ -38,10 +37,7 @@ func baseMetric() telegraf.Metric {
}
now := time.Now()

m, err := New("cpu", tags, fields, now)
if err != nil {
panic(err)
}
m := New("cpu", tags, fields, now)
return m
}

Expand Down Expand Up @@ -176,7 +172,7 @@ func TestTagList_Sorted(t *testing.T) {

func TestEquals(t *testing.T) {
now := time.Now()
m1, err := New("cpu",
m1 := New("cpu",
map[string]string{
"host": "localhost",
},
Expand All @@ -185,9 +181,8 @@ func TestEquals(t *testing.T) {
},
now,
)
require.NoError(t, err)

m2, err := New("cpu",
m2 := New("cpu",
map[string]string{
"host": "localhost",
},
Expand All @@ -196,7 +191,6 @@ func TestEquals(t *testing.T) {
},
now,
)
require.NoError(t, err)

lhs := m1.(*metric)
require.Equal(t, lhs, m2)
Expand All @@ -208,7 +202,7 @@ func TestEquals(t *testing.T) {
}

func TestHashID(t *testing.T) {
m, _ := New(
m := New(
"cpu",
map[string]string{
"datacenter": "us-east-1",
Expand Down Expand Up @@ -241,7 +235,7 @@ func TestHashID(t *testing.T) {
}

func TestHashID_Consistency(t *testing.T) {
m, _ := New(
m := New(
"cpu",
map[string]string{
"datacenter": "us-east-1",
Expand All @@ -255,7 +249,7 @@ func TestHashID_Consistency(t *testing.T) {
)
hash := m.HashID()

m2, _ := New(
m2 := New(
"cpu",
map[string]string{
"datacenter": "us-east-1",
Expand All @@ -274,7 +268,7 @@ func TestHashID_Consistency(t *testing.T) {
}

func TestHashID_Delimiting(t *testing.T) {
m1, _ := New(
m1 := New(
"cpu",
map[string]string{
"a": "x",
Expand All @@ -286,7 +280,7 @@ func TestHashID_Delimiting(t *testing.T) {
},
time.Now(),
)
m2, _ := New(
m2 := New(
"cpu",
map[string]string{
"a": "xbycz",
Expand Down Expand Up @@ -328,8 +322,7 @@ func TestValueType(t *testing.T) {
fields := map[string]interface{}{
"value": float64(42),
}
m, err := New("cpu", tags, fields, now, telegraf.Gauge)
assert.NoError(t, err)
m := New("cpu", tags, fields, now, telegraf.Gauge)

assert.Equal(t, telegraf.Gauge, m.Type())
}
16 changes: 6 additions & 10 deletions metric/series_grouper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,14 @@ func (g *SeriesGrouper) Add(
}
sort.Slice(taglist, func(i, j int) bool { return taglist[i].Key < taglist[j].Key })

var err error
id := groupID(g.hashSeed, measurement, taglist, tm)
metric := g.metrics[id]
if metric == nil {
metric, err = New(measurement, tags, map[string]interface{}{field: fieldValue}, tm)
if err != nil {
return err
}
g.metrics[id] = metric
g.ordered = append(g.ordered, metric)
m := g.metrics[id]
if m == nil {
m = New(measurement, tags, map[string]interface{}{field: fieldValue}, tm)
g.metrics[id] = m
g.ordered = append(g.ordered, m)
} else {
metric.AddField(field, fieldValue)
m.AddField(field, fieldValue)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion metric/series_grouper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

var m, _ = New(
var m = New(
"mymetric",
map[string]string{
"host": "host.example.com",
Expand Down
5 changes: 1 addition & 4 deletions metric/tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ func mustMetric(
tm time.Time,
tp ...telegraf.ValueType,
) telegraf.Metric {
m, err := New(name, tags, fields, tm, tp...)
if err != nil {
panic("mustMetric")
}
m := New(name, tags, fields, tm, tp...)
return m
}

Expand Down
5 changes: 1 addition & 4 deletions models/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,14 @@ func Metric() telegraf.Metric {
}

func MetricTime(sec int64) telegraf.Metric {
m, err := metric.New(
m := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(sec, 0),
)
if err != nil {
panic(err)
}
return m
}

Expand Down
21 changes: 7 additions & 14 deletions models/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ func TestFilter_ApplyEmpty(t *testing.T) {
require.NoError(t, f.Compile())
require.False(t, f.IsActive())

m, err := metric.New("m",
m := metric.New("m",
map[string]string{},
map[string]interface{}{"value": int64(1)},
time.Now())
require.NoError(t, err)
require.True(t, f.Select(m))
}

Expand All @@ -37,11 +36,10 @@ func TestFilter_ApplyTagsDontPass(t *testing.T) {
require.NoError(t, f.Compile())
require.True(t, f.IsActive())

m, err := metric.New("m",
m := metric.New("m",
map[string]string{"cpu": "cpu-total"},
map[string]interface{}{"value": int64(1)},
time.Now())
require.NoError(t, err)
require.False(t, f.Select(m))
}

Expand All @@ -53,14 +51,13 @@ func TestFilter_ApplyDeleteFields(t *testing.T) {
require.NoError(t, f.Compile())
require.True(t, f.IsActive())

m, err := metric.New("m",
m := metric.New("m",
map[string]string{},
map[string]interface{}{
"value": int64(1),
"value2": int64(2),
},
time.Now())
require.NoError(t, err)
require.True(t, f.Select(m))
f.Modify(m)
require.Equal(t, map[string]interface{}{"value2": int64(2)}, m.Fields())
Expand All @@ -74,14 +71,13 @@ func TestFilter_ApplyDeleteAllFields(t *testing.T) {
require.NoError(t, f.Compile())
require.True(t, f.IsActive())

m, err := metric.New("m",
m := metric.New("m",
map[string]string{},
map[string]interface{}{
"value": int64(1),
"value2": int64(2),
},
time.Now())
require.NoError(t, err)
require.True(t, f.Select(m))
f.Modify(m)
require.Len(t, m.FieldList(), 0)
Expand Down Expand Up @@ -332,14 +328,13 @@ func TestFilter_TagDrop(t *testing.T) {
}

func TestFilter_FilterTagsNoMatches(t *testing.T) {
m, err := metric.New("m",
m := metric.New("m",
map[string]string{
"host": "localhost",
"mytag": "foobar",
},
map[string]interface{}{"value": int64(1)},
time.Now())
require.NoError(t, err)
f := Filter{
TagExclude: []string{"nomatch"},
}
Expand All @@ -361,14 +356,13 @@ func TestFilter_FilterTagsNoMatches(t *testing.T) {
}

func TestFilter_FilterTagsMatches(t *testing.T) {
m, err := metric.New("m",
m := metric.New("m",
map[string]string{
"host": "localhost",
"mytag": "foobar",
},
map[string]interface{}{"value": int64(1)},
time.Now())
require.NoError(t, err)
f := Filter{
TagExclude: []string{"ho*"},
}
Expand All @@ -379,14 +373,13 @@ func TestFilter_FilterTagsMatches(t *testing.T) {
"mytag": "foobar",
}, m.Tags())

m, err = metric.New("m",
m = metric.New("m",
map[string]string{
"host": "localhost",
"mytag": "foobar",
},
map[string]interface{}{"value": int64(1)},
time.Now())
require.NoError(t, err)
f = Filter{
TagInclude: []string{"my*"},
}
Expand Down
Loading

0 comments on commit 842a788

Please sign in to comment.