Skip to content

Commit

Permalink
Add adapter for github.com/uber-go/tally
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Shkuro committed Feb 11, 2017
1 parent 27257c4 commit 798e145
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
8 changes: 6 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import:
- package: github.com/codahale/hdrhistogram
- package: github.com/go-kit/kit
version: a5e3d03d36fa4f9f2915d10d6b17fa0e02b734cc
- package: github.com/uber-go/tally
version: ^1.1.0
testImport:
- package: github.com/stretchr/testify
49 changes: 49 additions & 0 deletions metrics/tally/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tally

import (
"github.com/uber-go/tally"

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

// Wrap takes a tally Scope and returns jaeger-lib metrics.Factory.
func Wrap(scope tally.Scope) metrics.Factory {
return &factory{
tally: scope,
}
}

// TODO implement support for tags if tally.Scope does not support them
type factory struct {
tally tally.Scope
}

func (f *factory) Counter(name string, tags map[string]string) metrics.Counter {
scope := f.tally
if len(tags) > 0 {
scope = scope.Tagged(tags)
}
return NewCounter(scope.Counter(name))
}

func (f *factory) Gauge(name string, tags map[string]string) metrics.Gauge {
scope := f.tally
if len(tags) > 0 {
scope = scope.Tagged(tags)
}
return NewGauge(scope.Gauge(name))
}

func (f *factory) Timer(name string, tags map[string]string) metrics.Timer {
scope := f.tally
if len(tags) > 0 {
scope = scope.Tagged(tags)
}
return NewTimer(scope.Timer(name))
}

func (f *factory) Namespace(name string, tags map[string]string) metrics.Factory {
return &factory{
tally: f.tally.SubScope(name),
}
}
31 changes: 31 additions & 0 deletions metrics/tally/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tally

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/uber-go/tally"
)

func TestFactory(t *testing.T) {
testScope := tally.NewTestScope("prefix", map[string]string{"a": "b"})
factory := Wrap(testScope)
counter := factory.Counter("counter", map[string]string{"x": "y"})
counter.Inc(42)
gauge := factory.Gauge("gauge", map[string]string{"x": "y"})
gauge.Update(42)
timer := factory.Timer("timer", map[string]string{"x": "y"})
timer.Record(42 * time.Millisecond)
snapshot := testScope.Snapshot()
c := snapshot.Counters()["prefix.counter"]
g := snapshot.Gauges()["prefix.gauge"]
h := snapshot.Timers()["prefix.timer"]
expectedTags := map[string]string{"a": "b", "x": "y"}
assert.EqualValues(t, 42, c.Value())
assert.EqualValues(t, expectedTags, c.Tags())
assert.EqualValues(t, 42, g.Value())
assert.EqualValues(t, expectedTags, g.Tags())
assert.Equal(t, []time.Duration{42 * time.Millisecond}, h.Values())
assert.EqualValues(t, expectedTags, h.Tags())
}
52 changes: 52 additions & 0 deletions metrics/tally/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tally

import (
"time"

"github.com/uber-go/tally"
)

// Counter is an adapter from go-tally Counter to jaeger-lib Counter
type Counter struct {
counter tally.Counter
}

// NewCounter creates a new Counter
func NewCounter(counter tally.Counter) *Counter {
return &Counter{counter: counter}
}

// Inc adds the given value to the counter.
func (c *Counter) Inc(delta int64) {
c.counter.Inc(delta)
}

// Gauge is an adapter from go-tally Gauge to jaeger-lib Gauge
type Gauge struct {
gauge tally.Gauge
}

// NewGauge creates a new Gauge
func NewGauge(gauge tally.Gauge) *Gauge {
return &Gauge{gauge: gauge}
}

// Update the gauge to the value passed in.
func (g *Gauge) Update(value int64) {
g.gauge.Update(float64(value))
}

// Timer is an adapter from go-tally Histogram to jaeger-lib Timer
type Timer struct {
timer tally.Timer
}

// NewTimer creates a new Timer
func NewTimer(timer tally.Timer) *Timer {
return &Timer{timer: timer}
}

// Record saves the time passed in.
func (t *Timer) Record(delta time.Duration) {
t.timer.Record(delta)
}

0 comments on commit 798e145

Please sign in to comment.