You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/08/08 20:45:53 UTC
[GitHub] rob05c closed pull request #2648: add pprof debugging served on
localhost:6060
rob05c closed pull request #2648: add pprof debugging served on localhost:6060
URL: https://github.com/apache/trafficcontrol/pull/2648
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 13e047040..2f1249197 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -386,54 +386,41 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
{http.MethodGet, `tools/write_crconfig/{cdn}/?$`, crconfig.SnapshotOldGUIHandler(d.DB, d.Config), auth.PrivLevelOperations, Authenticated, nil},
// DEPRECATED - use GET /api/1.2/cdns/{cdn}/snapshot
{http.MethodGet, `CRConfig-Snapshots/{cdn}/CRConfig.json?$`, crconfig.SnapshotOldGetHandler(d.DB, d.Config), auth.PrivLevelReadOnly, Authenticated, nil},
- // USED FOR Debugging
- {http.MethodGet, `admin/memory-stats`, memoryStatsHandler(d.Profiling), auth.PrivLevelOperations, Authenticated, nil},
- {http.MethodGet, `admin/db-stats`, dbStatsHandler(d.Profiling, d.DB), auth.PrivLevelOperations, Authenticated, nil},
}
return routes, rawRoutes, proxyHandler, nil
}
-func memoryStatsHandler(profiling *bool) http.HandlerFunc {
+func memoryStatsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handleErrs := tc.GetHandleErrorsFunc(w, r)
- if *profiling {
- stats := runtime.MemStats{}
- runtime.ReadMemStats(&stats)
-
- bytes, err := json.Marshal(stats)
- if err != nil {
- tclog.Errorln("unable to marshal stats: " + err.Error())
- handleErrs(http.StatusInternalServerError, errors.New("marshalling error"))
- return
- }
- w.Header().Set("Content-Type", "application/json")
- w.Write(bytes)
- } else {
- handleErrs(http.StatusPreconditionFailed, errors.New("profiling is not enabled"))
+ stats := runtime.MemStats{}
+ runtime.ReadMemStats(&stats)
+
+ bytes, err := json.Marshal(stats)
+ if err != nil {
+ tclog.Errorln("unable to marshal stats: " + err.Error())
+ handleErrs(http.StatusInternalServerError, errors.New("marshalling error"))
return
}
+ w.Header().Set("Content-Type", "application/json")
+ w.Write(bytes)
}
}
-func dbStatsHandler(profiling *bool, db *sqlx.DB) http.HandlerFunc {
+func dbStatsHandler(db *sqlx.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
handleErrs := tc.GetHandleErrorsFunc(w, r)
- if *profiling {
- stats := db.DB.Stats()
-
- bytes, err := json.Marshal(stats)
- if err != nil {
- tclog.Errorln("unable to marshal stats: " + err.Error())
- handleErrs(http.StatusInternalServerError, errors.New("marshalling error"))
- return
- }
- w.Header().Set("Content-Type", "application/json")
- w.Write(bytes)
- } else {
- handleErrs(http.StatusPreconditionFailed, errors.New("profiling is not enabled"))
+ stats := db.DB.Stats()
+
+ bytes, err := json.Marshal(stats)
+ if err != nil {
+ tclog.Errorln("unable to marshal stats: " + err.Error())
+ handleErrs(http.StatusInternalServerError, errors.New("marshalling error"))
return
}
+ w.Header().Set("Content-Type", "application/json")
+ w.Write(bytes)
}
}
diff --git a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
index 0de7644ca..c8348fbf5 100644
--- a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
+++ b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
@@ -24,6 +24,7 @@ import (
"flag"
"fmt"
"net/http"
+ _ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
@@ -125,6 +126,19 @@ func main() {
profiling := cfg.ProfilingEnabled
+ pprofMux := http.DefaultServeMux
+ http.DefaultServeMux = http.NewServeMux() // this is so we don't serve pprof over 443.
+
+ pprofMux.Handle("/db-stats", dbStatsHandler(db))
+ pprofMux.Handle("/memory-stats", memoryStatsHandler())
+ go func() {
+ debugServer := http.Server{
+ Addr: "localhost:6060",
+ Handler: pprofMux,
+ }
+ log.Errorln(debugServer.ListenAndServe())
+ }()
+
if err := RegisterRoutes(ServerData{DB: db, Config: cfg, Profiling: &profiling}); err != nil {
log.Errorf("registering routes: %v\n", err)
return
@@ -179,6 +193,7 @@ func SetNewProfilingInfo(configFileName string, currentProfilingEnabled *bool, c
}
if *currentProfilingEnabled != newProfilingEnabled {
log.Infof("profiling enabled set to %t\n", newProfilingEnabled)
+ log.Infof("profiling location set to: %s\n", *currentProfilingLocation)
*currentProfilingEnabled = newProfilingEnabled
if *currentProfilingEnabled {
continuousProfile(currentProfilingEnabled, currentProfilingLocation, version)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services