From d59ea7d0cef7163e49e446823ad1c40f4338f1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=BBak?= Date: Fri, 3 Mar 2023 19:04:41 +0100 Subject: [PATCH] chore: Remove usages of github.com/pkg/errors dependency (archived) (#12787) Co-authored-by: Pawel Zak --- go.mod | 2 +- plugins/common/opcua/opcua_util.go | 4 +--- plugins/inputs/aliyuncms/aliyuncms.go | 18 +++++++-------- plugins/inputs/aliyuncms/aliyuncms_test.go | 11 +++++---- plugins/inputs/aliyuncms/discovery.go | 27 +++++++++++----------- plugins/inputs/ethtool/ethtool_linux.go | 7 ++---- plugins/inputs/ethtool/ethtool_test.go | 2 +- plugins/inputs/filecount/filecount.go | 5 ++-- plugins/outputs/sumologic/sumologic.go | 17 ++++++-------- 9 files changed, 43 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index f243f82c6d5e0..37b61c5f55bd8 100644 --- a/go.mod +++ b/go.mod @@ -138,7 +138,6 @@ require ( github.com/p4lang/p4runtime v1.3.0 github.com/pborman/ansi v1.0.0 github.com/pion/dtls/v2 v2.2.4 - github.com/pkg/errors v0.9.1 github.com/prometheus-community/pro-bing v0.1.0 github.com/prometheus/client_golang v1.14.0 github.com/prometheus/client_model v0.3.0 @@ -374,6 +373,7 @@ require ( github.com/pion/transport/v2 v2.0.0 // indirect github.com/pion/udp v0.1.4 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pkg/sftp v1.13.5 // indirect github.com/pkg/xattr v0.4.9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/plugins/common/opcua/opcua_util.go b/plugins/common/opcua/opcua_util.go index ec7c9e87744a4..cc1a8781fb386 100644 --- a/plugins/common/opcua/opcua_util.go +++ b/plugins/common/opcua/opcua_util.go @@ -19,7 +19,6 @@ import ( "github.com/gopcua/opcua" "github.com/gopcua/opcua/debug" "github.com/gopcua/opcua/ua" - "github.com/pkg/errors" ) // SELF SIGNED CERT FUNCTIONS @@ -345,6 +344,5 @@ func validateEndpointConfig(endpoints []*ua.EndpointDescription, secPolicy strin } } - err := errors.Errorf("server does not support an endpoint with security : %s , %s", secPolicy, secMode) - return err + return fmt.Errorf("server does not support an endpoint with security: %q, %q", secPolicy, secMode) } diff --git a/plugins/inputs/aliyuncms/aliyuncms.go b/plugins/inputs/aliyuncms/aliyuncms.go index a62a695611f4f..d4c3a1237ea3b 100644 --- a/plugins/inputs/aliyuncms/aliyuncms.go +++ b/plugins/inputs/aliyuncms/aliyuncms.go @@ -4,6 +4,7 @@ package aliyuncms import ( _ "embed" "encoding/json" + "errors" "fmt" "strconv" "strings" @@ -14,7 +15,6 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/providers" "github.com/aliyun/alibaba-cloud-sdk-go/services/cms" "github.com/jmespath/go-jmespath" - "github.com/pkg/errors" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" @@ -142,11 +142,11 @@ func (s *AliyunCMS) Init() error { } credential, err := providers.NewChainProvider(credentialProviders).Retrieve() if err != nil { - return errors.Errorf("failed to retrieve credential: %v", err) + return fmt.Errorf("failed to retrieve credential: %w", err) } s.client, err = cms.NewClientWithOptions("", sdk.NewConfig(), credential) if err != nil { - return errors.Errorf("failed to create cms client: %v", err) + return fmt.Errorf("failed to create cms client: %w", err) } //check metrics dimensions consistency @@ -162,7 +162,7 @@ func (s *AliyunCMS) Init() error { err := json.Unmarshal([]byte(metric.Dimensions), &metric.dimensionsUdArr) if err != nil { - return errors.Errorf("cannot parse dimensions (neither obj, nor array) %q :%v", metric.Dimensions, err) + return fmt.Errorf("cannot parse dimensions (neither obj, nor array) %q: %w", metric.Dimensions, err) } } } @@ -282,7 +282,7 @@ func (s *AliyunCMS) gatherMetric(acc telegraf.Accumulator, metricName string, me for more := true; more; { resp, err := s.client.DescribeMetricList(req) if err != nil { - return errors.Errorf("failed to query metricName list: %v", err) + return fmt.Errorf("failed to query metricName list: %w", err) } if resp.Code != "200" { s.Log.Errorf("failed to query metricName list: %v", resp.Message) @@ -291,7 +291,7 @@ func (s *AliyunCMS) gatherMetric(acc telegraf.Accumulator, metricName string, me var datapoints []map[string]interface{} if err := json.Unmarshal([]byte(resp.Datapoints), &datapoints); err != nil { - return errors.Errorf("failed to decode response datapoints: %v", err) + return fmt.Errorf("failed to decode response datapoints: %w", err) } if len(datapoints) == 0 { @@ -356,8 +356,7 @@ func parseTag(tagSpec string, data interface{}) (tagKey string, tagValue string, tagRawValue, err := jmespath.Search(queryPath, data) if err != nil { - return "", "", errors.Errorf("Can't query data from discovery data using query path %q: %v", - queryPath, err) + return "", "", fmt.Errorf("can't query data from discovery data using query path %q: %w", queryPath, err) } if tagRawValue == nil { //Nothing found @@ -366,8 +365,7 @@ func parseTag(tagSpec string, data interface{}) (tagKey string, tagValue string, tagValue, ok = tagRawValue.(string) if !ok { - return "", "", errors.Errorf("Tag value %v parsed by query %q is not a string value", - tagRawValue, queryPath) + return "", "", fmt.Errorf("tag value %q parsed by query %q is not a string value", tagRawValue, queryPath) } return tagKey, tagValue, nil diff --git a/plugins/inputs/aliyuncms/aliyuncms_test.go b/plugins/inputs/aliyuncms/aliyuncms_test.go index 8b48ef0408681..8812dc4361e34 100644 --- a/plugins/inputs/aliyuncms/aliyuncms_test.go +++ b/plugins/inputs/aliyuncms/aliyuncms_test.go @@ -2,6 +2,8 @@ package aliyuncms import ( "bytes" + "errors" + "fmt" "io" "net/http" "testing" @@ -12,7 +14,6 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses" "github.com/aliyun/alibaba-cloud-sdk-go/services/cms" - "github.com/pkg/errors" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf" @@ -93,13 +94,13 @@ func getDiscoveryTool(project string, discoverRegions []string) (*discoveryTool, } credential, err = providers.NewChainProvider(credentialProviders).Retrieve() if err != nil { - return nil, errors.Errorf("failed to retrieve credential: %v", err) + return nil, fmt.Errorf("failed to retrieve credential: %w", err) } dt, err := newDiscoveryTool(discoverRegions, project, testutil.Logger{Name: inputTitle}, credential, 1, time.Minute*2) if err != nil { - return nil, errors.Errorf("Can't create discovery tool object: %v", err) + return nil, fmt.Errorf("can't create discovery tool object: %w", err) } return dt, nil } @@ -107,7 +108,7 @@ func getDiscoveryTool(project string, discoverRegions []string) (*discoveryTool, func getMockSdkCli(httpResp *http.Response) (mockAliyunSDKCli, error) { resp := responses.NewCommonResponse() if err := responses.Unmarshal(resp, httpResp, "JSON"); err != nil { - return mockAliyunSDKCli{}, errors.Errorf("Can't parse response: %v", err) + return mockAliyunSDKCli{}, fmt.Errorf("can't parse response: %w", err) } return mockAliyunSDKCli{resp: resp}, nil } @@ -273,7 +274,7 @@ func TestPluginMetricsInitialize(t *testing.T) { regions: []string{"cn-shanghai"}, accessKeyID: "dummy", accessKeySecret: "dummy", - expectedErrorString: `cannot parse dimensions (neither obj, nor array) "[" :unexpected end of JSON input`, + expectedErrorString: `cannot parse dimensions (neither obj, nor array) "[": unexpected end of JSON input`, metrics: []*Metric{ { MetricNames: []string{}, diff --git a/plugins/inputs/aliyuncms/discovery.go b/plugins/inputs/aliyuncms/discovery.go index 2815c57a3c85d..358cef09b8762 100644 --- a/plugins/inputs/aliyuncms/discovery.go +++ b/plugins/inputs/aliyuncms/discovery.go @@ -2,6 +2,8 @@ package aliyuncms import ( "encoding/json" + "errors" + "fmt" "reflect" "strconv" "strings" @@ -16,7 +18,6 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/services/rds" "github.com/aliyun/alibaba-cloud-sdk-go/services/slb" "github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" - "github.com/pkg/errors" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal/limiter" @@ -58,7 +59,7 @@ type parsedDResp struct { func getRPCReqFromDiscoveryRequest(req discoveryRequest) (*requests.RpcRequest, error) { if reflect.ValueOf(req).Type().Kind() != reflect.Ptr || reflect.ValueOf(req).IsNil() { - return nil, errors.Errorf("unexpected type of the discovery request object: %q, %q", reflect.ValueOf(req).Type(), reflect.ValueOf(req).Kind()) + return nil, fmt.Errorf("unexpected type of the discovery request object: %q, %q", reflect.ValueOf(req).Type(), reflect.ValueOf(req).Kind()) } ptrV := reflect.Indirect(reflect.ValueOf(req)) @@ -66,19 +67,19 @@ func getRPCReqFromDiscoveryRequest(req discoveryRequest) (*requests.RpcRequest, for i := 0; i < ptrV.NumField(); i++ { if ptrV.Field(i).Type().String() == "*requests.RpcRequest" { if !ptrV.Field(i).CanInterface() { - return nil, errors.Errorf("can't get interface of %v", ptrV.Field(i)) + return nil, fmt.Errorf("can't get interface of %q", ptrV.Field(i)) } rpcReq, ok := ptrV.Field(i).Interface().(*requests.RpcRequest) if !ok { - return nil, errors.Errorf("can't convert interface of %v to '*requests.RpcRequest' type", ptrV.Field(i).Interface()) + return nil, fmt.Errorf("can't convert interface of %q to '*requests.RpcRequest' type", ptrV.Field(i).Interface()) } return rpcReq, nil } } - return nil, errors.Errorf("didn't find *requests.RpcRequest embedded struct in %q", ptrV.Type()) + return nil, fmt.Errorf("didn't find *requests.RpcRequest embedded struct in %q", ptrV.Type()) } // newDiscoveryTool function returns discovery tool object. @@ -101,7 +102,7 @@ func newDiscoveryTool( responseRootKey string responseObjectIDKey string err error - noDiscoverySupportErr = errors.Errorf("no discovery support for project %q", project) + noDiscoverySupportErr = fmt.Errorf("no discovery support for project %q", project) ) if len(regions) == 0 { @@ -232,7 +233,7 @@ func newDiscoveryTool( case "acs_cds": return nil, noDiscoverySupportErr default: - return nil, errors.Errorf("project %q is not recognized by discovery", project) + return nil, fmt.Errorf("project %q is not recognized by discovery", project) } cli[region], err = sdk.NewClientWithOptions(region, sdk.NewConfig(), credential) @@ -242,7 +243,7 @@ func newDiscoveryTool( } if len(dscReq) == 0 || len(cli) == 0 { - return nil, errors.Errorf("can't build discovery request for project: %q, regions: %v", project, regions) + return nil, fmt.Errorf("can't build discovery request for project: %q, regions: %v", project, regions) } return &discoveryTool{ @@ -273,7 +274,7 @@ func (dt *discoveryTool) parseDiscoveryResponse(resp *responses.CommonResponse) } if err := json.Unmarshal(data, &fullOutput); err != nil { - return nil, errors.Errorf("can't parse JSON from discovery response: %v", err) + return nil, fmt.Errorf("can't parse JSON from discovery response: %w", err) } for key, val := range fullOutput { @@ -282,7 +283,7 @@ func (dt *discoveryTool) parseDiscoveryResponse(resp *responses.CommonResponse) foundRootKey = true rootKeyVal, ok := val.(map[string]interface{}) if !ok { - return nil, errors.Errorf("content of root key %q, is not an object: %v", key, val) + return nil, fmt.Errorf("content of root key %q, is not an object: %q", key, val) } //It should contain the array with discovered data @@ -292,7 +293,7 @@ func (dt *discoveryTool) parseDiscoveryResponse(resp *responses.CommonResponse) } } if !foundDataItem { - return nil, errors.Errorf("didn't find array item in root key %q", key) + return nil, fmt.Errorf("didn't find array item in root key %q", key) } case "TotalCount", "TotalRecordCount": pdResp.totalCount = int(val.(float64)) @@ -303,7 +304,7 @@ func (dt *discoveryTool) parseDiscoveryResponse(resp *responses.CommonResponse) } } if !foundRootKey { - return nil, errors.Errorf("didn't find root key %q in discovery response", dt.respRootKey) + return nil, fmt.Errorf("didn't find root key %q in discovery response", dt.respRootKey) } return pdResp, nil @@ -371,7 +372,7 @@ func (dt *discoveryTool) getDiscoveryDataAcrossRegions(lmtr chan bool) (map[stri //which aliyun object type (project) is used dscReq, ok := dt.req[region] if !ok { - return nil, errors.Errorf("error building common discovery request: not valid region %q", region) + return nil, fmt.Errorf("error building common discovery request: not valid region %q", region) } rpcReq, err := getRPCReqFromDiscoveryRequest(dscReq) diff --git a/plugins/inputs/ethtool/ethtool_linux.go b/plugins/inputs/ethtool/ethtool_linux.go index 06906ff1fcb9c..f46022abb9331 100644 --- a/plugins/inputs/ethtool/ethtool_linux.go +++ b/plugins/inputs/ethtool/ethtool_linux.go @@ -11,7 +11,6 @@ import ( "strings" "sync" - "github.com/pkg/errors" "github.com/vishvananda/netns" "github.com/influxdata/telegraf" @@ -115,8 +114,7 @@ func (e *Ethtool) gatherEthtoolStats(iface NamespacedInterface, acc telegraf.Acc driverName, err := e.command.DriverName(iface) if err != nil { - driverErr := errors.Wrapf(err, "%s driver", iface.Name) - acc.AddError(driverErr) + acc.AddError(fmt.Errorf("%q driver: %w", iface.Name, err)) return } @@ -125,8 +123,7 @@ func (e *Ethtool) gatherEthtoolStats(iface NamespacedInterface, acc telegraf.Acc fields := make(map[string]interface{}) stats, err := e.command.Stats(iface) if err != nil { - statsErr := errors.Wrapf(err, "%s stats", iface.Name) - acc.AddError(statsErr) + acc.AddError(fmt.Errorf("%q stats: %w", iface.Name, err)) return } diff --git a/plugins/inputs/ethtool/ethtool_test.go b/plugins/inputs/ethtool/ethtool_test.go index e9d627759d5f1..8ecc852e8b758 100644 --- a/plugins/inputs/ethtool/ethtool_test.go +++ b/plugins/inputs/ethtool/ethtool_test.go @@ -3,10 +3,10 @@ package ethtool import ( + "errors" "net" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf/testutil" diff --git a/plugins/inputs/filecount/filecount.go b/plugins/inputs/filecount/filecount.go index c4a2c8a0836af..de19a487c8f05 100644 --- a/plugins/inputs/filecount/filecount.go +++ b/plugins/inputs/filecount/filecount.go @@ -3,12 +3,13 @@ package filecount import ( _ "embed" + "errors" + "io/fs" "os" "path/filepath" "time" "github.com/karrick/godirwalk" - "github.com/pkg/errors" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" @@ -177,7 +178,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa Unsorted: true, FollowSymbolicLinks: fc.FollowSymlinks, ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction { - if os.IsPermission(errors.Cause(err)) { + if errors.Is(err, fs.ErrPermission) { fc.Log.Debug(err) return godirwalk.SkipNode } diff --git a/plugins/outputs/sumologic/sumologic.go b/plugins/outputs/sumologic/sumologic.go index b6dd518e835b7..224f38910a458 100644 --- a/plugins/outputs/sumologic/sumologic.go +++ b/plugins/outputs/sumologic/sumologic.go @@ -5,11 +5,11 @@ import ( "bytes" "compress/gzip" _ "embed" + "errors" + "fmt" "net/http" "time" - "github.com/pkg/errors" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/internal" @@ -88,7 +88,7 @@ func (s *SumoLogic) SetSerializer(serializer serializers.Serializer) { s.headers[contentTypeHeader] = prometheusContentType default: - s.err = errors.Errorf("unsupported serializer %T", serializer) + s.err = fmt.Errorf("unsupported serializer %T", serializer) } s.serializer = serializer @@ -105,7 +105,7 @@ func (s *SumoLogic) createClient() *http.Client { func (s *SumoLogic) Connect() error { if s.err != nil { - return errors.Wrap(s.err, "sumologic: incorrect configuration") + return fmt.Errorf("sumologic: incorrect configuration: %w", s.err) } if s.Timeout == 0 { @@ -123,7 +123,7 @@ func (s *SumoLogic) Close() error { func (s *SumoLogic) Write(metrics []telegraf.Metric) error { if s.err != nil { - return errors.Wrap(s.err, "sumologic: incorrect configuration") + return fmt.Errorf("sumologic: incorrect configuration: %w", s.err) } if s.serializer == nil { return errors.New("sumologic: serializer unset") @@ -193,15 +193,12 @@ func (s *SumoLogic) writeRequestChunk(reqBody []byte) error { resp, err := s.client.Do(req) if err != nil { - return errors.Wrapf(err, "sumologic: failed sending request to [%s]", s.URL) + return fmt.Errorf("sumologic: failed sending request to %q: %w", s.URL, err) } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return errors.Errorf( - "sumologic: when writing to [%s] received status code: %d", - s.URL, resp.StatusCode, - ) + return fmt.Errorf("sumologic: when writing to %q received status code: %d", s.URL, resp.StatusCode) } return nil