Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 1 | package stats |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 6 | "v.io/core/veyron/lib/stats/histogram" |
Robin Thellend | ac59e97 | 2014-08-19 18:26:11 -0700 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // NewHistogram creates a new Histogram StatsObject with the given name and |
| 10 | // returns a pointer to it. |
| 11 | func NewHistogram(name string, opts histogram.Options) *histogram.Histogram { |
| 12 | lock.Lock() |
| 13 | defer lock.Unlock() |
| 14 | |
| 15 | node := findNodeLocked(name, true) |
| 16 | h := histogram.New(opts) |
| 17 | hw := &histogramWrapper{h} |
| 18 | node.object = hw |
| 19 | |
| 20 | addHistogramChild(node, name+"/delta1h", hw, time.Hour, hw.Delta1h) |
| 21 | addHistogramChild(node, name+"/delta10m", hw, 10*time.Minute, hw.Delta10m) |
| 22 | addHistogramChild(node, name+"/delta1m", hw, time.Minute, hw.Delta1m) |
| 23 | return h |
| 24 | } |
| 25 | |
| 26 | type histogramWrapper struct { |
| 27 | h *histogram.Histogram |
| 28 | } |
| 29 | |
| 30 | func (hw histogramWrapper) LastUpdate() time.Time { |
| 31 | return hw.h.LastUpdate() |
| 32 | } |
| 33 | |
| 34 | func (hw histogramWrapper) Value() interface{} { |
| 35 | return hw.h.Value() |
| 36 | } |
| 37 | |
| 38 | func (hw histogramWrapper) Delta1h() interface{} { |
| 39 | return hw.h.Delta1h() |
| 40 | } |
| 41 | |
| 42 | func (hw histogramWrapper) Delta10m() interface{} { |
| 43 | return hw.h.Delta10m() |
| 44 | } |
| 45 | |
| 46 | func (hw histogramWrapper) Delta1m() interface{} { |
| 47 | return hw.h.Delta1m() |
| 48 | } |
| 49 | |
| 50 | type histogramChild struct { |
| 51 | h *histogramWrapper |
| 52 | period time.Duration |
| 53 | value func() interface{} |
| 54 | } |
| 55 | |
| 56 | func (hc histogramChild) LastUpdate() time.Time { |
| 57 | now := time.Now() |
| 58 | if t := hc.h.LastUpdate().Add(hc.period); t.Before(now) { |
| 59 | return t |
| 60 | } |
| 61 | return now |
| 62 | } |
| 63 | |
| 64 | func (hc histogramChild) Value() interface{} { |
| 65 | return hc.value() |
| 66 | } |
| 67 | |
| 68 | func addHistogramChild(parent *node, name string, h *histogramWrapper, period time.Duration, value func() interface{}) { |
| 69 | child := findNodeLocked(name, true) |
| 70 | child.object = &histogramChild{h, period, value} |
| 71 | } |