Skip to content

Commit

Permalink
clarify Latency histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
tbg committed Dec 28, 2015
1 parent 64feb78 commit c26a3c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
16 changes: 1 addition & 15 deletions util/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ type periodic interface {
var _ periodic = &Histogram{}
var _ periodic = &Rate{}

// Unit is a base unit for a histogram.
type Unit int64

// UnitMs is the number of milliseconds in a nanosecond.
const UnitMs = Unit(time.Millisecond)

// MaxVal is a maximum value for a histogram.
type MaxVal int64

// MaxMinute truncates histogram values larger than one minute.
const MaxMinute = MaxVal(time.Minute)

func maybeTick(m periodic) {
for m.nextTick().Before(time.Now()) {
m.tick()
Expand All @@ -81,7 +69,6 @@ func maybeTick(m periodic) {

// A Histogram is a wrapper around an hdrhistogram.WindowedHistogram.
type Histogram struct {
unit int64
maxVal int64

mu sync.Mutex
Expand Down Expand Up @@ -112,9 +99,8 @@ func (h *Histogram) RecordValue(v int64) {
h.mu.Lock()
defer h.mu.Unlock()
maybeTick(h)
v /= h.unit
for h.windowed.Current.RecordValue(v) != nil {
v = h.maxVal / h.unit
v = h.maxVal
}
}

Expand Down
11 changes: 6 additions & 5 deletions util/metric/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,28 @@ func (r *Registry) MarshalJSON() ([]byte, error) {
// Histogram registers a new windowed HDRHistogram with the given
// parameters. Data is kept in the active window for approximately the given
// duration.
func (r *Registry) Histogram(name string, duration time.Duration, unit Unit, maxVal MaxVal, sigFigs int) *Histogram {
func (r *Registry) Histogram(name string, duration time.Duration, maxVal int64, sigFigs int) *Histogram {
const n = 4
h := &Histogram{}
h.maxVal = int64(maxVal)
h.unit = int64(unit)
h.interval = duration / n
h.nextT = time.Now()

h.windowed = hdrhistogram.NewWindowed(n, 0, h.maxVal/h.unit, sigFigs)
h.windowed = hdrhistogram.NewWindowed(n, 0, h.maxVal, sigFigs)
r.MustAdd(name, h)
return h
}

// Latency is a convenience function which registers histograms with
// suitable defaults for latency tracking on millisecond to minute time scales.
// suitable defaults for latency tracking. Values are expressed in ns,
// are truncated into the interval [0, time.Minute] and are recorded
// with two digits of precision (i.e. errors of <1ms at 100ms, <.6s at 1m).
// The generated names of the metric will begin with the given prefix.
func (r *Registry) Latency(prefix string) Histograms {
windows := []timeScale{scale1M, scale10M, scale1H}
hs := make([]*Histogram, 0, 3)
for _, w := range windows {
h := r.Histogram(prefix+w.name, w.d, UnitMs, MaxMinute, 2)
h := r.Histogram(prefix+w.name, w.d, int64(time.Minute), 2)
hs = append(hs, h)
}
return hs
Expand Down

0 comments on commit c26a3c3

Please sign in to comment.