diff --git a/server/monitor.go b/server/monitor.go index 00614c50..8ba4f9b8 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -26,6 +26,7 @@ func init() { // Connz represents detailed information on current client connections. type Connz struct { + ID string `json:"server_id"` Now time.Time `json:"now"` NumConns int `json:"num_connections"` Total int `json:"total"` @@ -95,6 +96,9 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) { s.httpReqStats[ConnzPath]++ tlsRequired := s.info.TLSRequired + // copy the server id for monitoring + c.ID = s.info.ID + // number total of clients. The resulting ConnInfo array // may be smaller if pagination is used. totalClients := len(s.clients) @@ -264,6 +268,7 @@ type Subsz struct { // Routez represents detailed information on current client connections. type Routez struct { + ID string `json:"server_id"` Now time.Time `json:"now"` NumRoutes int `json:"num_routes"` Routes []*RouteInfo `json:"routes"` @@ -299,6 +304,9 @@ func (s *Server) HandleRoutez(w http.ResponseWriter, r *http.Request) { s.httpReqStats[RoutezPath]++ rs.NumRoutes = len(s.routes) + // copy the server id for monitoring + rs.ID = s.info.ID + for _, r := range s.routes { r.mu.Lock() ri := &RouteInfo{ diff --git a/server/monitor_test.go b/server/monitor_test.go index a94e0e67..a7790af0 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -1435,3 +1435,68 @@ func TestMonitorRoutezRace(t *testing.T) { } } } + +func getJSONData(url string) ([]byte, error) { + resp, err := http.Get(url) + if err != nil { + return nil, fmt.Errorf("Expected no error from %s: Got %v\n", url, err) + } + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Expected a 200 response from %s, got %d\n", url, resp.StatusCode) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("Got an error reading the %s body: %v\n", url, err) + } + return body, nil +} + +func TestServerIDs(t *testing.T) { + s := runMonitorServer() + defer s.Shutdown() + + v := Varz{} + c := Connz{} + r := Routez{} + + murl := fmt.Sprintf("http://localhost:%d/", s.MonitorAddr().Port) + + data, err := getJSONData(murl + "varz") + if err != nil { + t.Fatalf("%v", err) + } + if err := json.Unmarshal(data, &v); err != nil { + t.Fatalf("Got an error unmarshalling the varz body: %v\n", err) + } + + data, err = getJSONData(murl + "connz") + if err != nil { + t.Fatalf("%v", err) + } + if err := json.Unmarshal(data, &c); err != nil { + t.Fatalf("Got an error unmarshalling the connz body: %v\n", err) + } + + data, err = getJSONData(murl + "routez") + if err != nil { + t.Fatalf("%v", err) + } + if err := json.Unmarshal(data, &r); err != nil { + t.Fatalf("Got an error unmarshalling the connz routez: %v\n", err) + } + + if v.ID == "" { + t.Fatal("Varz ID is empty") + } + if c.ID == "" { + t.Fatal("Connz ID is empty") + } + if r.ID == "" { + t.Fatal("Routez ID is empty") + } + + if v.ID != c.ID || v.ID != r.ID { + t.Fatalf("Varz ID [%s] is not equal to Connz ID [%s] or Routez ID [%s]", v.ID, c.ID, r.ID) + } +}