Report cmdline and memstats in /debug/vars

When we refactored expvar, the cmdline and memstats sections were not
readded to the output. This adds it back if they can be found inside of
`expvar`.

It also stops trying to sort the output of the statistics so they get
returned faster. JSON doesn't need them to be sorted and it causes
enough latency problems that sorting them hurts performance.
pull/7277/head
Jonathan A. Sternberg 2016-09-09 14:32:41 -05:00
parent 337dcb6bdb
commit ab4bca8495
3 changed files with 27 additions and 25 deletions

View File

@ -4,6 +4,7 @@
- [#7271](https://github.com/influxdata/influxdb/issues/7271): Fixing typo within example configuration file. Thanks @andyfeller!
- [#7270](https://github.com/influxdata/influxdb/issues/7270): Implement time math for lazy time literals.
- [#7272](https://github.com/influxdata/influxdb/issues/7272): Report cmdline and memstats in /debug/vars.
## v1.0.0 [2016-09-07]

View File

@ -313,8 +313,6 @@ func (m *Monitor) Statistics(tags map[string]string) ([]*Statistic, error) {
statistics = append(statistics, statistic)
statistics = m.gatherStatistics(statistics, tags)
sort.Sort(Statistics(statistics))
return statistics, nil
}

View File

@ -5,6 +5,7 @@ import (
"compress/gzip"
"encoding/json"
"errors"
"expvar"
"fmt"
"io"
"log"
@ -12,7 +13,6 @@ import (
"net/http/pprof"
"os"
"runtime/debug"
"sort"
"strconv"
"strings"
"sync/atomic"
@ -690,8 +690,31 @@ func (h *Handler) serveExpvar(w http.ResponseWriter, r *http.Request) {
return
}
m := make(map[string]*monitor.Statistic)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintln(w, "{")
first := true
if val := expvar.Get("cmdline"); val != nil {
if !first {
fmt.Fprintln(w, ",")
}
first = false
fmt.Fprintf(w, "\"cmdline\": %s", val)
}
if val := expvar.Get("memstats"); val != nil {
if !first {
fmt.Fprintln(w, ",")
}
first = false
fmt.Fprintf(w, "\"memstats\": %s", val)
}
for _, s := range stats {
val, err := json.Marshal(s)
if err != nil {
continue
}
// Very hackily create a unique key.
buf := bytes.NewBufferString(s.Name)
if path, ok := s.Tags["path"]; ok {
@ -718,32 +741,12 @@ func (h *Handler) serveExpvar(w http.ResponseWriter, r *http.Request) {
}
key := buf.String()
m[key] = s
}
// Sort the keys to simulate /debug/vars output.
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintln(w, "{")
first := true
for _, key := range keys {
// Marshal this statistic to JSON.
out, err := json.Marshal(m[key])
if err != nil {
continue
}
if !first {
fmt.Fprintln(w, ",")
}
first = false
fmt.Fprintf(w, "%q: ", key)
w.Write(bytes.TrimSpace(out))
w.Write(bytes.TrimSpace(val))
}
fmt.Fprintln(w, "\n}")
}