diff --git a/stats/stats.go b/stats/stats.go index 22f4866db0b4..eaae2bf09674 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -466,13 +466,15 @@ func (m *Metric) Sample(t time.Time, tags *SampleTags, value float64) Sample { } } -func New(name string, typ MetricType, t ...ValueType) *Metric { - vt := Default - if len(t) > 0 { - vt = t[0] +// newMetric instantiates a new Metric +func newMetric(name string, mt MetricType, vt ...ValueType) *Metric { + contains := Default + if len(vt) > 0 { + contains = vt[0] } + var sink Sink - switch typ { + switch mt { case Counter: sink = &CounterSink{} case Gauge: @@ -484,7 +486,8 @@ func New(name string, typ MetricType, t ...ValueType) *Metric { default: return nil } - return &Metric{Name: name, Type: typ, Contains: vt, Sink: sink} + + return &Metric{Name: name, Type: mt, Contains: contains, Sink: sink} } // A Submetric represents a filtered dataset based on a parent metric. @@ -539,7 +542,7 @@ func (m *Metric) AddSubmetric(keyValues string) (*Submetric, error) { Tags: tags, Parent: m, } - subMetricMetric := New(subMetric.Name, m.Type, m.Contains) + subMetricMetric := newMetric(subMetric.Name, m.Type, m.Contains) subMetricMetric.Sub = subMetric // sigh subMetric.Metric = subMetricMetric diff --git a/stats/stats_test.go b/stats/stats_test.go index a206be7707ca..7ec7ae3887c8 100644 --- a/stats/stats_test.go +++ b/stats/stats_test.go @@ -30,7 +30,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestNew(t *testing.T) { +func TestNewMetric(t *testing.T) { t.Parallel() testdata := map[string]struct { Type MetricType @@ -46,7 +46,7 @@ func TestNew(t *testing.T) { name, data := name, data t.Run(name, func(t *testing.T) { t.Parallel() - m := New("my_metric", data.Type) + m := newMetric("my_metric", data.Type) assert.Equal(t, "my_metric", m.Name) assert.IsType(t, data.SinkType, m.Sink) }) @@ -77,7 +77,7 @@ func TestAddSubmetric(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - m := New("metric", Trend) + m := newMetric("metric", Trend) sm, err := m.AddSubmetric(name) if expected.err { require.Error(t, err) @@ -156,7 +156,7 @@ func TestSampleImplementations(t *testing.T) { now := time.Now() sample := Sample{ - Metric: New("test_metric", Counter), + Metric: newMetric("test_metric", Counter), Time: now, Tags: NewSampleTags(tagMap), Value: 1.0,