Skip to content

Commit

Permalink
feat: add extra goos support (#1646)
Browse files Browse the repository at this point in the history
syscall.Errno is not available is some GOOS causing compile errors
Move syscall specific code to separate file behind build tags making it
future proof and adding support for all goos supported by the go runtime
  • Loading branch information
kruskall committed Aug 29, 2024
1 parent 86ad209 commit 9b26590
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
8 changes: 0 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net"
"os"
"reflect"
"syscall"
"time"

"go.elastic.co/apm/v2/model"
Expand Down Expand Up @@ -596,13 +595,6 @@ func init() {
details.SetAttr("syscall", syscallErr.Syscall)
details.Cause = append(details.Cause, syscallErr.Err)
}))
RegisterTypeErrorDetailer(reflect.TypeOf(syscall.Errno(0)), ErrorDetailerFunc(func(err error, details *ErrorDetails) {
errno := err.(syscall.Errno)
details.Code.String = errnoName(errno)
if details.Code.String == "" {
details.Code.Number = float64(errno)
}
}))
}

func errTemporary(err error) bool {
Expand Down
33 changes: 12 additions & 21 deletions error_go120_test.go → error_syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,21 @@
// specific language governing permissions and limitations
// under the License.

//go:build go1.20
// +build go1.20
//go:build unix || windows

package apm_test
package apm // import "go.elastic.co/apm/v2"

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.elastic.co/apm/v2"
"go.elastic.co/apm/v2/apmtest"
"reflect"
"syscall"
)

func TestErrorCauseUnwrapJoined(t *testing.T) {
err := errors.Join(errors.New("cause1"), errors.New("cause2"))
_, _, errors := apmtest.WithTransaction(func(ctx context.Context) {
apm.CaptureError(ctx, err).Send()
})
require.Len(t, errors, 1)
require.Len(t, errors[0].Exception.Cause, 2)
assert.Equal(t, "cause1", errors[0].Exception.Cause[0].Message)
assert.Equal(t, "cause2", errors[0].Exception.Cause[1].Message)
func init() {
RegisterTypeErrorDetailer(reflect.TypeOf(syscall.Errno(0)), ErrorDetailerFunc(func(err error, details *ErrorDetails) {
errno := err.(syscall.Errno)
details.Code.String = errnoName(errno)
if details.Code.String == "" {
details.Code.Number = float64(errno)
}
}))
}
12 changes: 12 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package apm_test

import (
"context"
goerrors "errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -52,6 +53,17 @@ func TestErrorID(t *testing.T) {
assert.Equal(t, model.TraceID(errorID), errors[0].ID)
}

func TestErrorCauseUnwrapJoined(t *testing.T) {
err := goerrors.Join(errors.New("cause1"), errors.New("cause2"))
_, _, errors := apmtest.WithTransaction(func(ctx context.Context) {
apm.CaptureError(ctx, err).Send()
})
require.Len(t, errors, 1)
require.Len(t, errors[0].Exception.Cause, 2)
assert.Equal(t, "cause1", errors[0].Exception.Cause[0].Message)
assert.Equal(t, "cause2", errors[0].Exception.Cause[1].Message)
}

func TestErrorsStackTrace(t *testing.T) {
modelError := sendError(t, &errorsStackTracer{
"zing", newErrorsStackTrace(0, 2),
Expand Down
3 changes: 1 addition & 2 deletions error_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
// specific language governing permissions and limitations
// under the License.

//go:build !windows
// +build !windows
//go:build unix

package apm // import "go.elastic.co/apm/v2"

Expand Down
2 changes: 2 additions & 0 deletions error_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

//go:build windows

package apm // import "go.elastic.co/apm/v2"

import (
Expand Down

0 comments on commit 9b26590

Please sign in to comment.