Codebase list golang-github-go-kit-kit / 962040e
metrics: pcp: add reporting Suyash 7 years ago
2 changed file(s) with 55 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1616 if err != nil {
1717 panic(err)
1818 }
19
20 registry.AddMetric(c)
21 if err != nil {
22 panic(err)
23 }
24
1925 return &Counter{c}
2026 }
2127
4248 if err != nil {
4349 panic(err)
4450 }
51
52 err = registry.AddMetric(g)
53 if err != nil {
54 panic(err)
55 }
56
4557 return &Gauge{g}
4658 }
4759
6476 // NewHistogram creates a new Histogram
6577 // minimum observeable value is 0
6678 // maximum observeable value is 3600000000
67 func NewHistogram(name string, min, max int64) *Histogram {
68 h, err := speed.NewPCPHistogram(name, min, max, 5)
79 func NewHistogram(name string) *Histogram {
80 h, err := speed.NewPCPHistogram(name, 0, 3600000000, 5)
6981 if err != nil {
7082 panic(err)
7183 }
84
85 err = registry.AddMetric(h)
86 if err != nil {
87 panic(err)
88 }
89
7290 return &Histogram{h}
7391 }
7492
86104
87105 // Percentile returns a percentile between 0 and 100
88106 func (h *Histogram) Percentile(p float64) int64 { return h.h.Percentile(p) }
107
108 ////////////////////////////////////////////////////////////////////////////
109
110 var registry *speed.PCPRegistry
111 var client *speed.PCPClient
112
113 func init() {
114 registry = speed.NewPCPRegistry()
115 }
116
117 // StartReporting starts reporting currently registered metrics to the PCP backend
118 func StartReporting(appname string) {
119 if client != nil {
120 panic("reporting is already enabled")
121 }
122
123 var err error
124 client, err = speed.NewPCPClientWithRegistry(appname, registry)
125 if err != nil {
126 panic(err)
127 }
128
129 client.MustStart()
130 }
131
132 // StopReporting stops all reporting
133 func StopReporting() {
134 if client == nil {
135 panic("reporting is not active")
136 }
137
138 client.MustStop()
139 client = nil
140 }
2222 }
2323
2424 func TestHistogram(t *testing.T) {
25 histogram := NewHistogram("speed_histogram", 0, 50).With("label values", "not supported").(*Histogram)
25 histogram := NewHistogram("speed_histogram").With("label values", "not supported").(*Histogram)
2626 quantiles := func() (float64, float64, float64, float64) {
2727 p50 := float64(histogram.Percentile(50))
2828 p90 := float64(histogram.Percentile(90))