-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
85 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package utilroutes | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"runtime" | ||
"runtime/pprof" | ||
"runtime/trace" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// cpuProfile responds with the pprof-formatted cpu profile. | ||
// Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified. | ||
func cpuProfile(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
sec, err := strconv.ParseInt(r.FormValue("seconds"), 10, 64) | ||
if sec <= 0 || err != nil { | ||
sec = 30 | ||
} | ||
|
||
if durationExceedsWriteTimeout(r, float64(sec)) { | ||
serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout") | ||
return | ||
} | ||
|
||
// Set Content Type assuming StartCPUProfile will work, | ||
// because if it does it starts writing. | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Disposition", `attachment; filename="cpu.bin"`) | ||
if err := pprof.StartCPUProfile(w); err != nil { | ||
// StartCPUProfile failed, so no writes yet. | ||
serveError(w, http.StatusInternalServerError, | ||
fmt.Sprintf("Could not enable CPU profiling: %s", err)) | ||
return | ||
} | ||
sleep(r, time.Duration(sec)*time.Second) | ||
pprof.StopCPUProfile() | ||
} | ||
|
||
func getProfile(name string, w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
profile := pprof.Lookup(name) | ||
if profile == nil { | ||
w.WriteHeader(404) | ||
fmt.Fprintf(w, "Unknown profile: %s\n", name) | ||
return | ||
} | ||
if name == "heap" && r.FormValue("gc") != `` { | ||
runtime.GC() | ||
} | ||
debugLevel, _ := strconv.Atoi(r.FormValue("debug")) | ||
profile.WriteTo(w, debugLevel) | ||
} | ||
|
||
// trace responds with the execution trace in binary form. | ||
// Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified. | ||
func runTrace(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
sec, err := strconv.ParseFloat(r.FormValue("seconds"), 64) | ||
if sec <= 0 || err != nil { | ||
sec = 1 | ||
} | ||
|
||
if durationExceedsWriteTimeout(r, sec) { | ||
serveError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout") | ||
return | ||
} | ||
|
||
// Set Content Type assuming trace.Start will work, | ||
// because if it does it starts writing. | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Disposition", `attachment; filename="trace.bin"`) | ||
if err := trace.Start(w); err != nil { | ||
// trace.Start failed, so no writes yet. | ||
serveError(w, http.StatusInternalServerError, | ||
fmt.Sprintf("Could not enable tracing: %s", err)) | ||
return | ||
} | ||
sleep(r, time.Duration(sec*float64(time.Second))) | ||
trace.Stop() | ||
} | ||
|
||
func sleep(r *http.Request, d time.Duration) { | ||
select { | ||
case <-time.After(d): | ||
case <-r.Context().Done(): | ||
} | ||
} | ||
|
||
func durationExceedsWriteTimeout(r *http.Request, seconds float64) bool { | ||
srv, ok := r.Context().Value(http.ServerContextKey).(*http.Server) | ||
return ok && srv.WriteTimeout != 0 && seconds >= srv.WriteTimeout.Seconds() | ||
} | ||
|
||
func serveError(w http.ResponseWriter, status int, txt string) { | ||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
w.Header().Set("X-Go-Pprof", "1") | ||
w.Header().Del("Content-Disposition") | ||
w.WriteHeader(status) | ||
fmt.Fprintln(w, txt) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters