From 647337fa981e6546a9f5cf10de0a64fe6dcf8344 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Mon, 19 Nov 2018 23:04:53 +0000 Subject: [PATCH 1/5] Add metric description to factory API Signed-off-by: Gary Brown --- metrics/adapters/cache_test.go | 6 +-- metrics/adapters/factory.go | 18 ++++---- metrics/adapters/factory_test.go | 24 +++++----- metrics/adapters/tagless.go | 18 ++++---- metrics/expvar/factory.go | 6 +-- metrics/expvar/factory_test.go | 12 ++--- metrics/factory.go | 18 +++++--- metrics/go-kit/factory.go | 6 +-- metrics/go-kit/factory_test.go | 6 +-- metrics/go-kit/influx/factory_test.go | 8 ++-- metrics/metrics.go | 7 +-- metrics/metrics_test.go | 8 ++-- metrics/metricstest/local.go | 6 +-- metrics/metricstest/local_test.go | 22 ++++----- metrics/multi/multi.go | 12 ++--- metrics/multi/multi_test.go | 6 +-- metrics/prometheus/factory.go | 24 +++++++--- metrics/prometheus/factory_test.go | 64 ++++++++++++++++++++++----- metrics/tally/factory.go | 6 +-- metrics/tally/factory_test.go | 6 +-- 20 files changed, 172 insertions(+), 111 deletions(-) diff --git a/metrics/adapters/cache_test.go b/metrics/adapters/cache_test.go index 1053d18..7ee83ff 100644 --- a/metrics/adapters/cache_test.go +++ b/metrics/adapters/cache_test.go @@ -26,9 +26,9 @@ import ( func TestCache(t *testing.T) { f := metricstest.NewFactory(100 * time.Second) - c1 := f.Counter("x", nil) - g1 := f.Gauge("y", nil) - t1 := f.Timer("z", nil) + c1 := f.Counter("x", nil, "") + g1 := f.Gauge("y", nil, "") + t1 := f.Timer("z", nil, "") c := newCache() diff --git a/metrics/adapters/factory.go b/metrics/adapters/factory.go index bc2e137..f5459a9 100644 --- a/metrics/adapters/factory.go +++ b/metrics/adapters/factory.go @@ -20,9 +20,9 @@ import ( // FactoryWithTags creates metrics with fully qualified name and tags. type FactoryWithTags interface { - Counter(name string, tags map[string]string) metrics.Counter - Gauge(name string, tags map[string]string) metrics.Gauge - Timer(name string, tags map[string]string) metrics.Timer + Counter(name string, tags map[string]string, description string) metrics.Counter + Gauge(name string, tags map[string]string, description string) metrics.Gauge + Timer(name string, tags map[string]string, description string) metrics.Timer } // Options affect how the adapter factory behaves. @@ -63,24 +63,24 @@ type factory struct { cache *cache } -func (f *factory) Counter(name string, tags map[string]string) metrics.Counter { +func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { fullName, fullTags, key := f.getKey(name, tags) return f.cache.getOrSetCounter(key, func() metrics.Counter { - return f.factory.Counter(fullName, fullTags) + return f.factory.Counter(fullName, fullTags, description) }) } -func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { fullName, fullTags, key := f.getKey(name, tags) return f.cache.getOrSetGauge(key, func() metrics.Gauge { - return f.factory.Gauge(fullName, fullTags) + return f.factory.Gauge(fullName, fullTags, description) }) } -func (f *factory) Timer(name string, tags map[string]string) metrics.Timer { +func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { fullName, fullTags, key := f.getKey(name, tags) return f.cache.getOrSetTimer(key, func() metrics.Timer { - return f.factory.Timer(fullName, fullTags) + return f.factory.Timer(fullName, fullTags, description) }) } diff --git a/metrics/adapters/factory_test.go b/metrics/adapters/factory_test.go index c133248..8eeff9a 100644 --- a/metrics/adapters/factory_test.go +++ b/metrics/adapters/factory_test.go @@ -82,13 +82,13 @@ func TestFactory(t *testing.T) { if testCase.namespace != "" || testCase.nsTags != nil { f = f.Namespace(testCase.namespace, testCase.nsTags) } - counter := f.Counter(counterPrefix+testCase.name, testCase.tags) - gauge := f.Gauge(gaugePrefix+testCase.name, testCase.tags) - timer := f.Timer(timerPrefix+testCase.name, testCase.tags) + counter := f.Counter(counterPrefix+testCase.name, testCase.tags, "") + gauge := f.Gauge(gaugePrefix+testCase.name, testCase.tags, "") + timer := f.Timer(timerPrefix+testCase.name, testCase.tags, "") - assert.Equal(t, counter, f.Counter(counterPrefix+testCase.name, testCase.tags)) - assert.Equal(t, gauge, f.Gauge(gaugePrefix+testCase.name, testCase.tags)) - assert.Equal(t, timer, f.Timer(timerPrefix+testCase.name, testCase.tags)) + assert.Equal(t, counter, f.Counter(counterPrefix+testCase.name, testCase.tags, "")) + assert.Equal(t, gauge, f.Gauge(gaugePrefix+testCase.name, testCase.tags, "")) + assert.Equal(t, timer, f.Timer(timerPrefix+testCase.name, testCase.tags, "")) assert.Equal(t, fmt.Sprintf(testCase.fullName, counterPrefix), ff.counter) assert.Equal(t, fmt.Sprintf(testCase.fullName, gaugePrefix), ff.gauge) @@ -104,17 +104,17 @@ type fakeTagless struct { timer string } -func (f *fakeTagless) Counter(name string) metrics.Counter { +func (f *fakeTagless) Counter(name string, description string) metrics.Counter { f.counter = name - return f.factory.Counter(name, nil) + return f.factory.Counter(name, nil, description) } -func (f *fakeTagless) Gauge(name string) metrics.Gauge { +func (f *fakeTagless) Gauge(name string, description string) metrics.Gauge { f.gauge = name - return f.factory.Gauge(name, nil) + return f.factory.Gauge(name, nil, description) } -func (f *fakeTagless) Timer(name string) metrics.Timer { +func (f *fakeTagless) Timer(name string, description string) metrics.Timer { f.timer = name - return f.factory.Timer(name, nil) + return f.factory.Timer(name, nil, description) } diff --git a/metrics/adapters/tagless.go b/metrics/adapters/tagless.go index a393b0a..feae5a7 100644 --- a/metrics/adapters/tagless.go +++ b/metrics/adapters/tagless.go @@ -19,9 +19,9 @@ import "github.com/uber/jaeger-lib/metrics" // FactoryWithoutTags creates metrics based on name only, without tags. // Suitable for integrating with statsd-like backends that don't support tags. type FactoryWithoutTags interface { - Counter(name string) metrics.Counter - Gauge(name string) metrics.Gauge - Timer(name string) metrics.Timer + Counter(name string, description string) metrics.Counter + Gauge(name string, description string) metrics.Gauge + Timer(name string, description string) metrics.Timer } // WrapFactoryWithoutTags creates a real metrics.Factory that supports subscopes. @@ -41,19 +41,19 @@ type tagless struct { factory FactoryWithoutTags } -func (f *tagless) Counter(name string, tags map[string]string) metrics.Counter { +func (f *tagless) Counter(name string, tags map[string]string, description string) metrics.Counter { fullName := f.getFullName(name, tags) - return f.factory.Counter(fullName) + return f.factory.Counter(fullName, description) } -func (f *tagless) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *tagless) Gauge(name string, tags map[string]string, description string) metrics.Gauge { fullName := f.getFullName(name, tags) - return f.factory.Gauge(fullName) + return f.factory.Gauge(fullName, description) } -func (f *tagless) Timer(name string, tags map[string]string) metrics.Timer { +func (f *tagless) Timer(name string, tags map[string]string, description string) metrics.Timer { fullName := f.getFullName(name, tags) - return f.factory.Timer(fullName) + return f.factory.Timer(fullName, description) } func (f *tagless) getFullName(name string, tags map[string]string) string { diff --git a/metrics/expvar/factory.go b/metrics/expvar/factory.go index 114f092..6dce399 100644 --- a/metrics/expvar/factory.go +++ b/metrics/expvar/factory.go @@ -36,14 +36,14 @@ type factory struct { factory xkit.Factory } -func (f *factory) Counter(name string) metrics.Counter { +func (f *factory) Counter(name string, description string) metrics.Counter { return xkit.NewCounter(f.factory.Counter(name)) } -func (f *factory) Gauge(name string) metrics.Gauge { +func (f *factory) Gauge(name string, description string) metrics.Gauge { return xkit.NewGauge(f.factory.Gauge(name)) } -func (f *factory) Timer(name string) metrics.Timer { +func (f *factory) Timer(name string, description string) metrics.Timer { return xkit.NewTimer(f.factory.Histogram(name)) } diff --git a/metrics/expvar/factory_test.go b/metrics/expvar/factory_test.go index a13f8f0..2d197f3 100644 --- a/metrics/expvar/factory_test.go +++ b/metrics/expvar/factory_test.go @@ -65,14 +65,14 @@ func TestFactory(t *testing.T) { if testCase.namespace != "" || testCase.nsTags != nil { ff = f.Namespace(testCase.namespace, testCase.nsTags) } - counter := ff.Counter(counterPrefix+testCase.name, testCase.tags) - gauge := ff.Gauge(gaugePrefix+testCase.name, testCase.tags) - timer := ff.Timer(timerPrefix+testCase.name, testCase.tags) + counter := ff.Counter(counterPrefix+testCase.name, testCase.tags, "") + gauge := ff.Gauge(gaugePrefix+testCase.name, testCase.tags, "") + timer := ff.Timer(timerPrefix+testCase.name, testCase.tags, "") // register second time, should not panic - ff.Counter(counterPrefix+testCase.name, testCase.tags) - ff.Gauge(gaugePrefix+testCase.name, testCase.tags) - ff.Timer(timerPrefix+testCase.name, testCase.tags) + ff.Counter(counterPrefix+testCase.name, testCase.tags, "") + ff.Gauge(gaugePrefix+testCase.name, testCase.tags, "") + ff.Timer(timerPrefix+testCase.name, testCase.tags, "") counter.Inc(42) gauge.Update(42) diff --git a/metrics/factory.go b/metrics/factory.go index a744a89..c2a7986 100644 --- a/metrics/factory.go +++ b/metrics/factory.go @@ -16,9 +16,9 @@ package metrics // Factory creates new metrics type Factory interface { - Counter(name string, tags map[string]string) Counter - Timer(name string, tags map[string]string) Timer - Gauge(name string, tags map[string]string) Gauge + Counter(name string, tags map[string]string, description string) Counter + Timer(name string, tags map[string]string, description string) Timer + Gauge(name string, tags map[string]string, description string) Gauge // Namespace returns a nested metrics factory. Namespace(name string, tags map[string]string) Factory @@ -29,7 +29,13 @@ var NullFactory Factory = nullFactory{} type nullFactory struct{} -func (nullFactory) Counter(name string, tags map[string]string) Counter { return NullCounter } -func (nullFactory) Timer(name string, tags map[string]string) Timer { return NullTimer } -func (nullFactory) Gauge(name string, tags map[string]string) Gauge { return NullGauge } +func (nullFactory) Counter(name string, tags map[string]string, description string) Counter { + return NullCounter +} +func (nullFactory) Timer(name string, tags map[string]string, description string) Timer { + return NullTimer +} +func (nullFactory) Gauge(name string, tags map[string]string, description string) Gauge { + return NullGauge +} func (nullFactory) Namespace(name string, tags map[string]string) Factory { return NullFactory } diff --git a/metrics/go-kit/factory.go b/metrics/go-kit/factory.go index d19c23c..dc999d7 100644 --- a/metrics/go-kit/factory.go +++ b/metrics/go-kit/factory.go @@ -103,7 +103,7 @@ func (f *factory) nameAndTagsList(nom string, tags map[string]string) (name stri return } -func (f *factory) Counter(name string, tags map[string]string) metrics.Counter { +func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { name, tagsList := f.nameAndTagsList(name, tags) counter := f.factory.Counter(name) if len(tagsList) > 0 { @@ -112,7 +112,7 @@ func (f *factory) Counter(name string, tags map[string]string) metrics.Counter { return NewCounter(counter) } -func (f *factory) Timer(name string, tags map[string]string) metrics.Timer { +func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { name, tagsList := f.nameAndTagsList(name, tags) hist := f.factory.Histogram(name) if len(tagsList) > 0 { @@ -121,7 +121,7 @@ func (f *factory) Timer(name string, tags map[string]string) metrics.Timer { return NewTimer(hist) } -func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { name, tagsList := f.nameAndTagsList(name, tags) gauge := f.factory.Gauge(name) if len(tagsList) > 0 { diff --git a/metrics/go-kit/factory_test.go b/metrics/go-kit/factory_test.go index d5e7d0e..de1a7f7 100644 --- a/metrics/go-kit/factory_test.go +++ b/metrics/go-kit/factory_test.go @@ -166,7 +166,7 @@ func TestFactoryScoping(t *testing.T) { } func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - c := f.Counter(testCase.name, testCase.tags) + c := f.Counter(testCase.name, testCase.tags, "") c.Inc(123) kc := c.(*Counter).counter var gc *generic.Counter @@ -182,7 +182,7 @@ func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func( } func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - g := f.Gauge(testCase.name, testCase.tags) + g := f.Gauge(testCase.name, testCase.tags, "") g.Update(123) gg := g.(*Gauge).gauge.(*generic.Gauge) assert.EqualValues(t, 123.0, gg.Value()) @@ -192,7 +192,7 @@ func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() } func testTimer(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - tm := f.Timer(testCase.name, testCase.tags) + tm := f.Timer(testCase.name, testCase.tags, "") tm.Record(123 * time.Millisecond) gt := tm.(*Timer).hist.(*generic.Histogram) assert.InDelta(t, 0.123, gt.Quantile(0.9), 0.00001) diff --git a/metrics/go-kit/influx/factory_test.go b/metrics/go-kit/influx/factory_test.go index f4dc71c..27c5c17 100644 --- a/metrics/go-kit/influx/factory_test.go +++ b/metrics/go-kit/influx/factory_test.go @@ -19,7 +19,7 @@ func TestCounter(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - c := wf.Counter("gokit.infl-counter", map[string]string{"label": "val1"}) + c := wf.Counter("gokit.infl-counter", map[string]string{"label": "val1"}, "") c.Inc(7) assert.Contains(t, reportToString(in), "namespace.gokit.infl-counter,label=val1 count=7") @@ -30,7 +30,7 @@ func TestGauge(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - g := wf.Gauge("gokit.infl-gauge", map[string]string{"x": "y"}) + g := wf.Gauge("gokit.infl-gauge", map[string]string{"x": "y"}, "") g.Update(42) assert.Contains(t, reportToString(in), "namespace.gokit.infl-gauge,x=y value=42") @@ -41,7 +41,7 @@ func TestTimer(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - timer := wf.Timer("gokit.infl-timer", map[string]string{"x": "y"}) + timer := wf.Timer("gokit.infl-timer", map[string]string{"x": "y"}, "") timer.Record(time.Second * 1) timer.Record(time.Second * 1) timer.Record(time.Second * 10) @@ -56,7 +56,7 @@ func TestWrapperNamespaces(t *testing.T) { wf = wf.Namespace("bar", map[string]string{"bar_tag": "bar_tag"}) - c := wf.Counter("gokit.prom-wrapped-counter", map[string]string{"x": "y"}) + c := wf.Counter("gokit.prom-wrapped-counter", map[string]string{"x": "y"}, "") c.Inc(42) assert.Contains(t, reportToString(in), "namespace.bar.gokit.prom-wrapped-counter,bar_tag=bar_tag,x=y count=42") diff --git a/metrics/metrics.go b/metrics/metrics.go index 4c36b75..9e86d26 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -72,13 +72,14 @@ func InitOrError(m interface{}, factory Factory, globalTags map[string]string) e tags[tag[0]] = tag[1] } } + description := field.Tag.Get("description") var obj interface{} if field.Type.AssignableTo(counterPtrType) { - obj = factory.Counter(metric, tags) + obj = factory.Counter(metric, tags, description) } else if field.Type.AssignableTo(gaugePtrType) { - obj = factory.Gauge(metric, tags) + obj = factory.Gauge(metric, tags, description) } else if field.Type.AssignableTo(timerPtrType) { - obj = factory.Timer(metric, tags) + obj = factory.Timer(metric, tags, description) } else { return fmt.Errorf( "Field %s is not a pointer to timer, gauge, or counter", diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 3316a31..dd306d8 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -86,8 +86,8 @@ func TestInitPanic(t *testing.T) { func TestNullMetrics(t *testing.T) { // This test is just for cover - metrics.NullFactory.Timer("name", nil).Record(0) - metrics.NullFactory.Counter("name", nil).Inc(0) - metrics.NullFactory.Gauge("name", nil).Update(0) - metrics.NullFactory.Namespace("name", nil).Gauge("name2", nil).Update(0) + metrics.NullFactory.Timer("name", nil, "").Record(0) + metrics.NullFactory.Counter("name", nil, "").Inc(0) + metrics.NullFactory.Gauge("name", nil, "").Update(0) + metrics.NullFactory.Namespace("name", nil).Gauge("name2", nil, "").Update(0) } diff --git a/metrics/metricstest/local.go b/metrics/metricstest/local.go index e727b5f..ed7f2d0 100644 --- a/metrics/metricstest/local.go +++ b/metrics/metricstest/local.go @@ -279,7 +279,7 @@ func (l *Factory) newNamespace(name string) string { } // Counter returns a local stats counter -func (l *Factory) Counter(name string, tags map[string]string) metrics.Counter { +func (l *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { return &localCounter{ stats{ name: l.newNamespace(name), @@ -290,7 +290,7 @@ func (l *Factory) Counter(name string, tags map[string]string) metrics.Counter { } // Timer returns a local stats timer. -func (l *Factory) Timer(name string, tags map[string]string) metrics.Timer { +func (l *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { return &localTimer{ stats{ name: l.newNamespace(name), @@ -301,7 +301,7 @@ func (l *Factory) Timer(name string, tags map[string]string) metrics.Timer { } // Gauge returns a local stats gauge. -func (l *Factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (l *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { return &localGauge{ stats{ name: l.newNamespace(name), diff --git a/metrics/metricstest/local_test.go b/metrics/metricstest/local_test.go index ae21589..bbbc64d 100644 --- a/metrics/metricstest/local_test.go +++ b/metrics/metricstest/local_test.go @@ -15,15 +15,15 @@ func TestLocalMetrics(t *testing.T) { f := NewFactory(0) defer f.Stop() - f.Counter("my-counter", tags).Inc(4) - f.Counter("my-counter", tags).Inc(6) - f.Counter("my-counter", nil).Inc(6) - f.Counter("other-counter", nil).Inc(8) - f.Gauge("my-gauge", nil).Update(25) - f.Gauge("my-gauge", nil).Update(43) - f.Gauge("other-gauge", nil).Update(74) - f.Namespace("namespace", tags).Counter("my-counter", nil).Inc(7) - f.Namespace("ns.subns", nil).Counter("", map[string]string{"service": "a-service"}).Inc(9) + f.Counter("my-counter", tags, "").Inc(4) + f.Counter("my-counter", tags, "").Inc(6) + f.Counter("my-counter", nil, "").Inc(6) + f.Counter("other-counter", nil, "").Inc(8) + f.Gauge("my-gauge", nil, "").Update(25) + f.Gauge("my-gauge", nil, "").Update(43) + f.Gauge("other-gauge", nil, "").Update(74) + f.Namespace("namespace", tags).Counter("my-counter", nil, "").Inc(7) + f.Namespace("ns.subns", nil).Counter("", map[string]string{"service": "a-service"}, "").Inc(9) timings := map[string][]time.Duration{ "foo-latency": { @@ -42,7 +42,7 @@ func TestLocalMetrics(t *testing.T) { for metric, timing := range timings { for _, d := range timing { - f.Timer(metric, nil).Record(d) + f.Timer(metric, nil, "").Record(d) } } @@ -90,7 +90,7 @@ func TestLocalMetricsInterval(t *testing.T) { f := NewFactory(refreshInterval) defer f.Stop() - f.Timer("timer", nil).Record(1) + f.Timer("timer", nil, "").Record(1) f.tm.Lock() timer := f.timers["timer"] diff --git a/metrics/multi/multi.go b/metrics/multi/multi.go index c3791b6..90fc184 100644 --- a/metrics/multi/multi.go +++ b/metrics/multi/multi.go @@ -43,12 +43,12 @@ func (c *counter) Inc(delta int64) { } // Counter implements metrics.Factory interface -func (f *Factory) Counter(name string, tags map[string]string) metrics.Counter { +func (f *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { counter := &counter{ counters: make([]metrics.Counter, len(f.factories)), } for i, factory := range f.factories { - counter.counters[i] = factory.Counter(name, tags) + counter.counters[i] = factory.Counter(name, tags, description) } return counter } @@ -64,12 +64,12 @@ func (t *timer) Record(delta time.Duration) { } // Timer implements metrics.Factory interface -func (f *Factory) Timer(name string, tags map[string]string) metrics.Timer { +func (f *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { timer := &timer{ timers: make([]metrics.Timer, len(f.factories)), } for i, factory := range f.factories { - timer.timers[i] = factory.Timer(name, tags) + timer.timers[i] = factory.Timer(name, tags, description) } return timer } @@ -85,12 +85,12 @@ func (t *gauge) Update(value int64) { } // Gauge implements metrics.Factory interface -func (f *Factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { gauge := &gauge{ gauges: make([]metrics.Gauge, len(f.factories)), } for i, factory := range f.factories { - gauge.gauges[i] = factory.Gauge(name, tags) + gauge.gauges[i] = factory.Gauge(name, tags, description) } return gauge } diff --git a/metrics/multi/multi_test.go b/metrics/multi/multi_test.go index 1ad0a03..031e92b 100644 --- a/metrics/multi/multi_test.go +++ b/metrics/multi/multi_test.go @@ -17,9 +17,9 @@ func TestMultiFactory(t *testing.T) { multi1 := New(f1, f2) multi2 := multi1.Namespace("ns2", nil) tags := map[string]string{"x": "y"} - multi2.Counter("counter", tags).Inc(42) - multi2.Gauge("gauge", tags).Update(42) - multi2.Timer("timer", tags).Record(42 * time.Millisecond) + multi2.Counter("counter", tags, "").Inc(42) + multi2.Gauge("gauge", tags, "").Update(42) + multi2.Timer("timer", tags, "").Record(42 * time.Millisecond) for _, f := range []*metricstest.Factory{f1, f2} { f.AssertCounterMetrics(t, diff --git a/metrics/prometheus/factory.go b/metrics/prometheus/factory.go index 1050cb1..8c90371 100644 --- a/metrics/prometheus/factory.go +++ b/metrics/prometheus/factory.go @@ -125,13 +125,17 @@ func newFactory(parent *Factory, scope string, tags map[string]string) *Factory } // Counter implements Counter of metrics.Factory. -func (f *Factory) Counter(name string, tags map[string]string) metrics.Counter { +func (f *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { + description = strings.TrimSpace(description) + if len(description) == 0 { + description = name + } name = counterNamingConvention(f.subScope(name)) tags = f.mergeTags(tags) labelNames := f.tagNames(tags) opts := prometheus.CounterOpts{ Name: name, - Help: name, + Help: description, } cv := f.cache.getOrMakeCounterVec(opts, labelNames) return &counter{ @@ -140,13 +144,17 @@ func (f *Factory) Counter(name string, tags map[string]string) metrics.Counter { } // Gauge implements Gauge of metrics.Factory. -func (f *Factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { + description = strings.TrimSpace(description) + if len(description) == 0 { + description = name + } name = f.subScope(name) tags = f.mergeTags(tags) labelNames := f.tagNames(tags) opts := prometheus.GaugeOpts{ Name: name, - Help: name, + Help: description, } gv := f.cache.getOrMakeGaugeVec(opts, labelNames) return &gauge{ @@ -155,13 +163,17 @@ func (f *Factory) Gauge(name string, tags map[string]string) metrics.Gauge { } // Timer implements Timer of metrics.Factory. -func (f *Factory) Timer(name string, tags map[string]string) metrics.Timer { +func (f *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { + description = strings.TrimSpace(description) + if len(description) == 0 { + description = name + } name = f.subScope(name) tags = f.mergeTags(tags) labelNames := f.tagNames(tags) opts := prometheus.HistogramOpts{ Name: name, - Help: name, + Help: description, Buckets: f.buckets, } hv := f.cache.getOrMakeHistogramVec(opts, labelNames) diff --git a/metrics/prometheus/factory_test.go b/metrics/prometheus/factory_test.go index 9c42da0..c17eb39 100644 --- a/metrics/prometheus/factory_test.go +++ b/metrics/prometheus/factory_test.go @@ -37,7 +37,7 @@ func TestOptions(t *testing.T) { func TestSeparator(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithSeparator(SeparatorColon)) - c1 := f1.Namespace("bender", nil).Counter("rodriguez", map[string]string{"a": "b"}) + c1 := f1.Namespace("bender", nil).Counter("rodriguez", map[string]string{"a": "b"}, "Help message") c1.Inc(1) snapshot, err := registry.Gather() require.NoError(t, err) @@ -52,9 +52,9 @@ func TestCounter(t *testing.T) { f2 := fDummy.Namespace("bender", map[string]string{"a": "b"}) f3 := f2.Namespace("", nil) - c1 := f2.Counter("rodriguez", map[string]string{"x": "y"}) - c2 := f2.Counter("rodriguez", map[string]string{"x": "z"}) - c3 := f3.Counter("rodriguez", map[string]string{"x": "z"}) // same tags as c2, but from f3 + c1 := f2.Counter("rodriguez", map[string]string{"x": "y"}, "Help message") + c2 := f2.Counter("rodriguez", map[string]string{"x": "z"}, "Help message") + c3 := f3.Counter("rodriguez", map[string]string{"x": "z"}, "Help message") // same tags as c2, but from f3 c1.Inc(1) c1.Inc(2) c2.Inc(3) @@ -63,6 +63,8 @@ func TestCounter(t *testing.T) { snapshot, err := registry.Gather() require.NoError(t, err) + assert.EqualValues(t, "Help message", snapshot[0].GetHelp()) + m1 := findMetric(t, snapshot, "bender_rodriguez_total", map[string]string{"a": "b", "x": "y"}) assert.EqualValues(t, 3, m1.GetCounter().GetValue(), "%+v", m1) @@ -70,14 +72,26 @@ func TestCounter(t *testing.T) { assert.EqualValues(t, 7, m2.GetCounter().GetValue(), "%+v", m2) } +func TestCounterDefaultHelp(t *testing.T) { + registry := prometheus.NewPedanticRegistry() + f1 := New(WithRegisterer(registry)) + c1 := f1.Counter("rodriguez", map[string]string{"x": "y"}, "") + c1.Inc(1) + + snapshot, err := registry.Gather() + require.NoError(t, err) + + assert.EqualValues(t, "rodriguez", snapshot[0].GetHelp()) +} + func TestGauge(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) f2 := f1.Namespace("bender", map[string]string{"a": "b"}) f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2 - g1 := f2.Gauge("rodriguez", map[string]string{"x": "y"}) - g2 := f2.Gauge("rodriguez", map[string]string{"x": "z"}) - g3 := f3.Gauge("rodriguez", map[string]string{"x": "z"}) // same as g2, but from f3 + g1 := f2.Gauge("rodriguez", map[string]string{"x": "y"}, "Help message") + g2 := f2.Gauge("rodriguez", map[string]string{"x": "z"}, "Help message") + g3 := f3.Gauge("rodriguez", map[string]string{"x": "z"}, "Help message") // same as g2, but from f3 g1.Update(1) g1.Update(2) g2.Update(3) @@ -86,6 +100,8 @@ func TestGauge(t *testing.T) { snapshot, err := registry.Gather() require.NoError(t, err) + assert.EqualValues(t, "Help message", snapshot[0].GetHelp()) + m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"}) assert.EqualValues(t, 2, m1.GetGauge().GetValue(), "%+v", m1) @@ -93,14 +109,26 @@ func TestGauge(t *testing.T) { assert.EqualValues(t, 4, m2.GetGauge().GetValue(), "%+v", m2) } +func TestGaugeDefaultHelp(t *testing.T) { + registry := prometheus.NewPedanticRegistry() + f1 := New(WithRegisterer(registry)) + g1 := f1.Gauge("rodriguez", map[string]string{"x": "y"}, "") + g1.Update(1) + + snapshot, err := registry.Gather() + require.NoError(t, err) + + assert.EqualValues(t, "rodriguez", snapshot[0].GetHelp()) +} + func TestTimer(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) f2 := f1.Namespace("bender", map[string]string{"a": "b"}) f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2 - t1 := f2.Timer("rodriguez", map[string]string{"x": "y"}) - t2 := f2.Timer("rodriguez", map[string]string{"x": "z"}) - t3 := f3.Timer("rodriguez", map[string]string{"x": "z"}) // same as t2, but from f3 + t1 := f2.Timer("rodriguez", map[string]string{"x": "y"}, "Help message") + t2 := f2.Timer("rodriguez", map[string]string{"x": "z"}, "Help message") + t3 := f3.Timer("rodriguez", map[string]string{"x": "z"}, "Help message") // same as t2, but from f3 t1.Record(1 * time.Second) t1.Record(2 * time.Second) t2.Record(3 * time.Second) @@ -109,6 +137,8 @@ func TestTimer(t *testing.T) { snapshot, err := registry.Gather() require.NoError(t, err) + assert.EqualValues(t, "Help message", snapshot[0].GetHelp()) + m1 := findMetric(t, snapshot, "bender_rodriguez", map[string]string{"a": "b", "x": "y"}) assert.EqualValues(t, 2, m1.GetHistogram().GetSampleCount(), "%+v", m1) assert.EqualValues(t, 3, m1.GetHistogram().GetSampleSum(), "%+v", m1) @@ -136,11 +166,23 @@ func TestTimer(t *testing.T) { } } +func TestTimerDefaultHelp(t *testing.T) { + registry := prometheus.NewPedanticRegistry() + f1 := New(WithRegisterer(registry)) + t1 := f1.Timer("rodriguez", map[string]string{"x": "y"}, "") + t1.Record(1 * time.Second) + + snapshot, err := registry.Gather() + require.NoError(t, err) + + assert.EqualValues(t, "rodriguez", snapshot[0].GetHelp()) +} + func TestTimerCustomBuckets(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithBuckets([]float64{1.5})) // dot and dash in the metric name will be replaced with underscore - t1 := f1.Timer("bender.bending-rodriguez", map[string]string{"x": "y"}) + t1 := f1.Timer("bender.bending-rodriguez", map[string]string{"x": "y"}, "") t1.Record(1 * time.Second) t1.Record(2 * time.Second) diff --git a/metrics/tally/factory.go b/metrics/tally/factory.go index 7923772..8481a64 100644 --- a/metrics/tally/factory.go +++ b/metrics/tally/factory.go @@ -32,7 +32,7 @@ type factory struct { tally tally.Scope } -func (f *factory) Counter(name string, tags map[string]string) metrics.Counter { +func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { scope := f.tally if len(tags) > 0 { scope = scope.Tagged(tags) @@ -40,7 +40,7 @@ func (f *factory) Counter(name string, tags map[string]string) metrics.Counter { return NewCounter(scope.Counter(name)) } -func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge { +func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { scope := f.tally if len(tags) > 0 { scope = scope.Tagged(tags) @@ -48,7 +48,7 @@ func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge { return NewGauge(scope.Gauge(name)) } -func (f *factory) Timer(name string, tags map[string]string) metrics.Timer { +func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { scope := f.tally if len(tags) > 0 { scope = scope.Tagged(tags) diff --git a/metrics/tally/factory_test.go b/metrics/tally/factory_test.go index fbbbe20..9ccfcef 100644 --- a/metrics/tally/factory_test.go +++ b/metrics/tally/factory_test.go @@ -12,11 +12,11 @@ import ( func TestFactory(t *testing.T) { testScope := tally.NewTestScope("pre", map[string]string{"a": "b"}) factory := Wrap(testScope).Namespace("fix", map[string]string{"c": "d"}) - counter := factory.Counter("counter", map[string]string{"x": "y"}) + counter := factory.Counter("counter", map[string]string{"x": "y"}, "") counter.Inc(42) - gauge := factory.Gauge("gauge", map[string]string{"x": "y"}) + gauge := factory.Gauge("gauge", map[string]string{"x": "y"}, "") gauge.Update(42) - timer := factory.Timer("timer", map[string]string{"x": "y"}) + timer := factory.Timer("timer", map[string]string{"x": "y"}, "") timer.Record(42 * time.Millisecond) snapshot := testScope.Snapshot() From 72aa500142e4bc137d10787b074e22e3cc089a58 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Thu, 22 Nov 2018 12:17:15 +0000 Subject: [PATCH 2/5] Change to use struct instead of separate parameters Signed-off-by: Gary Brown --- metrics/adapters/cache_test.go | 18 +++- metrics/adapters/factory.go | 24 ++--- metrics/adapters/factory_test.go | 68 +++++++++++-- metrics/expvar/factory_test.go | 48 +++++++-- metrics/factory.go | 28 ++++-- metrics/go-kit/factory.go | 18 ++-- metrics/go-kit/factory_test.go | 26 ++++- metrics/go-kit/influx/factory_test.go | 36 +++++-- metrics/metrics.go | 13 ++- metrics/metrics_test.go | 26 ++++- metrics/metricstest/local.go | 24 ++--- metrics/metricstest/local_test.go | 74 ++++++++++++-- metrics/multi/multi.go | 16 +-- metrics/multi/multi_test.go | 25 ++++- metrics/prometheus/factory.go | 34 +++---- metrics/prometheus/factory_test.go | 139 ++++++++++++++++++++++---- metrics/tally/factory.go | 28 +++--- metrics/tally/factory_test.go | 27 ++++- 18 files changed, 514 insertions(+), 158 deletions(-) diff --git a/metrics/adapters/cache_test.go b/metrics/adapters/cache_test.go index 7ee83ff..ffcbada 100644 --- a/metrics/adapters/cache_test.go +++ b/metrics/adapters/cache_test.go @@ -26,9 +26,21 @@ import ( func TestCache(t *testing.T) { f := metricstest.NewFactory(100 * time.Second) - c1 := f.Counter("x", nil, "") - g1 := f.Gauge("y", nil, "") - t1 := f.Timer("z", nil, "") + c1 := f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "x", + }, + }) + g1 := f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "y", + }, + }) + t1 := f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "z", + }, + }) c := newCache() diff --git a/metrics/adapters/factory.go b/metrics/adapters/factory.go index f5459a9..8022f14 100644 --- a/metrics/adapters/factory.go +++ b/metrics/adapters/factory.go @@ -63,32 +63,32 @@ type factory struct { cache *cache } -func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { - fullName, fullTags, key := f.getKey(name, tags) +func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { + fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) return f.cache.getOrSetCounter(key, func() metrics.Counter { - return f.factory.Counter(fullName, fullTags, description) + return f.factory.Counter(fullName, fullTags, metricInfo.Description) }) } -func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { - fullName, fullTags, key := f.getKey(name, tags) +func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { + fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) return f.cache.getOrSetGauge(key, func() metrics.Gauge { - return f.factory.Gauge(fullName, fullTags, description) + return f.factory.Gauge(fullName, fullTags, metricInfo.Description) }) } -func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { - fullName, fullTags, key := f.getKey(name, tags) +func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { + fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) return f.cache.getOrSetTimer(key, func() metrics.Timer { - return f.factory.Timer(fullName, fullTags, description) + return f.factory.Timer(fullName, fullTags, metricInfo.Description) }) } -func (f *factory) Namespace(name string, tags map[string]string) metrics.Factory { +func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { return &factory{ cache: f.cache, - scope: f.subScope(name), - tags: f.mergeTags(tags), + scope: f.subScope(metricScope.Name), + tags: f.mergeTags(metricScope.Tags), factory: f.factory, Options: f.Options, } diff --git a/metrics/adapters/factory_test.go b/metrics/adapters/factory_test.go index 8eeff9a..3a8a10d 100644 --- a/metrics/adapters/factory_test.go +++ b/metrics/adapters/factory_test.go @@ -80,15 +80,48 @@ func TestFactory(t *testing.T) { ff := &fakeTagless{factory: local} f := WrapFactoryWithoutTags(ff, Options{}) if testCase.namespace != "" || testCase.nsTags != nil { - f = f.Namespace(testCase.namespace, testCase.nsTags) + f = f.Namespace(metrics.MetricScope{ + Name: testCase.namespace, + Tags: testCase.nsTags, + }) } - counter := f.Counter(counterPrefix+testCase.name, testCase.tags, "") - gauge := f.Gauge(gaugePrefix+testCase.name, testCase.tags, "") - timer := f.Timer(timerPrefix+testCase.name, testCase.tags, "") + counter := f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, + }, + }) + gauge := f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, + }, + }) + timer := f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, + }, + }) - assert.Equal(t, counter, f.Counter(counterPrefix+testCase.name, testCase.tags, "")) - assert.Equal(t, gauge, f.Gauge(gaugePrefix+testCase.name, testCase.tags, "")) - assert.Equal(t, timer, f.Timer(timerPrefix+testCase.name, testCase.tags, "")) + assert.Equal(t, counter, f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, + }, + })) + assert.Equal(t, gauge, f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, + }, + })) + assert.Equal(t, timer, f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, + }, + })) assert.Equal(t, fmt.Sprintf(testCase.fullName, counterPrefix), ff.counter) assert.Equal(t, fmt.Sprintf(testCase.fullName, gaugePrefix), ff.gauge) @@ -106,15 +139,30 @@ type fakeTagless struct { func (f *fakeTagless) Counter(name string, description string) metrics.Counter { f.counter = name - return f.factory.Counter(name, nil, description) + return f.factory.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: name, + }, + Description: description, + }) } func (f *fakeTagless) Gauge(name string, description string) metrics.Gauge { f.gauge = name - return f.factory.Gauge(name, nil, description) + return f.factory.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: name, + }, + Description: description, + }) } func (f *fakeTagless) Timer(name string, description string) metrics.Timer { f.timer = name - return f.factory.Timer(name, nil, description) + return f.factory.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: name, + }, + Description: description, + }) } diff --git a/metrics/expvar/factory_test.go b/metrics/expvar/factory_test.go index 2d197f3..4840e95 100644 --- a/metrics/expvar/factory_test.go +++ b/metrics/expvar/factory_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/uber/jaeger-lib/metrics" ) var ( @@ -63,16 +64,49 @@ func TestFactory(t *testing.T) { } ff := f if testCase.namespace != "" || testCase.nsTags != nil { - ff = f.Namespace(testCase.namespace, testCase.nsTags) + ff = f.Namespace(metrics.MetricScope{ + Name: testCase.namespace, + Tags: testCase.nsTags, + }) } - counter := ff.Counter(counterPrefix+testCase.name, testCase.tags, "") - gauge := ff.Gauge(gaugePrefix+testCase.name, testCase.tags, "") - timer := ff.Timer(timerPrefix+testCase.name, testCase.tags, "") + counter := ff.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, + }, + }) + gauge := ff.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, + }, + }) + timer := ff.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, + }, + }) // register second time, should not panic - ff.Counter(counterPrefix+testCase.name, testCase.tags, "") - ff.Gauge(gaugePrefix+testCase.name, testCase.tags, "") - ff.Timer(timerPrefix+testCase.name, testCase.tags, "") + ff.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, + }, + }) + ff.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, + }, + }) + ff.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, + }, + }) counter.Inc(42) gauge.Update(42) diff --git a/metrics/factory.go b/metrics/factory.go index c2a7986..bde005a 100644 --- a/metrics/factory.go +++ b/metrics/factory.go @@ -14,14 +14,26 @@ package metrics +// MetricScope defines the name and tags map associated with a metric +type MetricScope struct { + Name string + Tags map[string]string +} + +// MetricInfo defines the information associated with a metric +type MetricInfo struct { + MetricScope + Description string +} + // Factory creates new metrics type Factory interface { - Counter(name string, tags map[string]string, description string) Counter - Timer(name string, tags map[string]string, description string) Timer - Gauge(name string, tags map[string]string, description string) Gauge + Counter(metric MetricInfo) Counter + Timer(metric MetricInfo) Timer + Gauge(metric MetricInfo) Gauge // Namespace returns a nested metrics factory. - Namespace(name string, tags map[string]string) Factory + Namespace(metricScope MetricScope) Factory } // NullFactory is a metrics factory that returns NullCounter, NullTimer, and NullGauge. @@ -29,13 +41,13 @@ var NullFactory Factory = nullFactory{} type nullFactory struct{} -func (nullFactory) Counter(name string, tags map[string]string, description string) Counter { +func (nullFactory) Counter(metricInfo MetricInfo) Counter { return NullCounter } -func (nullFactory) Timer(name string, tags map[string]string, description string) Timer { +func (nullFactory) Timer(metricInfo MetricInfo) Timer { return NullTimer } -func (nullFactory) Gauge(name string, tags map[string]string, description string) Gauge { +func (nullFactory) Gauge(metricInfo MetricInfo) Gauge { return NullGauge } -func (nullFactory) Namespace(name string, tags map[string]string) Factory { return NullFactory } +func (nullFactory) Namespace(metricScope MetricScope) Factory { return NullFactory } diff --git a/metrics/go-kit/factory.go b/metrics/go-kit/factory.go index dc999d7..a2e7bde 100644 --- a/metrics/go-kit/factory.go +++ b/metrics/go-kit/factory.go @@ -103,8 +103,8 @@ func (f *factory) nameAndTagsList(nom string, tags map[string]string) (name stri return } -func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { - name, tagsList := f.nameAndTagsList(name, tags) +func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { + name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) counter := f.factory.Counter(name) if len(tagsList) > 0 { counter = counter.With(tagsList...) @@ -112,8 +112,8 @@ func (f *factory) Counter(name string, tags map[string]string, description strin return NewCounter(counter) } -func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { - name, tagsList := f.nameAndTagsList(name, tags) +func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { + name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) hist := f.factory.Histogram(name) if len(tagsList) > 0 { hist = hist.With(tagsList...) @@ -121,8 +121,8 @@ func (f *factory) Timer(name string, tags map[string]string, description string) return NewTimer(hist) } -func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { - name, tagsList := f.nameAndTagsList(name, tags) +func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { + name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) gauge := f.factory.Gauge(name) if len(tagsList) > 0 { gauge = gauge.With(tagsList...) @@ -130,10 +130,10 @@ func (f *factory) Gauge(name string, tags map[string]string, description string) return NewGauge(gauge) } -func (f *factory) Namespace(name string, tags map[string]string) metrics.Factory { +func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { return &factory{ - scope: f.subScope(name), - tags: f.mergeTags(tags), + scope: f.subScope(metricScope.Name), + tags: f.mergeTags(metricScope.Tags), factory: f.factory, scopeSep: f.scopeSep, tagsSep: f.tagsSep, diff --git a/metrics/go-kit/factory_test.go b/metrics/go-kit/factory_test.go index de1a7f7..f98d6eb 100644 --- a/metrics/go-kit/factory_test.go +++ b/metrics/go-kit/factory_test.go @@ -153,7 +153,10 @@ func TestFactoryScoping(t *testing.T) { t.Run(factoryName+"_"+testSuite.metricType+"_"+testCase.expName, func(t *testing.T) { f := Wrap(testCase.prefix, testCase.f, testCase.options...) if testCase.useNamespace { - f = f.Namespace(testCase.namespace, testCase.namespaceTags) + f = f.Namespace(metrics.MetricScope{ + Name: testCase.namespace, + Tags: testCase.namespaceTags, + }) } name, labels := testSuite.metricFunc(t, testCase, f) assert.Equal(t, testCase.expName, name()) @@ -166,7 +169,12 @@ func TestFactoryScoping(t *testing.T) { } func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - c := f.Counter(testCase.name, testCase.tags, "") + c := f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: testCase.name, + Tags: testCase.tags, + }, + }) c.Inc(123) kc := c.(*Counter).counter var gc *generic.Counter @@ -182,7 +190,12 @@ func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func( } func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - g := f.Gauge(testCase.name, testCase.tags, "") + g := f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: testCase.name, + Tags: testCase.tags, + }, + }) g.Update(123) gg := g.(*Gauge).gauge.(*generic.Gauge) assert.EqualValues(t, 123.0, gg.Value()) @@ -192,7 +205,12 @@ func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() } func testTimer(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - tm := f.Timer(testCase.name, testCase.tags, "") + tm := f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: testCase.name, + Tags: testCase.tags, + }, + }) tm.Record(123 * time.Millisecond) gt := tm.(*Timer).hist.(*generic.Histogram) assert.InDelta(t, 0.123, gt.Quantile(0.9), 0.00001) diff --git a/metrics/go-kit/influx/factory_test.go b/metrics/go-kit/influx/factory_test.go index 27c5c17..9dcfed5 100644 --- a/metrics/go-kit/influx/factory_test.go +++ b/metrics/go-kit/influx/factory_test.go @@ -11,6 +11,7 @@ import ( influxdb "github.com/influxdata/influxdb/client/v2" "github.com/stretchr/testify/assert" + "github.com/uber/jaeger-lib/metrics" "github.com/uber/jaeger-lib/metrics/go-kit" ) @@ -19,7 +20,12 @@ func TestCounter(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - c := wf.Counter("gokit.infl-counter", map[string]string{"label": "val1"}, "") + c := wf.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gokit.infl-counter", + Tags: map[string]string{"label": "val1"}, + }, + }) c.Inc(7) assert.Contains(t, reportToString(in), "namespace.gokit.infl-counter,label=val1 count=7") @@ -30,7 +36,12 @@ func TestGauge(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - g := wf.Gauge("gokit.infl-gauge", map[string]string{"x": "y"}, "") + g := wf.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gokit.infl-gauge", + Tags: map[string]string{"x": "y"}, + }, + }) g.Update(42) assert.Contains(t, reportToString(in), "namespace.gokit.infl-gauge,x=y value=42") @@ -41,7 +52,12 @@ func TestTimer(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - timer := wf.Timer("gokit.infl-timer", map[string]string{"x": "y"}, "") + timer := wf.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gokit.infl-timer", + Tags: map[string]string{"x": "y"}, + }, + }) timer.Record(time.Second * 1) timer.Record(time.Second * 1) timer.Record(time.Second * 10) @@ -54,9 +70,17 @@ func TestWrapperNamespaces(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - wf = wf.Namespace("bar", map[string]string{"bar_tag": "bar_tag"}) - - c := wf.Counter("gokit.prom-wrapped-counter", map[string]string{"x": "y"}, "") + wf = wf.Namespace(metrics.MetricScope{ + Name: "bar", + Tags: map[string]string{"bar_tag": "bar_tag"}, + }) + + c := wf.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gokit.prom-wrapped-counter", + Tags: map[string]string{"x": "y"}, + }, + }) c.Inc(42) assert.Contains(t, reportToString(in), "namespace.bar.gokit.prom-wrapped-counter,bar_tag=bar_tag,x=y count=42") diff --git a/metrics/metrics.go b/metrics/metrics.go index 9e86d26..0f9e877 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -74,12 +74,19 @@ func InitOrError(m interface{}, factory Factory, globalTags map[string]string) e } description := field.Tag.Get("description") var obj interface{} + metricInfo := MetricInfo{ + MetricScope: MetricScope{ + Name: metric, + Tags: tags, + }, + Description: description, + } if field.Type.AssignableTo(counterPtrType) { - obj = factory.Counter(metric, tags, description) + obj = factory.Counter(metricInfo) } else if field.Type.AssignableTo(gaugePtrType) { - obj = factory.Gauge(metric, tags, description) + obj = factory.Gauge(metricInfo) } else if field.Type.AssignableTo(timerPtrType) { - obj = factory.Timer(metric, tags, description) + obj = factory.Timer(metricInfo) } else { return fmt.Errorf( "Field %s is not a pointer to timer, gauge, or counter", diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index dd306d8..4cbb861 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -86,8 +86,26 @@ func TestInitPanic(t *testing.T) { func TestNullMetrics(t *testing.T) { // This test is just for cover - metrics.NullFactory.Timer("name", nil, "").Record(0) - metrics.NullFactory.Counter("name", nil, "").Inc(0) - metrics.NullFactory.Gauge("name", nil, "").Update(0) - metrics.NullFactory.Namespace("name", nil).Gauge("name2", nil, "").Update(0) + metrics.NullFactory.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "name", + }, + }).Record(0) + metrics.NullFactory.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "name", + }, + }).Inc(0) + metrics.NullFactory.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "name", + }, + }).Update(0) + metrics.NullFactory.Namespace(metrics.MetricScope{ + Name: "name", + }).Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "name2", + }, + }).Update(0) } diff --git a/metrics/metricstest/local.go b/metrics/metricstest/local.go index ed7f2d0..cb68dfc 100644 --- a/metrics/metricstest/local.go +++ b/metrics/metricstest/local.go @@ -279,43 +279,43 @@ func (l *Factory) newNamespace(name string) string { } // Counter returns a local stats counter -func (l *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { +func (l *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { return &localCounter{ stats{ - name: l.newNamespace(name), - tags: l.appendTags(tags), + name: l.newNamespace(metricInfo.Name), + tags: l.appendTags(metricInfo.Tags), localBackend: l.Backend, }, } } // Timer returns a local stats timer. -func (l *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { +func (l *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { return &localTimer{ stats{ - name: l.newNamespace(name), - tags: l.appendTags(tags), + name: l.newNamespace(metricInfo.Name), + tags: l.appendTags(metricInfo.Tags), localBackend: l.Backend, }, } } // Gauge returns a local stats gauge. -func (l *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { +func (l *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { return &localGauge{ stats{ - name: l.newNamespace(name), - tags: l.appendTags(tags), + name: l.newNamespace(metricInfo.Name), + tags: l.appendTags(metricInfo.Tags), localBackend: l.Backend, }, } } // Namespace returns a new namespace. -func (l *Factory) Namespace(name string, tags map[string]string) metrics.Factory { +func (l *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { return &Factory{ - namespace: l.newNamespace(name), - tags: l.appendTags(tags), + namespace: l.newNamespace(metricScope.Name), + tags: l.appendTags(metricScope.Tags), Backend: l.Backend, } } diff --git a/metrics/metricstest/local_test.go b/metrics/metricstest/local_test.go index bbbc64d..52de776 100644 --- a/metrics/metricstest/local_test.go +++ b/metrics/metricstest/local_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/uber/jaeger-lib/metrics" ) func TestLocalMetrics(t *testing.T) { @@ -15,15 +16,58 @@ func TestLocalMetrics(t *testing.T) { f := NewFactory(0) defer f.Stop() - f.Counter("my-counter", tags, "").Inc(4) - f.Counter("my-counter", tags, "").Inc(6) - f.Counter("my-counter", nil, "").Inc(6) - f.Counter("other-counter", nil, "").Inc(8) - f.Gauge("my-gauge", nil, "").Update(25) - f.Gauge("my-gauge", nil, "").Update(43) - f.Gauge("other-gauge", nil, "").Update(74) - f.Namespace("namespace", tags).Counter("my-counter", nil, "").Inc(7) - f.Namespace("ns.subns", nil).Counter("", map[string]string{"service": "a-service"}, "").Inc(9) + f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-counter", + Tags: tags, + }, + }).Inc(4) + f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-counter", + Tags: tags, + }, + }).Inc(6) + f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-counter", + }, + }).Inc(6) + f.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "other-counter", + }, + }).Inc(8) + f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-gauge", + }, + }).Update(25) + f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-gauge", + }, + }).Update(43) + f.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "other-gauge", + }, + }).Update(74) + f.Namespace(metrics.MetricScope{ + Name: "namespace", + Tags: tags, + }).Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "my-counter", + }, + }).Inc(7) + f.Namespace(metrics.MetricScope{ + Name: "ns.subns", + }).Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Tags: map[string]string{"service": "a-service"}, + }, + }).Inc(9) timings := map[string][]time.Duration{ "foo-latency": { @@ -42,7 +86,11 @@ func TestLocalMetrics(t *testing.T) { for metric, timing := range timings { for _, d := range timing { - f.Timer(metric, nil, "").Record(d) + f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: metric, + }, + }).Record(d) } } @@ -90,7 +138,11 @@ func TestLocalMetricsInterval(t *testing.T) { f := NewFactory(refreshInterval) defer f.Stop() - f.Timer("timer", nil, "").Record(1) + f.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "timer", + }, + }).Record(1) f.tm.Lock() timer := f.timers["timer"] diff --git a/metrics/multi/multi.go b/metrics/multi/multi.go index 90fc184..267f88f 100644 --- a/metrics/multi/multi.go +++ b/metrics/multi/multi.go @@ -43,12 +43,12 @@ func (c *counter) Inc(delta int64) { } // Counter implements metrics.Factory interface -func (f *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { +func (f *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { counter := &counter{ counters: make([]metrics.Counter, len(f.factories)), } for i, factory := range f.factories { - counter.counters[i] = factory.Counter(name, tags, description) + counter.counters[i] = factory.Counter(metricInfo) } return counter } @@ -64,12 +64,12 @@ func (t *timer) Record(delta time.Duration) { } // Timer implements metrics.Factory interface -func (f *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { +func (f *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { timer := &timer{ timers: make([]metrics.Timer, len(f.factories)), } for i, factory := range f.factories { - timer.timers[i] = factory.Timer(name, tags, description) + timer.timers[i] = factory.Timer(metricInfo) } return timer } @@ -85,23 +85,23 @@ func (t *gauge) Update(value int64) { } // Gauge implements metrics.Factory interface -func (f *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { +func (f *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { gauge := &gauge{ gauges: make([]metrics.Gauge, len(f.factories)), } for i, factory := range f.factories { - gauge.gauges[i] = factory.Gauge(name, tags, description) + gauge.gauges[i] = factory.Gauge(metricInfo) } return gauge } // Namespace implements metrics.Factory interface -func (f *Factory) Namespace(name string, tags map[string]string) metrics.Factory { +func (f *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { newFactory := &Factory{ factories: make([]metrics.Factory, len(f.factories)), } for i, factory := range f.factories { - newFactory.factories[i] = factory.Namespace(name, tags) + newFactory.factories[i] = factory.Namespace(metricScope) } return newFactory } diff --git a/metrics/multi/multi_test.go b/metrics/multi/multi_test.go index 031e92b..6018fac 100644 --- a/metrics/multi/multi_test.go +++ b/metrics/multi/multi_test.go @@ -15,11 +15,28 @@ func TestMultiFactory(t *testing.T) { f1 := metricstest.NewFactory(time.Second) f2 := metricstest.NewFactory(time.Second) multi1 := New(f1, f2) - multi2 := multi1.Namespace("ns2", nil) + multi2 := multi1.Namespace(metrics.MetricScope{ + Name: "ns2", + }) tags := map[string]string{"x": "y"} - multi2.Counter("counter", tags, "").Inc(42) - multi2.Gauge("gauge", tags, "").Update(42) - multi2.Timer("timer", tags, "").Record(42 * time.Millisecond) + multi2.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "counter", + Tags: tags, + }, + }).Inc(42) + multi2.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gauge", + Tags: tags, + }, + }).Update(42) + multi2.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "timer", + Tags: tags, + }, + }).Record(42 * time.Millisecond) for _, f := range []*metricstest.Factory{f1, f2} { f.AssertCounterMetrics(t, diff --git a/metrics/prometheus/factory.go b/metrics/prometheus/factory.go index 8c90371..5e1467b 100644 --- a/metrics/prometheus/factory.go +++ b/metrics/prometheus/factory.go @@ -125,13 +125,13 @@ func newFactory(parent *Factory, scope string, tags map[string]string) *Factory } // Counter implements Counter of metrics.Factory. -func (f *Factory) Counter(name string, tags map[string]string, description string) metrics.Counter { - description = strings.TrimSpace(description) +func (f *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { + description := strings.TrimSpace(metricInfo.Description) if len(description) == 0 { - description = name + description = metricInfo.Name } - name = counterNamingConvention(f.subScope(name)) - tags = f.mergeTags(tags) + name := counterNamingConvention(f.subScope(metricInfo.Name)) + tags := f.mergeTags(metricInfo.Tags) labelNames := f.tagNames(tags) opts := prometheus.CounterOpts{ Name: name, @@ -144,13 +144,13 @@ func (f *Factory) Counter(name string, tags map[string]string, description strin } // Gauge implements Gauge of metrics.Factory. -func (f *Factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { - description = strings.TrimSpace(description) +func (f *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { + description := strings.TrimSpace(metricInfo.Description) if len(description) == 0 { - description = name + description = metricInfo.Name } - name = f.subScope(name) - tags = f.mergeTags(tags) + name := f.subScope(metricInfo.Name) + tags := f.mergeTags(metricInfo.Tags) labelNames := f.tagNames(tags) opts := prometheus.GaugeOpts{ Name: name, @@ -163,13 +163,13 @@ func (f *Factory) Gauge(name string, tags map[string]string, description string) } // Timer implements Timer of metrics.Factory. -func (f *Factory) Timer(name string, tags map[string]string, description string) metrics.Timer { - description = strings.TrimSpace(description) +func (f *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { + description := strings.TrimSpace(metricInfo.Description) if len(description) == 0 { - description = name + description = metricInfo.Name } - name = f.subScope(name) - tags = f.mergeTags(tags) + name := f.subScope(metricInfo.Name) + tags := f.mergeTags(metricInfo.Tags) labelNames := f.tagNames(tags) opts := prometheus.HistogramOpts{ Name: name, @@ -183,8 +183,8 @@ func (f *Factory) Timer(name string, tags map[string]string, description string) } // Namespace implements Namespace of metrics.Factory. -func (f *Factory) Namespace(name string, tags map[string]string) metrics.Factory { - return newFactory(f, f.subScope(name), f.mergeTags(tags)) +func (f *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { + return newFactory(f, f.subScope(metricScope.Name), f.mergeTags(metricScope.Tags)) } type counter struct { diff --git a/metrics/prometheus/factory_test.go b/metrics/prometheus/factory_test.go index c17eb39..4e96600 100644 --- a/metrics/prometheus/factory_test.go +++ b/metrics/prometheus/factory_test.go @@ -37,7 +37,15 @@ func TestOptions(t *testing.T) { func TestSeparator(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithSeparator(SeparatorColon)) - c1 := f1.Namespace("bender", nil).Counter("rodriguez", map[string]string{"a": "b"}, "Help message") + c1 := f1.Namespace(metrics.MetricScope{ + Name: "bender", + }).Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"a": "b"}, + }, + Description: "Help message", + }) c1.Inc(1) snapshot, err := registry.Gather() require.NoError(t, err) @@ -48,13 +56,34 @@ func TestSeparator(t *testing.T) { func TestCounter(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - fDummy := f1.Namespace("", nil) - f2 := fDummy.Namespace("bender", map[string]string{"a": "b"}) - f3 := f2.Namespace("", nil) - - c1 := f2.Counter("rodriguez", map[string]string{"x": "y"}, "Help message") - c2 := f2.Counter("rodriguez", map[string]string{"x": "z"}, "Help message") - c3 := f3.Counter("rodriguez", map[string]string{"x": "z"}, "Help message") // same tags as c2, but from f3 + fDummy := f1.Namespace(metrics.MetricScope{}) + f2 := fDummy.Namespace(metrics.MetricScope{ + Name: "bender", + Tags: map[string]string{"a": "b"}, + }) + f3 := f2.Namespace(metrics.MetricScope{}) + + c1 := f2.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + Description: "Help message", + }) + c2 := f2.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) + c3 := f3.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) // same tags as c2, but from f3 c1.Inc(1) c1.Inc(2) c2.Inc(3) @@ -75,7 +104,12 @@ func TestCounter(t *testing.T) { func TestCounterDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - c1 := f1.Counter("rodriguez", map[string]string{"x": "y"}, "") + c1 := f1.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + }) c1.Inc(1) snapshot, err := registry.Gather() @@ -87,11 +121,34 @@ func TestCounterDefaultHelp(t *testing.T) { func TestGauge(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace("bender", map[string]string{"a": "b"}) - f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2 - g1 := f2.Gauge("rodriguez", map[string]string{"x": "y"}, "Help message") - g2 := f2.Gauge("rodriguez", map[string]string{"x": "z"}, "Help message") - g3 := f3.Gauge("rodriguez", map[string]string{"x": "z"}, "Help message") // same as g2, but from f3 + f2 := f1.Namespace(metrics.MetricScope{ + Name: "bender", + Tags: map[string]string{"a": "b"}, + }) + f3 := f2.Namespace(metrics.MetricScope{ + Tags: map[string]string{"a": "b"}, + }) // essentially same as f2 + g1 := f2.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + Description: "Help message", + }) + g2 := f2.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) + g3 := f3.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) // same as g2, but from f3 g1.Update(1) g1.Update(2) g2.Update(3) @@ -112,7 +169,12 @@ func TestGauge(t *testing.T) { func TestGaugeDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - g1 := f1.Gauge("rodriguez", map[string]string{"x": "y"}, "") + g1 := f1.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + }) g1.Update(1) snapshot, err := registry.Gather() @@ -124,11 +186,34 @@ func TestGaugeDefaultHelp(t *testing.T) { func TestTimer(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace("bender", map[string]string{"a": "b"}) - f3 := f2.Namespace("", map[string]string{"a": "b"}) // essentially same as f2 - t1 := f2.Timer("rodriguez", map[string]string{"x": "y"}, "Help message") - t2 := f2.Timer("rodriguez", map[string]string{"x": "z"}, "Help message") - t3 := f3.Timer("rodriguez", map[string]string{"x": "z"}, "Help message") // same as t2, but from f3 + f2 := f1.Namespace(metrics.MetricScope{ + Name: "bender", + Tags: map[string]string{"a": "b"}, + }) + f3 := f2.Namespace(metrics.MetricScope{ + Tags: map[string]string{"a": "b"}, + }) // essentially same as f2 + t1 := f2.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + Description: "Help message", + }) + t2 := f2.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) + t3 := f3.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + }, + Description: "Help message", + }) // same as t2, but from f3 t1.Record(1 * time.Second) t1.Record(2 * time.Second) t2.Record(3 * time.Second) @@ -169,7 +254,12 @@ func TestTimer(t *testing.T) { func TestTimerDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - t1 := f1.Timer("rodriguez", map[string]string{"x": "y"}, "") + t1 := f1.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + }, + }) t1.Record(1 * time.Second) snapshot, err := registry.Gather() @@ -182,7 +272,12 @@ func TestTimerCustomBuckets(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithBuckets([]float64{1.5})) // dot and dash in the metric name will be replaced with underscore - t1 := f1.Timer("bender.bending-rodriguez", map[string]string{"x": "y"}, "") + t1 := f1.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "bender.bending-rodriguez", + Tags: map[string]string{"x": "y"}, + }, + }) t1.Record(1 * time.Second) t1.Record(2 * time.Second) diff --git a/metrics/tally/factory.go b/metrics/tally/factory.go index 8481a64..3e64e5f 100644 --- a/metrics/tally/factory.go +++ b/metrics/tally/factory.go @@ -32,32 +32,32 @@ type factory struct { tally tally.Scope } -func (f *factory) Counter(name string, tags map[string]string, description string) metrics.Counter { +func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { scope := f.tally - if len(tags) > 0 { - scope = scope.Tagged(tags) + if len(metricInfo.Tags) > 0 { + scope = scope.Tagged(metricInfo.Tags) } - return NewCounter(scope.Counter(name)) + return NewCounter(scope.Counter(metricInfo.Name)) } -func (f *factory) Gauge(name string, tags map[string]string, description string) metrics.Gauge { +func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { scope := f.tally - if len(tags) > 0 { - scope = scope.Tagged(tags) + if len(metricInfo.Tags) > 0 { + scope = scope.Tagged(metricInfo.Tags) } - return NewGauge(scope.Gauge(name)) + return NewGauge(scope.Gauge(metricInfo.Name)) } -func (f *factory) Timer(name string, tags map[string]string, description string) metrics.Timer { +func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { scope := f.tally - if len(tags) > 0 { - scope = scope.Tagged(tags) + if len(metricInfo.Tags) > 0 { + scope = scope.Tagged(metricInfo.Tags) } - return NewTimer(scope.Timer(name)) + return NewTimer(scope.Timer(metricInfo.Name)) } -func (f *factory) Namespace(name string, tags map[string]string) metrics.Factory { +func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { return &factory{ - tally: f.tally.SubScope(name).Tagged(tags), + tally: f.tally.SubScope(metricScope.Name).Tagged(metricScope.Tags), } } diff --git a/metrics/tally/factory_test.go b/metrics/tally/factory_test.go index 9ccfcef..39f866b 100644 --- a/metrics/tally/factory_test.go +++ b/metrics/tally/factory_test.go @@ -5,18 +5,37 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/uber/jaeger-lib/metrics" "github.com/uber-go/tally" ) func TestFactory(t *testing.T) { testScope := tally.NewTestScope("pre", map[string]string{"a": "b"}) - factory := Wrap(testScope).Namespace("fix", map[string]string{"c": "d"}) - counter := factory.Counter("counter", map[string]string{"x": "y"}, "") + factory := Wrap(testScope).Namespace(metrics.MetricScope{ + Name: "fix", + Tags: map[string]string{"c": "d"}, + }) + counter := factory.Counter(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "counter", + Tags: map[string]string{"x": "y"}, + }, + }) counter.Inc(42) - gauge := factory.Gauge("gauge", map[string]string{"x": "y"}, "") + gauge := factory.Gauge(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "gauge", + Tags: map[string]string{"x": "y"}, + }, + }) gauge.Update(42) - timer := factory.Timer("timer", map[string]string{"x": "y"}, "") + timer := factory.Timer(metrics.MetricInfo{ + MetricScope: metrics.MetricScope{ + Name: "timer", + Tags: map[string]string{"x": "y"}, + }, + }) timer.Record(42 * time.Millisecond) snapshot := testScope.Snapshot() From a2eaf3e099fd9c1eda1cbdc93a3267acb4d1aac2 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Sat, 24 Nov 2018 20:07:33 +0000 Subject: [PATCH 3/5] Rename the types to Options and Scope, and don't embed the Scope inside the Options. Signed-off-by: Gary Brown --- metrics/adapters/cache_test.go | 18 ++-- metrics/adapters/factory.go | 24 ++--- metrics/adapters/factory_test.go | 68 +++++--------- metrics/expvar/factory_test.go | 50 ++++------ metrics/factory.go | 27 +++--- metrics/go-kit/factory.go | 18 ++-- metrics/go-kit/factory_test.go | 26 ++---- metrics/go-kit/influx/factory_test.go | 34 +++---- metrics/metrics.go | 14 ++- metrics/metrics_test.go | 26 ++---- metrics/metricstest/local.go | 24 ++--- metrics/metricstest/local_test.go | 74 ++++++--------- metrics/multi/multi.go | 16 ++-- metrics/multi/multi_test.go | 26 ++---- metrics/prometheus/factory.go | 34 +++---- metrics/prometheus/factory_test.go | 128 ++++++++++---------------- metrics/tally/factory.go | 28 +++--- metrics/tally/factory_test.go | 26 ++---- 18 files changed, 270 insertions(+), 391 deletions(-) diff --git a/metrics/adapters/cache_test.go b/metrics/adapters/cache_test.go index ffcbada..5f3401d 100644 --- a/metrics/adapters/cache_test.go +++ b/metrics/adapters/cache_test.go @@ -26,20 +26,14 @@ import ( func TestCache(t *testing.T) { f := metricstest.NewFactory(100 * time.Second) - c1 := f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "x", - }, + c1 := f.Counter(metrics.Options{ + Name: "x", }) - g1 := f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "y", - }, + g1 := f.Gauge(metrics.Options{ + Name: "y", }) - t1 := f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "z", - }, + t1 := f.Timer(metrics.Options{ + Name: "z", }) c := newCache() diff --git a/metrics/adapters/factory.go b/metrics/adapters/factory.go index 8022f14..7f4925d 100644 --- a/metrics/adapters/factory.go +++ b/metrics/adapters/factory.go @@ -63,32 +63,32 @@ type factory struct { cache *cache } -func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { - fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) +func (f *factory) Counter(options metrics.Options) metrics.Counter { + fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetCounter(key, func() metrics.Counter { - return f.factory.Counter(fullName, fullTags, metricInfo.Description) + return f.factory.Counter(fullName, fullTags, options.Description) }) } -func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { - fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) +func (f *factory) Gauge(options metrics.Options) metrics.Gauge { + fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetGauge(key, func() metrics.Gauge { - return f.factory.Gauge(fullName, fullTags, metricInfo.Description) + return f.factory.Gauge(fullName, fullTags, options.Description) }) } -func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { - fullName, fullTags, key := f.getKey(metricInfo.Name, metricInfo.Tags) +func (f *factory) Timer(options metrics.Options) metrics.Timer { + fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetTimer(key, func() metrics.Timer { - return f.factory.Timer(fullName, fullTags, metricInfo.Description) + return f.factory.Timer(fullName, fullTags, options.Description) }) } -func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { +func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { return &factory{ cache: f.cache, - scope: f.subScope(metricScope.Name), - tags: f.mergeTags(metricScope.Tags), + scope: f.subScope(scope.Name), + tags: f.mergeTags(scope.Tags), factory: f.factory, Options: f.Options, } diff --git a/metrics/adapters/factory_test.go b/metrics/adapters/factory_test.go index 3a8a10d..03b1577 100644 --- a/metrics/adapters/factory_test.go +++ b/metrics/adapters/factory_test.go @@ -80,47 +80,35 @@ func TestFactory(t *testing.T) { ff := &fakeTagless{factory: local} f := WrapFactoryWithoutTags(ff, Options{}) if testCase.namespace != "" || testCase.nsTags != nil { - f = f.Namespace(metrics.MetricScope{ + f = f.Namespace(metrics.Scope{ Name: testCase.namespace, Tags: testCase.nsTags, }) } - counter := f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: counterPrefix + testCase.name, - Tags: testCase.tags, - }, + counter := f.Counter(metrics.Options{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, }) - gauge := f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: gaugePrefix + testCase.name, - Tags: testCase.tags, - }, + gauge := f.Gauge(metrics.Options{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, }) - timer := f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: timerPrefix + testCase.name, - Tags: testCase.tags, - }, + timer := f.Timer(metrics.Options{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, }) - assert.Equal(t, counter, f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: counterPrefix + testCase.name, - Tags: testCase.tags, - }, + assert.Equal(t, counter, f.Counter(metrics.Options{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, })) - assert.Equal(t, gauge, f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: gaugePrefix + testCase.name, - Tags: testCase.tags, - }, + assert.Equal(t, gauge, f.Gauge(metrics.Options{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, })) - assert.Equal(t, timer, f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: timerPrefix + testCase.name, - Tags: testCase.tags, - }, + assert.Equal(t, timer, f.Timer(metrics.Options{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, })) assert.Equal(t, fmt.Sprintf(testCase.fullName, counterPrefix), ff.counter) @@ -139,30 +127,24 @@ type fakeTagless struct { func (f *fakeTagless) Counter(name string, description string) metrics.Counter { f.counter = name - return f.factory.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: name, - }, + return f.factory.Counter(metrics.Options{ + Name: name, Description: description, }) } func (f *fakeTagless) Gauge(name string, description string) metrics.Gauge { f.gauge = name - return f.factory.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: name, - }, + return f.factory.Gauge(metrics.Options{ + Name: name, Description: description, }) } func (f *fakeTagless) Timer(name string, description string) metrics.Timer { f.timer = name - return f.factory.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: name, - }, + return f.factory.Timer(metrics.Options{ + Name: name, Description: description, }) } diff --git a/metrics/expvar/factory_test.go b/metrics/expvar/factory_test.go index 4840e95..9eca488 100644 --- a/metrics/expvar/factory_test.go +++ b/metrics/expvar/factory_test.go @@ -64,48 +64,36 @@ func TestFactory(t *testing.T) { } ff := f if testCase.namespace != "" || testCase.nsTags != nil { - ff = f.Namespace(metrics.MetricScope{ + ff = f.Namespace(metrics.Scope{ Name: testCase.namespace, Tags: testCase.nsTags, }) } - counter := ff.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: counterPrefix + testCase.name, - Tags: testCase.tags, - }, + counter := ff.Counter(metrics.Options{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, }) - gauge := ff.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: gaugePrefix + testCase.name, - Tags: testCase.tags, - }, + gauge := ff.Gauge(metrics.Options{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, }) - timer := ff.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: timerPrefix + testCase.name, - Tags: testCase.tags, - }, + timer := ff.Timer(metrics.Options{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, }) // register second time, should not panic - ff.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: counterPrefix + testCase.name, - Tags: testCase.tags, - }, + ff.Counter(metrics.Options{ + Name: counterPrefix + testCase.name, + Tags: testCase.tags, }) - ff.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: gaugePrefix + testCase.name, - Tags: testCase.tags, - }, + ff.Gauge(metrics.Options{ + Name: gaugePrefix + testCase.name, + Tags: testCase.tags, }) - ff.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: timerPrefix + testCase.name, - Tags: testCase.tags, - }, + ff.Timer(metrics.Options{ + Name: timerPrefix + testCase.name, + Tags: testCase.tags, }) counter.Inc(42) diff --git a/metrics/factory.go b/metrics/factory.go index bde005a..e3cae01 100644 --- a/metrics/factory.go +++ b/metrics/factory.go @@ -14,26 +14,27 @@ package metrics -// MetricScope defines the name and tags map associated with a metric -type MetricScope struct { +// Scope defines the name and tags map associated with a metric +type Scope struct { Name string Tags map[string]string } -// MetricInfo defines the information associated with a metric -type MetricInfo struct { - MetricScope +// Options defines the information associated with a metric +type Options struct { + Name string + Tags map[string]string Description string } // Factory creates new metrics type Factory interface { - Counter(metric MetricInfo) Counter - Timer(metric MetricInfo) Timer - Gauge(metric MetricInfo) Gauge + Counter(metric Options) Counter + Timer(metric Options) Timer + Gauge(metric Options) Gauge // Namespace returns a nested metrics factory. - Namespace(metricScope MetricScope) Factory + Namespace(scope Scope) Factory } // NullFactory is a metrics factory that returns NullCounter, NullTimer, and NullGauge. @@ -41,13 +42,13 @@ var NullFactory Factory = nullFactory{} type nullFactory struct{} -func (nullFactory) Counter(metricInfo MetricInfo) Counter { +func (nullFactory) Counter(options Options) Counter { return NullCounter } -func (nullFactory) Timer(metricInfo MetricInfo) Timer { +func (nullFactory) Timer(options Options) Timer { return NullTimer } -func (nullFactory) Gauge(metricInfo MetricInfo) Gauge { +func (nullFactory) Gauge(options Options) Gauge { return NullGauge } -func (nullFactory) Namespace(metricScope MetricScope) Factory { return NullFactory } +func (nullFactory) Namespace(scope Scope) Factory { return NullFactory } diff --git a/metrics/go-kit/factory.go b/metrics/go-kit/factory.go index a2e7bde..f6d8f9e 100644 --- a/metrics/go-kit/factory.go +++ b/metrics/go-kit/factory.go @@ -103,8 +103,8 @@ func (f *factory) nameAndTagsList(nom string, tags map[string]string) (name stri return } -func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { - name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) +func (f *factory) Counter(options metrics.Options) metrics.Counter { + name, tagsList := f.nameAndTagsList(options.Name, options.Tags) counter := f.factory.Counter(name) if len(tagsList) > 0 { counter = counter.With(tagsList...) @@ -112,8 +112,8 @@ func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { return NewCounter(counter) } -func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { - name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) +func (f *factory) Timer(options metrics.Options) metrics.Timer { + name, tagsList := f.nameAndTagsList(options.Name, options.Tags) hist := f.factory.Histogram(name) if len(tagsList) > 0 { hist = hist.With(tagsList...) @@ -121,8 +121,8 @@ func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { return NewTimer(hist) } -func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { - name, tagsList := f.nameAndTagsList(metricInfo.Name, metricInfo.Tags) +func (f *factory) Gauge(options metrics.Options) metrics.Gauge { + name, tagsList := f.nameAndTagsList(options.Name, options.Tags) gauge := f.factory.Gauge(name) if len(tagsList) > 0 { gauge = gauge.With(tagsList...) @@ -130,10 +130,10 @@ func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { return NewGauge(gauge) } -func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { +func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { return &factory{ - scope: f.subScope(metricScope.Name), - tags: f.mergeTags(metricScope.Tags), + scope: f.subScope(scope.Name), + tags: f.mergeTags(scope.Tags), factory: f.factory, scopeSep: f.scopeSep, tagsSep: f.tagsSep, diff --git a/metrics/go-kit/factory_test.go b/metrics/go-kit/factory_test.go index f98d6eb..b16ffa3 100644 --- a/metrics/go-kit/factory_test.go +++ b/metrics/go-kit/factory_test.go @@ -153,7 +153,7 @@ func TestFactoryScoping(t *testing.T) { t.Run(factoryName+"_"+testSuite.metricType+"_"+testCase.expName, func(t *testing.T) { f := Wrap(testCase.prefix, testCase.f, testCase.options...) if testCase.useNamespace { - f = f.Namespace(metrics.MetricScope{ + f = f.Namespace(metrics.Scope{ Name: testCase.namespace, Tags: testCase.namespaceTags, }) @@ -169,11 +169,9 @@ func TestFactoryScoping(t *testing.T) { } func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - c := f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: testCase.name, - Tags: testCase.tags, - }, + c := f.Counter(metrics.Options{ + Name: testCase.name, + Tags: testCase.tags, }) c.Inc(123) kc := c.(*Counter).counter @@ -190,11 +188,9 @@ func testCounter(t *testing.T, testCase testCase, f metrics.Factory) (name func( } func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - g := f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: testCase.name, - Tags: testCase.tags, - }, + g := f.Gauge(metrics.Options{ + Name: testCase.name, + Tags: testCase.tags, }) g.Update(123) gg := g.(*Gauge).gauge.(*generic.Gauge) @@ -205,11 +201,9 @@ func testGauge(t *testing.T, testCase testCase, f metrics.Factory) (name func() } func testTimer(t *testing.T, testCase testCase, f metrics.Factory) (name func() string, labels func() []string) { - tm := f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: testCase.name, - Tags: testCase.tags, - }, + tm := f.Timer(metrics.Options{ + Name: testCase.name, + Tags: testCase.tags, }) tm.Record(123 * time.Millisecond) gt := tm.(*Timer).hist.(*generic.Histogram) diff --git a/metrics/go-kit/influx/factory_test.go b/metrics/go-kit/influx/factory_test.go index 9dcfed5..f3811b1 100644 --- a/metrics/go-kit/influx/factory_test.go +++ b/metrics/go-kit/influx/factory_test.go @@ -20,11 +20,9 @@ func TestCounter(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - c := wf.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gokit.infl-counter", - Tags: map[string]string{"label": "val1"}, - }, + c := wf.Counter(metrics.Options{ + Name: "gokit.infl-counter", + Tags: map[string]string{"label": "val1"}, }) c.Inc(7) @@ -36,11 +34,9 @@ func TestGauge(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - g := wf.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gokit.infl-gauge", - Tags: map[string]string{"x": "y"}, - }, + g := wf.Gauge(metrics.Options{ + Name: "gokit.infl-gauge", + Tags: map[string]string{"x": "y"}, }) g.Update(42) @@ -52,11 +48,9 @@ func TestTimer(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - timer := wf.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gokit.infl-timer", - Tags: map[string]string{"x": "y"}, - }, + timer := wf.Timer(metrics.Options{ + Name: "gokit.infl-timer", + Tags: map[string]string{"x": "y"}, }) timer.Record(time.Second * 1) timer.Record(time.Second * 1) @@ -70,16 +64,14 @@ func TestWrapperNamespaces(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - wf = wf.Namespace(metrics.MetricScope{ + wf = wf.Namespace(metrics.Scope{ Name: "bar", Tags: map[string]string{"bar_tag": "bar_tag"}, }) - c := wf.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gokit.prom-wrapped-counter", - Tags: map[string]string{"x": "y"}, - }, + c := wf.Counter(metrics.Options{ + Name: "gokit.prom-wrapped-counter", + Tags: map[string]string{"x": "y"}, }) c.Inc(42) diff --git a/metrics/metrics.go b/metrics/metrics.go index 0f9e877..82cbcd0 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -74,19 +74,17 @@ func InitOrError(m interface{}, factory Factory, globalTags map[string]string) e } description := field.Tag.Get("description") var obj interface{} - metricInfo := MetricInfo{ - MetricScope: MetricScope{ - Name: metric, - Tags: tags, - }, + options := Options{ + Name: metric, + Tags: tags, Description: description, } if field.Type.AssignableTo(counterPtrType) { - obj = factory.Counter(metricInfo) + obj = factory.Counter(options) } else if field.Type.AssignableTo(gaugePtrType) { - obj = factory.Gauge(metricInfo) + obj = factory.Gauge(options) } else if field.Type.AssignableTo(timerPtrType) { - obj = factory.Timer(metricInfo) + obj = factory.Timer(options) } else { return fmt.Errorf( "Field %s is not a pointer to timer, gauge, or counter", diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 4cbb861..5a36de3 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -86,26 +86,18 @@ func TestInitPanic(t *testing.T) { func TestNullMetrics(t *testing.T) { // This test is just for cover - metrics.NullFactory.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "name", - }, + metrics.NullFactory.Timer(metrics.Options{ + Name: "name", }).Record(0) - metrics.NullFactory.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "name", - }, + metrics.NullFactory.Counter(metrics.Options{ + Name: "name", }).Inc(0) - metrics.NullFactory.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "name", - }, + metrics.NullFactory.Gauge(metrics.Options{ + Name: "name", }).Update(0) - metrics.NullFactory.Namespace(metrics.MetricScope{ + metrics.NullFactory.Namespace(metrics.Scope{ Name: "name", - }).Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "name2", - }, + }).Gauge(metrics.Options{ + Name: "name2", }).Update(0) } diff --git a/metrics/metricstest/local.go b/metrics/metricstest/local.go index cb68dfc..ca1bf23 100644 --- a/metrics/metricstest/local.go +++ b/metrics/metricstest/local.go @@ -279,43 +279,43 @@ func (l *Factory) newNamespace(name string) string { } // Counter returns a local stats counter -func (l *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { +func (l *Factory) Counter(options metrics.Options) metrics.Counter { return &localCounter{ stats{ - name: l.newNamespace(metricInfo.Name), - tags: l.appendTags(metricInfo.Tags), + name: l.newNamespace(options.Name), + tags: l.appendTags(options.Tags), localBackend: l.Backend, }, } } // Timer returns a local stats timer. -func (l *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { +func (l *Factory) Timer(options metrics.Options) metrics.Timer { return &localTimer{ stats{ - name: l.newNamespace(metricInfo.Name), - tags: l.appendTags(metricInfo.Tags), + name: l.newNamespace(options.Name), + tags: l.appendTags(options.Tags), localBackend: l.Backend, }, } } // Gauge returns a local stats gauge. -func (l *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { +func (l *Factory) Gauge(options metrics.Options) metrics.Gauge { return &localGauge{ stats{ - name: l.newNamespace(metricInfo.Name), - tags: l.appendTags(metricInfo.Tags), + name: l.newNamespace(options.Name), + tags: l.appendTags(options.Tags), localBackend: l.Backend, }, } } // Namespace returns a new namespace. -func (l *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { +func (l *Factory) Namespace(scope metrics.Scope) metrics.Factory { return &Factory{ - namespace: l.newNamespace(metricScope.Name), - tags: l.appendTags(metricScope.Tags), + namespace: l.newNamespace(scope.Name), + tags: l.appendTags(scope.Tags), Backend: l.Backend, } } diff --git a/metrics/metricstest/local_test.go b/metrics/metricstest/local_test.go index 52de776..2f497e1 100644 --- a/metrics/metricstest/local_test.go +++ b/metrics/metricstest/local_test.go @@ -16,57 +16,39 @@ func TestLocalMetrics(t *testing.T) { f := NewFactory(0) defer f.Stop() - f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-counter", - Tags: tags, - }, + f.Counter(metrics.Options{ + Name: "my-counter", + Tags: tags, }).Inc(4) - f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-counter", - Tags: tags, - }, + f.Counter(metrics.Options{ + Name: "my-counter", + Tags: tags, }).Inc(6) - f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-counter", - }, + f.Counter(metrics.Options{ + Name: "my-counter", }).Inc(6) - f.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "other-counter", - }, + f.Counter(metrics.Options{ + Name: "other-counter", }).Inc(8) - f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-gauge", - }, + f.Gauge(metrics.Options{ + Name: "my-gauge", }).Update(25) - f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-gauge", - }, + f.Gauge(metrics.Options{ + Name: "my-gauge", }).Update(43) - f.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "other-gauge", - }, + f.Gauge(metrics.Options{ + Name: "other-gauge", }).Update(74) - f.Namespace(metrics.MetricScope{ + f.Namespace(metrics.Scope{ Name: "namespace", Tags: tags, - }).Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "my-counter", - }, + }).Counter(metrics.Options{ + Name: "my-counter", }).Inc(7) - f.Namespace(metrics.MetricScope{ + f.Namespace(metrics.Scope{ Name: "ns.subns", - }).Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Tags: map[string]string{"service": "a-service"}, - }, + }).Counter(metrics.Options{ + Tags: map[string]string{"service": "a-service"}, }).Inc(9) timings := map[string][]time.Duration{ @@ -86,10 +68,8 @@ func TestLocalMetrics(t *testing.T) { for metric, timing := range timings { for _, d := range timing { - f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: metric, - }, + f.Timer(metrics.Options{ + Name: metric, }).Record(d) } } @@ -138,10 +118,8 @@ func TestLocalMetricsInterval(t *testing.T) { f := NewFactory(refreshInterval) defer f.Stop() - f.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "timer", - }, + f.Timer(metrics.Options{ + Name: "timer", }).Record(1) f.tm.Lock() diff --git a/metrics/multi/multi.go b/metrics/multi/multi.go index 267f88f..bf40a2b 100644 --- a/metrics/multi/multi.go +++ b/metrics/multi/multi.go @@ -43,12 +43,12 @@ func (c *counter) Inc(delta int64) { } // Counter implements metrics.Factory interface -func (f *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { +func (f *Factory) Counter(options metrics.Options) metrics.Counter { counter := &counter{ counters: make([]metrics.Counter, len(f.factories)), } for i, factory := range f.factories { - counter.counters[i] = factory.Counter(metricInfo) + counter.counters[i] = factory.Counter(options) } return counter } @@ -64,12 +64,12 @@ func (t *timer) Record(delta time.Duration) { } // Timer implements metrics.Factory interface -func (f *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { +func (f *Factory) Timer(options metrics.Options) metrics.Timer { timer := &timer{ timers: make([]metrics.Timer, len(f.factories)), } for i, factory := range f.factories { - timer.timers[i] = factory.Timer(metricInfo) + timer.timers[i] = factory.Timer(options) } return timer } @@ -85,23 +85,23 @@ func (t *gauge) Update(value int64) { } // Gauge implements metrics.Factory interface -func (f *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { +func (f *Factory) Gauge(options metrics.Options) metrics.Gauge { gauge := &gauge{ gauges: make([]metrics.Gauge, len(f.factories)), } for i, factory := range f.factories { - gauge.gauges[i] = factory.Gauge(metricInfo) + gauge.gauges[i] = factory.Gauge(options) } return gauge } // Namespace implements metrics.Factory interface -func (f *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { +func (f *Factory) Namespace(scope metrics.Scope) metrics.Factory { newFactory := &Factory{ factories: make([]metrics.Factory, len(f.factories)), } for i, factory := range f.factories { - newFactory.factories[i] = factory.Namespace(metricScope) + newFactory.factories[i] = factory.Namespace(scope) } return newFactory } diff --git a/metrics/multi/multi_test.go b/metrics/multi/multi_test.go index 6018fac..c65938a 100644 --- a/metrics/multi/multi_test.go +++ b/metrics/multi/multi_test.go @@ -15,27 +15,21 @@ func TestMultiFactory(t *testing.T) { f1 := metricstest.NewFactory(time.Second) f2 := metricstest.NewFactory(time.Second) multi1 := New(f1, f2) - multi2 := multi1.Namespace(metrics.MetricScope{ + multi2 := multi1.Namespace(metrics.Scope{ Name: "ns2", }) tags := map[string]string{"x": "y"} - multi2.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "counter", - Tags: tags, - }, + multi2.Counter(metrics.Options{ + Name: "counter", + Tags: tags, }).Inc(42) - multi2.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gauge", - Tags: tags, - }, + multi2.Gauge(metrics.Options{ + Name: "gauge", + Tags: tags, }).Update(42) - multi2.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "timer", - Tags: tags, - }, + multi2.Timer(metrics.Options{ + Name: "timer", + Tags: tags, }).Record(42 * time.Millisecond) for _, f := range []*metricstest.Factory{f1, f2} { diff --git a/metrics/prometheus/factory.go b/metrics/prometheus/factory.go index 5e1467b..a544574 100644 --- a/metrics/prometheus/factory.go +++ b/metrics/prometheus/factory.go @@ -125,13 +125,13 @@ func newFactory(parent *Factory, scope string, tags map[string]string) *Factory } // Counter implements Counter of metrics.Factory. -func (f *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { - description := strings.TrimSpace(metricInfo.Description) +func (f *Factory) Counter(options metrics.Options) metrics.Counter { + description := strings.TrimSpace(options.Description) if len(description) == 0 { - description = metricInfo.Name + description = options.Name } - name := counterNamingConvention(f.subScope(metricInfo.Name)) - tags := f.mergeTags(metricInfo.Tags) + name := counterNamingConvention(f.subScope(options.Name)) + tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.CounterOpts{ Name: name, @@ -144,13 +144,13 @@ func (f *Factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { } // Gauge implements Gauge of metrics.Factory. -func (f *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { - description := strings.TrimSpace(metricInfo.Description) +func (f *Factory) Gauge(options metrics.Options) metrics.Gauge { + description := strings.TrimSpace(options.Description) if len(description) == 0 { - description = metricInfo.Name + description = options.Name } - name := f.subScope(metricInfo.Name) - tags := f.mergeTags(metricInfo.Tags) + name := f.subScope(options.Name) + tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.GaugeOpts{ Name: name, @@ -163,13 +163,13 @@ func (f *Factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { } // Timer implements Timer of metrics.Factory. -func (f *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { - description := strings.TrimSpace(metricInfo.Description) +func (f *Factory) Timer(options metrics.Options) metrics.Timer { + description := strings.TrimSpace(options.Description) if len(description) == 0 { - description = metricInfo.Name + description = options.Name } - name := f.subScope(metricInfo.Name) - tags := f.mergeTags(metricInfo.Tags) + name := f.subScope(options.Name) + tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.HistogramOpts{ Name: name, @@ -183,8 +183,8 @@ func (f *Factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { } // Namespace implements Namespace of metrics.Factory. -func (f *Factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { - return newFactory(f, f.subScope(metricScope.Name), f.mergeTags(metricScope.Tags)) +func (f *Factory) Namespace(scope metrics.Scope) metrics.Factory { + return newFactory(f, f.subScope(scope.Name), f.mergeTags(scope.Tags)) } type counter struct { diff --git a/metrics/prometheus/factory_test.go b/metrics/prometheus/factory_test.go index 4e96600..66569e5 100644 --- a/metrics/prometheus/factory_test.go +++ b/metrics/prometheus/factory_test.go @@ -37,13 +37,11 @@ func TestOptions(t *testing.T) { func TestSeparator(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithSeparator(SeparatorColon)) - c1 := f1.Namespace(metrics.MetricScope{ + c1 := f1.Namespace(metrics.Scope{ Name: "bender", - }).Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"a": "b"}, - }, + }).Counter(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"a": "b"}, Description: "Help message", }) c1.Inc(1) @@ -56,32 +54,26 @@ func TestSeparator(t *testing.T) { func TestCounter(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - fDummy := f1.Namespace(metrics.MetricScope{}) - f2 := fDummy.Namespace(metrics.MetricScope{ + fDummy := f1.Namespace(metrics.Scope{}) + f2 := fDummy.Namespace(metrics.Scope{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.MetricScope{}) + f3 := f2.Namespace(metrics.Scope{}) - c1 := f2.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + c1 := f2.Counter(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, Description: "Help message", }) - c2 := f2.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + c2 := f2.Counter(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) - c3 := f3.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + c3 := f3.Counter(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) // same tags as c2, but from f3 c1.Inc(1) @@ -104,11 +96,9 @@ func TestCounter(t *testing.T) { func TestCounterDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - c1 := f1.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + c1 := f1.Counter(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, }) c1.Inc(1) @@ -121,32 +111,26 @@ func TestCounterDefaultHelp(t *testing.T) { func TestGauge(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace(metrics.MetricScope{ + f2 := f1.Namespace(metrics.Scope{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.MetricScope{ + f3 := f2.Namespace(metrics.Scope{ Tags: map[string]string{"a": "b"}, }) // essentially same as f2 - g1 := f2.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + g1 := f2.Gauge(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, Description: "Help message", }) - g2 := f2.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + g2 := f2.Gauge(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) - g3 := f3.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + g3 := f3.Gauge(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) // same as g2, but from f3 g1.Update(1) @@ -169,11 +153,9 @@ func TestGauge(t *testing.T) { func TestGaugeDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - g1 := f1.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + g1 := f1.Gauge(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, }) g1.Update(1) @@ -186,32 +168,26 @@ func TestGaugeDefaultHelp(t *testing.T) { func TestTimer(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace(metrics.MetricScope{ + f2 := f1.Namespace(metrics.Scope{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.MetricScope{ + f3 := f2.Namespace(metrics.Scope{ Tags: map[string]string{"a": "b"}, }) // essentially same as f2 - t1 := f2.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + t1 := f2.Timer(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, Description: "Help message", }) - t2 := f2.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + t2 := f2.Timer(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) - t3 := f3.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - }, + t3 := f3.Timer(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, Description: "Help message", }) // same as t2, but from f3 t1.Record(1 * time.Second) @@ -254,11 +230,9 @@ func TestTimer(t *testing.T) { func TestTimerDefaultHelp(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - t1 := f1.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - }, + t1 := f1.Timer(metrics.Options{ + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, }) t1.Record(1 * time.Second) @@ -272,11 +246,9 @@ func TestTimerCustomBuckets(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithBuckets([]float64{1.5})) // dot and dash in the metric name will be replaced with underscore - t1 := f1.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "bender.bending-rodriguez", - Tags: map[string]string{"x": "y"}, - }, + t1 := f1.Timer(metrics.Options{ + Name: "bender.bending-rodriguez", + Tags: map[string]string{"x": "y"}, }) t1.Record(1 * time.Second) t1.Record(2 * time.Second) diff --git a/metrics/tally/factory.go b/metrics/tally/factory.go index 3e64e5f..e5daae9 100644 --- a/metrics/tally/factory.go +++ b/metrics/tally/factory.go @@ -32,32 +32,32 @@ type factory struct { tally tally.Scope } -func (f *factory) Counter(metricInfo metrics.MetricInfo) metrics.Counter { +func (f *factory) Counter(options metrics.Options) metrics.Counter { scope := f.tally - if len(metricInfo.Tags) > 0 { - scope = scope.Tagged(metricInfo.Tags) + if len(options.Tags) > 0 { + scope = scope.Tagged(options.Tags) } - return NewCounter(scope.Counter(metricInfo.Name)) + return NewCounter(scope.Counter(options.Name)) } -func (f *factory) Gauge(metricInfo metrics.MetricInfo) metrics.Gauge { +func (f *factory) Gauge(options metrics.Options) metrics.Gauge { scope := f.tally - if len(metricInfo.Tags) > 0 { - scope = scope.Tagged(metricInfo.Tags) + if len(options.Tags) > 0 { + scope = scope.Tagged(options.Tags) } - return NewGauge(scope.Gauge(metricInfo.Name)) + return NewGauge(scope.Gauge(options.Name)) } -func (f *factory) Timer(metricInfo metrics.MetricInfo) metrics.Timer { +func (f *factory) Timer(options metrics.Options) metrics.Timer { scope := f.tally - if len(metricInfo.Tags) > 0 { - scope = scope.Tagged(metricInfo.Tags) + if len(options.Tags) > 0 { + scope = scope.Tagged(options.Tags) } - return NewTimer(scope.Timer(metricInfo.Name)) + return NewTimer(scope.Timer(options.Name)) } -func (f *factory) Namespace(metricScope metrics.MetricScope) metrics.Factory { +func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { return &factory{ - tally: f.tally.SubScope(metricScope.Name).Tagged(metricScope.Tags), + tally: f.tally.SubScope(scope.Name).Tagged(scope.Tags), } } diff --git a/metrics/tally/factory_test.go b/metrics/tally/factory_test.go index 39f866b..5ca9ab2 100644 --- a/metrics/tally/factory_test.go +++ b/metrics/tally/factory_test.go @@ -12,29 +12,23 @@ import ( func TestFactory(t *testing.T) { testScope := tally.NewTestScope("pre", map[string]string{"a": "b"}) - factory := Wrap(testScope).Namespace(metrics.MetricScope{ + factory := Wrap(testScope).Namespace(metrics.Scope{ Name: "fix", Tags: map[string]string{"c": "d"}, }) - counter := factory.Counter(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "counter", - Tags: map[string]string{"x": "y"}, - }, + counter := factory.Counter(metrics.Options{ + Name: "counter", + Tags: map[string]string{"x": "y"}, }) counter.Inc(42) - gauge := factory.Gauge(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "gauge", - Tags: map[string]string{"x": "y"}, - }, + gauge := factory.Gauge(metrics.Options{ + Name: "gauge", + Tags: map[string]string{"x": "y"}, }) gauge.Update(42) - timer := factory.Timer(metrics.MetricInfo{ - MetricScope: metrics.MetricScope{ - Name: "timer", - Tags: map[string]string{"x": "y"}, - }, + timer := factory.Timer(metrics.Options{ + Name: "timer", + Tags: map[string]string{"x": "y"}, }) timer.Record(42 * time.Millisecond) snapshot := testScope.Snapshot() From 7c07d8197abada799d59ea8dbd66bb2085e2951d Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Sat, 1 Dec 2018 10:43:47 +0000 Subject: [PATCH 4/5] Rename description to help Signed-off-by: Gary Brown --- metrics/adapters/factory.go | 12 +++--- metrics/adapters/factory_test.go | 18 ++++----- metrics/adapters/tagless.go | 18 ++++----- metrics/expvar/factory.go | 6 +-- metrics/factory.go | 6 +-- metrics/metrics.go | 8 ++-- metrics/prometheus/factory.go | 24 ++++++------ metrics/prometheus/factory_test.go | 60 +++++++++++++++--------------- 8 files changed, 76 insertions(+), 76 deletions(-) diff --git a/metrics/adapters/factory.go b/metrics/adapters/factory.go index 7f4925d..93b90dc 100644 --- a/metrics/adapters/factory.go +++ b/metrics/adapters/factory.go @@ -20,9 +20,9 @@ import ( // FactoryWithTags creates metrics with fully qualified name and tags. type FactoryWithTags interface { - Counter(name string, tags map[string]string, description string) metrics.Counter - Gauge(name string, tags map[string]string, description string) metrics.Gauge - Timer(name string, tags map[string]string, description string) metrics.Timer + Counter(name string, tags map[string]string, help string) metrics.Counter + Gauge(name string, tags map[string]string, help string) metrics.Gauge + Timer(name string, tags map[string]string, help string) metrics.Timer } // Options affect how the adapter factory behaves. @@ -66,21 +66,21 @@ type factory struct { func (f *factory) Counter(options metrics.Options) metrics.Counter { fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetCounter(key, func() metrics.Counter { - return f.factory.Counter(fullName, fullTags, options.Description) + return f.factory.Counter(fullName, fullTags, options.Help) }) } func (f *factory) Gauge(options metrics.Options) metrics.Gauge { fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetGauge(key, func() metrics.Gauge { - return f.factory.Gauge(fullName, fullTags, options.Description) + return f.factory.Gauge(fullName, fullTags, options.Help) }) } func (f *factory) Timer(options metrics.Options) metrics.Timer { fullName, fullTags, key := f.getKey(options.Name, options.Tags) return f.cache.getOrSetTimer(key, func() metrics.Timer { - return f.factory.Timer(fullName, fullTags, options.Description) + return f.factory.Timer(fullName, fullTags, options.Help) }) } diff --git a/metrics/adapters/factory_test.go b/metrics/adapters/factory_test.go index 03b1577..916e74a 100644 --- a/metrics/adapters/factory_test.go +++ b/metrics/adapters/factory_test.go @@ -125,26 +125,26 @@ type fakeTagless struct { timer string } -func (f *fakeTagless) Counter(name string, description string) metrics.Counter { +func (f *fakeTagless) Counter(name string, help string) metrics.Counter { f.counter = name return f.factory.Counter(metrics.Options{ - Name: name, - Description: description, + Name: name, + Help: help, }) } -func (f *fakeTagless) Gauge(name string, description string) metrics.Gauge { +func (f *fakeTagless) Gauge(name string, help string) metrics.Gauge { f.gauge = name return f.factory.Gauge(metrics.Options{ - Name: name, - Description: description, + Name: name, + Help: help, }) } -func (f *fakeTagless) Timer(name string, description string) metrics.Timer { +func (f *fakeTagless) Timer(name string, help string) metrics.Timer { f.timer = name return f.factory.Timer(metrics.Options{ - Name: name, - Description: description, + Name: name, + Help: help, }) } diff --git a/metrics/adapters/tagless.go b/metrics/adapters/tagless.go index feae5a7..d9552c7 100644 --- a/metrics/adapters/tagless.go +++ b/metrics/adapters/tagless.go @@ -19,9 +19,9 @@ import "github.com/uber/jaeger-lib/metrics" // FactoryWithoutTags creates metrics based on name only, without tags. // Suitable for integrating with statsd-like backends that don't support tags. type FactoryWithoutTags interface { - Counter(name string, description string) metrics.Counter - Gauge(name string, description string) metrics.Gauge - Timer(name string, description string) metrics.Timer + Counter(name string, help string) metrics.Counter + Gauge(name string, help string) metrics.Gauge + Timer(name string, help string) metrics.Timer } // WrapFactoryWithoutTags creates a real metrics.Factory that supports subscopes. @@ -41,19 +41,19 @@ type tagless struct { factory FactoryWithoutTags } -func (f *tagless) Counter(name string, tags map[string]string, description string) metrics.Counter { +func (f *tagless) Counter(name string, tags map[string]string, help string) metrics.Counter { fullName := f.getFullName(name, tags) - return f.factory.Counter(fullName, description) + return f.factory.Counter(fullName, help) } -func (f *tagless) Gauge(name string, tags map[string]string, description string) metrics.Gauge { +func (f *tagless) Gauge(name string, tags map[string]string, help string) metrics.Gauge { fullName := f.getFullName(name, tags) - return f.factory.Gauge(fullName, description) + return f.factory.Gauge(fullName, help) } -func (f *tagless) Timer(name string, tags map[string]string, description string) metrics.Timer { +func (f *tagless) Timer(name string, tags map[string]string, help string) metrics.Timer { fullName := f.getFullName(name, tags) - return f.factory.Timer(fullName, description) + return f.factory.Timer(fullName, help) } func (f *tagless) getFullName(name string, tags map[string]string) string { diff --git a/metrics/expvar/factory.go b/metrics/expvar/factory.go index 6dce399..6b42d78 100644 --- a/metrics/expvar/factory.go +++ b/metrics/expvar/factory.go @@ -36,14 +36,14 @@ type factory struct { factory xkit.Factory } -func (f *factory) Counter(name string, description string) metrics.Counter { +func (f *factory) Counter(name string, help string) metrics.Counter { return xkit.NewCounter(f.factory.Counter(name)) } -func (f *factory) Gauge(name string, description string) metrics.Gauge { +func (f *factory) Gauge(name string, help string) metrics.Gauge { return xkit.NewGauge(f.factory.Gauge(name)) } -func (f *factory) Timer(name string, description string) metrics.Timer { +func (f *factory) Timer(name string, help string) metrics.Timer { return xkit.NewTimer(f.factory.Histogram(name)) } diff --git a/metrics/factory.go b/metrics/factory.go index e3cae01..184f493 100644 --- a/metrics/factory.go +++ b/metrics/factory.go @@ -22,9 +22,9 @@ type Scope struct { // Options defines the information associated with a metric type Options struct { - Name string - Tags map[string]string - Description string + Name string + Tags map[string]string + Help string } // Factory creates new metrics diff --git a/metrics/metrics.go b/metrics/metrics.go index 82cbcd0..fdea7e2 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -72,12 +72,12 @@ func InitOrError(m interface{}, factory Factory, globalTags map[string]string) e tags[tag[0]] = tag[1] } } - description := field.Tag.Get("description") + help := field.Tag.Get("help") var obj interface{} options := Options{ - Name: metric, - Tags: tags, - Description: description, + Name: metric, + Tags: tags, + Help: help, } if field.Type.AssignableTo(counterPtrType) { obj = factory.Counter(options) diff --git a/metrics/prometheus/factory.go b/metrics/prometheus/factory.go index a544574..7044c12 100644 --- a/metrics/prometheus/factory.go +++ b/metrics/prometheus/factory.go @@ -126,16 +126,16 @@ func newFactory(parent *Factory, scope string, tags map[string]string) *Factory // Counter implements Counter of metrics.Factory. func (f *Factory) Counter(options metrics.Options) metrics.Counter { - description := strings.TrimSpace(options.Description) - if len(description) == 0 { - description = options.Name + help := strings.TrimSpace(options.Help) + if len(help) == 0 { + help = options.Name } name := counterNamingConvention(f.subScope(options.Name)) tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.CounterOpts{ Name: name, - Help: description, + Help: help, } cv := f.cache.getOrMakeCounterVec(opts, labelNames) return &counter{ @@ -145,16 +145,16 @@ func (f *Factory) Counter(options metrics.Options) metrics.Counter { // Gauge implements Gauge of metrics.Factory. func (f *Factory) Gauge(options metrics.Options) metrics.Gauge { - description := strings.TrimSpace(options.Description) - if len(description) == 0 { - description = options.Name + help := strings.TrimSpace(options.Help) + if len(help) == 0 { + help = options.Name } name := f.subScope(options.Name) tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.GaugeOpts{ Name: name, - Help: description, + Help: help, } gv := f.cache.getOrMakeGaugeVec(opts, labelNames) return &gauge{ @@ -164,16 +164,16 @@ func (f *Factory) Gauge(options metrics.Options) metrics.Gauge { // Timer implements Timer of metrics.Factory. func (f *Factory) Timer(options metrics.Options) metrics.Timer { - description := strings.TrimSpace(options.Description) - if len(description) == 0 { - description = options.Name + help := strings.TrimSpace(options.Help) + if len(help) == 0 { + help = options.Name } name := f.subScope(options.Name) tags := f.mergeTags(options.Tags) labelNames := f.tagNames(tags) opts := prometheus.HistogramOpts{ Name: name, - Help: description, + Help: help, Buckets: f.buckets, } hv := f.cache.getOrMakeHistogramVec(opts, labelNames) diff --git a/metrics/prometheus/factory_test.go b/metrics/prometheus/factory_test.go index 66569e5..93207b4 100644 --- a/metrics/prometheus/factory_test.go +++ b/metrics/prometheus/factory_test.go @@ -40,9 +40,9 @@ func TestSeparator(t *testing.T) { c1 := f1.Namespace(metrics.Scope{ Name: "bender", }).Counter(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"a": "b"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"a": "b"}, + Help: "Help message", }) c1.Inc(1) snapshot, err := registry.Gather() @@ -62,19 +62,19 @@ func TestCounter(t *testing.T) { f3 := f2.Namespace(metrics.Scope{}) c1 := f2.Counter(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + Help: "Help message", }) c2 := f2.Counter(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) c3 := f3.Counter(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) // same tags as c2, but from f3 c1.Inc(1) c1.Inc(2) @@ -119,19 +119,19 @@ func TestGauge(t *testing.T) { Tags: map[string]string{"a": "b"}, }) // essentially same as f2 g1 := f2.Gauge(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + Help: "Help message", }) g2 := f2.Gauge(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) g3 := f3.Gauge(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) // same as g2, but from f3 g1.Update(1) g1.Update(2) @@ -176,19 +176,19 @@ func TestTimer(t *testing.T) { Tags: map[string]string{"a": "b"}, }) // essentially same as f2 t1 := f2.Timer(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "y"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "y"}, + Help: "Help message", }) t2 := f2.Timer(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) t3 := f3.Timer(metrics.Options{ - Name: "rodriguez", - Tags: map[string]string{"x": "z"}, - Description: "Help message", + Name: "rodriguez", + Tags: map[string]string{"x": "z"}, + Help: "Help message", }) // same as t2, but from f3 t1.Record(1 * time.Second) t1.Record(2 * time.Second) From 2dde29079bbcd190c274da6a6468846b2c278b91 Mon Sep 17 00:00:00 2001 From: Gary Brown Date: Sun, 2 Dec 2018 10:22:58 +0000 Subject: [PATCH 5/5] Fix multiline and change Scope to NSOptions Signed-off-by: Gary Brown --- metrics/adapters/cache_test.go | 12 +++--------- metrics/adapters/factory.go | 2 +- metrics/adapters/factory_test.go | 2 +- metrics/expvar/factory_test.go | 2 +- metrics/factory.go | 8 ++++---- metrics/go-kit/factory.go | 2 +- metrics/go-kit/factory_test.go | 2 +- metrics/go-kit/influx/factory_test.go | 2 +- metrics/metrics_test.go | 2 +- metrics/metricstest/local.go | 2 +- metrics/metricstest/local_test.go | 4 ++-- metrics/multi/multi.go | 2 +- metrics/multi/multi_test.go | 2 +- metrics/prometheus/factory.go | 2 +- metrics/prometheus/factory_test.go | 16 ++++++++-------- metrics/tally/factory.go | 2 +- metrics/tally/factory_test.go | 2 +- 17 files changed, 30 insertions(+), 36 deletions(-) diff --git a/metrics/adapters/cache_test.go b/metrics/adapters/cache_test.go index 5f3401d..dc79a66 100644 --- a/metrics/adapters/cache_test.go +++ b/metrics/adapters/cache_test.go @@ -26,15 +26,9 @@ import ( func TestCache(t *testing.T) { f := metricstest.NewFactory(100 * time.Second) - c1 := f.Counter(metrics.Options{ - Name: "x", - }) - g1 := f.Gauge(metrics.Options{ - Name: "y", - }) - t1 := f.Timer(metrics.Options{ - Name: "z", - }) + c1 := f.Counter(metrics.Options{Name: "x"}) + g1 := f.Gauge(metrics.Options{Name: "y"}) + t1 := f.Timer(metrics.Options{Name: "z"}) c := newCache() diff --git a/metrics/adapters/factory.go b/metrics/adapters/factory.go index 93b90dc..e054fa2 100644 --- a/metrics/adapters/factory.go +++ b/metrics/adapters/factory.go @@ -84,7 +84,7 @@ func (f *factory) Timer(options metrics.Options) metrics.Timer { }) } -func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { +func (f *factory) Namespace(scope metrics.NSOptions) metrics.Factory { return &factory{ cache: f.cache, scope: f.subScope(scope.Name), diff --git a/metrics/adapters/factory_test.go b/metrics/adapters/factory_test.go index 916e74a..89f9156 100644 --- a/metrics/adapters/factory_test.go +++ b/metrics/adapters/factory_test.go @@ -80,7 +80,7 @@ func TestFactory(t *testing.T) { ff := &fakeTagless{factory: local} f := WrapFactoryWithoutTags(ff, Options{}) if testCase.namespace != "" || testCase.nsTags != nil { - f = f.Namespace(metrics.Scope{ + f = f.Namespace(metrics.NSOptions{ Name: testCase.namespace, Tags: testCase.nsTags, }) diff --git a/metrics/expvar/factory_test.go b/metrics/expvar/factory_test.go index 9eca488..ae6650b 100644 --- a/metrics/expvar/factory_test.go +++ b/metrics/expvar/factory_test.go @@ -64,7 +64,7 @@ func TestFactory(t *testing.T) { } ff := f if testCase.namespace != "" || testCase.nsTags != nil { - ff = f.Namespace(metrics.Scope{ + ff = f.Namespace(metrics.NSOptions{ Name: testCase.namespace, Tags: testCase.nsTags, }) diff --git a/metrics/factory.go b/metrics/factory.go index 184f493..7cca820 100644 --- a/metrics/factory.go +++ b/metrics/factory.go @@ -14,8 +14,8 @@ package metrics -// Scope defines the name and tags map associated with a metric -type Scope struct { +// NSOptions defines the name and tags map associated with a metric +type NSOptions struct { Name string Tags map[string]string } @@ -34,7 +34,7 @@ type Factory interface { Gauge(metric Options) Gauge // Namespace returns a nested metrics factory. - Namespace(scope Scope) Factory + Namespace(scope NSOptions) Factory } // NullFactory is a metrics factory that returns NullCounter, NullTimer, and NullGauge. @@ -51,4 +51,4 @@ func (nullFactory) Timer(options Options) Timer { func (nullFactory) Gauge(options Options) Gauge { return NullGauge } -func (nullFactory) Namespace(scope Scope) Factory { return NullFactory } +func (nullFactory) Namespace(scope NSOptions) Factory { return NullFactory } diff --git a/metrics/go-kit/factory.go b/metrics/go-kit/factory.go index f6d8f9e..30aa5c2 100644 --- a/metrics/go-kit/factory.go +++ b/metrics/go-kit/factory.go @@ -130,7 +130,7 @@ func (f *factory) Gauge(options metrics.Options) metrics.Gauge { return NewGauge(gauge) } -func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { +func (f *factory) Namespace(scope metrics.NSOptions) metrics.Factory { return &factory{ scope: f.subScope(scope.Name), tags: f.mergeTags(scope.Tags), diff --git a/metrics/go-kit/factory_test.go b/metrics/go-kit/factory_test.go index b16ffa3..dcc5bd6 100644 --- a/metrics/go-kit/factory_test.go +++ b/metrics/go-kit/factory_test.go @@ -153,7 +153,7 @@ func TestFactoryScoping(t *testing.T) { t.Run(factoryName+"_"+testSuite.metricType+"_"+testCase.expName, func(t *testing.T) { f := Wrap(testCase.prefix, testCase.f, testCase.options...) if testCase.useNamespace { - f = f.Namespace(metrics.Scope{ + f = f.Namespace(metrics.NSOptions{ Name: testCase.namespace, Tags: testCase.namespaceTags, }) diff --git a/metrics/go-kit/influx/factory_test.go b/metrics/go-kit/influx/factory_test.go index f3811b1..3f29424 100644 --- a/metrics/go-kit/influx/factory_test.go +++ b/metrics/go-kit/influx/factory_test.go @@ -64,7 +64,7 @@ func TestWrapperNamespaces(t *testing.T) { inf := NewFactory(in) wf := xkit.Wrap("namespace", inf) - wf = wf.Namespace(metrics.Scope{ + wf = wf.Namespace(metrics.NSOptions{ Name: "bar", Tags: map[string]string{"bar_tag": "bar_tag"}, }) diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 5a36de3..f27f056 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -95,7 +95,7 @@ func TestNullMetrics(t *testing.T) { metrics.NullFactory.Gauge(metrics.Options{ Name: "name", }).Update(0) - metrics.NullFactory.Namespace(metrics.Scope{ + metrics.NullFactory.Namespace(metrics.NSOptions{ Name: "name", }).Gauge(metrics.Options{ Name: "name2", diff --git a/metrics/metricstest/local.go b/metrics/metricstest/local.go index ca1bf23..ea543a7 100644 --- a/metrics/metricstest/local.go +++ b/metrics/metricstest/local.go @@ -312,7 +312,7 @@ func (l *Factory) Gauge(options metrics.Options) metrics.Gauge { } // Namespace returns a new namespace. -func (l *Factory) Namespace(scope metrics.Scope) metrics.Factory { +func (l *Factory) Namespace(scope metrics.NSOptions) metrics.Factory { return &Factory{ namespace: l.newNamespace(scope.Name), tags: l.appendTags(scope.Tags), diff --git a/metrics/metricstest/local_test.go b/metrics/metricstest/local_test.go index 2f497e1..9aa73ef 100644 --- a/metrics/metricstest/local_test.go +++ b/metrics/metricstest/local_test.go @@ -39,13 +39,13 @@ func TestLocalMetrics(t *testing.T) { f.Gauge(metrics.Options{ Name: "other-gauge", }).Update(74) - f.Namespace(metrics.Scope{ + f.Namespace(metrics.NSOptions{ Name: "namespace", Tags: tags, }).Counter(metrics.Options{ Name: "my-counter", }).Inc(7) - f.Namespace(metrics.Scope{ + f.Namespace(metrics.NSOptions{ Name: "ns.subns", }).Counter(metrics.Options{ Tags: map[string]string{"service": "a-service"}, diff --git a/metrics/multi/multi.go b/metrics/multi/multi.go index bf40a2b..aa84d29 100644 --- a/metrics/multi/multi.go +++ b/metrics/multi/multi.go @@ -96,7 +96,7 @@ func (f *Factory) Gauge(options metrics.Options) metrics.Gauge { } // Namespace implements metrics.Factory interface -func (f *Factory) Namespace(scope metrics.Scope) metrics.Factory { +func (f *Factory) Namespace(scope metrics.NSOptions) metrics.Factory { newFactory := &Factory{ factories: make([]metrics.Factory, len(f.factories)), } diff --git a/metrics/multi/multi_test.go b/metrics/multi/multi_test.go index c65938a..923fc9d 100644 --- a/metrics/multi/multi_test.go +++ b/metrics/multi/multi_test.go @@ -15,7 +15,7 @@ func TestMultiFactory(t *testing.T) { f1 := metricstest.NewFactory(time.Second) f2 := metricstest.NewFactory(time.Second) multi1 := New(f1, f2) - multi2 := multi1.Namespace(metrics.Scope{ + multi2 := multi1.Namespace(metrics.NSOptions{ Name: "ns2", }) tags := map[string]string{"x": "y"} diff --git a/metrics/prometheus/factory.go b/metrics/prometheus/factory.go index 7044c12..7641361 100644 --- a/metrics/prometheus/factory.go +++ b/metrics/prometheus/factory.go @@ -183,7 +183,7 @@ func (f *Factory) Timer(options metrics.Options) metrics.Timer { } // Namespace implements Namespace of metrics.Factory. -func (f *Factory) Namespace(scope metrics.Scope) metrics.Factory { +func (f *Factory) Namespace(scope metrics.NSOptions) metrics.Factory { return newFactory(f, f.subScope(scope.Name), f.mergeTags(scope.Tags)) } diff --git a/metrics/prometheus/factory_test.go b/metrics/prometheus/factory_test.go index 93207b4..9615f1a 100644 --- a/metrics/prometheus/factory_test.go +++ b/metrics/prometheus/factory_test.go @@ -37,7 +37,7 @@ func TestOptions(t *testing.T) { func TestSeparator(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry), WithSeparator(SeparatorColon)) - c1 := f1.Namespace(metrics.Scope{ + c1 := f1.Namespace(metrics.NSOptions{ Name: "bender", }).Counter(metrics.Options{ Name: "rodriguez", @@ -54,12 +54,12 @@ func TestSeparator(t *testing.T) { func TestCounter(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - fDummy := f1.Namespace(metrics.Scope{}) - f2 := fDummy.Namespace(metrics.Scope{ + fDummy := f1.Namespace(metrics.NSOptions{}) + f2 := fDummy.Namespace(metrics.NSOptions{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.Scope{}) + f3 := f2.Namespace(metrics.NSOptions{}) c1 := f2.Counter(metrics.Options{ Name: "rodriguez", @@ -111,11 +111,11 @@ func TestCounterDefaultHelp(t *testing.T) { func TestGauge(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace(metrics.Scope{ + f2 := f1.Namespace(metrics.NSOptions{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.Scope{ + f3 := f2.Namespace(metrics.NSOptions{ Tags: map[string]string{"a": "b"}, }) // essentially same as f2 g1 := f2.Gauge(metrics.Options{ @@ -168,11 +168,11 @@ func TestGaugeDefaultHelp(t *testing.T) { func TestTimer(t *testing.T) { registry := prometheus.NewPedanticRegistry() f1 := New(WithRegisterer(registry)) - f2 := f1.Namespace(metrics.Scope{ + f2 := f1.Namespace(metrics.NSOptions{ Name: "bender", Tags: map[string]string{"a": "b"}, }) - f3 := f2.Namespace(metrics.Scope{ + f3 := f2.Namespace(metrics.NSOptions{ Tags: map[string]string{"a": "b"}, }) // essentially same as f2 t1 := f2.Timer(metrics.Options{ diff --git a/metrics/tally/factory.go b/metrics/tally/factory.go index e5daae9..b66f987 100644 --- a/metrics/tally/factory.go +++ b/metrics/tally/factory.go @@ -56,7 +56,7 @@ func (f *factory) Timer(options metrics.Options) metrics.Timer { return NewTimer(scope.Timer(options.Name)) } -func (f *factory) Namespace(scope metrics.Scope) metrics.Factory { +func (f *factory) Namespace(scope metrics.NSOptions) metrics.Factory { return &factory{ tally: f.tally.SubScope(scope.Name).Tagged(scope.Tags), } diff --git a/metrics/tally/factory_test.go b/metrics/tally/factory_test.go index 5ca9ab2..b21b8fc 100644 --- a/metrics/tally/factory_test.go +++ b/metrics/tally/factory_test.go @@ -12,7 +12,7 @@ import ( func TestFactory(t *testing.T) { testScope := tally.NewTestScope("pre", map[string]string{"a": "b"}) - factory := Wrap(testScope).Namespace(metrics.Scope{ + factory := Wrap(testScope).Namespace(metrics.NSOptions{ Name: "fix", Tags: map[string]string{"c": "d"}, })