ScuttleBot

scuttlebot / internal / api / metrics.go
Blame History Raw 70 lines
1
package api
2
3
import (
4
"net/http"
5
"runtime"
6
"time"
7
)
8
9
type metricsResponse struct {
10
Timestamp string `json:"timestamp"`
11
Runtime runtimeMetrics `json:"runtime"`
12
Bridge *bridgeMetrics `json:"bridge,omitempty"`
13
Registry registryMetrics `json:"registry"`
14
}
15
16
type runtimeMetrics struct {
17
Goroutines int `json:"goroutines"`
18
HeapAlloc uint64 `json:"heap_alloc_bytes"`
19
HeapSys uint64 `json:"heap_sys_bytes"`
20
GCRuns uint32 `json:"gc_runs"`
21
}
22
23
type bridgeMetrics struct {
24
Channels int `json:"channels"`
25
MessagesTotal int64 `json:"messages_total"`
26
ActiveSubs int `json:"active_subscribers"`
27
}
28
29
type registryMetrics struct {
30
Total int `json:"total"`
31
Active int `json:"active"`
32
Revoked int `json:"revoked"`
33
}
34
35
func (s *Server) handleMetrics(w http.ResponseWriter, r *http.Request) {
36
var ms runtime.MemStats
37
runtime.ReadMemStats(&ms)
38
39
resp := metricsResponse{
40
Timestamp: time.Now().UTC().Format(time.RFC3339),
41
Runtime: runtimeMetrics{
42
Goroutines: runtime.NumGoroutine(),
43
HeapAlloc: ms.HeapAlloc,
44
HeapSys: ms.HeapSys,
45
GCRuns: ms.NumGC,
46
},
47
}
48
49
if s.bridge != nil {
50
st := s.bridge.Stats()
51
resp.Bridge = &bridgeMetrics{
52
Channels: st.Channels,
53
MessagesTotal: st.MessagesTotal,
54
ActiveSubs: st.ActiveSubs,
55
}
56
}
57
58
agents := s.registry.List()
59
for _, a := range agents {
60
resp.Registry.Total++
61
if a.Revoked {
62
resp.Registry.Revoked++
63
} else {
64
resp.Registry.Active++
65
}
66
}
67
68
writeJSON(w, http.StatusOK, resp)
69
}
70

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button