blob: 9baa8a810d0ab0bb05d532d63bbd5d780c20b011 [file] [log] [blame]
Robin Thellendac59e972014-08-19 18:26:11 -07001package stats
2
3import (
4 "time"
5
Jiri Simsa764efb72014-12-25 20:57:03 -08006 "v.io/core/veyron/lib/stats/histogram"
Robin Thellendac59e972014-08-19 18:26:11 -07007)
8
9// NewHistogram creates a new Histogram StatsObject with the given name and
10// returns a pointer to it.
11func 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
26type histogramWrapper struct {
27 h *histogram.Histogram
28}
29
30func (hw histogramWrapper) LastUpdate() time.Time {
31 return hw.h.LastUpdate()
32}
33
34func (hw histogramWrapper) Value() interface{} {
35 return hw.h.Value()
36}
37
38func (hw histogramWrapper) Delta1h() interface{} {
39 return hw.h.Delta1h()
40}
41
42func (hw histogramWrapper) Delta10m() interface{} {
43 return hw.h.Delta10m()
44}
45
46func (hw histogramWrapper) Delta1m() interface{} {
47 return hw.h.Delta1m()
48}
49
50type histogramChild struct {
51 h *histogramWrapper
52 period time.Duration
53 value func() interface{}
54}
55
56func (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
64func (hc histogramChild) Value() interface{} {
65 return hc.value()
66}
67
68func 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}