Skip to content

Commit 43e0968

Browse files
authored
feat: format healthcheck responses (#7723)
1 parent 73d795f commit 43e0968

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

coderd/debug.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
3737
// Get cached report if it exists.
3838
if report := api.healthCheckCache.Load(); report != nil {
3939
if time.Since(report.Time) < api.HealthcheckRefresh {
40-
httpapi.Write(ctx, rw, http.StatusOK, report)
40+
httpapi.WriteIndent(ctx, rw, http.StatusOK, report)
4141
return
4242
}
4343
}
@@ -61,7 +61,7 @@ func (api *API) debugDeploymentHealth(rw http.ResponseWriter, r *http.Request) {
6161
})
6262
return
6363
case res := <-resChan:
64-
httpapi.Write(ctx, rw, http.StatusOK, res.Val)
64+
httpapi.WriteIndent(ctx, rw, http.StatusOK, res.Val)
6565
return
6666
}
6767
}

coderd/httpapi/httpapi.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,40 @@ func RouteNotFound(rw http.ResponseWriter) {
136136
// marshaling, such as the number of elements in an array, which could help us
137137
// spot routes that need to be paginated.
138138
func Write(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
139+
// Pretty up JSON when testing.
140+
if flag.Lookup("test.v") != nil {
141+
WriteIndent(ctx, rw, status, response)
142+
return
143+
}
144+
139145
_, span := tracing.StartSpan(ctx)
140146
defer span.End()
141147

142-
buf := &bytes.Buffer{}
143-
enc := json.NewEncoder(buf)
148+
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
149+
rw.WriteHeader(status)
150+
151+
enc := json.NewEncoder(rw)
144152
enc.SetEscapeHTML(true)
145-
// Pretty up JSON when testing.
146-
if flag.Lookup("test.v") != nil {
147-
enc.SetIndent("", "\t")
148-
}
153+
149154
err := enc.Encode(response)
150155
if err != nil {
151156
http.Error(rw, err.Error(), http.StatusInternalServerError)
152157
return
153158
}
159+
}
160+
161+
func WriteIndent(ctx context.Context, rw http.ResponseWriter, status int, response interface{}) {
162+
_, span := tracing.StartSpan(ctx)
163+
defer span.End()
164+
154165
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
155166
rw.WriteHeader(status)
156-
_, err = rw.Write(buf.Bytes())
167+
168+
enc := json.NewEncoder(rw)
169+
enc.SetEscapeHTML(true)
170+
enc.SetIndent("", "\t")
171+
172+
err := enc.Encode(response)
157173
if err != nil {
158174
http.Error(rw, err.Error(), http.StatusInternalServerError)
159175
return

0 commit comments

Comments
 (0)