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