diff --git a/instrumentation/net/http/otelhttp/transport.go b/instrumentation/net/http/otelhttp/transport.go index b436f6a2a9e..6c32bb1174e 100644 --- a/instrumentation/net/http/otelhttp/transport.go +++ b/instrumentation/net/http/otelhttp/transport.go @@ -186,5 +186,8 @@ func (wb *wrappedBody) Read(b []byte) (int, error) { func (wb *wrappedBody) Close() error { wb.span.End() - return wb.body.Close() + if wb.body != nil { + return wb.body.Close() + } + return nil } diff --git a/instrumentation/net/http/otelhttp/transport_test.go b/instrumentation/net/http/otelhttp/transport_test.go index e155d3e088f..e032ba52ad1 100644 --- a/instrumentation/net/http/otelhttp/transport_test.go +++ b/instrumentation/net/http/otelhttp/transport_test.go @@ -279,6 +279,10 @@ func TestWrappedBodyClose(t *testing.T) { wb := &wrappedBody{span: trace.Span(s), body: readCloser{}} assert.NoError(t, wb.Close()) s.assert(t, true, nil, codes.Unset, "") + + var body io.ReadCloser + wb2 := newWrappedBody(s, body) + assert.NoError(t, wb2.Close()) } func TestWrappedBodyCloseError(t *testing.T) {