Skip to content

Commit 1816238

Browse files
committed
trace: export /debug/requests and /debug/events handlers
Fixes #20478. Change-Id: I739b6f1368889b735944d74be2d16b7e09eb11a2 Reviewed-on: https://go-review.googlesource.com/45998 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 973f3f3 commit 1816238

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

trace/events.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var buckets = []bucket{
3939
}
4040

4141
// RenderEvents renders the HTML page typically served at /debug/events.
42-
// It does not do any auth checking; see AuthRequest for the default auth check
43-
// used by the handler registered on http.DefaultServeMux.
44-
// req may be nil.
42+
// It does not do any auth checking. The request may be nil.
43+
//
44+
// Most users will use the Events handler.
4545
func RenderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
4646
now := time.Now()
4747
data := &struct {

trace/trace.go

+37-21
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,46 @@ var AuthRequest = func(req *http.Request) (any, sensitive bool) {
110110
}
111111

112112
func init() {
113-
http.HandleFunc("/debug/requests", func(w http.ResponseWriter, req *http.Request) {
114-
any, sensitive := AuthRequest(req)
115-
if !any {
116-
http.Error(w, "not allowed", http.StatusUnauthorized)
117-
return
118-
}
119-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
120-
Render(w, req, sensitive)
121-
})
122-
http.HandleFunc("/debug/events", func(w http.ResponseWriter, req *http.Request) {
123-
any, sensitive := AuthRequest(req)
124-
if !any {
125-
http.Error(w, "not allowed", http.StatusUnauthorized)
126-
return
127-
}
128-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
129-
RenderEvents(w, req, sensitive)
130-
})
113+
// TODO(jbd): Serve Traces from /debug/traces in the future?
114+
// There is no requirement for a request to be present to have traces.
115+
http.HandleFunc("/debug/requests", Traces)
116+
http.HandleFunc("/debug/events", Events)
117+
}
118+
119+
// Traces responds with traces from the program.
120+
// The package initialization registers it in http.DefaultServeMux
121+
// at /debug/requests.
122+
//
123+
// It performs authorization by running AuthRequest.
124+
func Traces(w http.ResponseWriter, req *http.Request) {
125+
any, sensitive := AuthRequest(req)
126+
if !any {
127+
http.Error(w, "not allowed", http.StatusUnauthorized)
128+
return
129+
}
130+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
131+
Render(w, req, sensitive)
132+
}
133+
134+
// Events responds with a page of events collected by EventLogs.
135+
// The package initialization registers it in http.DefaultServeMux
136+
// at /debug/events.
137+
//
138+
// It performs authorization by running AuthRequest.
139+
func Events(w http.ResponseWriter, req *http.Request) {
140+
any, sensitive := AuthRequest(req)
141+
if !any {
142+
http.Error(w, "not allowed", http.StatusUnauthorized)
143+
return
144+
}
145+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
146+
RenderEvents(w, req, sensitive)
131147
}
132148

133149
// Render renders the HTML page typically served at /debug/requests.
134-
// It does not do any auth checking; see AuthRequest for the default auth check
135-
// used by the handler registered on http.DefaultServeMux.
136-
// req may be nil.
150+
// It does not do any auth checking. The request may be nil.
151+
//
152+
// Most users will use the Traces handler.
137153
func Render(w io.Writer, req *http.Request, sensitive bool) {
138154
data := &struct {
139155
Families []string

0 commit comments

Comments
 (0)