From 8123eec1319f70361eba32acd5548c72794e165b Mon Sep 17 00:00:00 2001 From: Lars Stegman Date: Wed, 15 May 2024 10:54:53 +0200 Subject: [PATCH] fix(inputs.influxdb_v2_listener): Linting issues --- .../influxdb_v2_listener.go | 23 +++++++++++-------- .../influxdb_v2_listener_test.go | 12 +++++----- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go index 78b72fcb3a825..0c8e8d442599d 100644 --- a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go +++ b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go @@ -343,19 +343,14 @@ func (h *InfluxDBV2Listener) handleWrite() http.HandlerFunc { } if h.MaxUndeliveredMetrics > 0 { - h.writeTracking(res, metrics) + h.writeWithTracking(res, metrics) } else { - for _, m := range metrics { - h.acc.AddMetric(m) - } - - res.WriteHeader(http.StatusNoContent) - return + h.write(res, metrics) } } } -func (h *InfluxDBV2Listener) writeTracking(res http.ResponseWriter, metrics []telegraf.Metric) { +func (h *InfluxDBV2Listener) writeWithTracking(res http.ResponseWriter, metrics []telegraf.Metric) { pending := h.totalUndeliveredMetrics.Load() if pending+int64(len(metrics)) > int64(h.MaxUndeliveredMetrics) { res.WriteHeader(http.StatusTooManyRequests) @@ -363,14 +358,22 @@ func (h *InfluxDBV2Listener) writeTracking(res http.ResponseWriter, metrics []te } h.countLock.Lock() - trackingId := h.trackingAcc.AddTrackingMetricGroup(metrics) - h.trackingMetricCount[trackingId] = int64(len(metrics)) + trackingID := h.trackingAcc.AddTrackingMetricGroup(metrics) + h.trackingMetricCount[trackingID] = int64(len(metrics)) h.totalUndeliveredMetrics.Add(int64(len(metrics))) h.countLock.Unlock() res.WriteHeader(http.StatusNoContent) } +func (h *InfluxDBV2Listener) write(res http.ResponseWriter, metrics []telegraf.Metric) { + for _, m := range metrics { + h.acc.AddMetric(m) + } + + res.WriteHeader(http.StatusNoContent) +} + func tooLarge(res http.ResponseWriter, maxLength int64) error { res.Header().Set("Content-Type", "application/json") res.Header().Set("X-Influxdb-Error", "http: request body too large") diff --git a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go index eb58b0c1f9d0b..23b6d77c9ff1b 100644 --- a/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go +++ b/plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go @@ -613,13 +613,13 @@ func TestRateLimitedConnectionDropsSecondRequest(t *testing.T) { defer listener.Stop() msg := "xyzzy value=42\n" - postUrl := createURL(listener, "http", "/api/v2/write", "bucket=mybucket&precision=s") - resp, err := http.Post(postUrl, "", bytes.NewBuffer([]byte(msg))) + postURL := createURL(listener, "http", "/api/v2/write", "bucket=mybucket&precision=s") + resp, err := http.Post(postURL, "", bytes.NewBuffer([]byte(msg))) // #nosec G107 -- url has to be dynamic due to dynamic port number require.NoError(t, err) require.NoError(t, resp.Body.Close()) require.EqualValues(t, 204, resp.StatusCode) - resp, err = http.Post(postUrl, "", bytes.NewBuffer([]byte(msg))) + resp, err = http.Post(postURL, "", bytes.NewBuffer([]byte(msg))) // #nosec G107 -- url has to be dynamic due to dynamic port number require.NoError(t, err) require.NoError(t, resp.Body.Close()) require.EqualValues(t, 429, resp.StatusCode) @@ -633,8 +633,8 @@ func TestRateLimitedConnectionAcceptsNewRequestOnDelivery(t *testing.T) { defer listener.Stop() msg := "xyzzy value=42\n" - postUrl := createURL(listener, "http", "/api/v2/write", "bucket=mybucket&precision=s") - resp, err := http.Post(postUrl, "", bytes.NewBuffer([]byte(msg))) + postURL := createURL(listener, "http", "/api/v2/write", "bucket=mybucket&precision=s") + resp, err := http.Post(postURL, "", bytes.NewBuffer([]byte(msg))) // #nosec G107 -- url has to be dynamic due to dynamic port number require.NoError(t, err) require.NoError(t, resp.Body.Close()) require.EqualValues(t, 204, resp.StatusCode) @@ -644,7 +644,7 @@ func TestRateLimitedConnectionAcceptsNewRequestOnDelivery(t *testing.T) { m.Accept() } - resp, err = http.Post(postUrl, "", bytes.NewBuffer([]byte(msg))) + resp, err = http.Post(postURL, "", bytes.NewBuffer([]byte(msg))) // #nosec G107 -- url has to be dynamic due to dynamic port number require.NoError(t, err) require.NoError(t, resp.Body.Close()) require.EqualValues(t, 204, resp.StatusCode)