Browse Source

trace: Export Render and RenderEvents functions.

These expose the main HTML rendering functions of this package
for use with alternate HTTP muxes and on alternate paths.

Fixes golang/go#12195.

Change-Id: I679583fd26116bc83ff551a5d2a1d73ffa1e01f0
Reviewed-on: https://go-review.googlesource.com/13825
Reviewed-by: Dave Day <djd@golang.org>
David Symonds 10 years ago
parent
commit
7654728e38
2 changed files with 29 additions and 19 deletions
  1. 20 14
      trace/events.go
  2. 9 5
      trace/trace.go

+ 20 - 14
trace/events.go

@@ -43,7 +43,11 @@ var buckets = []bucket{
 	{24000 * time.Hour, "errors"},
 }
 
-func renderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
+// RenderEvents renders the HTML page typically served at /debug/events.
+// It does not do any auth checking; see AuthRequest for the default auth check
+// used by the handler registered on http.DefaultServeMux.
+// req may be nil.
+func RenderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
 	now := time.Now()
 	data := &struct {
 		Families []string // family names
@@ -78,19 +82,21 @@ func renderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
 		}
 	}
 
-	var ok bool
-	data.Family, data.Bucket, ok = parseEventsArgs(req)
-	if !ok {
-		// No-op
-	} else {
-		data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge)
-	}
-	if data.EventLogs != nil {
-		defer data.EventLogs.Free()
-		sort.Sort(data.EventLogs)
-	}
-	if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil {
-		data.Expanded = exp
+	if req != nil {
+		var ok bool
+		data.Family, data.Bucket, ok = parseEventsArgs(req)
+		if !ok {
+			// No-op
+		} else {
+			data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge)
+		}
+		if data.EventLogs != nil {
+			defer data.EventLogs.Free()
+			sort.Sort(data.EventLogs)
+		}
+		if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil {
+			data.Expanded = exp
+		}
 	}
 
 	famMu.RLock()

+ 9 - 5
trace/trace.go

@@ -84,7 +84,9 @@ import (
 // FOR DEBUGGING ONLY. This will slow down the program.
 var DebugUseAfterFinish = false
 
-// AuthRequest determines whether a specific request is permitted to load the /debug/requests page.
+// AuthRequest determines whether a specific request is permitted to load the
+// /debug/requests or /debug/events pages.
+//
 // It returns two bools; the first indicates whether the page may be viewed at all,
 // and the second indicates whether sensitive events will be shown.
 //
@@ -110,7 +112,7 @@ func init() {
 			http.Error(w, "not allowed", http.StatusUnauthorized)
 			return
 		}
-		render(w, req, sensitive)
+		Render(w, req, sensitive)
 	})
 	http.HandleFunc("/debug/events", func(w http.ResponseWriter, req *http.Request) {
 		any, sensitive := AuthRequest(req)
@@ -118,13 +120,15 @@ func init() {
 			http.Error(w, "not allowed", http.StatusUnauthorized)
 			return
 		}
-		renderEvents(w, req, sensitive)
+		RenderEvents(w, req, sensitive)
 	})
 }
 
-// render renders the HTML page.
+// Render renders the HTML page typically served at /debug/requests.
+// It does not do any auth checking; see AuthRequest for the default auth check
+// used by the handler registered on http.DefaultServeMux.
 // req may be nil.
-func render(w io.Writer, req *http.Request, sensitive bool) {
+func Render(w io.Writer, req *http.Request, sensitive bool) {
 	data := &struct {
 		Families         []string
 		ActiveTraceCount map[string]int