diff --git a/.golangci.yml b/.golangci.yml index 27ddc1f75c0..a5a1faa4dc1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,6 +11,7 @@ linters: # Specifically enable linters we want to use. enable: - asasalint + - bodyclose - depguard - errcheck - errorlint diff --git a/exporters/autoexport/metrics_test.go b/exporters/autoexport/metrics_test.go index c95a077d176..228d2e808c4 100644 --- a/exporters/autoexport/metrics_test.go +++ b/exporters/autoexport/metrics_test.go @@ -16,6 +16,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/goleak" "google.golang.org/protobuf/proto" @@ -136,9 +137,10 @@ func TestMetricExporterPrometheus(t *testing.T) { } resp, err := http.Get(fmt.Sprintf("http://%s/metrics", rws.addr)) - assert.NoError(t, err) + require.NoError(t, err) + defer func() { assert.NoError(t, resp.Body.Close()) }() body, err := io.ReadAll(resp.Body) - assert.NoError(t, err) + require.NoError(t, err) assert.Contains(t, string(body), "# HELP") assert.NoError(t, mp.Shutdown(context.Background())) @@ -213,9 +215,10 @@ func TestMetricProducerPrometheusWithPrometheusExporter(t *testing.T) { } resp, err := http.Get(fmt.Sprintf("http://%s/metrics", rws.addr)) - assert.NoError(t, err) + require.NoError(t, err) + defer func() { assert.NoError(t, resp.Body.Close()) }() body, err := io.ReadAll(resp.Body) - assert.NoError(t, err) + require.NoError(t, err) // By default there are two metrics exporter. target_info and promhttp_metric_handler_errors_total. // But by including the prometheus producer we should have more. @@ -254,9 +257,10 @@ func TestMetricProducerFallbackWithPrometheusExporter(t *testing.T) { } resp, err := http.Get(fmt.Sprintf("http://%s/metrics", rws.addr)) - assert.NoError(t, err) + require.NoError(t, err) + defer func() { assert.NoError(t, resp.Body.Close()) }() body, err := io.ReadAll(resp.Body) - assert.NoError(t, err) + require.NoError(t, err) assert.Contains(t, string(body), "HELP dummy_metric_total dummy metric") diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/main.go b/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/main.go index bdcafc7008f..c9fd617c2b0 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/main.go +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/main.go @@ -6,7 +6,6 @@ package main import ( "context" "encoding/json" - "io" "log" "net/http" @@ -65,12 +64,12 @@ func lambdaHandler(ctx context.Context) error { return err } - defer func(Body io.ReadCloser) { - err := Body.Close() + defer func() { + err := res.Body.Close() if err != nil { log.Printf("failed to close http response body, %v\n", err) } - }(res.Body) + }() var data map[string]interface{} err = json.NewDecoder(res.Body).Decode(&data) diff --git a/instrumentation/github.com/emicklei/go-restful/otelrestful/restful_test.go b/instrumentation/github.com/emicklei/go-restful/otelrestful/restful_test.go index 7659ab0ffe5..53038955d7d 100644 --- a/instrumentation/github.com/emicklei/go-restful/otelrestful/restful_test.go +++ b/instrumentation/github.com/emicklei/go-restful/otelrestful/restful_test.go @@ -154,7 +154,7 @@ func TestWithPublicEndpoint(t *testing.T) { rr := httptest.NewRecorder() container.ServeHTTP(rr, r) - assert.Equal(t, 200, rr.Result().StatusCode) + assert.Equal(t, 200, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, spanRecorder.ForceFlush(ctx)) @@ -244,7 +244,7 @@ func TestWithPublicEndpointFn(t *testing.T) { rr := httptest.NewRecorder() container.ServeHTTP(rr, r) - assert.Equal(t, http.StatusOK, rr.Result().StatusCode) + assert.Equal(t, http.StatusOK, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, spanRecorder.ForceFlush(ctx)) diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go index 8b02ac49f67..ecef39e3aeb 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace_test.go @@ -38,7 +38,7 @@ func TestGetSpanNotInstrumented(t *testing.T) { r := httptest.NewRequest("GET", "/ping", nil) w := httptest.NewRecorder() router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Equal(t, http.StatusOK, response.StatusCode) } diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go index 628955c4586..162f59d988f 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go @@ -77,7 +77,7 @@ func TestTrace200(t *testing.T) { // do and verify the request router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. require.Equal(t, http.StatusOK, response.StatusCode) // verify traces look good @@ -109,7 +109,7 @@ func TestError(t *testing.T) { r := httptest.NewRequest("GET", "/server_err", nil) w := httptest.NewRecorder() router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Equal(t, http.StatusInternalServerError, response.StatusCode) // verify the errors and status are correct @@ -201,7 +201,7 @@ func TestHTTPRouteWithSpanNameFormatter(t *testing.T) { // do and verify the request router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. require.Equal(t, http.StatusOK, response.StatusCode) // verify traces look good @@ -234,7 +234,7 @@ func TestHTML(t *testing.T) { r := httptest.NewRequest("GET", "/hello", nil) w := httptest.NewRecorder() router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Equal(t, http.StatusOK, response.StatusCode) assert.Equal(t, "hello world", w.Body.String()) diff --git a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go index 15e4b340567..d5e489b2eb6 100644 --- a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go +++ b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go @@ -188,7 +188,7 @@ func TestWithPublicEndpoint(t *testing.T) { prop.Inject(ctx, propagation.HeaderCarrier(r0.Header)) router.ServeHTTP(w, r0) - assert.Equal(t, http.StatusOK, w.Result().StatusCode) + assert.Equal(t, http.StatusOK, w.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, sr.ForceFlush(ctx)) @@ -273,7 +273,7 @@ func TestWithPublicEndpointFn(t *testing.T) { prop.Inject(ctx, propagation.HeaderCarrier(r0.Header)) router.ServeHTTP(w, r0) - assert.Equal(t, http.StatusOK, w.Result().StatusCode) + assert.Equal(t, http.StatusOK, w.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, sr.ForceFlush(ctx)) diff --git a/instrumentation/github.com/labstack/echo/otelecho/echo_test.go b/instrumentation/github.com/labstack/echo/otelecho/echo_test.go index e35e56035aa..9e424a617c3 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/echo_test.go +++ b/instrumentation/github.com/labstack/echo/otelecho/echo_test.go @@ -34,7 +34,7 @@ func TestGetSpanNotInstrumented(t *testing.T) { r := httptest.NewRequest("GET", "/ping", nil) w := httptest.NewRecorder() router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Equal(t, http.StatusOK, response.StatusCode) } @@ -65,7 +65,7 @@ func TestPropagationWithGlobalPropagators(t *testing.T) { router.ServeHTTP(w, r) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator()) - assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") + assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. } func TestPropagationWithCustomPropagators(t *testing.T) { @@ -95,7 +95,7 @@ func TestPropagationWithCustomPropagators(t *testing.T) { }) router.ServeHTTP(w, r) - assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") + assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. } func TestSkipper(t *testing.T) { @@ -116,5 +116,5 @@ func TestSkipper(t *testing.T) { }) router.ServeHTTP(w, r) - assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'ping' handler") + assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'ping' handler") //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. } diff --git a/instrumentation/github.com/labstack/echo/otelecho/test/echo_test.go b/instrumentation/github.com/labstack/echo/otelecho/test/echo_test.go index 237d73d6fed..2ca3e21cce5 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/test/echo_test.go +++ b/instrumentation/github.com/labstack/echo/otelecho/test/echo_test.go @@ -41,7 +41,7 @@ func TestChildSpanFromGlobalTracer(t *testing.T) { w := httptest.NewRecorder() router.ServeHTTP(w, r) - assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") + assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Len(t, sr.Ended(), 1) } @@ -59,7 +59,7 @@ func TestChildSpanFromCustomTracer(t *testing.T) { w := httptest.NewRecorder() router.ServeHTTP(w, r) - assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") + assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler") //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Len(t, sr.Ended(), 1) } @@ -79,7 +79,7 @@ func TestTrace200(t *testing.T) { // do and verify the request router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. require.Equal(t, http.StatusOK, response.StatusCode) // verify traces look good @@ -111,7 +111,7 @@ func TestError(t *testing.T) { r := httptest.NewRequest("GET", "/server_err", nil) w := httptest.NewRecorder() router.ServeHTTP(w, r) - response := w.Result() + response := w.Result() //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. assert.Equal(t, http.StatusInternalServerError, response.StatusCode) // verify the errors and status are correct diff --git a/instrumentation/host/example/go.mod b/instrumentation/host/example/go.mod index f4e023fb625..47ae3136d26 100644 --- a/instrumentation/host/example/go.mod +++ b/instrumentation/host/example/go.mod @@ -19,7 +19,7 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/lufia/plan9stats v0.0.0-20240513124658-fba389f38bae // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect - github.com/shirou/gopsutil/v4 v4.24.6 // indirect + github.com/shirou/gopsutil/v4 v4.24.7 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.8.0 // indirect diff --git a/instrumentation/host/example/go.sum b/instrumentation/host/example/go.sum index 42459349476..a0c62ba76e8 100644 --- a/instrumentation/host/example/go.sum +++ b/instrumentation/host/example/go.sum @@ -18,8 +18,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/shirou/gopsutil/v4 v4.24.6 h1:9qqCSYF2pgOU+t+NgJtp7Co5+5mHF/HyKBUckySQL64= -github.com/shirou/gopsutil/v4 v4.24.6/go.mod h1:aoebb2vxetJ/yIDZISmduFvVNPHqXQ9SEJwRXxkf0RA= +github.com/shirou/gopsutil/v4 v4.24.7 h1:V9UGTK4gQ8HvcnPKf6Zt3XHyQq/peaekfxpJ2HSocJk= +github.com/shirou/gopsutil/v4 v4.24.7/go.mod h1:0uW/073rP7FYLOkvxolUQM5rMOLTNmRXnFKafpb71rw= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= diff --git a/instrumentation/host/go.mod b/instrumentation/host/go.mod index c26ad5dcd99..f14a02757cd 100644 --- a/instrumentation/host/go.mod +++ b/instrumentation/host/go.mod @@ -3,7 +3,7 @@ module go.opentelemetry.io/contrib/instrumentation/host go 1.21 require ( - github.com/shirou/gopsutil/v4 v4.24.6 + github.com/shirou/gopsutil/v4 v4.24.7 go.opentelemetry.io/otel v1.28.0 go.opentelemetry.io/otel/metric v1.28.0 ) diff --git a/instrumentation/host/go.sum b/instrumentation/host/go.sum index 848301859b2..c53d61a0d3b 100644 --- a/instrumentation/host/go.sum +++ b/instrumentation/host/go.sum @@ -16,8 +16,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/shirou/gopsutil/v4 v4.24.6 h1:9qqCSYF2pgOU+t+NgJtp7Co5+5mHF/HyKBUckySQL64= -github.com/shirou/gopsutil/v4 v4.24.6/go.mod h1:aoebb2vxetJ/yIDZISmduFvVNPHqXQ9SEJwRXxkf0RA= +github.com/shirou/gopsutil/v4 v4.24.7 h1:V9UGTK4gQ8HvcnPKf6Zt3XHyQq/peaekfxpJ2HSocJk= +github.com/shirou/gopsutil/v4 v4.24.7/go.mod h1:0uW/073rP7FYLOkvxolUQM5rMOLTNmRXnFKafpb71rw= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= diff --git a/instrumentation/net/http/otelhttp/handler_test.go b/instrumentation/net/http/otelhttp/handler_test.go index 8b0b7a23a3b..2e8b6b49db1 100644 --- a/instrumentation/net/http/otelhttp/handler_test.go +++ b/instrumentation/net/http/otelhttp/handler_test.go @@ -84,7 +84,7 @@ func TestHandler(t *testing.T) { rr := httptest.NewRecorder() tc.handler(t).ServeHTTP(rr, r) - assert.Equal(t, tc.expectedStatusCode, rr.Result().StatusCode) + assert.Equal(t, tc.expectedStatusCode, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. }) } } diff --git a/instrumentation/net/http/otelhttp/test/config_test.go b/instrumentation/net/http/otelhttp/test/config_test.go index aade2f458ce..d8c438643b0 100644 --- a/instrumentation/net/http/otelhttp/test/config_test.go +++ b/instrumentation/net/http/otelhttp/test/config_test.go @@ -39,7 +39,7 @@ func TestBasicFilter(t *testing.T) { t.Fatal(err) } h.ServeHTTP(rr, r) - if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { + if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. t.Fatalf("got %d, expected %d", got, expected) } if got := rr.Header().Get("Traceparent"); got != "" { @@ -48,7 +48,7 @@ func TestBasicFilter(t *testing.T) { if got, expected := len(spanRecorder.Ended()), 0; got != expected { t.Fatalf("got %d recorded spans, expected %d", got, expected) } - d, err := io.ReadAll(rr.Result().Body) + d, err := io.ReadAll(rr.Result().Body) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. if err != nil { t.Fatal(err) } @@ -111,7 +111,7 @@ func TestSpanNameFormatter(t *testing.T) { t.Fatal(err) } h.ServeHTTP(rr, r) - if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { + if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. t.Fatalf("got %d, expected %d", got, expected) } diff --git a/instrumentation/net/http/otelhttp/test/handler_test.go b/instrumentation/net/http/otelhttp/test/handler_test.go index e500b493d42..1f2034df6d5 100644 --- a/instrumentation/net/http/otelhttp/test/handler_test.go +++ b/instrumentation/net/http/otelhttp/test/handler_test.go @@ -118,7 +118,7 @@ func TestHandlerBasics(t *testing.T) { ) assertScopeMetrics(t, rm.ScopeMetrics[0], attrs) - if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { + if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. t.Fatalf("got %d, expected %d", got, expected) } @@ -130,7 +130,7 @@ func TestHandlerBasics(t *testing.T) { t.Fatalf("invalid span created: %#v", spans[0].SpanContext()) } - d, err := io.ReadAll(rr.Result().Body) + d, err := io.ReadAll(rr.Result().Body) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. if err != nil { t.Fatal(err) } @@ -291,7 +291,7 @@ func TestHandlerRequestWithTraceContext(t *testing.T) { r = r.WithContext(ctx) h.ServeHTTP(rr, r) - assert.Equal(t, http.StatusOK, rr.Result().StatusCode) + assert.Equal(t, http.StatusOK, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. span.End() @@ -339,7 +339,7 @@ func TestWithPublicEndpoint(t *testing.T) { rr := httptest.NewRecorder() h.ServeHTTP(rr, r) - assert.Equal(t, http.StatusOK, rr.Result().StatusCode) + assert.Equal(t, http.StatusOK, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, spanRecorder.ForceFlush(ctx)) @@ -423,7 +423,7 @@ func TestWithPublicEndpointFn(t *testing.T) { rr := httptest.NewRecorder() h.ServeHTTP(rr, r) - assert.Equal(t, http.StatusOK, rr.Result().StatusCode) + assert.Equal(t, http.StatusOK, rr.Result().StatusCode) //nolint:bodyclose // False positive for httptest.ResponseRecorder: https://github.com/timakin/bodyclose/issues/59. // Recorded span should be linked with an incoming span context. assert.NoError(t, spanRecorder.ForceFlush(ctx)) diff --git a/instrumentation/net/http/otelhttp/test/transport_test.go b/instrumentation/net/http/otelhttp/test/transport_test.go index cfbc27cc2b2..e61950dd7dd 100644 --- a/instrumentation/net/http/otelhttp/test/transport_test.go +++ b/instrumentation/net/http/otelhttp/test/transport_test.go @@ -99,8 +99,11 @@ func TestTransportErrorStatus(t *testing.T) { if err != nil { t.Fatal(err) } - _, err = c.Do(r) + resp, err := c.Do(r) if err == nil { + if err := resp.Body.Close(); err != nil { + t.Errorf("close response body: %v", err) + } t.Fatal("transport should have returned an error, it didn't") } @@ -160,6 +163,7 @@ func TestTransportRequestWithTraceContext(t *testing.T) { c := http.Client{Transport: tr} res, err := c.Do(r) require.NoError(t, err) + defer func() { assert.NoError(t, res.Body.Close()) }() span.End() @@ -220,6 +224,7 @@ func TestWithHTTPTrace(t *testing.T) { c := http.Client{Transport: tr} res, err := c.Do(r) require.NoError(t, err) + defer func() { assert.NoError(t, res.Body.Close()) }() span.End() @@ -513,8 +518,9 @@ func TestCustomAttributesHandling(t *testing.T) { // test bonus: intententionally ignoring response to confirm that // http.client.response.size metric is not recorded // by the Transport.RoundTrip logic - _, err = client.Do(r) + resp, err := client.Do(r) require.NoError(t, err) + defer func() { assert.NoError(t, resp.Body.Close()) }() err = reader.Collect(ctx, &rm) assert.NoError(t, err) @@ -587,7 +593,8 @@ func BenchmarkTransportRoundTrip(b *testing.B) { b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { - _, _ = c.Do(r) + resp, _ := c.Do(r) + resp.Body.Close() } }) } diff --git a/instrumentation/net/http/otelhttp/transport_test.go b/instrumentation/net/http/otelhttp/transport_test.go index eaf44ae2dd3..a88d45c20b9 100644 --- a/instrumentation/net/http/otelhttp/transport_test.go +++ b/instrumentation/net/http/otelhttp/transport_test.go @@ -124,6 +124,11 @@ func TestTransportBasics(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + if err := res.Body.Close(); err != nil { + t.Errorf("close response body: %v", err) + } + }() body, err := io.ReadAll(res.Body) if err != nil { @@ -170,6 +175,11 @@ func TestNilTransport(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + if err := res.Body.Close(); err != nil { + t.Errorf("close response body: %v", err) + } + }() body, err := io.ReadAll(res.Body) if err != nil {