github.com/sym3tri/etcd@v0.2.1-0.20140422215517-a563d82f95d6/metrics/standard.go (about) 1 package metrics 2 3 import ( 4 "io" 5 "net" 6 "sync" 7 "time" 8 9 gometrics "github.com/coreos/etcd/third_party/github.com/rcrowley/go-metrics" 10 ) 11 12 const ( 13 // RuntimeMemStatsSampleInterval is the interval in seconds at which the 14 // Go runtime's memory statistics will be gathered. 15 RuntimeMemStatsSampleInterval = time.Duration(2) * time.Second 16 17 // GraphitePublishInterval is the interval in seconds at which all 18 // gathered statistics will be published to a Graphite endpoint. 19 GraphitePublishInterval = time.Duration(2) * time.Second 20 ) 21 22 type standardBucket struct { 23 sync.Mutex 24 name string 25 registry gometrics.Registry 26 timers map[string]Timer 27 gauges map[string]Gauge 28 } 29 30 func newStandardBucket(name string) standardBucket { 31 registry := gometrics.NewRegistry() 32 33 gometrics.RegisterRuntimeMemStats(registry) 34 go gometrics.CaptureRuntimeMemStats(registry, RuntimeMemStatsSampleInterval) 35 36 return standardBucket{ 37 name: name, 38 registry: registry, 39 timers: make(map[string]Timer), 40 gauges: make(map[string]Gauge), 41 } 42 } 43 44 func (smb standardBucket) Dump(w io.Writer) { 45 gometrics.WriteOnce(smb.registry, w) 46 return 47 } 48 49 func (smb standardBucket) Timer(name string) Timer { 50 smb.Lock() 51 defer smb.Unlock() 52 53 timer, ok := smb.timers[name] 54 if !ok { 55 timer = gometrics.NewTimer() 56 smb.timers[name] = timer 57 smb.registry.Register(name, timer) 58 } 59 60 return timer 61 } 62 63 func (smb standardBucket) Gauge(name string) Gauge { 64 smb.Lock() 65 defer smb.Unlock() 66 67 gauge, ok := smb.gauges[name] 68 if !ok { 69 gauge = gometrics.NewGauge() 70 smb.gauges[name] = gauge 71 smb.registry.Register(name, gauge) 72 } 73 74 return gauge 75 } 76 77 func (smb standardBucket) Publish(graphite_addr string) error { 78 addr, err := net.ResolveTCPAddr("tcp", graphite_addr) 79 if err != nil { 80 return err 81 } 82 83 go gometrics.Graphite(smb.registry, GraphitePublishInterval, smb.name, addr) 84 85 return nil 86 }