diff --git a/client-ctl.go b/client-ctl.go index bf75697..8182944 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -3,6 +3,7 @@ package hodu import "encoding/json" import "net/http" import "net/url" +import "runtime" import "strconv" /* @@ -65,6 +66,11 @@ type json_out_client_peer struct { ServerPeerAddr string `json:"server-peer-addr"` ServerLocalAddr string `json:"server-local-addr"` } + +type json_out_client_stats struct { + NumCPUs int `json:"num-cpus"` + NumGoroutines int `json:"num-goroutines"` +} // ------------------------------------ type client_ctl_client_conns struct { @@ -91,6 +97,9 @@ type client_ctl_client_conns_id_routes_id_peers_id struct { c *Client } +type client_ctl_stats struct { + c *Client +} // ------------------------------------ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) { @@ -622,3 +631,40 @@ oops: c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) return } + +// ------------------------------------ + +func (ctl *client_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { + var c *Client + var status_code int + var err error + var je *json.Encoder + + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + + c = ctl.c + je = json.NewEncoder(w) + + switch req.Method { + case http.MethodGet: + var stats json_out_client_stats + stats.NumCPUs = runtime.NumCPU() + stats.NumGoroutines = runtime.NumGoroutine() + status_code = http.StatusOK; w.WriteHeader(status_code) + if err = je.Encode(stats); err != nil { goto oops } + + default: + status_code = http.StatusBadRequest; w.WriteHeader(status_code) + } + +//done: + c.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken + return + +oops: + c.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error()) + return +} diff --git a/client.go b/client.go index 74804e6..1990943 100644 --- a/client.go +++ b/client.go @@ -1005,6 +1005,7 @@ func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}", &client_ctl_client_conns_id_routes_id{c: &c}) c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers", &client_ctl_client_conns_id_routes_id_peers{c: &c}) c.ctl_mux.Handle(c.ctl_prefix + "/client-conns/{conn_id}/routes/{route_id}/peers/{peer_id}", &client_ctl_client_conns_id_routes_id_peers_id{c: &c}) + c.ctl_mux.Handle(c.ctl_prefix + "/stats", &client_ctl_stats{c: &c}) c.ctl_addr = make([]string, len(ctl_addrs)) c.ctl = make([]*http.Server, len(ctl_addrs)) diff --git a/server-ctl.go b/server-ctl.go index ad58262..e67f09c 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -20,7 +20,7 @@ type json_out_server_route struct { ServerPeerProto ROUTE_PROTO `json:"server-peer-proto"` } -type json_out_stats struct { +type json_out_server_stats struct { NumCPUs int `json:"num-cpus"` NumGoroutines int `json:"num-goroutines"` } @@ -43,7 +43,7 @@ type server_ctl_server_conns_id_routes_id struct { s *Server } -type server_ctl_server_stats struct { +type server_ctl_stats struct { s *Server } @@ -215,7 +215,7 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter // ------------------------------------ -func (ctl *server_ctl_server_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func (ctl *server_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) { var s *Server var status_code int var err error @@ -231,7 +231,7 @@ func (ctl *server_ctl_server_stats) ServeHTTP(w http.ResponseWriter, req *http.R switch req.Method { case http.MethodGet: - var stats json_out_stats + var stats json_out_server_stats stats.NumCPUs = runtime.NumCPU() stats.NumGoroutines = runtime.NumGoroutine() status_code = http.StatusOK; w.WriteHeader(status_code) diff --git a/server.go b/server.go index f0b2862..d426843 100644 --- a/server.go +++ b/server.go @@ -881,7 +881,7 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}", &server_ctl_server_conns_id{s: &s}) s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes", &server_ctl_server_conns_id_routes{s: &s}) s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes/{route_id}", &server_ctl_server_conns_id_routes_id{s: &s}) - s.ctl_mux.Handle(s.ctl_prefix + "/stats", &server_ctl_server_stats{s: &s}) + s.ctl_mux.Handle(s.ctl_prefix + "/stats", &server_ctl_stats{s: &s}) s.ctl_addr = make([]string, len(ctl_addrs)) s.ctl = make([]*http.Server, len(ctl_addrs))