Skip to content

Commit ae69bb3

Browse files
author
Nigel Deakin
committed
Update global stats charts to show bteakdown by function
1 parent 6698efe commit ae69bb3

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

api/agent/agent.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (a *agent) Submit(callI Call) error {
173173
default:
174174
}
175175

176-
a.stats.Enqueue()
176+
a.stats.Enqueue(callI.Model().Path)
177177

178178
call := callI.(*call)
179179
ctx := call.req.Context()
@@ -188,6 +188,7 @@ func (a *agent) Submit(callI Call) error {
188188

189189
slot, err := a.getSlot(ctx, call) // find ram available / running
190190
if err != nil {
191+
a.stats.Dequeue(callI.Model().Path)
191192
return err
192193
}
193194
// TODO if the call times out & container is created, we need
@@ -197,16 +198,17 @@ func (a *agent) Submit(callI Call) error {
197198
// TODO Start is checking the timer now, we could do it here, too.
198199
err = call.Start(ctx)
199200
if err != nil {
201+
a.stats.Dequeue(callI.Model().Path)
200202
return err
201203
}
202204

203-
a.stats.Start()
205+
a.stats.Start(callI.Model().Path)
204206

205207
err = slot.exec(ctx, call)
206208
// pass this error (nil or otherwise) to end directly, to store status, etc
207209
// End may rewrite the error or elect to return it
208210

209-
a.stats.Complete()
211+
a.stats.Complete(callI.Model().Path)
210212

211213
// TODO: we need to allocate more time to store the call + logs in case the call timed out,
212214
// but this could put us over the timeout if the call did not reply yet (need better policy).

api/agent/stats.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,83 @@ import "sync"
66
// * hot containers active
77
// * memory used / available
88

9+
// global statistics
910
type stats struct {
10-
mu sync.Mutex
11+
mu sync.Mutex
12+
// statistics for all functions combined
13+
queue uint64
14+
running uint64
15+
complete uint64
16+
// statistics for individual functions, keyed by function path
17+
functionStatsMap map[string]*functionStats
18+
}
19+
20+
// statistics for an individual function
21+
type functionStats struct {
1122
queue uint64
1223
running uint64
1324
complete uint64
1425
}
1526

1627
type Stats struct {
28+
// statistics for all functions combined
29+
Queue uint64
30+
Running uint64
31+
Complete uint64
32+
// statistics for individual functions, keyed by function path
33+
FunctionStatsMap map[string]*FunctionStats
34+
}
35+
36+
// statistics for an individual function
37+
type FunctionStats struct {
1738
Queue uint64
1839
Running uint64
1940
Complete uint64
2041
}
2142

22-
func (s *stats) Enqueue() {
43+
func (s *stats) getStatsForFunction(path string) *functionStats {
44+
if s.functionStatsMap == nil {
45+
s.functionStatsMap = make(map[string]*functionStats)
46+
}
47+
thisFunctionStats, found := s.functionStatsMap[path]
48+
if !found {
49+
thisFunctionStats = &functionStats{}
50+
s.functionStatsMap[path] = thisFunctionStats
51+
}
52+
53+
return thisFunctionStats
54+
}
55+
56+
func (s *stats) Enqueue(path string) {
2357
s.mu.Lock()
2458
s.queue++
59+
s.getStatsForFunction(path).queue++
60+
s.mu.Unlock()
61+
}
62+
63+
// Call when a function has been queued but cannot be started because of an error
64+
func (s *stats) Dequeue(path string) {
65+
s.mu.Lock()
66+
s.queue--
67+
s.getStatsForFunction(path).queue--
2568
s.mu.Unlock()
2669
}
2770

28-
func (s *stats) Start() {
71+
func (s *stats) Start(path string) {
2972
s.mu.Lock()
3073
s.queue--
74+
s.getStatsForFunction(path).queue--
3175
s.running++
76+
s.getStatsForFunction(path).running++
3277
s.mu.Unlock()
3378
}
3479

35-
func (s *stats) Complete() {
80+
func (s *stats) Complete(path string) {
3681
s.mu.Lock()
3782
s.running--
83+
s.getStatsForFunction(path).running--
3884
s.complete++
85+
s.getStatsForFunction(path).complete++
3986
s.mu.Unlock()
4087
}
4188

@@ -45,6 +92,11 @@ func (s *stats) Stats() Stats {
4592
stats.Running = s.running
4693
stats.Complete = s.complete
4794
stats.Queue = s.queue
95+
stats.FunctionStatsMap = make(map[string]*FunctionStats)
96+
for key, value := range s.functionStatsMap {
97+
thisFunctionStats := &FunctionStats{Queue: value.queue, Running: value.running, Complete: value.complete}
98+
stats.FunctionStatsMap[key] = thisFunctionStats
99+
}
48100
s.mu.Unlock()
49101
return stats
50102
}

0 commit comments

Comments
 (0)