-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
debug: add trace flight recorder #5337
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can just be
1.22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
1.22
makes more sense to me, but since the same file was using1.21.0
I kept it pinned to a patch release just in case there was some BuiltKit convention I was not aware of. I've updated it to use1.22
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was because of a dep forcing it, see #5118 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah actually I was wrong, exp/trace also set patch so we need it as well https://cs.opensource.google/go/x/exp/+/master:go.mod;l=3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, good point. I've changed it back to 1.22.0.