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

feat: replace multierror with stdlib errors wrapping #41043

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

kruskall
Copy link
Member

Proposed commit message

replace multierror library with stdlib error wrapping

Go 1.20 added errors.Join allowing for multierrors. Replace go-multierror usage with native go errors

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Disruptive User Impact

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

Logs

replace multierror library with stdlib error wrapping
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Sep 29, 2024
Copy link
Contributor

mergify bot commented Sep 29, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @kruskall? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Sep 29, 2024

backport-8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Sep 29, 2024
@kruskall kruskall marked this pull request as ready for review September 30, 2024 09:43
@kruskall kruskall requested review from a team as code owners September 30, 2024 09:43
@shmsr
Copy link
Member

shmsr commented Sep 30, 2024

@kruskall can we also remove the use of "github.com/pkg/errors"?

@pierrehilbert pierrehilbert added Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team Team:Security-Linux Platform Linux Platform Team in Security Solution Team:Security-Windows Platform Windows Platform Team in Security Solution Team:Monitoring Stack Monitoring team labels Sep 30, 2024
@elasticmachine
Copy link
Collaborator

Pinging @elastic/sec-windows-platform (Team:Security-Windows Platform)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/sec-linux-platform (Team:Security-Linux Platform)

@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Sep 30, 2024
Copy link
Contributor

mergify bot commented Sep 30, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b feat/drop-multierror upstream/feat/drop-multierror
git merge upstream/main
git push upstream feat/drop-multierror

@kruskall
Copy link
Member Author

@kruskall can we also remove the use of "github.com/pkg/errors"?

That's not possible:

"github.com/pkg/errors" //nolint:gomodguard // go-cfclient uses pkg/errors internally, must be upgraded to at least https://github.com/cloudfoundry-community/go-cfclient/commit/0ada4e9452

}
log.Warnf("errors loading rules: %v", errs.Err())
log.Warnf("errors loading rules: %v", errors.Join(errs...))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Warnf("errors loading rules: %v", errors.Join(errs...))
log.Warnf("errors loading rules: %w", errors.Join(errs...))

merr, ok := ucfgErr.Reason().(*multierror.MultiError)
merr, ok := ucfgErr.Reason().(interface {
Unwrap() []error
})
if !ok {
t.Fatal("expected MultiError")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Fatal("expected MultiError")
t.Fatal("expected slice error unwrapper")

err.SetKey(convMap.Key + "." + err.Key())
_, errs := convMap.Schema.ApplyTo(subEvent, subData.(map[string]interface{}))
for _, err := range errs {
var keyErr schema.KeyError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising. It would be made less so if there were a comment explaining that while schema.KeyError doesn't satisfy error, its implementations do.

@@ -536,7 +536,7 @@ func TestFieldConcat(t *testing.T) {
{Name: "foo", Fields: Fields{{Name: "b", Fields: Fields{{Name: "c"}}}}},
},

err: "2 errors: fields contain key <a>; fields contain non object node conflicting with key <foo.b.c>",
err: "fields contain key <a>\nfields contain non object node conflicting with key <foo.b.c>",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about this. How does this impact on error rendering to users? I suspect it would be and improvement, but it's worth checking.

@@ -136,11 +135,13 @@ func eventMapping(entries []Entry, mapping AttributeMapping) ([]mapstr.M, error)
errs = append(errs, err)
}
case map[string]interface{}:
constructEvents(entryValues, v, mbeanEvents, mapping, errs)
errs = constructEvents(entryValues, v, mbeanEvents, mapping, errs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice incidental fixes.

@@ -106,6 +108,7 @@ func (lo *IPv6Loopback) AddRandomAddress() (addr net.IP, err error) {
if err = unix.Bind(fd, &bindAddr); err == nil {
break
}
//nolint:errorlint // ignore
if errno, ok := err.(unix.Errno); !ok || errno != unix.EADDRNOTAVAIL {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be expressible as if err != unix.EADDRNOTAVAIL {; syscall.Errno is an error so they are comparable. Caveat if someone ever implements an error that is not on a pointer and contains a slice or a map then ¡bang!, but that's true for most uses of errors.

@@ -66,16 +65,16 @@ func (r *Coordinator) Run(ctx context.Context, t telemetry.T) error {
}(ctx, t, rfn)
}

// Wait for goroutine to complete and aggregate any errors from the goroutine and
// Wait for goroutine to complete and aggregate any errs from the goroutine and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this line? This is English.

// reloadInputs wraps the multierror so we have to call Unwrap
wr := errors.Unwrap(err)

//nolint:errorlint // ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it complaining about here?

// the standard library errors package.
if errors.As(err, &merror) {
for _, err := range merror.Errors {
//nolint:errorlint // There are no new changes to this line but
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finish sentence?

@@ -824,17 +828,17 @@ func (cm *BeatV2Manager) reloadInputs(inputUnits []*agentUnit) error {
}

if err := obj.Reload(inputBeatCfgs); err != nil {
merror := &multierror.MultiError{}
realErrors := multierror.Errors{}
var realErrors []error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/realErrors/errs/g?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify enhancement Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team Team:Monitoring Stack Monitoring team Team:Security-Linux Platform Linux Platform Team in Security Solution Team:Security-Windows Platform Windows Platform Team in Security Solution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants