Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace go-multierror with native std go errors #13368

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,002 changes: 1,001 additions & 1,001 deletions NOTICE.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/google/go-cmp v0.6.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v1.0.2
github.com/jaegertracing/jaeger v1.57.0
github.com/libp2p/go-reuseport v0.4.0
Expand Down Expand Up @@ -106,6 +105,7 @@ require (
github.com/gomodule/redigo v1.8.9 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
17 changes: 7 additions & 10 deletions internal/beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package beater
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -28,8 +29,6 @@ import (
"time"

"github.com/dustin/go-humanize"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"go.elastic.co/apm/module/apmgrpc/v2"
"go.elastic.co/apm/module/apmotel/v2"
"go.elastic.co/apm/v2"
Expand Down Expand Up @@ -328,7 +327,7 @@ func (s *Runner) Run(ctx context.Context) error {
if err := s.waitReady(ctx, tracer); err != nil {
// One or more preconditions failed; drop events.
close(drain)
return errors.Wrap(err, "error waiting for server to be ready")
return fmt.Errorf("error waiting for server to be ready: %w", err)
}
// All preconditions have been met; start indexing documents
// into elasticsearch.
Expand Down Expand Up @@ -534,10 +533,8 @@ func (s *Runner) Run(ctx context.Context) error {
}

result := g.Wait()
if err := closeFinalBatchProcessor(backgroundContext); err != nil {
result = multierror.Append(result, err)
}
return result
closeErr := closeFinalBatchProcessor(backgroundContext)
return errors.Join(result, closeErr)
}

func maxConcurrentDecoders(memLimitGB float64) uint {
Expand Down Expand Up @@ -595,10 +592,10 @@ func (s *Runner) waitReady(
preconditions = append(preconditions, func(ctx context.Context) error {
license, err := getElasticsearchLicense(ctx, esOutputClient)
if err != nil {
return errors.Wrap(err, "error getting Elasticsearch licensing information")
return fmt.Errorf("error getting Elasticsearch licensing information: %w", err)
}
if licenser.IsExpired(license) {
return errors.New("Elasticsearch license is expired")
return errors.New("the Elasticsearch license is expired")
}
if license.Type == licenser.Trial || license.Cover(requiredLicenseLevel) {
return nil
Expand Down Expand Up @@ -676,7 +673,7 @@ func (s *Runner) newFinalBatchProcessor(
if esConfig.FlushBytes != "" {
b, err := humanize.ParseBytes(esConfig.FlushBytes)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse flush_bytes")
return nil, nil, fmt.Errorf("failed to parse flush_bytes: %w", err)
}
flushBytes = int(b)
}
Expand Down
23 changes: 9 additions & 14 deletions x-pack/apm-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"sync"

"github.com/dgraph-io/badger/v2"
"github.com/gofrs/uuid"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"

"github.com/elastic/beats/v7/libbeat/common/reload"
Expand Down Expand Up @@ -101,7 +101,7 @@ func newProcessors(args beater.ServerParams) ([]namedProcessor, error) {
const name = "tail sampler"
sampler, err := newTailSamplingProcessor(args)
if err != nil {
return nil, errors.Wrapf(err, "error creating %s", name)
return nil, fmt.Errorf("error creating %s: %w", name, err)
}
samplingMonitoringRegistry.Remove("tail")
monitoring.NewFunc(samplingMonitoringRegistry, "tail", sampler.CollectMonitoring, monitoring.Report)
Expand All @@ -114,13 +114,13 @@ func newTailSamplingProcessor(args beater.ServerParams) (*sampling.Processor, er
tailSamplingConfig := args.Config.Sampling.Tail
es, err := args.NewElasticsearchClient(tailSamplingConfig.ESConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to create Elasticsearch client for tail-sampling")
return nil, fmt.Errorf("failed to create Elasticsearch client for tail-sampling: %w", err)
}

storageDir := paths.Resolve(paths.Data, tailSamplingStorageDir)
badgerDB, err = getBadgerDB(storageDir)
if err != nil {
return nil, errors.Wrap(err, "failed to get Badger database")
return nil, fmt.Errorf("failed to get Badger database: %w", err)
}
readWriters := getStorage(badgerDB)

Expand Down Expand Up @@ -267,14 +267,11 @@ func closeStorage() {
}
}

func cleanup() (result error) {
func cleanup() error {
// Close the underlying storage, the storage will be flushed on processor stop.
closeStorage()

if err := closeBadger(); err != nil {
result = multierror.Append(result, err)
}
return result
return closeBadger()
}

func Main() error {
Expand All @@ -288,10 +285,8 @@ func Main() error {
},
)
result := rootCmd.Execute()
if err := cleanup(); err != nil {
result = multierror.Append(result, err)
}
return result
cleanupErr := cleanup()
return errors.Join(result, cleanupErr)
}

func main() {
Expand Down
8 changes: 4 additions & 4 deletions x-pack/apm-server/sampling/eventstorage/sharded.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package eventstorage

import (
"errors"
"runtime"
"sync"

"github.com/cespare/xxhash/v2"
"github.com/hashicorp/go-multierror"

"github.com/elastic/apm-data/model/modelpb"
)
Expand Down Expand Up @@ -43,13 +43,13 @@ func (s *ShardedReadWriter) Close() {

// Flush flushes all sharded storage readWriters.
func (s *ShardedReadWriter) Flush() error {
var result error
var errs []error
for i := range s.readWriters {
if err := s.readWriters[i].Flush(); err != nil {
result = multierror.Append(result, err)
errs = append(errs, err)
}
}
return result
return errors.Join(errs...)
}

// ReadTraceEvents calls Writer.ReadTraceEvents, using a sharded, locked, Writer.
Expand Down
11 changes: 4 additions & 7 deletions x-pack/apm-server/sampling/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"go.elastic.co/fastjson"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -52,7 +51,7 @@ type Pubsub struct {
// events older than a configured age.
func New(config Config) (*Pubsub, error) {
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid pubsub config")
return nil, fmt.Errorf("invalid pubsub config: %w", err)
}
if config.Logger == nil {
config.Logger = logp.NewLogger(logs.Sampling)
Expand All @@ -79,10 +78,8 @@ func (p *Pubsub) PublishSampledTraceIDs(ctx context.Context, traceIDs <-chan str
result := p.indexSampledTraceIDs(ctx, traceIDs, appender)
ctx, cancel := context.WithTimeout(context.Background(), p.config.FlushInterval)
defer cancel()
if err := appender.Close(ctx); err != nil {
result = multierror.Append(result, err)
}
return result
closeErr := appender.Close(ctx)
return errors.Join(result, closeErr)
}

func (p *Pubsub) indexSampledTraceIDs(ctx context.Context, traceIDs <-chan string, appender *docappender.Appender) error {
Expand Down