From 2362b397fa3dffc25ad5b1733e139feb56dee883 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 23 Feb 2014 16:37:33 -0800 Subject: [PATCH] consul: Adding Stats() method to get various debugging information --- consul/client.go | 19 +++++++++++++++++++ consul/server.go | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/consul/client.go b/consul/client.go index 5f9fc99063..b209df4317 100644 --- a/consul/client.go +++ b/consul/client.go @@ -9,6 +9,7 @@ import ( "net" "os" "path/filepath" + "strconv" "strings" "sync" "time" @@ -307,3 +308,21 @@ TRY_RPC: c.lastRPCTime = time.Now() return nil } + +// Stats is used to return statistics for debugging and insight +// for various sub-systems +func (c *Client) Stats() map[string]map[string]string { + toString := func(v uint64) string { + return strconv.FormatUint(v, 10) + } + stats := map[string]map[string]string{ + "consul": map[string]string{ + "server": "false", + }, + "serf-lan": map[string]string{ + "members": toString(uint64(len(c.serf.Members()))), + "servers": toString(uint64(len(c.consuls))), + }, + } + return stats +} diff --git a/consul/server.go b/consul/server.go index 9d85544a4d..f78eca859f 100644 --- a/consul/server.go +++ b/consul/server.go @@ -9,6 +9,7 @@ import ( "net/rpc" "os" "path/filepath" + "strconv" "sync" "time" ) @@ -452,3 +453,27 @@ func (s *Server) RPC(method string, args interface{}, reply interface{}) error { addr := s.rpcListener.Addr() return s.connPool.RPC(addr, method, args, reply) } + +// Stats is used to return statistics for debugging and insight +// for various sub-systems +func (s *Server) Stats() map[string]map[string]string { + toString := func(v uint64) string { + return strconv.FormatUint(v, 10) + } + stats := map[string]map[string]string{ + "consul": map[string]string{ + "server": "true", + "leader": fmt.Sprintf("%v", s.IsLeader()), + "bootstrap": fmt.Sprintf("%v", s.config.Bootstrap), + }, + "raft": s.raft.Stats(), + "serf-lan": map[string]string{ + "members": toString(uint64(len(s.serfLAN.Members()))), + }, + "serf-wan": map[string]string{ + "members": toString(uint64(len(s.serfWAN.Members()))), + "datacenters": toString(uint64(len(s.remoteConsuls))), + }, + } + return stats +}