From 366c0115f91b3b04d4b35b90901f5e7b645625d0 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Tue, 1 Sep 2015 14:57:15 -0700 Subject: [PATCH] Serve expvar information from HTTP package --- etc/config.sample.toml | 1 - monitor/config.go | 1 - monitor/config_test.go | 5 +---- monitor/service.go | 16 ---------------- services/httpd/handler.go | 23 ++++++++++++++++++++--- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/etc/config.sample.toml b/etc/config.sample.toml index e5964c42de2..5910533a550 100644 --- a/etc/config.sample.toml +++ b/etc/config.sample.toml @@ -97,7 +97,6 @@ reporting-disabled = false store-database = "_internal" # The destination database for recorded statistics store-interval = "1m" # The interval at which to record statistics store-address = "http://127.0.0.1" # The protocol and host for the recorded data - # expvar-address = "127.0.0.1:9950" # Unset to expose expvar data over HTTP. ### ### [admin] diff --git a/monitor/config.go b/monitor/config.go index 324a62dea5e..061a472d5ae 100644 --- a/monitor/config.go +++ b/monitor/config.go @@ -27,7 +27,6 @@ type Config struct { StoreDatabase string `toml:"store-database"` StoreInterval toml.Duration `toml:"store-interval"` StoreAddress string `toml:"store-address"` - ExpvarAddress string `toml:"expvar-address"` } // NewConfig returns an instance of Config with defaults. diff --git a/monitor/config_test.go b/monitor/config_test.go index d3ec3fda14b..0626a7cfcae 100644 --- a/monitor/config_test.go +++ b/monitor/config_test.go @@ -16,21 +16,18 @@ store-enabled=true store-database="the_db" store-interval="10m" store-address="server1" -expvar-address="127.0.0.1:9950" `, &c); err != nil { t.Fatal(err) } // Validate configuration. if !c.StoreEnabled { - t.Fatalf("unexpected store-enabled: %s", c.StoreEnabled) + t.Fatalf("unexpected store-enabled: %v", c.StoreEnabled) } else if c.StoreDatabase != "the_db" { t.Fatalf("unexpected store-database: %s", c.StoreDatabase) } else if time.Duration(c.StoreInterval) != 10*time.Minute { t.Fatalf("unexpected store-interval: %s", c.StoreInterval) } else if c.StoreAddress != "server1" { t.Fatalf("unexpected store-address: %s", c.StoreAddress) - } else if c.ExpvarAddress != "127.0.0.1:9950" { - t.Fatalf("unexpected expvar-address: %s", c.ExpvarAddress) } } diff --git a/monitor/service.go b/monitor/service.go index 5c732824498..26381e06980 100644 --- a/monitor/service.go +++ b/monitor/service.go @@ -4,7 +4,6 @@ import ( "expvar" "fmt" "log" - "net" "net/http" "net/url" "os" @@ -38,8 +37,6 @@ type Service struct { storeAddress string storeInterval time.Duration - expvarAddress string - Logger *log.Logger } @@ -51,7 +48,6 @@ func NewService(c Config) *Service { storeDatabase: c.StoreDatabase, storeAddress: c.StoreAddress, storeInterval: time.Duration(c.StoreInterval), - expvarAddress: c.ExpvarAddress, Logger: log.New(os.Stderr, "[monitor] ", log.LstdFlags), } } @@ -82,18 +78,6 @@ func (s *Service) Open(clusterID, nodeID uint64, hostname string) error { go s.storeStatistics() } - // If enabled, expose all expvar data over HTTP. - if s.expvarAddress != "" { - listener, err := net.Listen("tcp", s.expvarAddress) - if err != nil { - return err - } - - go func() { - http.Serve(listener, nil) - }() - s.Logger.Printf("expvar information available on %s", s.expvarAddress) - } return nil } diff --git a/services/httpd/handler.go b/services/httpd/handler.go index 61684d6f7f9..b1011a72c7d 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "encoding/json" "errors" + "expvar" "fmt" "io" "io/ioutil" @@ -161,10 +162,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { default: pprof.Index(w, r) } - return + } else if strings.HasPrefix(r.URL.Path, "/debug/vars") { + serveExpvar(w, r) + } else { + h.mux.ServeHTTP(w, r) } - - h.mux.ServeHTTP(w, r) } func (h *Handler) serveProcessContinuousQueries(w http.ResponseWriter, r *http.Request, user *meta.UserInfo) { @@ -569,6 +571,21 @@ type Batch struct { Points []Point `json:"points"` } +// serveExpvar serves registered expvar information over HTTP. +func serveExpvar(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, "{\n") + first := true + expvar.Do(func(kv expvar.KeyValue) { + if !first { + fmt.Fprintf(w, ",\n") + } + first = false + fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) + }) + fmt.Fprintf(w, "\n}\n") +} + // httpError writes an error to the client in a standard format. func httpError(w http.ResponseWriter, error string, pretty bool, code int) { w.Header().Add("content-type", "application/json")