Skip to content

Commit

Permalink
fix(inputs.influxdb_v2_listener): Linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsStegman committed May 15, 2024
1 parent 8fade8b commit 8123eec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
23 changes: 13 additions & 10 deletions plugins/inputs/influxdb_v2_listener/influxdb_v2_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,34 +343,37 @@ 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)
return
}

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")
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/influxdb_v2_listener/influxdb_v2_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8123eec

Please sign in to comment.