-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstats.go
More file actions
166 lines (148 loc) · 2.75 KB
/
stats.go
File metadata and controls
166 lines (148 loc) · 2.75 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"html/template"
"math"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type Stats struct {
At time.Time
Metrics map[string]float64
}
func (s *Stats) add(k string, v float64) {
s.Metrics[k] = v
}
// StatsRing is a ring buffer of Stats objects. Kind of.
type StatsRing struct {
sync.Mutex
Values []*Stats
Head int
}
func NewStatsRing(n int) *StatsRing {
return &StatsRing{Values: make([]*Stats, n)}
}
func (r *StatsRing) Add(s *Stats) {
r.Lock()
defer r.Unlock()
r.Values[r.Head] = s
r.Head = (r.Head + 1) % len(r.Values)
}
type GraphData struct {
Idx int
Title string
Metrics []string
Datapoints []Datapoint
}
type Datapoint struct {
At time.Time
Values []float64
}
func (d *Datapoint) ValuesStr() template.JS {
parts := make([]string, len(d.Values))
for i, v := range d.Values {
if math.IsNaN(v) {
parts[i] = "null"
} else {
parts[i] = strconv.FormatFloat(v, 'f', -1, 64)
}
}
return template.JS(strings.Join(parts, ", "))
}
func (r *StatsRing) GetDataForGraph(names []string) (g GraphData) {
r.Lock()
defer r.Unlock()
g.Metrics = names
g.Datapoints = make([]Datapoint, 0, len(r.Values))
pos := r.Head
for {
pos = (pos + 1) % len(r.Values)
if pos == r.Head {
return
}
if r.Values[pos] != nil {
m := r.Values[pos].Metrics
dp := Datapoint{
At: r.Values[pos].At,
Values: make([]float64, len(names)),
}
allNaN := true
for i, n := range names {
if v, found := m[n]; found {
dp.Values[i] = v
allNaN = false
} else {
dp.Values[i] = math.NaN()
}
}
if !allNaN {
g.Datapoints = append(g.Datapoints, dp)
}
}
}
}
const (
mtCounter = iota
mtTimer
mtGauge
mtSet
mtTimerGen
)
type MetricNames struct {
Names map[string]int
sync.Mutex
}
func NewMetricNames() *MetricNames {
return &MetricNames{Names: make(map[string]int)}
}
func (m *MetricNames) List() (out [][]string) {
m.Lock()
out = make([][]string, 4)
for n, t := range m.Names {
if t != mtTimerGen {
out[t] = append(out[t], n)
}
}
m.Unlock()
return
}
func (m *MetricNames) Add(a *HoldingArea) {
m.Lock()
for n, _ := range a.counters {
m.Names[n] = mtCounter
}
for n, _ := range a.timers {
m.Names[n] = mtTimer
}
for n, _ := range a.gauges {
m.Names[n] = mtGauge
}
for n, _ := range a.sets {
m.Names[n] = mtSet
}
m.Unlock()
}
func (m *MetricNames) AddTimerGen(n string) {
m.Lock()
m.Names[n] = mtTimerGen
m.Unlock()
}
func (m *MetricNames) Find(r string) (out []string) {
m.Lock()
for n, _ := range m.Names {
if strings.HasPrefix(n, r) {
out = append(out, n)
}
}
m.Unlock()
sort.Strings(out)
return
}
func (m *MetricNames) FindAll(rs []string) (out []string) {
for _, r := range rs {
out = append(out, m.Find(r)...)
}
return
}