Skip to content

Commit

Permalink
support incr/decr/set
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed Aug 2, 2016
1 parent 5422eb1 commit 4270b1b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 16 deletions.
12 changes: 12 additions & 0 deletions track.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ type HandlerFunc func(Metric, Value) error
func (f HandlerFunc) Handle(m Metric, v Value) error { return f(m, v) }

type Tracker interface {
Incr(Metric, Value)

Decr(Metric, Value)

Set(Metric, Value)

Track(Metric, Value)
}

type TrackerFunc func(Metric, Value)

func (f TrackerFunc) Incr(m Metric, v Value) { f.Track(m, Incr(v)) }

func (f TrackerFunc) Decr(m Metric, v Value) { f.Track(m, Decr(v)) }

func (f TrackerFunc) Set(m Metric, v Value) { f.Track(m, Set(v)) }

func (f TrackerFunc) Track(m Metric, v Value) { f(m, v) }

func NewTracker(name string, handler Handler, errorHandler func(Metric, Value, error)) Tracker {
Expand Down
80 changes: 79 additions & 1 deletion track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestTrackerFunc(t *testing.T) {
}
}

func TestTracker(t *testing.T) {
func TestTrackerTrack(t *testing.T) {
var m0 = NewMetric("test")
var v0 = Count(1)

Expand All @@ -81,6 +81,84 @@ func TestTracker(t *testing.T) {
}
}

func TestTrackerSet(t *testing.T) {
var m0 = NewMetric("test")
var v0 = Count(1)

var m1 Metric
var v1 Value

x := NewTracker("test", HandlerFunc(func(m Metric, v Value) error {
m1 = m
v1 = v
return nil
}), func(m Metric, v Value, e error) {
t.Errorf("%#v:%#v: %s", m, v, e)
})

x.Set(m0, v0)

if !reflect.DeepEqual(m0, m1) {
t.Errorf("invalid metric seen by stats tracker: %#v != %#v", m0, m1)
}

if !reflect.DeepEqual(Set(v0), v1) {
t.Errorf("invalid value seen by stats tracker: %#v != %#v", v0, v1)
}
}

func TestTrackerIncr(t *testing.T) {
var m0 = NewMetric("test")
var v0 = Count(1)

var m1 Metric
var v1 Value

x := NewTracker("test", HandlerFunc(func(m Metric, v Value) error {
m1 = m
v1 = v
return nil
}), func(m Metric, v Value, e error) {
t.Errorf("%#v:%#v: %s", m, v, e)
})

x.Incr(m0, v0)

if !reflect.DeepEqual(m0, m1) {
t.Errorf("invalid metric seen by stats tracker: %#v != %#v", m0, m1)
}

if !reflect.DeepEqual(Incr(v0), v1) {
t.Errorf("invalid value seen by stats tracker: %#v != %#v", v0, v1)
}
}

func TestTrackerDecr(t *testing.T) {
var m0 = NewMetric("test")
var v0 = Count(1)

var m1 Metric
var v1 Value

x := NewTracker("test", HandlerFunc(func(m Metric, v Value) error {
m1 = m
v1 = v
return nil
}), func(m Metric, v Value, e error) {
t.Errorf("%#v:%#v: %s", m, v, e)
})

x.Decr(m0, v0)

if !reflect.DeepEqual(m0, m1) {
t.Errorf("invalid metric seen by stats tracker: %#v != %#v", m0, m1)
}

if !reflect.DeepEqual(Decr(v0), v1) {
t.Errorf("invalid value seen by stats tracker: %#v != %#v", v0, v1)
}
}

func TestTrackerError(t *testing.T) {
var m0 = NewMetric("test")
var v0 = Count(1)
Expand Down
62 changes: 48 additions & 14 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stats
import "time"

type Value interface {
Symbol() string
Measure() string

Value() float64
}
Expand All @@ -12,30 +12,64 @@ func NewValue(sym string, val float64) Value {
return value{symbol: sym, value: val}
}

type value struct {
symbol string
value float64
func Set(value Value) Value {
return value
}

func Incr(value Value) Value {
switch v := value.(type) {
case Increment:
return v
}
return Increment{value: value}
}

func Decr(value Value) Value {
switch v := value.(type) {
case Decrement:
return v
}
return Decrement{value: value}
}

type Increment struct {
value Value
}

func (v value) Symbol() string { return v.symbol }
func (v value) Value() float64 { return v.value }
func (i Increment) Measure() string { return i.value.Measure() }
func (i Increment) Value() float64 { return i.value.Value() }

type Decrement struct {
value Value
}

func (d Decrement) Measure() string { return d.value.Measure() }
func (d Decrement) Value() float64 { return d.value.Value() }

type Count uint64

func (v Count) Symbol() string { return "count" }
func (v Count) Value() float64 { return float64(v) }
func (v Count) Measure() string { return "count" }
func (v Count) Value() float64 { return float64(v) }

type Size uint64

func (v Size) Symbol() string { return "size" }
func (v Size) Value() float64 { return float64(v) }
func (v Size) Measure() string { return "size" }
func (v Size) Value() float64 { return float64(v) }

type Bytes uint64

func (v Bytes) Symbol() string { return "bytes" }
func (v Bytes) Value() float64 { return float64(v) }
func (v Bytes) Measure() string { return "bytes" }
func (v Bytes) Value() float64 { return float64(v) }

type Duration time.Duration

func (v Duration) Symbol() string { return "duration" }
func (v Duration) Value() float64 { return time.Duration(v).Seconds() }
func (v Duration) Measure() string { return "duration" }
func (v Duration) Value() float64 { return time.Duration(v).Seconds() }

type value struct {
symbol string
value float64
}

func (v value) Measure() string { return v.symbol }
func (v value) Value() float64 { return v.value }
67 changes: 66 additions & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestValues(t *testing.T) {
}

for _, test := range tests {
if symbol := test.object.Symbol(); symbol != test.symbol {
if symbol := test.object.Measure(); symbol != test.symbol {
t.Errorf("%#v: invalid symbol: %#v != %#v", test.object, test.symbol, symbol)
}

Expand All @@ -48,3 +48,68 @@ func TestValues(t *testing.T) {
}
}
}

func TestSet(t *testing.T) {
v0 := NewValue("test", 1)
v1 := Set(v0)

if v0 != v1 {
t.Errorf("set: %#v != %#v", v0, v1)
}
}

func TestIncrValue(t *testing.T) {
v0 := NewValue("test", 1)
v1 := Incr(v0)

switch x := v1.(type) {
case Increment:
if s := x.Measure(); s != v0.Measure() {
t.Errorf("incr: invalid measure: %#v != %#v", v0.Measure(), s)
}

if v := x.Value(); v != v0.Value() {
t.Errorf("incr: invalid value: %#v != %#v", v0.Value(), v)
}

default:
t.Errorf("incr: invalid type: %T", x)
}
}

func TestIncrIncrement(t *testing.T) {
v0 := Incr(NewValue("test", 1))
v1 := Incr(v0)

if v0 != v1 {
t.Errorf("incr: %#v != %#v", v0, v1)
}
}

func TestDecrValue(t *testing.T) {
v0 := NewValue("test", 1)
v1 := Decr(v0)

switch x := v1.(type) {
case Decrement:
if s := x.Measure(); s != v0.Measure() {
t.Errorf("decr: invalid measure: %#v != %#v", v0.Measure(), s)
}

if v := x.Value(); v != v0.Value() {
t.Errorf("decr: invalid value: %#v != %#v", v0.Value(), v)
}

default:
t.Errorf("decr: invalid type: %T", x)
}
}

func TestDecrDecrement(t *testing.T) {
v0 := Decr(NewValue("test", 1))
v1 := Decr(v0)

if v0 != v1 {
t.Errorf("decr: %#v != %#v", v0, v1)
}
}

0 comments on commit 4270b1b

Please sign in to comment.