forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
histogram.go
140 lines (121 loc) · 3.13 KB
/
histogram.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stats
import (
"bytes"
"fmt"
"sync"
)
// Histogram tracks counts and totals while
// splitting the counts under different buckets
// using specified cutoffs.
type Histogram struct {
cutoffs []int64
labels []string
countLabel string
totalLabel string
// mu controls buckets & total
mu sync.Mutex
buckets []int64
total int64
}
// NewHistogram creates a histogram with auto-generated labels
// based on the cutoffs. The buckets are categorized using the
// following criterion: cutoff[i-1] < value <= cutoff[i]. Anything
// higher than the highest cutoff is labeled as "inf".
func NewHistogram(name string, cutoffs []int64) *Histogram {
labels := make([]string, len(cutoffs)+1)
for i, v := range cutoffs {
labels[i] = fmt.Sprintf("%d", v)
}
labels[len(labels)-1] = "inf"
return NewGenericHistogram(name, cutoffs, labels, "Count", "Total")
}
// NewGenericHistogram creates a histogram where all the labels are
// supplied by the caller. The number of labels has to be one more than
// the number of cutoffs because the last label captures everything that
// exceeds the highest cutoff.
func NewGenericHistogram(name string, cutoffs []int64, labels []string, countLabel, totalLabel string) *Histogram {
if len(cutoffs) != len(labels)-1 {
panic("mismatched cutoff and label lengths")
}
h := &Histogram{
cutoffs: cutoffs,
labels: labels,
countLabel: countLabel,
totalLabel: totalLabel,
buckets: make([]int64, len(labels)),
total: 0,
}
if name != "" {
Publish(name, h)
}
return h
}
func (h *Histogram) Add(value int64) {
for i := range h.labels {
if i == len(h.labels)-1 || value <= h.cutoffs[i] {
h.mu.Lock()
h.buckets[i] += 1
h.total += value
h.mu.Unlock()
return
}
}
}
func (h *Histogram) String() string {
b, _ := h.MarshalJSON()
return string(b)
}
func (h *Histogram) MarshalJSON() ([]byte, error) {
h.mu.Lock()
defer h.mu.Unlock()
b := bytes.NewBuffer(make([]byte, 0, 4096))
fmt.Fprintf(b, "{")
totalCount := int64(0)
for i, label := range h.labels {
totalCount += h.buckets[i]
fmt.Fprintf(b, "\"%v\": %v, ", label, totalCount)
}
fmt.Fprintf(b, "\"%s\": %v, ", h.countLabel, totalCount)
fmt.Fprintf(b, "\"%s\": %v", h.totalLabel, h.total)
fmt.Fprintf(b, "}")
return b.Bytes(), nil
}
func (h *Histogram) Counts() map[string]int64 {
h.mu.Lock()
defer h.mu.Unlock()
counts := make(map[string]int64, len(h.labels))
for i, label := range h.labels {
counts[label] = h.buckets[i]
}
return counts
}
func (h *Histogram) CountLabel() string {
return h.countLabel
}
func (h *Histogram) Count() (count int64) {
h.mu.Lock()
defer h.mu.Unlock()
for _, v := range h.buckets {
count += v
}
return
}
func (h *Histogram) TotalLabel() string {
return h.totalLabel
}
func (h *Histogram) Total() (total int64) {
h.mu.Lock()
defer h.mu.Unlock()
return h.total
}
func (h *Histogram) Labels() []string {
return h.labels
}
func (h *Histogram) Buckets() []int64 {
h.mu.Lock()
defer h.mu.Unlock()
return h.buckets
}