import "github.com/heroku/instruments"
Package instruments allows you to collects metrics over discrete time intervals.
Collected metrics will only reflect observations from last time window only, rather than including observations from prior windows, contrary to EWMA based metrics.
timer := instruments.NewTimer(-1) registry := reporter.NewRegistry() registry.Register("processing-time", timer) go reporter.Log("process", registry, time.Minute) timer.Time(func() { ... })
Instruments support two types of instruments: Discrete instruments return a single value, and Sample instruments a sorted array of values.
Theses base instruments are available:
- Counter: holds a counter that can be incremented or decremented.
- Rate: tracks the rate of values per seconds.
- Reservoir: randomly samples values.
- Derive: tracks the rate of values based on the delta with previous value.
- Gauge: tracks last value.
- Timer: tracks durations.
You can create custom instruments or compose new instruments form the built-in instruments as long as they implements the Sample or Discrete interfaces.
Registry enforce the Discrete and Sample interfaces, creating a custom Reporter should be trivial, for example:
for k, m := range registry.Instruments() { switch i := m.(type) { case instruments.Discrete: s := i.Snapshot() report(k, s) case instruments.Sample: s := instruments.Quantile(i.Snapshot(), 0.95) report(k, s) } }
aggregate.go instruments.go math.go
Ceil returns the least integer value greater than or equal to x.
Floor returns the greatest integer value less than or equal to x.
Max returns maximun value of the given sample.
Mean returns the mean of the given sample.
Min returns minimun value of the given sample.
Quantile returns the nearest value to the given quantile.
Scale returns a conversion factor from one unit to another.
StandardDeviation returns standard deviation of the given sample.
Variance returns variance if the given sample.
type Counter struct {
// contains filtered or unexported fields
}
Counter holds a counter that can be incremented or decremented.
Code:
counter := NewCounter() counter.Update(20) counter.Update(25) s := counter.Snapshot() fmt.Println(s)
NewCounter creates a new counter instrument.
Snapshot returns the current value and reset the counter.
Update adds v to the counter.
type Derive struct {
// contains filtered or unexported fields
}
Derive tracks the rate of deltas per seconds.
Code:
derive := NewDerive(34) derive.Update(56) derive.Update(78) s := derive.Snapshot() fmt.Println(s)
NewDerive creates a new derive instruments.
NewDeriveScale creates a new derive instruments with the given unit.
Snapshot returns the number of values per seconds since the last snapshot, and reset the count to zero.
Update update rate value based on the stored previous value.
Discrete represents a single value instrument.
type Gauge struct {
// contains filtered or unexported fields
}
Gauge tracks a value.
Code:
gauge := NewGauge(34) gauge.Update(35) s := gauge.Snapshot() fmt.Println(s)
NewGauge creates a new Gauge with the given value.
Snapshot returns the current value.
Update updates the current stored value.
type Rate struct {
// contains filtered or unexported fields
}
Rate tracks the rate of values per second.
Code:
rate := NewRate() rate.Update(20) rate.Update(25) s := rate.Snapshot() fmt.Println(s)
NewRate creates a new rate instrument.
NewRateScale creates a new rate instruments with the given unit.
Snapshot returns the number of values per second since the last snapshot, and reset the count to zero.
Update updates rate value.
type Reservoir struct {
// contains filtered or unexported fields
}
Reservoir tracks a sample of values.
Code:
reservoir := NewReservoir(-1) reservoir.Update(12) reservoir.Update(54) reservoir.Update(34) s := reservoir.Snapshot() fmt.Println(Quantile(s, 0.99))
NewReservoir creates a new reservoir of the given size. If size is negative, it will create a sample of DefaultReservoirSize size.
Snapshot returns sample as a sorted array.
Update fills the sample randomly with given value, for reference, see: http://en.wikipedia.org/wiki/Reservoir_sampling
Sample represents a sample instrument.
type Timer struct {
// contains filtered or unexported fields
}
Timer tracks durations.
Code:
timer := NewTimer(-1) ts := time.Now() time.Sleep(10 * time.Second) timer.Since(ts) s := timer.Snapshot() fmt.Println(Quantile(s, 0.99))
NewTimer creates a new Timer with the given sample size.
Since records duration since the given start time.
Snapshot returns durations sample as a sorted array.
Time records given function execution time.
Code:
timer := NewTimer(-1) timer.Time(func() { time.Sleep(10 * time.Second) }) s := timer.Snapshot() fmt.Println(Quantile(s, 0.99))
Update adds duration to the sample in ms.
Path | Synopsis |
---|---|
reporter | Package reporter provides default reporting functionnality. |
runtime | Package runtime provides runtime instrumentations around memory usage, goroutine and cgo calls. |
Package instruments imports 6 packages (graph) and is imported by 2 packages. Updated 2017-10-07. Refresh now. Tools for package owners.