Skip to content

Commit

Permalink
chore: Remove usages of github.com/pkg/errors dependency (archived) (i…
Browse files Browse the repository at this point in the history
…nfluxdata#12787)

Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel committed Mar 3, 2023
1 parent dfe8b43 commit d59ea7d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions plugins/common/opcua/opcua_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
18 changes: 8 additions & 10 deletions plugins/inputs/aliyuncms/aliyuncms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package aliyuncms
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions plugins/inputs/aliyuncms/aliyuncms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aliyuncms

import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"testing"
Expand All @@ -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"
Expand Down Expand Up @@ -93,21 +94,21 @@ 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
}

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
}
Expand Down Expand Up @@ -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{},
Expand Down
27 changes: 14 additions & 13 deletions plugins/inputs/aliyuncms/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aliyuncms

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -58,27 +59,27 @@ 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))

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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions plugins/inputs/ethtool/ethtool_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"sync"

"github.com/pkg/errors"
"github.com/vishvananda/netns"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/ethtool/ethtool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package ethtool

import (
"errors"
"net"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/testutil"
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/filecount/filecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit d59ea7d

Please sign in to comment.