From 4e91905985d3e870814707c17cd01006a7a43b81 Mon Sep 17 00:00:00 2001 From: creker Date: Sun, 12 Jan 2020 23:56:57 +0300 Subject: [PATCH] nsqd: add include_mem parameter to HTTP /stats to exclude memory stats --- nsqd/http.go | 37 ++++++++++++++++++++++++------------- nsqd/http_test.go | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/nsqd/http.go b/nsqd/http.go index 9914b093f..fef9a05da 100644 --- a/nsqd/http.go +++ b/nsqd/http.go @@ -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() } @@ -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 } @@ -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 @@ -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") diff --git a/nsqd/http_test.go b/nsqd/http_test.go index 47e42f0bb..c6cb3df14 100644 --- a/nsqd/http_test.go +++ b/nsqd/http_test.go @@ -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) @@ -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) @@ -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) {