-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debug: add trace and CPU flight recorder
Use golang.org/x/exp/trace to implement an trace recorder that saves the trace to a circular buffer and can be retrieved at any time. Debug endpoints have been added under /debug/flight to start and stop the trace as well as to set its period. Endpoints for generating CPU profiles of arbitrary durations are also included. An endpoints starts the CPU profile and writes it to the client, while another endpoint stops the profiler and finishes writing the response to the client. Due to golang.org/x/exp/trace, the minimum go version has been bumped to 1.22.0.
- Loading branch information
Showing
30 changed files
with
7,641 additions
and
19 deletions.
There are no files selected for viewing
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
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,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"runtime/pprof" | ||
"sync" | ||
"time" | ||
|
||
"golang.org/x/exp/trace" | ||
) | ||
|
||
type flightRecorder struct { | ||
mu sync.Mutex | ||
cond *sync.Cond | ||
recorder *trace.FlightRecorder | ||
} | ||
|
||
func newFlightRecorder() *flightRecorder { | ||
dbg := &flightRecorder{ | ||
recorder: trace.NewFlightRecorder(), | ||
} | ||
dbg.cond = sync.NewCond(&dbg.mu) | ||
return dbg | ||
} | ||
|
||
func (r *flightRecorder) StartTrace(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if r.recorder.Enabled() { | ||
http.Error(w, "flight recorder is already running", http.StatusConflict) | ||
return | ||
} | ||
if err := r.recorder.Start(); err != nil { | ||
http.Error(w, fmt.Sprintf("could not start flight recorder: %s", err), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (r *flightRecorder) StopTrace(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if !r.recorder.Enabled() { | ||
http.Error(w, "flight recorder is not running", http.StatusConflict) | ||
return | ||
} | ||
if err := r.recorder.Stop(); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (r *flightRecorder) SetTracePeriod(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if r.recorder.Enabled() { | ||
http.Error(w, "flight recorder is running, stop it to change its period", http.StatusPreconditionFailed) | ||
return | ||
} | ||
periodValue := req.FormValue("period") | ||
period, err := time.ParseDuration(periodValue) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("invalid flight recorder period: %s", err), http.StatusBadRequest) | ||
} | ||
r.recorder.SetPeriod(period) | ||
} | ||
|
||
func (r *flightRecorder) Trace(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Disposition", `attachment; filename="trace"`) | ||
if _, err := r.recorder.WriteTo(w); err != nil { | ||
http.Error(w, fmt.Sprintf("could not write in-flight trace: %s", err), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func (r *flightRecorder) StartCPUProfile(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Disposition", `attachment; filename="profile"`) | ||
|
||
if err := pprof.StartCPUProfile(w); err != nil { | ||
http.Error(w, fmt.Sprintf("could not start CPU profile: %s", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
r.mu.Lock() | ||
r.cond.Wait() | ||
r.mu.Unlock() | ||
} | ||
|
||
func (r *flightRecorder) StopCPUProfile(w http.ResponseWriter, req *http.Request) { | ||
pprof.StopCPUProfile() | ||
r.cond.Broadcast() | ||
} | ||
|
||
func setupDebugFlight(m *http.ServeMux) { | ||
r := newFlightRecorder() | ||
|
||
const ( | ||
flightPattern = "/debug/flight" | ||
flightTracePattern = flightPattern + "/trace" | ||
flightCPUPattern = flightPattern + "/cpu" | ||
) | ||
|
||
m.HandleFunc("POST "+flightTracePattern+"/start", r.StartTrace) | ||
m.HandleFunc("POST "+flightTracePattern+"/stop", r.StopTrace) | ||
m.HandleFunc("POST "+flightTracePattern+"/set_period", r.SetTracePeriod) | ||
m.HandleFunc("GET "+flightTracePattern, r.Trace) | ||
|
||
m.HandleFunc("POST "+flightCPUPattern+"/start", r.StartCPUProfile) | ||
m.HandleFunc("POST "+flightCPUPattern+"/stop", r.StopCPUProfile) | ||
} |
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.