diff --git a/doc/environment-vars.txt b/doc/environment-vars.txt index e1b0c0d79..18e0dde2b 100644 --- a/doc/environment-vars.txt +++ b/doc/environment-vars.txt @@ -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/ diff --git a/pkg/serverconfig/serverconfig.go b/pkg/serverconfig/serverconfig.go index 119aaab05..506816c9b 100644 --- a/pkg/serverconfig/serverconfig.go +++ b/pkg/serverconfig/serverconfig.go @@ -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{}