diff --git a/server/server_test.go b/server/server_test.go index e2800cf4c5..5803334f4e 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -3431,7 +3431,7 @@ func TestUnversionedPost(t *testing.T) { f.reset() f.server.Handler.ServeHTTP(f.recorder, post()) - expected := `{"agg":6}` + expected := "{\"agg\":6}\n" if f.recorder.Code != 200 || f.recorder.Body.String() != expected { t.Fatalf(`Expected HTTP 200 / %v but got: %v`, expected, f.recorder) } diff --git a/server/writer/writer.go b/server/writer/writer.go index 3582fa90aa..e0131beeca 100644 --- a/server/writer/writer.go +++ b/server/writer/writer.go @@ -58,27 +58,18 @@ func Error(w http.ResponseWriter, status int, err *types.ErrorV1) { // JSON writes a response with the specified status code and object. The object // will be JSON serialized. func JSON(w http.ResponseWriter, code int, v interface{}, pretty bool) { - - var bs []byte - var err error - + enc := json.NewEncoder(w) if pretty { - bs, err = json.MarshalIndent(v, "", " ") - } else { - bs, err = json.Marshal(v) + enc.SetIndent("", " ") } - if err != nil { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(code) + + if err := enc.Encode(v); err != nil { ErrorAuto(w, err) return } - headers := w.Header() - headers.Add("Content-Type", "application/json") - Bytes(w, code, bs) - - if pretty { - _, _ = w.Write([]byte("\n")) - } } // Bytes writes a response with the specified status code and bytes. diff --git a/test/e2e/distributedtracing/distributedtracing_test.go b/test/e2e/distributedtracing/distributedtracing_test.go index 8509b1b678..73d795b45a 100644 --- a/test/e2e/distributedtracing/distributedtracing_test.go +++ b/test/e2e/distributedtracing/distributedtracing_test.go @@ -77,7 +77,7 @@ func TestServerSpan(t *testing.T) { attribute.Int("http.status_code", 200), attribute.String("http.target", "/v0/data"), attribute.String("http.user_agent", "Go-http-client/1.1"), - attribute.Int("http.wrote_bytes", 2), + attribute.Int("http.wrote_bytes", 3), attribute.String("net.transport", "ip_tcp"), } compareSpanAttributes(t, expected, attribute.NewSet(spans[0].Attributes...)) @@ -111,7 +111,7 @@ func TestServerSpan(t *testing.T) { attribute.Int("http.status_code", 200), attribute.String("http.target", "/v1/data"), attribute.String("http.user_agent", "Go-http-client/1.1"), - attribute.Int("http.wrote_bytes", 66), + attribute.Int("http.wrote_bytes", 67), attribute.String("net.transport", "ip_tcp"), } compareSpanAttributes(t, expected, attribute.NewSet(spans[0].Attributes...))