Skip to content

Commit

Permalink
Serve expvar information from HTTP package
Browse files Browse the repository at this point in the history
  • Loading branch information
otoolep committed Sep 1, 2015
1 parent 26147b2 commit 366c011
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
1 change: 0 additions & 1 deletion etc/config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion monitor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions monitor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 0 additions & 16 deletions monitor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"expvar"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -38,8 +37,6 @@ type Service struct {
storeAddress string
storeInterval time.Duration

expvarAddress string

Logger *log.Logger
}

Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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
}

Expand Down
23 changes: 20 additions & 3 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"encoding/json"
"errors"
"expvar"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 366c011

Please sign in to comment.