Skip to content

Commit 61bcc15

Browse files
authored
fix(httpcheckreceiver): always close resp.Body (#40552)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description The Go HTTP client documentation specifically states that when an error occurs, the response may still be non-nil, and you're responsible for closing the body to avoid resource leaks. And added a sensible timeout to prevent the connection hang indefinitely. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 6885321 commit 61bcc15

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/httpcheck
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Always close resp.Body and add timeout
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [40552]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/httpcheckreceiver/scraper.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-col
66
import (
77
"context"
88
"errors"
9+
"io"
910
"net/http"
1011
"sync"
1112
"time"
@@ -37,6 +38,11 @@ func (h *httpcheckScraper) start(ctx context.Context, host component.Host) (err
3738
var expandedTargets []*targetConfig
3839

3940
for _, target := range h.cfg.Targets {
41+
if target.Timeout == 0 {
42+
// Set a reasonable timeout to prevent hanging requests
43+
target.Timeout = 30 * time.Second
44+
}
45+
4046
// Create a unified list of endpoints
4147
var allEndpoints []string
4248
if len(target.Endpoints) > 0 {
@@ -103,8 +109,16 @@ func (h *httpcheckScraper) scrape(ctx context.Context) (pmetric.Metrics, error)
103109
// Send the request and measure response time
104110
start := time.Now()
105111
resp, err := targetClient.Do(req)
106-
if err == nil {
107-
defer resp.Body.Close()
112+
113+
// Always close response body if it exists, even on error
114+
if resp != nil && resp.Body != nil {
115+
defer func() {
116+
// Drain the body to allow connection reuse
117+
_, _ = io.Copy(io.Discard, resp.Body)
118+
if closeErr := resp.Body.Close(); closeErr != nil {
119+
h.settings.Logger.Error("failed to close response body", zap.Error(closeErr))
120+
}
121+
}()
108122
}
109123

110124
mux.Lock()

0 commit comments

Comments
 (0)