From 44c2c4de85cf72340cbc95f0146146be8c2578ad Mon Sep 17 00:00:00 2001 From: inosato <48383032+inosato@users.noreply.github.com> Date: Tue, 2 Aug 2022 17:27:49 +0900 Subject: [PATCH] Remove ioutil (#1096) Signed-off-by: inosato --- api/prometheus/v1/api_test.go | 4 ++-- prometheus/process_collector.go | 3 +-- prometheus/push/push.go | 6 +++--- prometheus/push/push_test.go | 4 ++-- prometheus/registry.go | 3 +-- prometheus/registry_test.go | 5 ++--- 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 9fbd29b..009035d 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -17,7 +17,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "math" "net/http" "net/http/httptest" @@ -1680,7 +1680,7 @@ func (c *httpTestClient) Do(ctx context.Context, req *http.Request) (*http.Respo var body []byte done := make(chan struct{}) go func() { - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) close(done) }() diff --git a/prometheus/process_collector.go b/prometheus/process_collector.go index 5bfe0ff..1245428 100644 --- a/prometheus/process_collector.go +++ b/prometheus/process_collector.go @@ -16,7 +16,6 @@ package prometheus import ( "errors" "fmt" - "io/ioutil" "os" "strconv" "strings" @@ -152,7 +151,7 @@ func (c *processCollector) reportError(ch chan<- Metric, desc *Desc, err error) // It is meant to be used for the PidFn field in ProcessCollectorOpts. func NewPidFileFn(pidFilePath string) func() (int, error) { return func() (int, error) { - content, err := ioutil.ReadFile(pidFilePath) + content, err := os.ReadFile(pidFilePath) if err != nil { return 0, fmt.Errorf("can't read pid file %q: %+v", pidFilePath, err) } diff --git a/prometheus/push/push.go b/prometheus/push/push.go index 9d03d8b..06dee37 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -40,7 +40,7 @@ import ( "encoding/base64" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -245,7 +245,7 @@ func (p *Pusher) Delete() error { } defer resp.Body.Close() if resp.StatusCode != http.StatusAccepted { - body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. + body, _ := io.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. return fmt.Errorf("unexpected status code %d while deleting %s: %s", resp.StatusCode, p.fullURL(), body) } return nil @@ -297,7 +297,7 @@ func (p *Pusher) push(ctx context.Context, method string) error { defer resp.Body.Close() // Depending on version and configuration of the PGW, StatusOK or StatusAccepted may be returned. if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { - body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. + body, _ := io.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. return fmt.Errorf("unexpected status code %d while pushing to %s: %s", resp.StatusCode, p.fullURL(), body) } return nil diff --git a/prometheus/push/push_test.go b/prometheus/push/push_test.go index 3367a4b..a0e8e9a 100644 --- a/prometheus/push/push_test.go +++ b/prometheus/push/push_test.go @@ -15,7 +15,7 @@ package push import ( "bytes" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -38,7 +38,7 @@ func TestPush(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { lastMethod = r.Method var err error - lastBody, err = ioutil.ReadAll(r.Body) + lastBody, err = io.ReadAll(r.Body) if err != nil { t.Fatal(err) } diff --git a/prometheus/registry.go b/prometheus/registry.go index eda2892..36fd64e 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -16,7 +16,6 @@ package prometheus import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -563,7 +562,7 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) { // This is intended for use with the textfile collector of the node exporter. // Note that the node exporter expects the filename to be suffixed with ".prom". func WriteToTextfile(filename string, g Gatherer) error { - tmp, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename)) + tmp, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename)) if err != nil { return err } diff --git a/prometheus/registry_test.go b/prometheus/registry_test.go index 0a3d746..9443ef3 100644 --- a/prometheus/registry_test.go +++ b/prometheus/registry_test.go @@ -23,7 +23,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "math/rand" "net/http" "net/http/httptest" @@ -1066,7 +1065,7 @@ test_summary_count{name="foo"} 2 gauge.With(prometheus.Labels{"name": "baz"}).Set(1.1) counter.With(prometheus.Labels{"name": "qux"}).Inc() - tmpfile, err := ioutil.TempFile("", "prom_registry_test") + tmpfile, err := os.CreateTemp("", "prom_registry_test") if err != nil { t.Fatal(err) } @@ -1076,7 +1075,7 @@ test_summary_count{name="foo"} 2 t.Fatal(err) } - fileBytes, err := ioutil.ReadFile(tmpfile.Name()) + fileBytes, err := os.ReadFile(tmpfile.Name()) if err != nil { t.Fatal(err) }