Skip to content

Commit

Permalink
nsqd: add include_mem parameter to HTTP /stats to exclude memory stats
Browse files Browse the repository at this point in the history
  • Loading branch information
creker committed Jan 12, 2020
1 parent b527515 commit 4e91905
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
37 changes: 24 additions & 13 deletions nsqd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,17 @@ func (s *httpServer) doStats(w http.ResponseWriter, req *http.Request, ps httpro
topicName, _ := reqParams.Get("topic")
channelName, _ := reqParams.Get("channel")
includeClientsParam, _ := reqParams.Get("include_clients")
includeMemParam, _ := reqParams.Get("include_mem")
jsonFormat := formatString == "json"

includeClients, ok := boolParams[includeClientsParam]
if !ok {
includeClients = true
}
includeMem, ok := boolParams[includeMemParam]
if !ok {
includeMem = true
}
if includeClients {
producerStats = s.ctx.nsqd.GetProducerStats()
}
Expand Down Expand Up @@ -538,7 +543,11 @@ func (s *httpServer) doStats(w http.ResponseWriter, req *http.Request, ps httpro
producerStats = filteredProducerStats
}

ms := getMemStats()
var ms *memStats
if includeMem {
m := getMemStats()
ms = &m
}
if !jsonFormat {
return s.printStats(stats, producerStats, ms, health, startTime, uptime), nil
}
Expand All @@ -548,12 +557,12 @@ func (s *httpServer) doStats(w http.ResponseWriter, req *http.Request, ps httpro
Health string `json:"health"`
StartTime int64 `json:"start_time"`
Topics []TopicStats `json:"topics"`
Memory memStats `json:"memory"`
Memory *memStats `json:"memory,omitempty"`
Producers []ClientStats `json:"producers"`
}{version.Binary, health, startTime.Unix(), stats, ms, producerStats}, nil
}

func (s *httpServer) printStats(stats []TopicStats, producerStats []ClientStats, ms memStats, health string, startTime time.Time, uptime time.Duration) []byte {
func (s *httpServer) printStats(stats []TopicStats, producerStats []ClientStats, ms *memStats, health string, startTime time.Time, uptime time.Duration) []byte {
var buf bytes.Buffer
w := &buf

Expand All @@ -565,16 +574,18 @@ func (s *httpServer) printStats(stats []TopicStats, producerStats []ClientStats,

fmt.Fprintf(w, "\nHealth: %s\n", health)

fmt.Fprintf(w, "\nMemory:\n")
fmt.Fprintf(w, " %-25s\t%d\n", "heap_objects", ms.HeapObjects)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_idle_bytes", ms.HeapIdleBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_in_use_bytes", ms.HeapInUseBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_released_bytes", ms.HeapReleasedBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_100", ms.GCPauseUsec100)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_99", ms.GCPauseUsec99)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_95", ms.GCPauseUsec95)
fmt.Fprintf(w, " %-25s\t%d\n", "next_gc_bytes", ms.NextGCBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_total_runs", ms.GCTotalRuns)
if ms != nil {
fmt.Fprintf(w, "\nMemory:\n")
fmt.Fprintf(w, " %-25s\t%d\n", "heap_objects", ms.HeapObjects)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_idle_bytes", ms.HeapIdleBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_in_use_bytes", ms.HeapInUseBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "heap_released_bytes", ms.HeapReleasedBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_100", ms.GCPauseUsec100)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_99", ms.GCPauseUsec99)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_pause_usec_95", ms.GCPauseUsec95)
fmt.Fprintf(w, " %-25s\t%d\n", "next_gc_bytes", ms.NextGCBytes)
fmt.Fprintf(w, " %-25s\t%d\n", "gc_total_runs", ms.GCTotalRuns)
}

if len(stats) == 0 {
fmt.Fprintf(w, "\nTopics: None\n")
Expand Down
15 changes: 15 additions & 0 deletions nsqd/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ func TestHTTPClientStats(t *testing.T) {
} `json:"clients"`
} `json:"channels"`
} `json:"topics"`
Memory *struct{} `json:"memory,omitempty"`
}

endpoint := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)
Expand All @@ -533,6 +534,7 @@ func TestHTTPClientStats(t *testing.T) {

test.Equal(t, 1, len(d.Topics[0].Channels[0].Clients))
test.Equal(t, 1, d.Topics[0].Channels[0].ClientCount)
test.NotNil(t, d.Memory)

endpoint = fmt.Sprintf("http://127.0.0.1:%d/stats?format=json&include_clients=true", httpAddr.Port)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &d)
Expand All @@ -547,6 +549,19 @@ func TestHTTPClientStats(t *testing.T) {

test.Equal(t, 0, len(d.Topics[0].Channels[0].Clients))
test.Equal(t, 1, d.Topics[0].Channels[0].ClientCount)

endpoint = fmt.Sprintf("http://127.0.0.1:%d/stats?format=json&include_mem=true", httpAddr.Port)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &d)
test.Nil(t, err)

test.NotNil(t, d.Memory)

d.Memory = nil
endpoint = fmt.Sprintf("http://127.0.0.1:%d/stats?format=json&include_mem=false", httpAddr.Port)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &d)
test.Nil(t, err)

test.Nil(t, d.Memory)
}

func TestHTTPgetStatusJSON(t *testing.T) {
Expand Down

0 comments on commit 4e91905

Please sign in to comment.