Add debug server to support pprof and expvar

If configured, a debug http server will be started to serve default registered
endpoints, such as pprof and expvar. The endpoint should be secured carefully
and not available to external traffic. It is disabled by default but the
development config has been modified to make it available on localhost.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2015-01-28 15:45:25 -08:00
parent e5de2594ad
commit 499382dd0b
3 changed files with 26 additions and 0 deletions

View file

@ -5,3 +5,6 @@ storage:
rootdirectory: /tmp/registry-dev
http:
addr: :5000
secret: asecretforlocaldevelopment
debug:
addr: localhost:5001

View file

@ -1,6 +1,7 @@
package main
import (
_ "expvar"
"flag"
"fmt"
"net/http"
@ -47,6 +48,10 @@ func main() {
handler = handlers.CombinedLoggingHandler(os.Stdout, handler)
log.SetLevel(logLevel(config.Loglevel))
if config.HTTP.Debug.Addr != "" {
go debugServer(config.HTTP.Debug.Addr)
}
if config.HTTP.TLS.Certificate == "" {
log.Infof("listening on %v", config.HTTP.Addr)
if err := http.ListenAndServe(config.HTTP.Addr, handler); err != nil {
@ -142,3 +147,13 @@ func configureReporting(app *registry.App) http.Handler {
return handler
}
// debugServer starts the debug server with pprof, expvar among other
// endpoints. The addr should not be exposed externally. For most of these to
// work, tls cannot be enabled on the endpoint, so it is generally separate.
func debugServer(addr string) {
log.Infof("debug server listening %v", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("error listening on debug interface: %v", err)
}
}

View file

@ -54,6 +54,14 @@ type Configuration struct {
// Certificate.
Key string `yaml:"key"`
} `yaml:"tls"`
// Debug configures the http debug interface, if specified. This can
// include services such as pprof, expvar and other data that should
// not be exposed externally. Left disabled by default.
Debug struct {
// Addr specifies the bind address for the debug server.
Addr string `yaml:"addr"`
} `yaml:"debug"`
} `yaml:"http"`
}