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 02ca2342ab
This commit is contained in:
Aliaksandr Valialkin 2022-08-22 00:13:44 +03:00
parent 2c3a89339d
commit 1f89278d88
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
19 changed files with 37 additions and 44 deletions

View file

@ -3,7 +3,7 @@ package remotewrite
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -351,7 +351,7 @@ again:
} }
metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.sanitizedURL, statusCode)).Inc() metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.sanitizedURL, statusCode)).Inc()
if statusCode == 409 || statusCode == 400 { if statusCode == 409 || statusCode == 400 {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
if err != nil { if err != nil {
remoteWriteRejectedLogger.Errorf("sending a block with size %d bytes to %q was rejected (skipping the block): status code %d; "+ 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 { if retryDuration > time.Minute {
retryDuration = time.Minute retryDuration = time.Minute
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
if err != nil { if err != nil {
logger.Errorf("cannot read response body from %q during retry #%d: %s", c.sanitizedURL, retriesCount, err) logger.Errorf("cannot read response body from %q during retry #%d: %s", c.sanitizedURL, retriesCount, err)

View file

@ -3,7 +3,7 @@ package datasource
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "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) return nil, fmt.Errorf("error getting response from %s: %w", req.URL.Redacted(), err)
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
return nil, fmt.Errorf("unexpected response code %d for %s. Response body %s", resp.StatusCode, req.URL.Redacted(), body) return nil, fmt.Errorf("unexpected response code %d for %s. Response body %s", resp.StatusCode, req.URL.Redacted(), body)
} }

View file

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -89,7 +89,7 @@ func (am *AlertManager) send(ctx context.Context, alerts []Alert) error {
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("failed to read response from %q: %w", am.addr, err) return fmt.Errorf("failed to read response from %q: %w", am.addr, err)
} }

View file

@ -5,7 +5,7 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"path" "path"
"strings" "strings"
@ -257,7 +257,7 @@ func (c *Client) send(ctx context.Context, data []byte) error {
} }
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { 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", return fmt.Errorf("unexpected response code %d for %s. Response body %q",
resp.StatusCode, req.URL.Redacted(), body) resp.StatusCode, req.URL.Redacted(), body)
} }

View file

@ -3,7 +3,7 @@ package remotewrite
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "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)) 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 { if err != nil {
rw.err(w, fmt.Errorf("body read err: %w", err)) rw.err(w, fmt.Errorf("body read err: %w", err))
return return

View file

@ -3,7 +3,7 @@ package opentsdb
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"strings" "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) return nil, fmt.Errorf("Bad return from OpenTSDB: %q: %v", resp.StatusCode, resp)
} }
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not retrieve metric data from %q: %s", q, err) 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) return nil, fmt.Errorf("Bad return from OpenTSDB: %q: %v", resp.StatusCode, resp)
} }
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not retrieve series data from %q: %s", q, err) 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 return Metric{}, nil
} }
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Println("couldn't read response body from OpenTSDB query...skipping") log.Println("couldn't read response body from OpenTSDB query...skipping")
return Metric{}, nil return Metric{}, nil

View file

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"net/http" "net/http"
"strings" "strings"
@ -394,7 +393,7 @@ func do(req *http.Request) error {
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
if resp.StatusCode != http.StatusNoContent { if resp.StatusCode != http.StatusNoContent {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err) return fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err)
} }

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
@ -148,7 +147,7 @@ func (c *vmNativeClient) do(req *http.Request, expSC int) (*http.Response, error
} }
if resp.StatusCode != expSC { if resp.StatusCode != expSC {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err) return nil, fmt.Errorf("failed to read response body for status code %d: %s", resp.StatusCode, err)
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -130,7 +130,7 @@ func (cfg *Config) SignRequest(req *http.Request, payloadHash string) error {
} }
func readResponseBody(resp *http.Response, apiURL string) ([]byte, 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)

View file

@ -3,7 +3,6 @@ package fs
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -383,7 +382,7 @@ func ReadFileOrHTTP(path string) ([]byte, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot fetch %q: %w", path, err) 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read %q: %s", path, err) return nil, fmt.Errorf("cannot read %q: %s", path, err)

View file

@ -3,7 +3,6 @@ package mergeset
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"sort" "sort"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" "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) { func unmarshalMetaindexRows(dst []metaindexRow, r io.Reader) ([]metaindexRow, error) {
// It is ok to read all the metaindex in memory, // It is ok to read all the metaindex in memory,
// since it is quite small. // since it is quite small.
compressedData, err := ioutil.ReadAll(r) compressedData, err := io.ReadAll(r)
if err != nil { if err != nil {
return dst, fmt.Errorf("cannot read metaindex data: %w", err) return dst, fmt.Errorf("cannot read metaindex data: %w", err)
} }

View file

@ -6,7 +6,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -198,7 +197,7 @@ func (c *client) GetStreamReader() (*streamReader, error) {
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
metrics.GetOrCreateCounter(fmt.Sprintf(`vm_promscrape_scrapes_total{status_code="%d"}`, resp.StatusCode)).Inc() 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() _ = resp.Body.Close()
cancel() cancel()
return nil, fmt.Errorf("unexpected status code returned when scraping %q: %d; expecting %d; response body: %q", return nil, fmt.Errorf("unexpected status code returned when scraping %q: %d; expecting %d; response body: %q",

View file

@ -3,7 +3,7 @@ package gce
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -94,7 +94,7 @@ func getAPIResponse(client *http.Client, apiURL, filter, pageToken string) ([]by
} }
func readResponseBody(resp *http.Response, apiURL string) ([]byte, 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)

View file

@ -6,7 +6,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -580,7 +579,7 @@ func (uw *urlWatcher) reloadObjects() string {
return "" return ""
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body) logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body)
return "" return ""
@ -676,7 +675,7 @@ func (uw *urlWatcher) watchForUpdates() {
uw.staleResourceVersions.Inc() uw.staleResourceVersions.Inc()
uw.resourceVersion = "" uw.resourceVersion = ""
} else { } else {
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body) logger.Errorf("unexpected status code for request to %q: %d; want %d; response: %q", requestURL, resp.StatusCode, http.StatusOK, body)
backoffSleep() backoffSleep()

View file

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
@ -138,7 +138,7 @@ func getCreds(cfg *apiConfig) (*apiCredentials, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed query openstack identity api, url: %s, err: %w", apiURL.String(), err) 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL.String(), err) 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. // readResponseBody reads body from http.Response.
func readResponseBody(resp *http.Response, apiURL string) ([]byte, 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)

View file

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"sync" "sync"
@ -235,7 +235,7 @@ func getAPIResponse(apiURL string, cfg *apiConfig) ([]byte, error) {
// readResponseBody reads body from http.Response. // readResponseBody reads body from http.Response.
func readResponseBody(resp *http.Response, apiURL string) ([]byte, 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() _ = resp.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err) return nil, fmt.Errorf("cannot read response from %q: %w", apiURL, err)

View file

@ -4,7 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" "github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"io/ioutil" "io"
"math" "math"
"math/bits" "math/bits"
"strconv" "strconv"
@ -406,7 +406,7 @@ func (sw *scrapeWork) getTargetResponse() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
data, err := ioutil.ReadAll(sr) data, err := io.ReadAll(sr)
sr.MustClose() sr.MustClose()
return data, err return data, err
} }

View file

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
@ -34,7 +34,7 @@ func Create(createSnapshotURL string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -72,7 +72,7 @@ func Delete(deleteSnapshotURL string, snapshotName string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View file

@ -3,7 +3,6 @@ package storage
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"sort" "sort"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding" "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) { func unmarshalMetaindexRows(dst []metaindexRow, r io.Reader) ([]metaindexRow, error) {
compressedData, err := ioutil.ReadAll(r) compressedData, err := io.ReadAll(r)
if err != nil { if err != nil {
return dst, fmt.Errorf("cannot read metaindex rows: %w", err) return dst, fmt.Errorf("cannot read metaindex rows: %w", err)
} }