From f488ece6bda5cac7ff043ebc985cbd2688beb668 Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Tue, 14 Jul 2015 14:08:09 -0700 Subject: [PATCH] Added info, better uptime formatting --- server/monitor.go | 33 +++++++++++++++++++++++++++++---- server/monitor_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/server/monitor.go b/server/monitor.go index 51a85ef8..5732a757 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -4,6 +4,7 @@ package server import ( "encoding/json" + "fmt" "net" "net/http" "runtime" @@ -195,8 +196,10 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) { // Varz will output server information on the monitoring port at /varz. type Varz struct { - Start time.Time `json:"start"` + Information *Info `json:"info"` Options *Options `json:"options"` + Start time.Time `json:"start"` + Uptime string `json:"uptime"` Mem int64 `json:"mem"` Cores int `json:"cores"` CPU float64 `json:"cpu"` @@ -207,7 +210,6 @@ type Varz struct { OutMsgs int64 `json:"out_msgs"` InBytes int64 `json:"in_bytes"` OutBytes int64 `json:"out_bytes"` - Uptime string `json:"uptime"` } type usage struct { @@ -216,10 +218,33 @@ type usage struct { Mem int64 } +func myUptime(d time.Duration) string { + // Just use total seconds for uptime, and display days / years + tsecs := d / time.Second + tmins := tsecs / 60 + thrs := tmins / 60 + tdays := thrs / 24 + tyrs := tdays / 365 + + if tyrs > 0 { + return fmt.Sprintf("%dy%dd%dh%dm%ds", tyrs, tdays%365, thrs%24, tmins%60, tsecs%60) + } + if tdays > 0 { + return fmt.Sprintf("%dd%dh%dm%ds", tdays, thrs%24, tmins%60, tsecs%60) + } + if thrs > 0 { + return fmt.Sprintf("%dh%dm%ds", thrs, tmins%60, tsecs%60) + } + if tmins > 0 { + return fmt.Sprintf("%dm%ds", tmins, tsecs%60) + } + return fmt.Sprintf("%ds", tsecs) +} + // HandleVarz will process HTTP requests for server information. func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) { - v := &Varz{Start: s.start, Options: s.opts} - v.Uptime = time.Since(s.start).String() + v := &Varz{Information: &s.info, Options: s.opts, Start: s.start} + v.Uptime = myUptime(time.Since(s.start)) updateUsage(v) diff --git a/server/monitor_test.go b/server/monitor_test.go index fbbc3ed1..f538b9d4 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -38,6 +38,38 @@ func resetPreviousHTTPConnections() { http.DefaultTransport = &http.Transport{} } +func TestMyUptime(t *testing.T) { + // Make sure we print this stuff right. + var d time.Duration + var s string + + d = 22 * time.Second + s = myUptime(d) + if s != "22s" { + t.Fatalf("Expected `22s`, go ``%s`", s) + } + d = 4*time.Minute + d + s = myUptime(d) + if s != "4m22s" { + t.Fatalf("Expected `4m22s`, go ``%s`", s) + } + d = 4*time.Hour + d + s = myUptime(d) + if s != "4h4m22s" { + t.Fatalf("Expected `4h4m22s`, go ``%s`", s) + } + d = 32*24*time.Hour + d + s = myUptime(d) + if s != "32d4h4m22s" { + t.Fatalf("Expected `32d4h4m22s`, go ``%s`", s) + } + d = 22*365*24*time.Hour + d + s = myUptime(d) + if s != "22y32d4h4m22s" { + t.Fatalf("Expected `22y32d4h4m22s`, go ``%s`", s) + } +} + // Make sure that we do not run the http server for monitoring unless asked. func TestNoMonitorPort(t *testing.T) { s := runMonitorServer(0)