Skip to content

Commit

Permalink
Add option to not register instrumentation routes. (#47)
Browse files Browse the repository at this point in the history
* Add option to not register instrumentation routes, so Server can be used for internet-facing services.

* Review feedback.
  • Loading branch information
tomwilkie authored and jml committed Jul 20, 2017
1 parent a3605f7 commit aef9a42
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Config struct {
HTTPListenPort int
GRPCListenPort int

RegisterInstrumentation bool

ServerGracefulShutdownTimeout time.Duration
HTTPServerReadTimeout time.Duration
HTTPServerWriteTimeout time.Duration
Expand All @@ -53,6 +55,7 @@ type Config struct {
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.IntVar(&cfg.HTTPListenPort, "server.http-listen-port", 80, "HTTP server listen port.")
f.IntVar(&cfg.GRPCListenPort, "server.grpc-listen-port", 9095, "gRPC server listen port.")
f.BoolVar(&cfg.RegisterInstrumentation, "server.register-instrumentation", true, "Register the intrumentation handlers (/metrics etc).")
f.DurationVar(&cfg.ServerGracefulShutdownTimeout, "server.graceful-shutdown-timeout", 5*time.Second, "Timeout for graceful shutdowns")
f.DurationVar(&cfg.HTTPServerReadTimeout, "server.http-read-timeout", 5*time.Second, "Read timeout for HTTP server")
f.DurationVar(&cfg.HTTPServerWriteTimeout, "server.http-write-timeout", 5*time.Second, "Write timeout for HTTP server")
Expand Down Expand Up @@ -111,9 +114,9 @@ func New(cfg Config) (*Server, error) {

// Setup HTTP server
router := mux.NewRouter()
router.Handle("/metrics", prometheus.Handler())
router.Handle("/traces", loki.Handler())
router.PathPrefix("/debug/pprof").Handler(http.DefaultServeMux)
if cfg.RegisterInstrumentation {
RegisterInstrumentation(router)
}
httpMiddleware := []middleware.Interface{
middleware.Log{},
middleware.Instrument{
Expand Down Expand Up @@ -144,6 +147,13 @@ func New(cfg Config) (*Server, error) {
}, nil
}

// RegisterInstrumentation on the given router.
func RegisterInstrumentation(router *mux.Router) {
router.Handle("/metrics", prometheus.Handler())
router.Handle("/traces", loki.Handler())
router.PathPrefix("/debug/pprof").Handler(http.DefaultServeMux)
}

// Run the server; blocks until SIGTERM is received.
func (s *Server) Run() {
go s.httpServer.Serve(s.httpListener)
Expand Down

0 comments on commit aef9a42

Please sign in to comment.