From 1f89278d886c7c2ad3ef897a35b2d03fe7b6f48a Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 22 Aug 2022 00:13:44 +0300 Subject: [PATCH] all: subsitute ioutil.ReadAll with io.ReadAll ioutil.ReadAll is deprecated since Go1.16 - see https://tip.golang.org/doc/go1.16#ioutil VictoriaMetrics requires at least Go1.18, so it is OK to switch from ioutil.ReadAll to io.ReadAll. This is a follow-up for 02ca2342ab01627eb5fdbb3174792b86fc1de7a9 --- app/vmagent/remotewrite/client.go | 6 +++--- app/vmalert/datasource/vm.go | 4 ++-- app/vmalert/notifier/alertmanager.go | 4 ++-- app/vmalert/remotewrite/remotewrite.go | 4 ++-- app/vmalert/remotewrite/remotewrite_test.go | 4 ++-- app/vmctl/opentsdb/opentsdb.go | 8 ++++---- app/vmctl/vm/vm.go | 3 +-- app/vmctl/vm_native.go | 3 +-- lib/awsapi/config.go | 4 ++-- lib/fs/fs.go | 3 +-- lib/mergeset/metaindex_row.go | 3 +-- lib/promscrape/client.go | 3 +-- lib/promscrape/discovery/gce/api.go | 4 ++-- lib/promscrape/discovery/kubernetes/api_watcher.go | 5 ++--- lib/promscrape/discovery/openstack/api.go | 6 +++--- lib/promscrape/discovery/yandexcloud/api.go | 4 ++-- lib/promscrape/scrapework.go | 4 ++-- lib/snapshot/snapshot.go | 6 +++--- lib/storage/metaindex_row.go | 3 +-- 19 files changed, 37 insertions(+), 44 deletions(-) diff --git a/app/vmagent/remotewrite/client.go b/app/vmagent/remotewrite/client.go index c888092a4..06f4f324d 100644 --- a/app/vmagent/remotewrite/client.go +++ b/app/vmagent/remotewrite/client.go @@ -3,7 +3,7 @@ package remotewrite import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -351,7 +351,7 @@ again: } metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.sanitizedURL, statusCode)).Inc() if statusCode == 409 || statusCode == 400 { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { remoteWriteRejectedLogger.Errorf("sending a block with size %d bytes to %q was rejected (skipping the block): status code %d; "+ @@ -375,7 +375,7 @@ again: if retryDuration > time.Minute { retryDuration = time.Minute } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { logger.Errorf("cannot read response body from %q during retry #%d: %s", c.sanitizedURL, retriesCount, err) diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 189eead00..36547481f 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -3,7 +3,7 @@ package datasource import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -156,7 +156,7 @@ func (s *VMStorage) do(ctx context.Context, req *http.Request) (*http.Response, return nil, fmt.Errorf("error getting response from %s: %w", req.URL.Redacted(), err) } if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() return nil, fmt.Errorf("unexpected response code %d for %s. Response body %s", resp.StatusCode, req.URL.Redacted(), body) } diff --git a/app/vmalert/notifier/alertmanager.go b/app/vmalert/notifier/alertmanager.go index de6d5c226..14ec932c7 100644 --- a/app/vmalert/notifier/alertmanager.go +++ b/app/vmalert/notifier/alertmanager.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -89,7 +89,7 @@ func (am *AlertManager) send(ctx context.Context, alerts []Alert) error { defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response from %q: %w", am.addr, err) } diff --git a/app/vmalert/remotewrite/remotewrite.go b/app/vmalert/remotewrite/remotewrite.go index ec5ba98e6..943bba901 100644 --- a/app/vmalert/remotewrite/remotewrite.go +++ b/app/vmalert/remotewrite/remotewrite.go @@ -5,7 +5,7 @@ import ( "context" "flag" "fmt" - "io/ioutil" + "io" "net/http" "path" "strings" @@ -257,7 +257,7 @@ func (c *Client) send(ctx context.Context, data []byte) error { } defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return fmt.Errorf("unexpected response code %d for %s. Response body %q", resp.StatusCode, req.URL.Redacted(), body) } diff --git a/app/vmalert/remotewrite/remotewrite_test.go b/app/vmalert/remotewrite/remotewrite_test.go index ed2320262..668c422df 100644 --- a/app/vmalert/remotewrite/remotewrite_test.go +++ b/app/vmalert/remotewrite/remotewrite_test.go @@ -3,7 +3,7 @@ package remotewrite import ( "context" "fmt" - "io/ioutil" + "io" "math/rand" "net/http" "net/http/httptest" @@ -96,7 +96,7 @@ func (rw *rwServer) handler(w http.ResponseWriter, r *http.Request) { rw.err(w, fmt.Errorf("header read error: X-Prometheus-Remote-Write-Version is not 0.1.0 (%q)", h)) } - data, err := ioutil.ReadAll(r.Body) + data, err := io.ReadAll(r.Body) if err != nil { rw.err(w, fmt.Errorf("body read err: %w", err)) return diff --git a/app/vmctl/opentsdb/opentsdb.go b/app/vmctl/opentsdb/opentsdb.go index f6700eb8a..bfa6bbcb6 100644 --- a/app/vmctl/opentsdb/opentsdb.go +++ b/app/vmctl/opentsdb/opentsdb.go @@ -3,7 +3,7 @@ package opentsdb import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "strings" @@ -115,7 +115,7 @@ func (c Client) FindMetrics(q string) ([]string, error) { return nil, fmt.Errorf("Bad return from OpenTSDB: %q: %v", resp.StatusCode, resp) } defer func() { _ = resp.Body.Close() }() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("could not retrieve metric data from %q: %s", q, err) } @@ -139,7 +139,7 @@ func (c Client) FindSeries(metric string) ([]Meta, error) { return nil, fmt.Errorf("Bad return from OpenTSDB: %q: %v", resp.StatusCode, resp) } defer func() { _ = resp.Body.Close() }() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("could not retrieve series data from %q: %s", q, err) } @@ -200,7 +200,7 @@ func (c Client) GetData(series Meta, rt RetentionMeta, start int64, end int64, m return Metric{}, nil } defer func() { _ = resp.Body.Close() }() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { log.Println("couldn't read response body from OpenTSDB query...skipping") return Metric{}, nil diff --git a/app/vmctl/vm/vm.go b/app/vmctl/vm/vm.go index 481258532..50b068211 100644 --- a/app/vmctl/vm/vm.go +++ b/app/vmctl/vm/vm.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math" "net/http" "strings" @@ -394,7 +393,7 @@ func do(req *http.Request) error { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusNoContent { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err) } diff --git a/app/vmctl/vm_native.go b/app/vmctl/vm_native.go index 88d687e3a..3c2105b18 100644 --- a/app/vmctl/vm_native.go +++ b/app/vmctl/vm_native.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "log" "net/http" @@ -148,7 +147,7 @@ func (c *vmNativeClient) do(req *http.Request, expSC int) (*http.Response, error } if resp.StatusCode != expSC { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err) } diff --git a/lib/awsapi/config.go b/lib/awsapi/config.go index a86ff7292..706f87526 100644 --- a/lib/awsapi/config.go +++ b/lib/awsapi/config.go @@ -4,7 +4,7 @@ import ( "encoding/json" "encoding/xml" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -130,7 +130,7 @@ func (cfg *Config) SignRequest(req *http.Request, payloadHash string) error { } func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) { - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) diff --git a/lib/fs/fs.go b/lib/fs/fs.go index c638028a2..9dc976f12 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -3,7 +3,6 @@ package fs import ( "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -383,7 +382,7 @@ func ReadFileOrHTTP(path string) ([]byte, error) { if err != nil { return nil, fmt.Errorf("cannot fetch %q: %w", path, err) } - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read %q: %s", path, err) diff --git a/lib/mergeset/metaindex_row.go b/lib/mergeset/metaindex_row.go index 3d5b07fe9..c06a4a194 100644 --- a/lib/mergeset/metaindex_row.go +++ b/lib/mergeset/metaindex_row.go @@ -3,7 +3,6 @@ package mergeset import ( "fmt" "io" - "io/ioutil" "sort" "github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" @@ -83,7 +82,7 @@ func (mr *metaindexRow) Unmarshal(src []byte) ([]byte, error) { func unmarshalMetaindexRows(dst []metaindexRow, r io.Reader) ([]metaindexRow, error) { // It is ok to read all the metaindex in memory, // since it is quite small. - compressedData, err := ioutil.ReadAll(r) + compressedData, err := io.ReadAll(r) if err != nil { return dst, fmt.Errorf("cannot read metaindex data: %w", err) } diff --git a/lib/promscrape/client.go b/lib/promscrape/client.go index 568dcea63..93df47862 100644 --- a/lib/promscrape/client.go +++ b/lib/promscrape/client.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -198,7 +197,7 @@ func (c *client) GetStreamReader() (*streamReader, error) { } if resp.StatusCode != http.StatusOK { metrics.GetOrCreateCounter(fmt.Sprintf(`vm_promscrape_scrapes_total{status_code="%d"}`, resp.StatusCode)).Inc() - respBody, _ := ioutil.ReadAll(resp.Body) + respBody, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() cancel() return nil, fmt.Errorf("unexpected status code returned when scraping %q: %d; expecting %d; response body: %q", diff --git a/lib/promscrape/discovery/gce/api.go b/lib/promscrape/discovery/gce/api.go index 695fc0842..fdf373901 100644 --- a/lib/promscrape/discovery/gce/api.go +++ b/lib/promscrape/discovery/gce/api.go @@ -3,7 +3,7 @@ package gce import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -94,7 +94,7 @@ func getAPIResponse(client *http.Client, apiURL, filter, pageToken string) ([]by } func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) { - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) diff --git a/lib/promscrape/discovery/kubernetes/api_watcher.go b/lib/promscrape/discovery/kubernetes/api_watcher.go index f0689d2e3..ad6ba53a2 100644 --- a/lib/promscrape/discovery/kubernetes/api_watcher.go +++ b/lib/promscrape/discovery/kubernetes/api_watcher.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -580,7 +579,7 @@ func (uw *urlWatcher) reloadObjects() string { return "" } if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body) return "" @@ -676,7 +675,7 @@ func (uw *urlWatcher) watchForUpdates() { uw.staleResourceVersions.Inc() uw.resourceVersion = "" } else { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body) backoffSleep() diff --git a/lib/promscrape/discovery/openstack/api.go b/lib/promscrape/discovery/openstack/api.go index eaa6a0e90..4dbc741ad 100644 --- a/lib/promscrape/discovery/openstack/api.go +++ b/lib/promscrape/discovery/openstack/api.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "path" @@ -138,7 +138,7 @@ func getCreds(cfg *apiConfig) (*apiCredentials, error) { if err != nil { return nil, fmt.Errorf("failed query openstack identity api, url: %s, err: %w", apiURL.String(), err) } - r, err := ioutil.ReadAll(resp.Body) + r, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read response from %q: %w", apiURL.String(), err) @@ -168,7 +168,7 @@ func getCreds(cfg *apiConfig) (*apiCredentials, error) { // readResponseBody reads body from http.Response. func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) { - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) diff --git a/lib/promscrape/discovery/yandexcloud/api.go b/lib/promscrape/discovery/yandexcloud/api.go index 5372b8e17..77591621b 100644 --- a/lib/promscrape/discovery/yandexcloud/api.go +++ b/lib/promscrape/discovery/yandexcloud/api.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "sync" @@ -235,7 +235,7 @@ func getAPIResponse(apiURL string, cfg *apiConfig) ([]byte, error) { // readResponseBody reads body from http.Response. func readResponseBody(resp *http.Response, apiURL string) ([]byte, error) { - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) _ = resp.Body.Close() if err != nil { return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) diff --git a/lib/promscrape/scrapework.go b/lib/promscrape/scrapework.go index c9e1fea7a..ae3dc4d4c 100644 --- a/lib/promscrape/scrapework.go +++ b/lib/promscrape/scrapework.go @@ -4,7 +4,7 @@ import ( "flag" "fmt" "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" - "io/ioutil" + "io" "math" "math/bits" "strconv" @@ -406,7 +406,7 @@ func (sw *scrapeWork) getTargetResponse() ([]byte, error) { if err != nil { return nil, err } - data, err := ioutil.ReadAll(sr) + data, err := io.ReadAll(sr) sr.MustClose() return data, err } diff --git a/lib/snapshot/snapshot.go b/lib/snapshot/snapshot.go index ba19ea8e2..b7847fa9b 100644 --- a/lib/snapshot/snapshot.go +++ b/lib/snapshot/snapshot.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "regexp" @@ -34,7 +34,7 @@ func Create(createSnapshotURL string) (string, error) { if err != nil { return "", err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -72,7 +72,7 @@ func Delete(deleteSnapshotURL string, snapshotName string) error { if err != nil { return err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return err } diff --git a/lib/storage/metaindex_row.go b/lib/storage/metaindex_row.go index 162bbd94f..2ec257630 100644 --- a/lib/storage/metaindex_row.go +++ b/lib/storage/metaindex_row.go @@ -3,7 +3,6 @@ package storage import ( "fmt" "io" - "io/ioutil" "sort" "github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" @@ -128,7 +127,7 @@ func (mr *metaindexRow) Unmarshal(src []byte) ([]byte, error) { } func unmarshalMetaindexRows(dst []metaindexRow, r io.Reader) ([]metaindexRow, error) { - compressedData, err := ioutil.ReadAll(r) + compressedData, err := io.ReadAll(r) if err != nil { return dst, fmt.Errorf("cannot read metaindex rows: %w", err) }