Commit 7654728e authored by David Symonds's avatar David Symonds

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/13825Reviewed-by: 's avatarDave Day <djd@golang.org>
parent 788d8d9e
...@@ -43,7 +43,11 @@ var buckets = []bucket{ ...@@ -43,7 +43,11 @@ var buckets = []bucket{
{24000 * time.Hour, "errors"}, {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() now := time.Now()
data := &struct { data := &struct {
Families []string // family names Families []string // family names
...@@ -78,19 +82,21 @@ func renderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) { ...@@ -78,19 +82,21 @@ func renderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
} }
} }
var ok bool if req != nil {
data.Family, data.Bucket, ok = parseEventsArgs(req) var ok bool
if !ok { data.Family, data.Bucket, ok = parseEventsArgs(req)
// No-op if !ok {
} else { // No-op
data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge) } else {
} data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge)
if data.EventLogs != nil { }
defer data.EventLogs.Free() if data.EventLogs != nil {
sort.Sort(data.EventLogs) defer data.EventLogs.Free()
} sort.Sort(data.EventLogs)
if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil { }
data.Expanded = exp if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil {
data.Expanded = exp
}
} }
famMu.RLock() famMu.RLock()
......
...@@ -84,7 +84,9 @@ import ( ...@@ -84,7 +84,9 @@ import (
// FOR DEBUGGING ONLY. This will slow down the program. // FOR DEBUGGING ONLY. This will slow down the program.
var DebugUseAfterFinish = false 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, // 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. // and the second indicates whether sensitive events will be shown.
// //
...@@ -110,7 +112,7 @@ func init() { ...@@ -110,7 +112,7 @@ func init() {
http.Error(w, "not allowed", http.StatusUnauthorized) http.Error(w, "not allowed", http.StatusUnauthorized)
return return
} }
render(w, req, sensitive) Render(w, req, sensitive)
}) })
http.HandleFunc("/debug/events", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/debug/events", func(w http.ResponseWriter, req *http.Request) {
any, sensitive := AuthRequest(req) any, sensitive := AuthRequest(req)
...@@ -118,13 +120,15 @@ func init() { ...@@ -118,13 +120,15 @@ func init() {
http.Error(w, "not allowed", http.StatusUnauthorized) http.Error(w, "not allowed", http.StatusUnauthorized)
return 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. // 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 { data := &struct {
Families []string Families []string
ActiveTraceCount map[string]int ActiveTraceCount map[string]int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment