Skip to content

Commit 791d763

Browse files
committed
refactor tracing status writer done func
1 parent 189b490 commit 791d763

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

coderd/tracing/status_writer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type StatusWriter struct {
2727
http.ResponseWriter
2828
Status int
2929
Hijacked bool
30-
Done []func() // If non-nil, this function will be called when the handler is done.
30+
doneFuncs []func() // If non-nil, this function will be called when the handler is done.
3131
responseBody []byte
3232

3333
wroteHeader bool
@@ -38,12 +38,17 @@ func StatusWriterMiddleware(next http.Handler) http.Handler {
3838
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3939
sw := &StatusWriter{ResponseWriter: rw}
4040
next.ServeHTTP(sw, r)
41-
for _, done := range sw.Done {
41+
for _, done := range sw.doneFuncs {
4242
done()
4343
}
4444
})
4545
}
4646

47+
func (w *StatusWriter) AddDoneFunc(f func()) {
48+
// Prepend, as if deferred.
49+
w.doneFuncs = append([]func(){f}, w.doneFuncs...)
50+
}
51+
4752
func (w *StatusWriter) WriteHeader(status int) {
4853
if buildinfo.IsDev() || flag.Lookup("test.v") != nil {
4954
if w.wroteHeader {

coderd/tracing/status_writer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ func TestStatusWriter(t *testing.T) {
116116
require.Error(t, err)
117117
require.Equal(t, "hijacked", err.Error())
118118
})
119+
120+
t.Run("Middleware", func(t *testing.T) {
121+
t.Parallel()
122+
123+
var (
124+
sw *tracing.StatusWriter
125+
done = false
126+
rr = httptest.NewRecorder()
127+
)
128+
tracing.StatusWriterMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129+
sw = w.(*tracing.StatusWriter)
130+
sw.AddDoneFunc(func() {
131+
done = true
132+
})
133+
w.WriteHeader(http.StatusNoContent)
134+
})).ServeHTTP(rr, httptest.NewRequest("GET", "/", nil))
135+
136+
require.Equal(t, http.StatusNoContent, rr.Code, "rr status code not set")
137+
require.Equal(t, http.StatusNoContent, sw.Status, "sw status code not set")
138+
require.True(t, done, "done func not called")
139+
})
119140
}
120141

121142
type hijacker struct {

coderd/workspaceapps/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
388388

389389
// Set the commit function on the status writer to create an audit
390390
// log, this ensures that the status and response body are available.
391-
sw.Done = append(sw.Done, func() {
391+
sw.AddDoneFunc(func() {
392392
p.Logger.Info(ctx, "workspace app audit session", slog.F("status", sw.Status), slog.F("body", string(sw.ResponseBody())), slog.F("api_key", aReq.apiKey), slog.F("db_req", aReq.dbReq))
393393

394394
if sw.Status == http.StatusSeeOther {

0 commit comments

Comments
 (0)