Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement expvar metrics to be tolerant of duplicates #40

Merged
merged 4 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions metrics/adapters/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adapters

import (
"sync"

"github.com/uber/jaeger-lib/metrics"
)

type cache struct {
lock sync.Mutex
counters map[string]metrics.Counter
gauges map[string]metrics.Gauge
timers map[string]metrics.Timer
}

func newCache() *cache {
return &cache{
counters: make(map[string]metrics.Counter),
gauges: make(map[string]metrics.Gauge),
timers: make(map[string]metrics.Timer),
}
}

func (r *cache) getOrSetCounter(name string, create func() metrics.Counter) metrics.Counter {
r.lock.Lock()
defer r.lock.Unlock()
c, ok := r.counters[name]
if !ok {
c = create()
r.counters[name] = c
}
return c
}

func (r *cache) getOrSetGauge(name string, create func() metrics.Gauge) metrics.Gauge {
r.lock.Lock()
defer r.lock.Unlock()
g, ok := r.gauges[name]
if !ok {
g = create()
r.gauges[name] = g
}
return g
}

func (r *cache) getOrSetTimer(name string, create func() metrics.Timer) metrics.Timer {
r.lock.Lock()
defer r.lock.Unlock()
t, ok := r.timers[name]
if !ok {
t = create()
r.timers[name] = t
}
return t
}
46 changes: 46 additions & 0 deletions metrics/adapters/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adapters

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/uber/jaeger-lib/metrics"
)

func TestCache(t *testing.T) {
f := metrics.NewLocalFactory(100 * time.Second)
c1 := f.Counter("x", nil)
g1 := f.Gauge("y", nil)
t1 := f.Timer("z", nil)

c := newCache()

c2 := c.getOrSetCounter("x", func() metrics.Counter { return c1 })
assert.Equal(t, c1, c2)
g2 := c.getOrSetGauge("y", func() metrics.Gauge { return g1 })
assert.Equal(t, g1, g2)
t2 := c.getOrSetTimer("z", func() metrics.Timer { return t1 })
assert.Equal(t, t1, t2)

c3 := c.getOrSetCounter("x", func() metrics.Counter { panic("c1") })
assert.Equal(t, c1, c3)
g3 := c.getOrSetGauge("y", func() metrics.Gauge { panic("g1") })
assert.Equal(t, g1, g3)
t3 := c.getOrSetTimer("z", func() metrics.Timer { panic("t1") })
assert.Equal(t, t1, t3)
}
123 changes: 123 additions & 0 deletions metrics/adapters/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adapters

import (
"github.com/uber/jaeger-lib/metrics"
)

// 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
}

// Options affect how the adapter factory behaves.
type Options struct {
ScopeSep string
TagsSep string
TagKVSep string
}

func defaultOptions(options Options) Options {
o := options
if o.ScopeSep == "" {
o.ScopeSep = "."
}
if o.TagsSep == "" {
o.TagsSep = "."
}
if o.TagKVSep == "" {
o.TagKVSep = "_"
}
return o
}

// WrapFactoryWithTags creates a real metrics.Factory that supports subscopes.
func WrapFactoryWithTags(f FactoryWithTags, options Options) metrics.Factory {
return &factory{
Options: defaultOptions(options),
factory: f,
cache: newCache(),
}
}

type factory struct {
Options
factory FactoryWithTags
scope string
tags map[string]string
cache *cache
}

func (f *factory) Counter(name string, tags map[string]string) metrics.Counter {
fullName, fullTags, key := f.getKey(name, tags)
return f.cache.getOrSetCounter(key, func() metrics.Counter {
return f.factory.Counter(fullName, fullTags)
})
}

func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge {
fullName, fullTags, key := f.getKey(name, tags)
return f.cache.getOrSetGauge(key, func() metrics.Gauge {
return f.factory.Gauge(fullName, fullTags)
})
}

func (f *factory) Timer(name string, tags map[string]string) metrics.Timer {
fullName, fullTags, key := f.getKey(name, tags)
return f.cache.getOrSetTimer(key, func() metrics.Timer {
return f.factory.Timer(fullName, fullTags)
})
}

func (f *factory) Namespace(name string, tags map[string]string) metrics.Factory {
return &factory{
cache: f.cache,
scope: f.subScope(name),
tags: f.mergeTags(tags),
factory: f.factory,
Options: f.Options,
}
}

func (f *factory) getKey(name string, tags map[string]string) (fullName string, fullTags map[string]string, key string) {
fullName = f.subScope(name)
fullTags = f.mergeTags(tags)
key = metrics.GetKey(fullName, fullTags, f.TagsSep, f.TagKVSep)
return
}

func (f *factory) mergeTags(tags map[string]string) map[string]string {
ret := make(map[string]string, len(f.tags)+len(tags))
for k, v := range f.tags {
ret[k] = v
}
for k, v := range tags {
ret[k] = v
}
return ret
}

func (f *factory) subScope(name string) string {
if f.scope == "" {
return name
}
if name == "" {
return f.scope
}
return f.scope + f.ScopeSep + name
}
119 changes: 119 additions & 0 deletions metrics/adapters/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adapters

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/uber/jaeger-lib/metrics"
)

func TestDefaultOptions(t *testing.T) {
o := defaultOptions(Options{})
assert.Equal(t, ".", o.ScopeSep)
assert.Equal(t, ".", o.TagsSep)
assert.Equal(t, "_", o.TagKVSep)
}

func TestSubScope(t *testing.T) {
f := &factory{
Options: defaultOptions(Options{}),
}
assert.Equal(t, "", f.subScope(""))
assert.Equal(t, "x", f.subScope("x"))
f.scope = "x"
assert.Equal(t, "x", f.subScope(""))
assert.Equal(t, "x.y", f.subScope("y"))
}

func TestFactory(t *testing.T) {
var (
counterPrefix = "counter_"
gaugePrefix = "gauge_"
timerPrefix = "timer_"

tagsA = map[string]string{"a": "b"}
tagsX = map[string]string{"x": "y"}
)

testCases := []struct {
name string
tags map[string]string
namespace string
nsTags map[string]string
fullName string
expectedCounter string
}{
{name: "x", fullName: "%sx"},
{tags: tagsX, fullName: "%s.x_y"},
{name: "x", tags: tagsA, fullName: "%sx.a_b"},
{namespace: "y", fullName: "y.%s"},
{nsTags: tagsA, fullName: "%s.a_b"},
{namespace: "y", nsTags: tagsX, fullName: "y.%s.x_y"},
{name: "x", namespace: "y", nsTags: tagsX, fullName: "y.%sx.x_y"},
{name: "x", tags: tagsX, namespace: "y", nsTags: tagsX, fullName: "y.%sx.x_y", expectedCounter: "84"},
{name: "x", tags: tagsA, namespace: "y", nsTags: tagsX, fullName: "y.%sx.a_b.x_y"},
{name: "x", tags: tagsX, namespace: "y", nsTags: tagsA, fullName: "y.%sx.a_b.x_y", expectedCounter: "84"},
}
local := metrics.NewLocalFactory(100 * time.Second)
for _, testCase := range testCases {
t.Run("", func(t *testing.T) {
if testCase.expectedCounter == "" {
testCase.expectedCounter = "42"
}
ff := &fakeTagless{factory: local}
f := WrapFactoryWithoutTags(ff, Options{})
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)

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)
assert.Equal(t, fmt.Sprintf(testCase.fullName, timerPrefix), ff.timer)
})
}
}

type fakeTagless struct {
factory metrics.Factory
counter string
gauge string
timer string
}

func (f *fakeTagless) Counter(name string) metrics.Counter {
f.counter = name
return f.factory.Counter(name, nil)
}

func (f *fakeTagless) Gauge(name string) metrics.Gauge {
f.gauge = name
return f.factory.Gauge(name, nil)
}

func (f *fakeTagless) Timer(name string) metrics.Timer {
f.timer = name
return f.factory.Timer(name, nil)
}
Loading