Skip to content

Commit

Permalink
refactor: replace urso/sderr with stdlib errors (#39839)
Browse files Browse the repository at this point in the history
* refactor: replace urso/sderr with stdlib errors

Go 1.20 added multiple errors wrapping so we can migrate to stdlib errors and drop
the additional dependency on github.com/urso/sderr

* refactor: avoid wrapping and unwrapping

* Update copytruncate_prospector.go

* Update statestore.go
  • Loading branch information
kruskall committed Jun 10, 2024
1 parent 1fd65c7 commit 3c9f4d9
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 243 deletions.
422 changes: 211 additions & 211 deletions NOTICE.txt

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions filebeat/input/filestream/copytruncate_prospector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
package filestream

import (
"errors"
"os"
"regexp"
"sort"
"strconv"
"time"

"github.com/urso/sderr"

loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile"
input "github.com/elastic/beats/v7/filebeat/input/v2"
"github.com/elastic/beats/v7/libbeat/common/file"
Expand Down Expand Up @@ -230,7 +229,7 @@ func (p *copyTruncateFileProspector) Run(ctx input.Context, s loginp.StateMetada

errs := tg.Wait()
if len(errs) > 0 {
log.Error("%s", sderr.WrapAll(errs, "running prospector failed"))
log.Errorf("running prospector failed: %v", errors.Join(errs...))
}
}

Expand Down
4 changes: 1 addition & 3 deletions filebeat/input/filestream/internal/input-logfile/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"sync"
"time"

"github.com/urso/sderr"

"github.com/elastic/go-concert/unison"

v2 "github.com/elastic/beats/v7/filebeat/input/v2"
Expand Down Expand Up @@ -141,7 +139,7 @@ func (cim *InputManager) Init(group unison.Group) error {
if err != nil {
store.Release()
cim.shutdown()
return sderr.Wrap(err, "Can not start registry cleanup process")
return fmt.Errorf("Can not start registry cleanup process: %w", err)
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions filebeat/input/filestream/prospector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package filestream

import (
"errors"
"fmt"
"time"

"github.com/urso/sderr"

loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile"
input "github.com/elastic/beats/v7/filebeat/input/v2"
"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -160,7 +159,7 @@ func (p *fileProspector) Run(ctx input.Context, s loginp.StateMetadataUpdater, h

errs := tg.Wait()
if len(errs) > 0 {
log.Error("%s", sderr.WrapAll(errs, "running prospector failed"))
log.Errorf("running prospector failed: %v", errors.Join(errs...))
}
}

Expand Down
4 changes: 2 additions & 2 deletions filebeat/input/journald/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
package journald

import (
"fmt"
"time"

"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/urso/sderr"

"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalfield"
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalread"
Expand Down Expand Up @@ -181,7 +181,7 @@ func (inp *journald) open(log *logp.Logger, canceler input.Canceler, src cursor.
withTransports(inp.Transports),
withSyslogIdentifiers(inp.Identifiers))
if err != nil {
return nil, sderr.Wrap(err, "failed to create reader for %{path} journal", src.Name())
return nil, fmt.Errorf("failed to create reader for %s journal: %w", src.Name(), err)
}

return reader, nil
Expand Down
9 changes: 4 additions & 5 deletions filebeat/input/journald/pkg/journalread/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/urso/sderr"

"github.com/elastic/beats/v7/libbeat/common/backoff"
"github.com/elastic/beats/v7/libbeat/common/cleanup"
Expand Down Expand Up @@ -96,27 +95,27 @@ func openJournal(path string) (*sdjournal.Journal, error) {
if path == localSystemJournalID || path == "" {
j, err := sdjournal.NewJournal()
if err != nil {
err = sderr.Wrap(err, "failed to open local journal")
err = fmt.Errorf("failed to open local journal: %w", err)
}
return j, err
}

stat, err := os.Stat(path)
if err != nil {
return nil, sderr.Wrap(err, "failed to read meta data for %{path}", path)
return nil, fmt.Errorf("failed to read meta data for %s: %w", path, err)
}

if stat.IsDir() {
j, err := sdjournal.NewJournalFromDir(path)
if err != nil {
err = sderr.Wrap(err, "failed to open journal directory %{path}", path)
err = fmt.Errorf("failed to open journal directory %s: %w", path, err)
}
return j, err
}

j, err := sdjournal.NewJournalFromFiles(path)
if err != nil {
err = sderr.Wrap(err, "failed to open journal file %{path}", path)
err = fmt.Errorf("failed to open journal file %s: %w", path, err)
}
return j, err
}
Expand Down
7 changes: 3 additions & 4 deletions filebeat/input/v2/input-cursor/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ package cursor

import (
"context"
"errors"
"fmt"
"runtime/debug"
"time"

"github.com/urso/sderr"

"github.com/elastic/go-concert/ctxtool"
"github.com/elastic/go-concert/unison"

Expand Down Expand Up @@ -81,7 +80,7 @@ func (inp *managedInput) Test(ctx input.TestContext) error {

errs := grp.Wait()
if len(errs) > 0 {
return sderr.WrapAll(errs, "input tests failed")
return fmt.Errorf("input tests failed: %w", errors.Join(errs...))
}
return nil
}
Expand Down Expand Up @@ -127,7 +126,7 @@ func (inp *managedInput) Run(
}

if errs := grp.Wait(); len(errs) > 0 {
return sderr.WrapAll(errs, "input %{id} failed", ctx.ID)
return fmt.Errorf("input %s failed: %w", ctx.ID, errors.Join(errs...))
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions filebeat/input/v2/input-cursor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ package cursor
import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/urso/sderr"

"github.com/elastic/go-concert/unison"

v2 "github.com/elastic/beats/v7/filebeat/input/v2"
Expand Down Expand Up @@ -131,7 +130,7 @@ func (cim *InputManager) Init(group unison.Group) error {
if err != nil {
store.Release()
cim.shutdown()
return sderr.Wrap(err, "Can not start registry cleanup process")
return fmt.Errorf("Can not start registry cleanup process: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tsg/go-daemon v0.0.0-20200207173439-e704b93fd89b
github.com/ugorji/go/codec v1.1.8
github.com/urso/sderr v0.0.0-20210525210834-52b04e8f5c71
github.com/urso/sderr v0.0.0-20210525210834-52b04e8f5c71 // indirect
github.com/vmware/govmomi v0.0.0-20170802214208-2cad15190b41
github.com/xdg/scram v1.0.3
go.elastic.co/ecszap v1.0.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/urso/sderr"

"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/internal/collections"
"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/internal/kvstore"
Expand Down Expand Up @@ -176,7 +175,7 @@ func (s *stateStore) close(commit bool) (err error) {
}

if err != nil {
err = sderr.WrapAll([]error{err, rollbackErr}, "multiple errors during statestore close")
err = fmt.Errorf("multiple errors during statestore close: %w", errors.Join(err, rollbackErr))
} else {
err = rollbackErr
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"fmt"
"time"

"github.com/urso/sderr"

"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/internal/kvstore"
"github.com/elastic/beats/v7/x-pack/filebeat/input/entityanalytics/provider/okta/internal/okta"
)
Expand Down Expand Up @@ -187,10 +185,8 @@ func (s *stateStore) close(commit bool) (err error) {
return
}
rollbackErr := s.tx.Rollback()
if rollbackErr == nil {
// FIXME: Use fmt.Errorf("multiple errors during statestore close: %w", errors.Join(err, rollbackErr))
// when go1.20 is supported.
err = sderr.WrapAll([]error{err, rollbackErr}, "multiple errors during statestore close")
if rollbackErr != nil {
err = fmt.Errorf("multiple errors during statestore close: %w", errors.Join(err, rollbackErr))
}
}()

Expand Down

0 comments on commit 3c9f4d9

Please sign in to comment.