Skip to content

Commit

Permalink
Mostly documentation and naming changes.
Browse files Browse the repository at this point in the history
Undoubtedly this commit doesn't build or test but it's part of a larger series of intertwined changes.
  • Loading branch information
rcrowley committed Jan 7, 2014
1 parent aefccf1 commit 6bd8d76
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 267 deletions.
27 changes: 13 additions & 14 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@ package metrics
import "sync/atomic"

// Counters hold an int64 value that can be incremented and decremented.
//
// This is an interface so as to encourage other structs to implement
// the Counter API as appropriate.
type Counter interface {
Clear()
Count() int64
Dec(int64)
Inc(int64)
Snapshot() Counter
}

// Get an existing or create and register a new Counter.
// GetOrRegisterCounter returns an existing Counter or constructs and registers
// a new StandardCounter.
func GetOrRegisterCounter(name string, r Registry) Counter {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewCounter()).(Counter)
}

// Create a new Counter.
// NewCounter constructs a new StandardCounter.
func NewCounter() Counter {
if UseNilMetrics {
return NilCounter{}
}
return &StandardCounter{0}
}

// Create and register a new Counter.
// NewRegisteredCounter constructs and registers a new StandardCounter.
func NewRegisteredCounter(name string, r Registry) Counter {
c := NewCounter()
if nil == r {
Expand All @@ -42,16 +41,16 @@ func NewRegisteredCounter(name string, r Registry) Counter {
// No-op Counter.
type NilCounter struct{}

// No-op.
// Clear is a no-op.
func (NilCounter) Clear() {}

// No-op.
// Count is a no-op.
func (NilCounter) Count() int64 { return 0 }

// No-op.
// Dec is a no-op.
func (NilCounter) Dec(i int64) {}

// No-op.
// Inc is a no-op.
func (NilCounter) Inc(i int64) {}

// The standard implementation of a Counter uses the sync/atomic package
Expand All @@ -60,22 +59,22 @@ type StandardCounter struct {
count int64
}

// Clear the counter: set it to zero.
// Clear sets the counter to zero.
func (c *StandardCounter) Clear() {
atomic.StoreInt64(&c.count, 0)
}

// Return the current count.
// Count returns the current count.
func (c *StandardCounter) Count() int64 {
return atomic.LoadInt64(&c.count)
}

// Decrement the counter by the given amount.
// Dec decrements the counter by the given amount.
func (c *StandardCounter) Dec(i int64) {
atomic.AddInt64(&c.count, -i)
}

// Increment the counter by the given amount.
// Inc increments the counter by the given amount.
func (c *StandardCounter) Inc(i int64) {
atomic.AddInt64(&c.count, i)
}
26 changes: 12 additions & 14 deletions ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,51 @@ import (

// EWMAs continuously calculate an exponentially-weighted moving average
// based on an outside source of clock ticks.
//
// This is an interface so as to encourage other structs to implement
// the EWMA API as appropriate.
type EWMA interface {
Rate() float64
Snapshot() EWMA
Tick()
Update(int64)
}

// Create a new EWMA with the given alpha.
// NewEWMA constructs a new EWMA with the given alpha.
func NewEWMA(alpha float64) EWMA {
if UseNilMetrics {
return NilEWMA{}
}
return &StandardEWMA{alpha: alpha}
}

// Create a new EWMA with alpha set for a one-minute moving average.
// NewEWMA1 constructs a new EWMA for a one-minute moving average.
func NewEWMA1() EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/1))
}

// Create a new EWMA with alpha set for a five-minute moving average.
// NewEWMA5 constructs a new EWMA for a five-minute moving average.
func NewEWMA5() EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/5))
}

// Create a new EWMA with alpha set for a fifteen-minute moving average.
// NewEWMA15 constructs a new EWMA for a fifteen-minute moving average.
func NewEWMA15() EWMA {
return NewEWMA(1 - math.Exp(-5.0/60.0/15))
}

// No-op EWMA.
type NilEWMA struct{}

// No-op.
// Rate is a no-op.
func (NilEWMA) Rate() float64 { return 0.0 }

// No-op.
func (NilEWMA) Tick() {}

// No-op.
// Update is a no-op.
func (NilEWMA) Update(n int64) {}

// The standard implementation of an EWMA tracks the number of uncounted
// events and processes them on each tick. It uses the sync/atomic package
// to manage uncounted events.
// StandardEWMA is the standard implementation of an EWMA and tracks the number
// of uncounted events and processes them on each tick. It uses the
// sync/atomic package to manage uncounted events.
type StandardEWMA struct {
alpha float64
init bool
Expand All @@ -63,7 +61,7 @@ type StandardEWMA struct {
uncounted int64
}

// Return the moving average rate of events per second.
// Rate returns the moving average rate of events per second.
func (a *StandardEWMA) Rate() float64 {
a.mutex.Lock()
defer a.mutex.Unlock()
Expand All @@ -85,7 +83,7 @@ func (a *StandardEWMA) Tick() {
}
}

// Add n uncounted events.
// Update adds n uncounted events.
func (a *StandardEWMA) Update(n int64) {
atomic.AddInt64(&a.uncounted, n)
}
18 changes: 8 additions & 10 deletions gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,29 @@ package metrics
import "sync/atomic"

// Gauges hold an int64 value that can be set arbitrarily.
//
// This is an interface so as to encourage other structs to implement
// the Gauge API as appropriate.
type Gauge interface {
Update(int64)
Value() int64
}

// Get an existing or create and register a new Gauge.
// GetOrRegisterGauge returns an existing Gauge or constructs and registers a
// new StandardGauge.
func GetOrRegisterGauge(name string, r Registry) Gauge {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewGauge()).(Gauge)
}

// Create a new Gauge.
// NewGauge constructs a new StandardGauge.
func NewGauge() Gauge {
if UseNilMetrics {
return NilGauge{}
}
return &StandardGauge{0}
}

// Create and register a new Gauge.
// NewRegisteredGauge constructs and registers a new StandardGauge.
func NewRegisteredGauge(name string, r Registry) Gauge {
c := NewGauge()
if nil == r {
Expand All @@ -43,11 +41,11 @@ type NilGauge struct{}
// No-op.
func (NilGauge) Update(v int64) {}

// No-op.
// Value is a no-op.
func (NilGauge) Value() int64 { return 0 }

// The standard implementation of a Gauge uses the sync/atomic package
// to manage a single int64 value.
// StandardGauge is the standard implementation of a Gauge and uses the
// sync/atomic package to manage a single int64 value.
type StandardGauge struct {
value int64
}
Expand All @@ -57,7 +55,7 @@ func (g *StandardGauge) Update(v int64) {
atomic.StoreInt64(&g.value, v)
}

// Return the gauge's current value.
// Value returns the gauge's current value.
func (g *StandardGauge) Value() int64 {
return atomic.LoadInt64(&g.value)
}
34 changes: 16 additions & 18 deletions healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
package metrics

// Healthchecks hold an os.Error value describing an arbitrary up/down status.
//
// This is an interface so as to encourage other structs to implement
// the Healthcheck API as appropriate.
// Healthchecks hold an error value describing an arbitrary up/down status.
type Healthcheck interface {
Check()
Error() error
Healthy()
Unhealthy(error)
}

// Create a new Healthcheck, which will use the given function to update
// its status.
// NewHealthcheck constructs a new Healthcheck which will use the given
// function to update its status.
func NewHealthcheck(f func(Healthcheck)) Healthcheck {
if UseNilMetrics {
return NilHealthcheck{}
}
return &StandardHealthcheck{nil, f}
}

// No-op Healthcheck.
// NilHealthcheck is a no-op.
type NilHealthcheck struct{}

// No-op.
// Check is a no-op.
func (NilHealthcheck) Check() {}

// No-op.
// Error is a no-op.
func (NilHealthcheck) Error() error { return nil }

// No-op.
// Healthy is a no-op.
func (NilHealthcheck) Healthy() {}

// No-op.
func (NilHealthcheck) Unhealthy(err error) {}
// Unhealthy is a no-op.
func (NilHealthcheck) Unhealthy(error) {}

// The standard implementation of a Healthcheck stores the status and a
// function to call to update the status.
// StandardHealthcheck is the standard implementation of a Healthcheck and
// stores the status and a function to call to update the status.
type StandardHealthcheck struct {
err error
f func(Healthcheck)
}

// Update the healthcheck's status.
// Check runs the healthcheck function to update the healthcheck's status.
func (h *StandardHealthcheck) Check() {
h.f(h)
}

// Return the healthcheck's status, which will be nil if it is healthy.
// Error returns the healthcheck's status, which will be nil if it is healthy.
func (h *StandardHealthcheck) Error() error {
return h.err
}

// Mark the healthcheck as healthy.
// Healthy marks the healthcheck as healthy.
func (h *StandardHealthcheck) Healthy() {
h.err = nil
}

// Mark the healthcheck as unhealthy. The error should provide details.
// Unhealthy marks the healthcheck as unhealthy. The error is stored and
// may be retrieved by the Error method.
func (h *StandardHealthcheck) Unhealthy(err error) {
h.err = err
}
Loading

0 comments on commit 6bd8d76

Please sign in to comment.