serverconfig: conditionally install expvar handler.

Add local copy of the standard library's expvar handler.

Change-Id: Ida41553345d7d29665f33268356bf16a80386e38
This commit is contained in:
Bill Thiede 2013-10-04 21:24:24 -07:00
parent 74eb8e6c3c
commit b576dcc481
2 changed files with 24 additions and 0 deletions

View File

@ -62,6 +62,9 @@ CAMLI_DEV_KEYBLOBS (string):
CAMLI_HTTP_DEBUG (bool):
Enable per-request logging in pkg/webserver.
CAMLI_HTTP_EXPVAR (bool):
Enable json export of expvars at /debug/vars
CAMLI_HTTP_PPROF (bool):
Enable standard library's pprof handler at /debug/pprof/

View File

@ -22,6 +22,7 @@ package serverconfig
import (
"encoding/json"
"errors"
"expvar"
"fmt"
"io"
"log"
@ -409,6 +410,9 @@ func (config *Config) InstallHandlers(hi HandlerInstaller, baseURL string, conte
}
hl.setupAll()
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_HTTP_EXPVAR")); v {
hi.Handle("/debug/vars", expvarHandler{})
}
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_HTTP_PPROF")); v {
hi.Handle("/debug/pprof/", profileHandler{})
}
@ -426,6 +430,23 @@ func (s multiCloser) Close() (err error) {
return
}
// expvarHandler publishes expvar stats.
type expvarHandler struct{}
func (expvarHandler) ServeHTTP(w http.ResponseWriter, req *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")
}
// profileHandler publishes server profile information.
type profileHandler struct{}