Skip to content

Commit

Permalink
Remove stats.New
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Mar 17, 2022
1 parent 9020c49 commit a69cc06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 10 additions & 7 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a69cc06

Please sign in to comment.