forked from fnproject/fn
/
stats.go
202 lines (168 loc) · 4.33 KB
/
stats.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package agent
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// TODO this should expose:
// * hot containers active
// * memory used / available
// global statistics
type stats struct {
mu sync.Mutex
// statistics for all functions combined
queue uint64
running uint64
complete uint64
failed uint64
// statistics for individual functions, keyed by function path
functionStatsMap map[string]*functionStats
}
// statistics for an individual function
type functionStats struct {
queue uint64
running uint64
complete uint64
failed uint64
}
type Stats struct {
// statistics for all functions combined
Queue uint64
Running uint64
Complete uint64
Failed uint64
// statistics for individual functions, keyed by function path
FunctionStatsMap map[string]*FunctionStats
}
// statistics for an individual function
type FunctionStats struct {
Queue uint64
Running uint64
Complete uint64
Failed uint64
}
var (
fnCalls = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_calls",
Help: "Function calls by app and path",
},
[](string){"app", "path"},
)
fnQueued = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fn_api_queued",
Help: "Queued requests by app and path",
},
[](string){"app", "path"},
)
fnRunning = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fn_api_running",
Help: "Running requests by app and path",
},
[](string){"app", "path"},
)
fnCompleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_completed",
Help: "Completed requests by app and path",
},
[](string){"app", "path"},
)
fnFailed = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_failed",
Help: "Failed requests by path",
},
[](string){"app", "path"},
)
)
func init() {
prometheus.MustRegister(fnCalls)
prometheus.MustRegister(fnQueued)
prometheus.MustRegister(fnRunning)
prometheus.MustRegister(fnFailed)
prometheus.MustRegister(fnCompleted)
}
func (s *stats) getStatsForFunction(path string) *functionStats {
if s.functionStatsMap == nil {
s.functionStatsMap = make(map[string]*functionStats)
}
thisFunctionStats, found := s.functionStatsMap[path]
if !found {
thisFunctionStats = &functionStats{}
s.functionStatsMap[path] = thisFunctionStats
}
return thisFunctionStats
}
func (s *stats) Enqueue(app string, path string) {
s.mu.Lock()
s.queue++
s.getStatsForFunction(path).queue++
fnQueued.WithLabelValues(app, path).Inc()
fnCalls.WithLabelValues(app, path).Inc()
s.mu.Unlock()
}
// Call when a function has been queued but cannot be started because of an error
func (s *stats) Dequeue(app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
s.mu.Unlock()
}
func (s *stats) DequeueAndStart(app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
s.running++
s.getStatsForFunction(path).running++
fnRunning.WithLabelValues(app, path).Inc()
s.mu.Unlock()
}
func (s *stats) Complete(app string, path string) {
s.mu.Lock()
s.running--
s.getStatsForFunction(path).running--
fnRunning.WithLabelValues(app, path).Dec()
s.complete++
s.getStatsForFunction(path).complete++
fnCompleted.WithLabelValues(app, path).Inc()
s.mu.Unlock()
}
func (s *stats) Failed(app string, path string) {
s.mu.Lock()
s.running--
s.getStatsForFunction(path).running--
fnRunning.WithLabelValues(app, path).Dec()
s.failed++
s.getStatsForFunction(path).failed++
fnFailed.WithLabelValues(app, path).Inc()
s.mu.Unlock()
}
func (s *stats) DequeueAndFail(app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
s.failed++
s.getStatsForFunction(path).failed++
fnFailed.WithLabelValues(app, path).Inc()
s.mu.Unlock()
}
func (s *stats) Stats() Stats {
var stats Stats
s.mu.Lock()
stats.Running = s.running
stats.Complete = s.complete
stats.Queue = s.queue
stats.Failed = s.failed
stats.FunctionStatsMap = make(map[string]*FunctionStats)
for key, value := range s.functionStatsMap {
thisFunctionStats := &FunctionStats{Queue: value.queue, Running: value.running, Complete: value.complete, Failed: value.failed}
stats.FunctionStatsMap[key] = thisFunctionStats
}
s.mu.Unlock()
return stats
}