From 78437aaa1c18fa4131318a9b1c399ab3eccc5079 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Wed, 6 Apr 2022 12:54:49 -0500 Subject: [PATCH] otelhttp: nil check wrappedBody.Close --- CHANGELOG.md | 4 ++++ instrumentation/net/http/otelhttp/transport.go | 5 ++++- instrumentation/net/http/otelhttp/transport_test.go | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e39fca8fa4..3fcdbac666a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Upgraded all dependencies on stable modules from `go.opentelemetry.io/otel` from v1.5.0 to v1.6.1. (#2134) - Upgraded all dependencies on metric modules from `go.opentelemetry.io/otel` from v0.27.0 to v0.28.0. (#1977) +### Fixed + +- otelhttp: nil check wrappedBody.Close + ## [1.5.0/0.30.0/0.1.0] - 2022-03-16 ### Added 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) {