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