Skip to content

Commit

Permalink
[NH-80033] Remove github.com/pkg/errors in favor of stdlib (#119)
Browse files Browse the repository at this point in the history
* Use stdlib errors in config.go

* Use stdlib errors in uams/file.go

* Use stdlib errors in config/env.go

* Use stdlib errors in entryspans.go

* Use stdlib errors in azure.go

* Use stdlib errors in swo/agent.go

* Use stdlib errors in methods_test.go

* Use stdlib errors in log_writer.go

* Use stdlib errors in xtrace.go

* Use stdlib errors in reporter.go

* Use stdlib errors in oboe/url.go

* Use stdlib errors in hdrhist/log_writer.go

* Use stdlib errors in reporter_grpc_test.go

* Use stdlib errors in logging_test.go

* Use stdlib errors in metrics.go

* Use stdlib errors in hdrhist/encode.go

* go mod tidy
  • Loading branch information
swi-jared authored Jul 30, 2024
1 parent d39f09d commit d7e10e5
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 56 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/aws/smithy-go v1.20.3
github.com/coocood/freecache v1.2.4
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
github.com/solarwinds/apm-proto v1.0.5
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down
24 changes: 12 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package config

import (
"errors"
"fmt"
"net"
"os"
Expand All @@ -36,7 +37,6 @@ import (

"github.com/solarwinds/apm-go/internal/log"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -200,7 +200,7 @@ func (f *TransactionFilter) UnmarshalYAML(unmarshal func(interface{}) error) err
}{}

if err := unmarshal(&aux); err != nil {
return errors.Wrap(err, "failed to unmarshal TransactionFilter")
return fmt.Errorf("failed to unmarshal TransactionFilter: %w", err)
}
if aux.Type != URL {
return ErrTFInvalidType
Expand Down Expand Up @@ -235,7 +235,7 @@ func (s *SamplingConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
SampleRate: -1,
}
if err := unmarshal(&aux); err != nil {
return errors.Wrap(err, "failed to unmarshal SamplingConfig")
return fmt.Errorf("failed to unmarshal SamplingConfig: %w", err)
}

if aux.TracingMode != "Invalid" {
Expand Down Expand Up @@ -373,7 +373,7 @@ func (c *Config) validate() error {
if c.ServiceKey != "" {
c.ServiceKey = ToServiceKey(c.ServiceKey)
if ok := IsValidServiceKey(c.ServiceKey); !ok {
return errors.Wrap(ErrInvalidServiceKey, fmt.Sprintf("service key: \"%s\"", c.ServiceKey))
return fmt.Errorf("service key: \"%s\": %w", c.ServiceKey, ErrInvalidServiceKey)
}
}
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func (c *Config) Load(opts ...Option) *Config {
c.reset()

if err := c.loadConfigFile(); err != nil {
log.Warning(errors.Wrap(err, "config file load error").Error())
log.Warning(fmt.Errorf("config file load error: %w", err).Error())
return c.resetThenDisable()
}
c.loadEnvs()
Expand All @@ -433,7 +433,7 @@ func (c *Config) Load(opts ...Option) *Config {
}

if err := c.validate(); err != nil {
log.Warning(errors.Wrap(err, "validation error").Error())
log.Warning(fmt.Errorf("validation error: %w", err).Error())
return c.resetThenDisable()
}

Expand Down Expand Up @@ -692,7 +692,7 @@ func (c *Config) getConfigPath() string {
func (c *Config) loadYaml(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return errors.Wrap(err, "loadYaml")
return fmt.Errorf("loadYaml: %w", err)
}

// A pointer field may be assigned with nil in unmarshal, so just keep the
Expand All @@ -703,7 +703,7 @@ func (c *Config) loadYaml(path string) error {
// The config struct is modified in place so we won't tolerate any error
err = yaml.Unmarshal(data, &c)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("loadYaml: %s", path))
return fmt.Errorf("loadYaml: %s: %w", path, err)
}

if c.Sampling == nil {
Expand All @@ -719,11 +719,11 @@ func (c *Config) loadYaml(path string) error {
func (c *Config) checkFileSize(path string) error {
file, err := os.Stat(path)
if err != nil {
return errors.Wrap(err, "checkFileSize")
return fmt.Errorf("checkFileSize: %w", err)
}
size := file.Size()
if size > maxConfigFileSize {
return errors.Wrap(ErrFileTooLarge, fmt.Sprintf("File size: %d", size))
return fmt.Errorf("file size: %d: %w", size, ErrFileTooLarge)
}
return nil
}
Expand All @@ -736,15 +736,15 @@ func (c *Config) loadConfigFile() error {
}

if err := c.checkFileSize(path); err != nil {
return errors.Wrap(err, "loadConfigFile")
return fmt.Errorf("loadConfigFile: %w", err)
}
ext := filepath.Ext(path)

switch ext {
case ".yml", ".yaml":
return c.loadYaml(path)
default:
return errors.Wrap(ErrUnsupportedFormat, path)
return fmt.Errorf("%s: %w", path, ErrUnsupportedFormat)
}
}

Expand Down
4 changes: 1 addition & 3 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"reflect"
"strconv"
"strings"

"github.com/pkg/errors"
)

func toBool(s string) (bool, error) {
Expand Down Expand Up @@ -116,7 +114,7 @@ func stringToValue(s string, typ reflect.Type) (reflect.Value, error) {
}
val, err = toBool(s)
if err != nil {
log.Warningf("Ignore invalid bool value: %s", errors.Wrap(err, s))
log.Warningf("Ignore invalid bool value: %s", fmt.Errorf("%w: %s", err, s))
}
case reflect.Slice:
if s == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/entryspans/entryspans.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package entryspans

import (
"errors"
"fmt"
"github.com/pkg/errors"
"github.com/solarwinds/apm-go/internal/config"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
Expand Down
19 changes: 11 additions & 8 deletions internal/hdrhist/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ import (
"compress/zlib"
"encoding/base64"
"encoding/binary"
"fmt"
"io"

"github.com/pkg/errors"
)

func EncodeCompressed(h *Hist) ([]byte, error) {
var buf bytes.Buffer
b64w := base64.NewEncoder(base64.StdEncoding, &buf)
if err := encodeCompressed(h, b64w, h.Max()); err != nil {
_ = b64w.Close()
return nil, errors.Wrap(err, "unable to encode histogram")
return nil, fmt.Errorf("unable to encode histogram: %w", err)
}
// DO NOT defer this close, otherwise that could prevent bytes from getting flushed
if err := b64w.Close(); err != nil {
Expand Down Expand Up @@ -58,8 +57,10 @@ func encodeCompressed(h *Hist, w io.Writer, histMax int64) error {
}
binary.BigEndian.PutUint32(buf.Bytes()[4:], uint32(buf.Len()-preCompressed))

_, err = buf.WriteTo(w)
return errors.Wrap(err, "unable to write compressed hist")
if _, err = buf.WriteTo(w); err != nil {
return fmt.Errorf("unable to write compressed hist: %w", err)
}
return nil
}

func encodeInto(h *Hist, w io.Writer, max int64) error {
Expand Down Expand Up @@ -88,8 +89,10 @@ func encodeInto(h *Hist, w io.Writer, max int64) error {
payloadStart := buf.Len()
fillBuffer(&buf, h, importantLen)
binary.BigEndian.PutUint32(buf.Bytes()[4:], uint32(buf.Len()-payloadStart))
_, err := buf.WriteTo(w)
return errors.Wrap(err, "unable to write uncompressed hist")
if _, err := buf.WriteTo(w); err != nil {
return fmt.Errorf("unable to write uncompressed hist: %w", err)
}
return nil
}

func fillBuffer(buf *bytes.Buffer, h *Hist, n int) {
Expand All @@ -101,7 +104,7 @@ func fillBuffer(buf *bytes.Buffer, h *Hist, n int) {
c := h.b.counts[srci]
srci++
if c < 0 {
panic(errors.Errorf(
panic(fmt.Errorf(
"can't encode hist with negative counts (count: %d, idx: %d, value range: [%d, %d])",
c,
srci,
Expand Down
10 changes: 4 additions & 6 deletions internal/hdrhist/log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"io"
"time"

"github.com/pkg/errors"
)

type LogWriter struct {
Expand All @@ -38,19 +36,19 @@ func (l *LogWriter) WriteStartTime(start time.Time) error {

_, err := fmt.Fprintf(l.w, "#[StartTime: %.3f (seconds since epoch), %s]\n",
float64(sec)+millis, start.Format(JavaDate))
return errors.Wrap(err, "unable to write start time")
return fmt.Errorf("unable to write start time: %w", err)
}

func (l *LogWriter) WriteBaseTime(base time.Time) error {
sec := base.Unix()
millis := float64(base.Nanosecond()) / 1e6 // Java version only stores ms
_, err := fmt.Fprintf(l.w, "#[BaseTime: %.3f (seconds since epoch)]\n", float64(sec)+millis)
return errors.Wrap(err, "unable to write base time")
return fmt.Errorf("unable to write base time: %w", err)
}

func (l *LogWriter) WriteComment(text string) error {
_, err := l.w.Write([]byte("#" + text + "\n"))
return errors.Wrapf(err, "unable to write comment")
return fmt.Errorf("unable to write comment: %w", err)
}

var logWriterLegend = []byte("\"StartTimestamp\",\"Interval_Length\",\"Interval_Max\",\"Interval_Compressed_Histogram\"\n")
Expand Down Expand Up @@ -100,5 +98,5 @@ func (l *LogWriter) writeHist(h *Hist, start time.Time, end time.Time) error {
}
l.buf.WriteString("\n")
_, err := l.buf.WriteTo(l.w)
return errors.Wrap(err, "unable to write hist")
return fmt.Errorf("unable to write hist: %w", err)
}
3 changes: 1 addition & 2 deletions internal/host/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/solarwinds/apm-go/internal/log"
collector "github.com/solarwinds/apm-proto/go/collectorpb"
"io"
Expand Down Expand Up @@ -114,7 +113,7 @@ func queryAzureIMDS(url_ string) (*MetadataCompute, error) {

m := &MetadataCompute{}
if err = json.Unmarshal(b, m); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal json")
return nil, fmt.Errorf("failed to unmarshal json: %w", err)
}
return m, err
}
2 changes: 1 addition & 1 deletion internal/log/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package log

import (
"bytes"
"errors"
"github.com/solarwinds/apm-go/internal/utils"
"io"
"math/rand"
Expand All @@ -25,7 +26,6 @@ import (

"sync"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 1 addition & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package metrics

import (
"errors"
"github.com/solarwinds/apm-go/internal/bson"
"github.com/solarwinds/apm-go/internal/config"
"github.com/solarwinds/apm-go/internal/hdrhist"
Expand All @@ -28,8 +29,6 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/pkg/errors"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions internal/oboe/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package oboe

import (
"fmt"
"github.com/solarwinds/apm-go/internal/config"
"github.com/solarwinds/apm-go/internal/log"
"path/filepath"
"regexp"
"strings"

"github.com/coocood/freecache"
"github.com/pkg/errors"
)

var urls *urlFilters
Expand Down Expand Up @@ -78,7 +78,7 @@ type regexFilter struct {
func newRegexFilter(regex string, mode TracingMode) (*regexFilter, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, errors.Wrap(err, "failed to parse regexp")
return nil, fmt.Errorf("failed to parse regexp: %w", err)
}
return &regexFilter{regex: re, trace: mode}, nil
}
Expand Down
7 changes: 3 additions & 4 deletions internal/reporter/log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package reporter
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/solarwinds/apm-go/internal/log"
"io"
"os"
"sync"

"github.com/pkg/errors"
)

// FlushWriter offers an interface to write a byte slice to a specific destination.
Expand Down Expand Up @@ -119,7 +118,7 @@ func (lr *logWriter) flush() error {

data, err := json.Marshal(lr.msg)
if err != nil {
return errors.Wrap(err, "error marshaling message")
return fmt.Errorf("error marshaling message: %w", err)
}
lr.msg.Data.Events = []string{}
lr.msg.Data.Metrics = []string{}
Expand All @@ -128,7 +127,7 @@ func (lr *logWriter) flush() error {
data = append(data, "\n"...)

if _, err := lr.dest.Write(data); err != nil {
return errors.Wrap(err, "write to log reporter failed")
return fmt.Errorf("write to log reporter failed: %w", err)
}

if file, ok := lr.dest.(*os.File); ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package reporter

import (
"context"
"errors"
"github.com/solarwinds/apm-go/internal/host"
"github.com/solarwinds/apm-go/internal/reporter/mocks"
"testing"

"github.com/pkg/errors"
collector "github.com/solarwinds/apm-proto/go/collectorpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down
3 changes: 1 addition & 2 deletions internal/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/solarwinds/apm-go/internal/config"
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/metrics"
Expand Down Expand Up @@ -114,7 +113,7 @@ func CreateInitMessage(tid trace.TraceID, r *resource.Resource) Event {

func sendInitMessage(r Reporter, rsrc *resource.Resource) {
if r.Closed() {
log.Info(errors.Wrap(ErrReporterIsClosed, "send init message"))
log.Info(fmt.Errorf("send init message: %w", ErrReporterIsClosed))
return
}
tid := trace.TraceID{0}
Expand Down
Loading

0 comments on commit d7e10e5

Please sign in to comment.