mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Track start and uptime per connection
This commit is contained in:
2
TODO.md
2
TODO.md
@@ -20,7 +20,7 @@
|
||||
- [ ] Add to varz, time for slow consumers, peek or total connections, memory, etc.
|
||||
- [ ] Add starttime and uptime to connz list.
|
||||
- [ ] Add in HTTP requests to varz?
|
||||
- [ ] Add favico and help link for monitoring?
|
||||
- [X] Add favico and help link for monitoring?
|
||||
- [X] Better user/pass support using bcrypt etc.
|
||||
- [X] SSL/TLS support
|
||||
- [X] Add support for / to point to varz, connz, etc..
|
||||
|
||||
@@ -31,22 +31,23 @@ const (
|
||||
)
|
||||
|
||||
type client struct {
|
||||
mu sync.Mutex
|
||||
typ int
|
||||
cid uint64
|
||||
lang string
|
||||
opts clientOpts
|
||||
nc net.Conn
|
||||
mpay int
|
||||
ncs string
|
||||
bw *bufio.Writer
|
||||
srv *Server
|
||||
subs *hashmap.HashMap
|
||||
pcd map[*client]struct{}
|
||||
atmr *time.Timer
|
||||
ptmr *time.Timer
|
||||
pout int
|
||||
msgb [msgScratchSize]byte
|
||||
mu sync.Mutex
|
||||
typ int
|
||||
cid uint64
|
||||
lang string
|
||||
opts clientOpts
|
||||
start time.Time
|
||||
nc net.Conn
|
||||
mpay int
|
||||
ncs string
|
||||
bw *bufio.Writer
|
||||
srv *Server
|
||||
subs *hashmap.HashMap
|
||||
pcd map[*client]struct{}
|
||||
atmr *time.Timer
|
||||
ptmr *time.Timer
|
||||
pout int
|
||||
msgb [msgScratchSize]byte
|
||||
|
||||
parseState
|
||||
stats
|
||||
|
||||
@@ -34,21 +34,23 @@ type Connz struct {
|
||||
|
||||
// ConnInfo has detailed information on a per connection basis.
|
||||
type ConnInfo struct {
|
||||
Cid uint64 `json:"cid"`
|
||||
IP string `json:"ip"`
|
||||
Port int `json:"port"`
|
||||
Pending int `json:"pending_bytes"`
|
||||
InMsgs int64 `json:"in_msgs"`
|
||||
OutMsgs int64 `json:"out_msgs"`
|
||||
InBytes int64 `json:"in_bytes"`
|
||||
OutBytes int64 `json:"out_bytes"`
|
||||
NumSubs uint32 `json:"subscriptions"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Lang string `json:"lang,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
TLSVersion string `json:"tls_version,omitempty"`
|
||||
TLSCipher string `json:"tls_cipher_suite,omitempty"`
|
||||
Subs []string `json:"subscriptions_list,omitempty"`
|
||||
Cid uint64 `json:"cid"`
|
||||
IP string `json:"ip"`
|
||||
Port int `json:"port"`
|
||||
Start time.Time `json:"start"`
|
||||
Uptime string `json:"uptime"`
|
||||
Pending int `json:"pending_bytes"`
|
||||
InMsgs int64 `json:"in_msgs"`
|
||||
OutMsgs int64 `json:"out_msgs"`
|
||||
InBytes int64 `json:"in_bytes"`
|
||||
OutBytes int64 `json:"out_bytes"`
|
||||
NumSubs uint32 `json:"subscriptions"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Lang string `json:"lang,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
TLSVersion string `json:"tls_version,omitempty"`
|
||||
TLSCipher string `json:"tls_cipher_suite,omitempty"`
|
||||
Subs []string `json:"subscriptions_list,omitempty"`
|
||||
}
|
||||
|
||||
const DefaultConnListSize = 1024
|
||||
@@ -114,12 +116,17 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
pairs = pairs[minoff:maxoff]
|
||||
|
||||
// Use same now for all uptime calculations.
|
||||
now := time.Now()
|
||||
|
||||
for _, pair := range pairs {
|
||||
client := pair.Val
|
||||
client.mu.Lock()
|
||||
|
||||
ci := &ConnInfo{
|
||||
Cid: client.cid,
|
||||
Start: client.start,
|
||||
Uptime: myUptime(now.Sub(client.start)),
|
||||
InMsgs: client.inMsgs,
|
||||
OutMsgs: client.outMsgs,
|
||||
InBytes: client.inBytes,
|
||||
|
||||
@@ -218,6 +218,7 @@ func TestConnz(t *testing.T) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Got an error reading the body: %v\n", err)
|
||||
}
|
||||
@@ -270,6 +271,12 @@ func TestConnz(t *testing.T) {
|
||||
if ci.OutBytes != 5 {
|
||||
t.Fatalf("Expected OutBytes of 1, got %v\n", ci.OutBytes)
|
||||
}
|
||||
if ci.Start.IsZero() {
|
||||
t.Fatalf("Expected Start to be valid\n")
|
||||
}
|
||||
if ci.Uptime == "" {
|
||||
t.Fatalf("Expected Uptime to be valid\n")
|
||||
}
|
||||
|
||||
// Test JSONP
|
||||
respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback")
|
||||
|
||||
@@ -432,7 +432,7 @@ func (s *Server) startMonitoring(secure bool) {
|
||||
}
|
||||
|
||||
func (s *Server) createClient(conn net.Conn) *client {
|
||||
c := &client{srv: s, nc: conn, opts: defaultOpts, mpay: s.info.MaxPayload}
|
||||
c := &client{srv: s, nc: conn, opts: defaultOpts, mpay: s.info.MaxPayload, start: time.Now()}
|
||||
|
||||
// Grab JSON info string
|
||||
s.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user