-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tharun <rajendrantharun@live.com>
- Loading branch information
Showing
8 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/railgun/internal/mgrutils" | ||
"github.com/kong/kubernetes-ingress-controller/railgun/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type diagnosticsServer struct { | ||
logger logrus.FieldLogger | ||
mux *http.ServeMux | ||
} | ||
|
||
func NewDiagnosticsServer(enableProfiling bool, log logrus.FieldLogger) *diagnosticsServer { | ||
mux := http.NewServeMux() | ||
if enableProfiling { | ||
mgrutils.Install(mux) | ||
} | ||
return &diagnosticsServer{ | ||
logger: log, | ||
mux: mux, | ||
} | ||
} | ||
|
||
func (s *diagnosticsServer) Start(ctx context.Context) error { | ||
httpServer := &http.Server{Addr: fmt.Sprintf(":%d", config.DiagnosticsPort), Handler: s.mux} | ||
errChan := make(chan error) | ||
go func() { | ||
err := httpServer.ListenAndServe() | ||
if err != nil { | ||
switch err { | ||
case http.ErrServerClosed: | ||
s.logger.Info("shutting down diagnostics server") | ||
default: | ||
s.logger.Error(err, "could not start a diagnostics server") | ||
errChan <- err | ||
} | ||
} | ||
}() | ||
|
||
s.logger.Info("started diagnostics server at port ", config.DiagnosticsPort) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
s.logger.Info("stopping down diagnostics server") | ||
return httpServer.Shutdown(context.Background()) | ||
case err := <-errChan: | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mgrutils | ||
|
||
import ( | ||
"net/http" | ||
"net/http/pprof" | ||
) | ||
|
||
// Install adds the Profiling webservice to the given mux. | ||
func Install(mux *http.ServeMux) { | ||
mux.HandleFunc("/debug/pprof", redirectTo("/debug/pprof/")) | ||
mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/heap", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/mutex", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/goroutine", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/threadcreate", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/block", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | ||
} | ||
|
||
// redirectTo redirects request to a certain destination. | ||
func redirectTo(to string) func(http.ResponseWriter, *http.Request) { | ||
return func(rw http.ResponseWriter, req *http.Request) { | ||
http.Redirect(rw, req, to, http.StatusFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters