-
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.
Merge branch 'moby:master' into os-mount-types-check
- Loading branch information
Showing
35 changed files
with
8,108 additions
and
320 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,87 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"golang.org/x/exp/trace" | ||
) | ||
|
||
type flightRecorder struct { | ||
mu sync.Mutex | ||
recorder *trace.FlightRecorder | ||
} | ||
|
||
func newFlightRecorder() *flightRecorder { | ||
dbg := &flightRecorder{ | ||
recorder: trace.NewFlightRecorder(), | ||
} | ||
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 setupDebugFlight(m *http.ServeMux) { | ||
r := newFlightRecorder() | ||
|
||
const ( | ||
flightPattern = "/debug/flight" | ||
flightTracePattern = flightPattern + "/trace" | ||
) | ||
|
||
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) | ||
} |
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
Oops, something went wrong.