From 9aadeadeaeb33d5d3a6c0d0ac2efb4cca8505633 Mon Sep 17 00:00:00 2001 From: Damian Murphy Date: Mon, 16 Dec 2024 19:11:32 +1030 Subject: [PATCH] datadogexporter: read response body on pushSketches (#36779) #### Description The pushSketches function in the datadogexporter does not read the contents of the HTTP response's Body on return. The Go documentation states that the full response body from the http request should be read and closed to ensure that connections can be properly re-used: https://pkg.go.dev/net/http#Client.Do We saw this manifest as a high load of incoming metric traffic through the exporter creating a connection per request and essentially killing platform infrastructure (proxy, DNS) as a result. #### Testing No additional testing added. Fix is running to great effect in private internal fork at extremely high scale. --- ...ud_datadogexporter-read-response-body.yaml | 28 +++++++++++++++++++ exporter/datadogexporter/metrics_exporter.go | 8 ++++++ 2 files changed, 36 insertions(+) create mode 100644 .chloggen/spud_datadogexporter-read-response-body.yaml diff --git a/.chloggen/spud_datadogexporter-read-response-body.yaml b/.chloggen/spud_datadogexporter-read-response-body.yaml new file mode 100644 index 000000000000..c11c5e108ff5 --- /dev/null +++ b/.chloggen/spud_datadogexporter-read-response-body.yaml @@ -0,0 +1,28 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: read response body on pushSketches to allow connection re-use + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: + - 36779 + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/exporter/datadogexporter/metrics_exporter.go b/exporter/datadogexporter/metrics_exporter.go index ed2938e0a474..89d8260eb3c1 100644 --- a/exporter/datadogexporter/metrics_exporter.go +++ b/exporter/datadogexporter/metrics_exporter.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "io" "net/http" "sync" "time" @@ -154,6 +155,13 @@ func (exp *metricsExporter) pushSketches(ctx context.Context, sl sketches.Sketch } defer resp.Body.Close() + // We must read the full response body from the http request to ensure that connections can be + // properly re-used. https://pkg.go.dev/net/http#Client.Do + _, err = io.Copy(io.Discard, resp.Body) + if err != nil { + return clientutil.WrapError(fmt.Errorf("failed to read response body from sketches HTTP request: %w", err), resp) + } + if resp.StatusCode >= 400 { return clientutil.WrapError(fmt.Errorf("error when sending payload to %s: %s", sketches.SketchSeriesEndpoint, resp.Status), resp) }