You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ro...@apache.org on 2018/08/08 20:45:54 UTC

[trafficcontrol] branch master updated: add pprof debugging served on localhost:6060 and move db-stats and memory-stats endpoints there as well

This is an automated email from the ASF dual-hosted git repository.

rob pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 8f303e1  add pprof debugging served on localhost:6060 and move db-stats and memory-stats endpoints there as well
8f303e1 is described below

commit 8f303e18106ec51b1431284ef89be9537ebf6385
Author: Dylan Volz <Dy...@comcast.com>
AuthorDate: Wed Aug 8 14:02:11 2018 -0600

    add pprof debugging served on localhost:6060 and move db-stats and memory-stats endpoints there as well
---
 traffic_ops/traffic_ops_golang/routes.go           | 51 ++++++++--------------
 .../traffic_ops_golang/traffic_ops_golang.go       | 15 +++++++
 2 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 13e0470..2f12491 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 0de7644..c8348fb 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)