Skip to content

Commit 0bf9572

Browse files
otlp: Clients to close body uniformly (#5954)
There were inconsistencies in closing the response body. For traces, the Close happened in a defer statement and any error was logged. Logs and metrics were less rigorous. It appeared Close() wasn't always called, and when it was, errors were returned sometimes and ignored at other times. This applies the defer logic from traces to the other two and removes other Close() calls. This was part of PR #5929, and has been split out as requested: #5929 (comment)).
1 parent ac5caea commit 0bf9572

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
### Fixed
12+
13+
- Fix inconsistent request body closing in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#5954)
14+
- Fix inconsistent request body closing in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5954)
15+
- Fix inconsistent request body closing in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#5954)
16+
1117
<!-- Released section -->
1218
<!-- Don't change this section unless doing release -->
1319

exporters/otlp/otlplog/otlploghttp/client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ func (c *httpClient) uploadLogs(ctx context.Context, data []*logpb.ResourceLogs)
149149
if err != nil {
150150
return err
151151
}
152+
if resp != nil && resp.Body != nil {
153+
defer func() {
154+
if err := resp.Body.Close(); err != nil {
155+
otel.Handle(err)
156+
}
157+
}()
158+
}
152159

153160
var rErr error
154161
switch sc := resp.StatusCode; {
@@ -193,7 +200,6 @@ func (c *httpClient) uploadLogs(ctx context.Context, data []*logpb.ResourceLogs)
193200
// debugging the actual issue.
194201
var respData bytes.Buffer
195202
if _, err := io.Copy(&respData, resp.Body); err != nil {
196-
_ = resp.Body.Close()
197203
return err
198204
}
199205

@@ -208,9 +214,6 @@ func (c *httpClient) uploadLogs(ctx context.Context, data []*logpb.ResourceLogs)
208214
rErr = fmt.Errorf("failed to send logs to %s: %s", request.URL, resp.Status)
209215
}
210216

211-
if err := resp.Body.Close(); err != nil {
212-
return err
213-
}
214217
return rErr
215218
})
216219
}

exporters/otlp/otlpmetric/otlpmetrichttp/client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func (c *client) UploadMetrics(ctx context.Context, protoMetrics *metricpb.Resou
152152
if err != nil {
153153
return err
154154
}
155+
if resp != nil && resp.Body != nil {
156+
defer func() {
157+
if err := resp.Body.Close(); err != nil {
158+
otel.Handle(err)
159+
}
160+
}()
161+
}
155162

156163
var rErr error
157164
switch sc := resp.StatusCode; {
@@ -196,7 +203,6 @@ func (c *client) UploadMetrics(ctx context.Context, protoMetrics *metricpb.Resou
196203
// debugging the actual issue.
197204
var respData bytes.Buffer
198205
if _, err := io.Copy(&respData, resp.Body); err != nil {
199-
_ = resp.Body.Close()
200206
return err
201207
}
202208

@@ -211,9 +217,6 @@ func (c *client) UploadMetrics(ctx context.Context, protoMetrics *metricpb.Resou
211217
rErr = fmt.Errorf("failed to send metrics to %s: %s", request.URL, resp.Status)
212218
}
213219

214-
if err := resp.Body.Close(); err != nil {
215-
return err
216-
}
217220
return rErr
218221
})
219222
}

exporters/otlp/otlptrace/otlptracehttp/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
208208
// debugging the actual issue.
209209
var respData bytes.Buffer
210210
if _, err := io.Copy(&respData, resp.Body); err != nil {
211-
_ = resp.Body.Close()
212211
return err
213212
}
214213

0 commit comments

Comments
 (0)