Skip to content

Commit

Permalink
Replace ioutil with io and os (#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
estensen committed Aug 3, 2022
1 parent e99a0ac commit eb55e60
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
)

// Size in bytes for 32-bit ints.
Expand Down Expand Up @@ -374,7 +373,7 @@ func (t *THeaderTransport) ReadFrame(ctx context.Context) error {
if err != nil {
return err
}
t.frameReader = ioutil.NopCloser(&t.frameBuffer)
t.frameReader = io.NopCloser(&t.frameBuffer)

// Peek and handle the next 32 bits.
buf = t.frameBuffer.Bytes()[:size32]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -138,7 +137,7 @@ func (p *THttpClient) closeResponse() error {
// reused. Errors are being ignored here because if the connection is invalid
// and this fails for some reason, the Close() method will do any remaining
// cleanup.
io.Copy(ioutil.Discard, p.response.Body)
io.Copy(io.Discard, p.response.Body)

err = p.response.Body.Close()
}
Expand Down
3 changes: 1 addition & 2 deletions exporters/jaeger/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -308,7 +307,7 @@ func (c *collectorUploader) upload(ctx context.Context, batch *gen.Batch) error
return err
}

_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
if err = resp.Body.Close(); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions exporters/otlp/otlpmetric/internal/otlpconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric

import (
"crypto/tls"
"io/ioutil"
"net/url"
"os"
"path"
Expand All @@ -29,7 +28,7 @@ import (
// DefaultEnvOptionsReader is the default environments reader.
var DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
GetEnv: os.Getenv,
ReadFile: ioutil.ReadFile,
ReadFile: os.ReadFile,
Namespace: "OTEL_EXPORTER_OTLP",
}

Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlpmetric/internal/otlpconfig/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"os"
)

// ReadTLSConfigFromFile reads a PEM certificate file and creates
// a tls.Config that will use this certifate to verify a server certificate.
func ReadTLSConfigFromFile(path string) (*tls.Config, error) {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions exporters/otlp/otlpmetric/otlpmetrichttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand All @@ -41,7 +40,7 @@ const contentTypeProto = "application/x-protobuf"

var gzPool = sync.Pool{
New: func() interface{} {
w := gzip.NewWriter(ioutil.Discard)
w := gzip.NewWriter(io.Discard)
return w
},
}
Expand Down Expand Up @@ -163,7 +162,7 @@ func (d *client) UploadMetrics(ctx context.Context, protoMetrics *metricpb.Resou
rErr = newResponseError(resp.Header)

// Going to retry, drain the body to reuse the connection.
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
_ = resp.Body.Close()
return err
}
Expand Down Expand Up @@ -223,7 +222,7 @@ func (d *client) newRequest(body []byte) (request, error) {
// bodyReader returns a closure returning a new reader for buf.
func bodyReader(buf []byte) func() io.ReadCloser {
return func() io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader(buf))
return io.NopCloser(bytes.NewReader(buf))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -149,7 +148,7 @@ func readRequest(r *http.Request) ([]byte, error) {
if r.Header.Get("Content-Encoding") == "gzip" {
return readGzipBody(r.Body)
}
return ioutil.ReadAll(r.Body)
return io.ReadAll(r.Body)
}

func readGzipBody(body io.Reader) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions exporters/otlp/otlptrace/internal/otlpconfig/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/

import (
"crypto/tls"
"io/ioutil"
"net/url"
"os"
"path"
Expand All @@ -29,7 +28,7 @@ import (
// DefaultEnvOptionsReader is the default environments reader.
var DefaultEnvOptionsReader = envconfig.EnvOptionsReader{
GetEnv: os.Getenv,
ReadFile: ioutil.ReadFile,
ReadFile: os.ReadFile,
Namespace: "OTEL_EXPORTER_OTLP",
}

Expand Down
7 changes: 3 additions & 4 deletions exporters/otlp/otlptrace/otlptracehttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand All @@ -41,7 +40,7 @@ const contentTypeProto = "application/x-protobuf"

var gzPool = sync.Pool{
New: func() interface{} {
w := gzip.NewWriter(ioutil.Discard)
w := gzip.NewWriter(io.Discard)
return w
},
}
Expand Down Expand Up @@ -165,7 +164,7 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc
rErr = newResponseError(resp.Header)

// Going to retry, drain the body to reuse the connection.
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
_ = resp.Body.Close()
return err
}
Expand Down Expand Up @@ -238,7 +237,7 @@ func (d *client) MarshalLog() interface{} {
// bodyReader returns a closure returning a new reader for buf.
func bodyReader(buf []byte) func() io.ReadCloser {
return func() io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader(buf))
return io.NopCloser(bytes.NewReader(buf))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -168,7 +167,7 @@ func readRequest(r *http.Request) ([]byte, error) {
if r.Header.Get("Content-Encoding") == "gzip" {
return readGzipBody(r.Body)
}
return ioutil.ReadAll(r.Body)
return io.ReadAll(r.Body)
}

func readGzipBody(body io.Reader) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions exporters/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -144,7 +143,7 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpa
// but it is still being read because according to https://golang.org/pkg/net/http/#Response
// > The default HTTP client's Transport may not reuse HTTP/1.x "keep-alive" TCP connections
// > if the Body is not read to completion and closed.
_, err = io.Copy(ioutil.Discard, resp.Body)
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return e.errf("failed to read response body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions exporters/zipkin/zipkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -135,7 +135,7 @@ func startMockZipkinCollector(t *testing.T) *mockZipkinCollector {
}

func (c *mockZipkinCollector) handler(w http.ResponseWriter, r *http.Request) {
jsonBytes, err := ioutil.ReadAll(r.Body)
jsonBytes, err := io.ReadAll(r.Body)
require.NoError(c.t, err)
var models []zkmodel.SpanModel
err = json.Unmarshal(jsonBytes, &models)
Expand Down
16 changes: 8 additions & 8 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package otel
import (
"bytes"
"errors"
"io/ioutil"
"io"
"log"
"os"
"testing"
Expand Down Expand Up @@ -129,9 +129,9 @@ func TestHandlerRace(t *testing.T) {
}

func BenchmarkErrorHandler(b *testing.B) {
primary := &errLogger{l: log.New(ioutil.Discard, "", 0)}
secondary := &errLogger{l: log.New(ioutil.Discard, "", 0)}
tertiary := &errLogger{l: log.New(ioutil.Discard, "", 0)}
primary := &errLogger{l: log.New(io.Discard, "", 0)}
secondary := &errLogger{l: log.New(io.Discard, "", 0)}
tertiary := &errLogger{l: log.New(io.Discard, "", 0)}

globalErrorHandler.setDelegate(primary)

Expand Down Expand Up @@ -167,7 +167,7 @@ func BenchmarkGetDefaultErrorHandler(b *testing.B) {
}

func BenchmarkGetDelegatedErrorHandler(b *testing.B) {
SetErrorHandler(&errLogger{l: log.New(ioutil.Discard, "", 0)})
SetErrorHandler(&errLogger{l: log.New(io.Discard, "", 0)})

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -180,7 +180,7 @@ func BenchmarkGetDelegatedErrorHandler(b *testing.B) {

func BenchmarkDefaultErrorHandlerHandle(b *testing.B) {
globalErrorHandler.setDelegate(
&errLogger{l: log.New(ioutil.Discard, "", 0)},
&errLogger{l: log.New(io.Discard, "", 0)},
)

eh := GetErrorHandler()
Expand All @@ -197,7 +197,7 @@ func BenchmarkDefaultErrorHandlerHandle(b *testing.B) {

func BenchmarkDelegatedErrorHandlerHandle(b *testing.B) {
eh := GetErrorHandler()
SetErrorHandler(&errLogger{l: log.New(ioutil.Discard, "", 0)})
SetErrorHandler(&errLogger{l: log.New(io.Discard, "", 0)})
err := errors.New("benchmark delegated error handler handle")

b.ReportAllocs()
Expand All @@ -210,7 +210,7 @@ func BenchmarkDelegatedErrorHandlerHandle(b *testing.B) {
}

func BenchmarkSetErrorHandlerDelegation(b *testing.B) {
alt := &errLogger{l: log.New(ioutil.Discard, "", 0)}
alt := &errLogger{l: log.New(io.Discard, "", 0)}

b.ReportAllocs()
b.ResetTimer()
Expand Down
6 changes: 3 additions & 3 deletions sdk/resource/os_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package resource_test

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -67,8 +67,8 @@ func TestUnameError(t *testing.T) {
func TestGetFirstAvailableFile(t *testing.T) {
tempDir := t.TempDir()

file1, _ := ioutil.TempFile(tempDir, "candidate_")
file2, _ := ioutil.TempFile(tempDir, "candidate_")
file1, _ := os.CreateTemp(tempDir, "candidate_")
file2, _ := os.CreateTemp(tempDir, "candidate_")

filename1, filename2 := file1.Name(), file2.Name()

Expand Down

0 comments on commit eb55e60

Please sign in to comment.