-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adapter for github.com/uber-go/tally
- Loading branch information
Yuri Shkuro
committed
Feb 11, 2017
1 parent
27257c4
commit 798e145
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |