Skip to content

Commit

Permalink
Remove usage of ioutil deprecated package since go1.16 (#13132)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Aug 9, 2022
1 parent 4dd1595 commit 94e5fd9
Show file tree
Hide file tree
Showing 159 changed files with 386 additions and 449 deletions.
3 changes: 1 addition & 2 deletions cmd/configschema/docsgen/docsgen/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package docsgen // import "github.com/open-telemetry/opentelemetry-collector-con
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -40,7 +39,7 @@ func CLI(factories component.Factories, dr configschema.DirResolver) {
panic(err)
}

handleCLI(factories, dr, tableTmpl, ioutil.WriteFile, os.Stdout, os.Args...)
handleCLI(factories, dr, tableTmpl, os.WriteFile, os.Stdout, os.Args...)
}

func handleCLI(
Expand Down
4 changes: 2 additions & 2 deletions cmd/configschema/docsgen/docsgen/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package docsgen

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -39,7 +39,7 @@ func TestTableTemplate(t *testing.T) {
}

func testDataField(t *testing.T) *configschema.Field {
jsonBytes, err := ioutil.ReadFile(filepath.Join("testdata", "otlp-receiver.json"))
jsonBytes, err := os.ReadFile(filepath.Join("testdata", "otlp-receiver.json"))
require.NoError(t, err)
field := configschema.Field{}
err = json.Unmarshal(jsonBytes, &field)
Expand Down
5 changes: 2 additions & 3 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -130,7 +129,7 @@ func generateMetrics(ymlDir string, thisDir string, md metadata, useExpGen bool)
}
}
outputFilepath := filepath.Join(outputDir, outputFile)
if err := ioutil.WriteFile(outputFilepath, formatted, 0600); err != nil {
if err := os.WriteFile(outputFilepath, formatted, 0600); err != nil {
return fmt.Errorf("failed writing %q: %w", outputFilepath, err)
}

Expand All @@ -157,7 +156,7 @@ func generateDocumentation(ymlDir string, thisDir string, md metadata, useExpGen
}

outputFile := filepath.Join(ymlDir, "documentation.md")
if err := ioutil.WriteFile(outputFile, buf.Bytes(), 0600); err != nil {
if err := os.WriteFile(outputFile, buf.Bytes(), 0600); err != nil {
return fmt.Errorf("failed writing %q: %w", outputFile, err)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -84,7 +84,7 @@ func Test_runContents(t *testing.T) {
tmpdir := t.TempDir()

metadataFile := filepath.Join(tmpdir, "metadata.yaml")
require.NoError(t, ioutil.WriteFile(metadataFile, []byte(tt.args.yml), 0600))
require.NoError(t, os.WriteFile(metadataFile, []byte(tt.args.yml), 0600))

err := run(metadataFile, tt.args.useExpGen)

Expand All @@ -102,10 +102,10 @@ func Test_runContents(t *testing.T) {
actualDocumentation := filepath.Join(tmpdir, "documentation.md")
require.FileExists(t, actualDocumentation)
if tt.expectedDocumentation != "" {
expectedFileBytes, err := ioutil.ReadFile(tt.expectedDocumentation)
expectedFileBytes, err := os.ReadFile(tt.expectedDocumentation)
require.NoError(t, err)

actualFileBytes, err := ioutil.ReadFile(actualDocumentation)
actualFileBytes, err := os.ReadFile(actualDocumentation)
require.NoError(t, err)

require.Equal(t, expectedFileBytes, actualFileBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package alibabacloudlogserviceexporter
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestTraceDataToLogService(t *testing.T) {
}

func loadFromJSON(file string, obj interface{}) error {
blob, err := ioutil.ReadFile(file)
blob, err := os.ReadFile(file)
if err == nil {
err = json.Unmarshal(blob, obj)
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package awsemfexporter

import (
"io/ioutil"
"os"
"sort"
"strings"
"testing"
Expand All @@ -39,7 +39,7 @@ import (
)

func readFromFile(filename string) string {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions exporter/awsprometheusremotewriteexporter/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -58,7 +57,7 @@ func (si *signingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
return nil, err
}

content, err := ioutil.ReadAll(reqBody)
content, err := io.ReadAll(reqBody)
reqBody.Close()
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ package k8s // import "github.com/open-telemetry/opentelemetry-collector-contrib
import (
"context"
"fmt"
"io/ioutil"
"os"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"

"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
)

type nodeNameProvider interface {
Expand All @@ -40,7 +39,7 @@ type nodeNameProviderImpl struct {

func (p *nodeNameProviderImpl) namespace() string {
namespacePath := "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
val, err := ioutil.ReadFile(namespacePath)
val, err := os.ReadFile(namespacePath)
if err == nil && val != nil {
return string(val)
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/datadogexporter/internal/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package metadata
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestPushMetadata(t *testing.T) {
assert.Equal(t, r.Header.Get("DD-Api-Key"), "apikey")
assert.Equal(t, r.Header.Get("User-Agent"), "otelcontribcol/1.0")

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)

var recvMetadata HostMetadata
Expand Down
4 changes: 1 addition & 3 deletions exporter/datadogexporter/internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"

"github.com/DataDog/datadog-agent/pkg/otlp/model/source"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand Down Expand Up @@ -136,7 +134,7 @@ func metricsEndpoint(w http.ResponseWriter, r *http.Request) {

func newMetadataEndpoint(c chan []byte) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
c <- body
}
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/dynatraceexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -225,7 +225,7 @@ func (e *exporter) sendBatch(ctx context.Context, lines []string) error {

if resp.StatusCode == http.StatusBadRequest {
// At least some metrics were not accepted
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
// if the response cannot be read, do not retry the batch as it may have been successful
e.settings.Logger.Error("Failed to read response from Dynatrace", zap.Error(err))
Expand Down
6 changes: 3 additions & 3 deletions exporter/dynatraceexporter/metrics_exporter_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"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -45,7 +45,7 @@ var testTimestamp = pcommon.Timestamp(time.Date(2021, 07, 16, 12, 30, 0, 0, time
func Test_exporter_PushMetricsData(t *testing.T) {
sent := "not sent"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bodyBytes, _ := ioutil.ReadAll(r.Body)
bodyBytes, _ := io.ReadAll(r.Body)
sent = string(bodyBytes)

response := metricsResponse{
Expand Down Expand Up @@ -302,7 +302,7 @@ func Test_SumMetrics(t *testing.T) {
// server setup:
sent := "nothing sent"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bodyBytes, _ := ioutil.ReadAll(r.Body)
bodyBytes, _ := io.ReadAll(r.Body)
sent = string(bodyBytes)

response := metricsResponse{
Expand Down
3 changes: 1 addition & 2 deletions exporter/elasticexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package elasticexporter
import (
"context"
"encoding/pem"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -144,7 +143,7 @@ func newRecorder(t *testing.T) (*transporttest.RecorderTransport, *Config) {
t.Cleanup(srv.Close)

// Write the server's self-signed certificate to a file to test the exporter's TLS config.
certfile, err := ioutil.TempFile("", "otel-elastic-cacert")
certfile, err := os.CreateTemp("", "otel-elastic-cacert")
require.NoError(t, err)
t.Cleanup(func() { os.Remove(certfile.Name()) })
err = pem.Encode(certfile, &pem.Block{
Expand Down
9 changes: 4 additions & 5 deletions exporter/fileexporter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package fileexporter
import (
"context"
"errors"
"io/ioutil"
"os"
"testing"

Expand All @@ -40,7 +39,7 @@ func TestFileTracesExporter(t *testing.T) {
assert.NoError(t, fe.Shutdown(context.Background()))

unmarshaler := ptrace.NewJSONUnmarshaler()
buf, err := ioutil.ReadFile(fe.path)
buf, err := os.ReadFile(fe.path)
assert.NoError(t, err)
got, err := unmarshaler.UnmarshalTraces(buf)
assert.NoError(t, err)
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestFileMetricsExporter(t *testing.T) {
assert.NoError(t, fe.Shutdown(context.Background()))

unmarshaler := pmetric.NewJSONUnmarshaler()
buf, err := ioutil.ReadFile(fe.path)
buf, err := os.ReadFile(fe.path)
assert.NoError(t, err)
got, err := unmarshaler.UnmarshalMetrics(buf)
assert.NoError(t, err)
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestFileLogsExporter(t *testing.T) {
assert.NoError(t, fe.Shutdown(context.Background()))

unmarshaler := plog.NewJSONUnmarshaler()
buf, err := ioutil.ReadFile(fe.path)
buf, err := os.ReadFile(fe.path)
assert.NoError(t, err)
got, err := unmarshaler.UnmarshalLogs(buf)
assert.NoError(t, err)
Expand All @@ -116,7 +115,7 @@ func TestFileLogsExporterErrors(t *testing.T) {

// tempFileName provides a temporary file name for testing.
func tempFileName(t *testing.T) string {
tmpfile, err := ioutil.TempFile("", "*.json")
tmpfile, err := os.CreateTemp("", "*.json")
require.NoError(t, err)
require.NoError(t, tmpfile.Close())
socket := tmpfile.Name()
Expand Down
4 changes: 2 additions & 2 deletions exporter/honeycombexporter/honeycomb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -49,7 +49,7 @@ func testingServer(callback func(data []honeycombData)) *httptest.Server {
return
}
defer req.Body.Close()
b, err := ioutil.ReadAll(uncompressed)
b, err := io.ReadAll(uncompressed)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
Expand Down
3 changes: 1 addition & 2 deletions exporter/humioexporter/humio_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -161,7 +160,7 @@ func (h *humioClient) sendEvents(ctx context.Context, evts interface{}, url stri
}
// Response body needs to both be read to EOF and closed to avoid leaks
defer res.Body.Close()
_, err = io.Copy(ioutil.Discard, res.Body)
_, err = io.Copy(io.Discard, res.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/humioexporter/humio_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -143,7 +143,7 @@ func executeRequest(fn func(s *httptest.Server) error) (result requestData) {
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
result.Path = r.URL.Path
result.Header = r.Header
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)

if err != nil {
result.Error = err
Expand Down
4 changes: 2 additions & 2 deletions exporter/influxdbexporter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"sort"
Expand Down Expand Up @@ -122,7 +122,7 @@ func (b *influxHTTPWriterBatch) flushAndClose(ctx context.Context) error {

if res, err := b.w.httpClient.Do(req); err != nil {
return err
} else if body, err := ioutil.ReadAll(res.Body); err != nil {
} else if body, err := io.ReadAll(res.Body); err != nil {
return err
} else if err = res.Body.Close(); err != nil {
return err
Expand Down
Loading

0 comments on commit 94e5fd9

Please sign in to comment.